From 55cb70bf70d6defe84fe44e0d942998adefbb71c Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Sat, 12 Nov 2016 09:58:29 -0800 Subject: Adding an overridable DrawableFactory to allow creating custom icon drawables > Adding ItemInfo as a parameter for creating drawable Change-Id: I793acb0381d2b8df4db0a08317dddf1464788ebc --- res/values/config.xml | 3 + src/com/android/launcher3/BubbleTextView.java | 20 +------ src/com/android/launcher3/FastBitmapDrawable.java | 19 ++++-- src/com/android/launcher3/Hotseat.java | 5 +- src/com/android/launcher3/Launcher.java | 18 ------ .../launcher3/PendingAppWidgetHostView.java | 9 ++- src/com/android/launcher3/folder/FolderIcon.java | 5 +- .../launcher3/graphics/DrawableFactory.java | 69 ++++++++++++++++++++++ 8 files changed, 100 insertions(+), 48 deletions(-) create mode 100644 src/com/android/launcher3/graphics/DrawableFactory.java diff --git a/res/values/config.xml b/res/values/config.xml index 5b3ee4633..d270def78 100644 --- a/res/values/config.xml +++ b/res/values/config.xml @@ -79,6 +79,9 @@ + + + diff --git a/src/com/android/launcher3/BubbleTextView.java b/src/com/android/launcher3/BubbleTextView.java index dbb797dc5..51cd0527f 100644 --- a/src/com/android/launcher3/BubbleTextView.java +++ b/src/com/android/launcher3/BubbleTextView.java @@ -16,7 +16,6 @@ package com.android.launcher3; -import android.annotation.TargetApi; import android.content.Context; import android.content.res.ColorStateList; import android.content.res.Resources; @@ -26,9 +25,7 @@ import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Region; -import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; -import android.os.Build; import android.util.AttributeSet; import android.util.SparseArray; import android.util.TypedValue; @@ -42,6 +39,7 @@ import android.widget.TextView; import com.android.launcher3.IconCache.IconLoadRequest; import com.android.launcher3.folder.FolderIcon; +import com.android.launcher3.graphics.DrawableFactory; import com.android.launcher3.graphics.HolographicOutlineHelper; import com.android.launcher3.model.PackageItemInfo; @@ -190,7 +188,7 @@ public class BubbleTextView extends TextView } private void applyIconAndLabel(Bitmap icon, ItemInfo info) { - FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(icon); + FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info); iconDrawable.setIsDisabled(info.isDisabled()); setIcon(iconDrawable); setText(info.title); @@ -201,15 +199,6 @@ public class BubbleTextView extends TextView } } - /** - * Used for measurement only, sets some dummy values on this view. - */ - public void applyDummyInfo() { - ColorDrawable d = new ColorDrawable(); - setIcon(mLauncher.resizeIconDrawable(d)); - setText(""); - } - /** * Overrides the default long press timeout. */ @@ -528,12 +517,9 @@ public class BubbleTextView extends TextView /** * Sets the icon for this view based on the layout direction. */ - @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) private void setIcon(Drawable icon) { mIcon = icon; - if (mIconSize != -1) { - mIcon.setBounds(0, 0, mIconSize, mIconSize); - } + mIcon.setBounds(0, 0, mIconSize, mIconSize); applyCompoundDrawables(mIcon); } diff --git a/src/com/android/launcher3/FastBitmapDrawable.java b/src/com/android/launcher3/FastBitmapDrawable.java index 270d53939..0cefc5739 100644 --- a/src/com/android/launcher3/FastBitmapDrawable.java +++ b/src/com/android/launcher3/FastBitmapDrawable.java @@ -94,7 +94,7 @@ public class FastBitmapDrawable extends Drawable { private static final ColorMatrix sTempBrightnessMatrix = new ColorMatrix(); private static final ColorMatrix sTempFilterMatrix = new ColorMatrix(); - private final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG); + protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG); private final Bitmap mBitmap; private State mState = State.NORMAL; private boolean mIsDisabled; @@ -116,6 +116,17 @@ public class FastBitmapDrawable extends Drawable { @Override public void draw(Canvas canvas) { + drawInternal(canvas); + } + + public void drawWithBrightness(Canvas canvas, float brightness) { + float oldBrightness = getBrightness(); + setBrightness(brightness); + drawInternal(canvas); + setBrightness(oldBrightness); + } + + protected void drawInternal(Canvas canvas) { canvas.drawBitmap(mBitmap, null, getBounds(), mPaint); } @@ -278,7 +289,7 @@ public class FastBitmapDrawable extends Drawable { /** * Sets the saturation of this icon, 0 [full color] -> 1 [desaturated] */ - public void setDesaturation(float desaturation) { + private void setDesaturation(float desaturation) { int newDesaturation = (int) Math.floor(desaturation * REDUCED_FILTER_VALUE_SPACE); if (mDesaturation != newDesaturation) { mDesaturation = newDesaturation; @@ -293,7 +304,7 @@ public class FastBitmapDrawable extends Drawable { /** * Sets the brightness of this icon, 0 [no add. brightness] -> 1 [2bright2furious] */ - public void setBrightness(float brightness) { + private void setBrightness(float brightness) { int newBrightness = (int) Math.floor(brightness * REDUCED_FILTER_VALUE_SPACE); if (mBrightness != newBrightness) { mBrightness = newBrightness; @@ -301,7 +312,7 @@ public class FastBitmapDrawable extends Drawable { } } - public float getBrightness() { + private float getBrightness() { return (float) mBrightness / REDUCED_FILTER_VALUE_SPACE; } diff --git a/src/com/android/launcher3/Hotseat.java b/src/com/android/launcher3/Hotseat.java index b93c6dfa1..5c96dde43 100644 --- a/src/com/android/launcher3/Hotseat.java +++ b/src/com/android/launcher3/Hotseat.java @@ -129,14 +129,15 @@ public class Hotseat extends FrameLayout if (!FeatureFlags.NO_ALL_APPS_ICON) { // Add the Apps button Context context = getContext(); - int allAppsButtonRank = mLauncher.getDeviceProfile().inv.getAllAppsButtonRank(); + DeviceProfile grid = mLauncher.getDeviceProfile(); + int allAppsButtonRank = grid.inv.getAllAppsButtonRank(); LayoutInflater inflater = LayoutInflater.from(context); TextView allAppsButton = (TextView) inflater.inflate(R.layout.all_apps_button, mContent, false); Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon); + d.setBounds(0, 0, grid.iconSizePx, grid.iconSizePx); - mLauncher.resizeIconDrawable(d); int scaleDownPx = getResources().getDimensionPixelSize(R.dimen.all_apps_button_scale_down); Rect bounds = d.getBounds(); d.setBounds(bounds.left, bounds.top + scaleDownPx / 2, bounds.right - scaleDownPx, diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java index 9160a012e..887a8bd2d 100644 --- a/src/com/android/launcher3/Launcher.java +++ b/src/com/android/launcher3/Launcher.java @@ -4034,24 +4034,6 @@ public class Launcher extends Activity mWorkspace.moveToDefaultScreen(false); } - /** - * Returns a FastBitmapDrawable with the icon, accurately sized. - */ - public FastBitmapDrawable createIconDrawable(Bitmap icon) { - FastBitmapDrawable d = new FastBitmapDrawable(icon); - d.setFilterBitmap(true); - resizeIconDrawable(d); - return d; - } - - /** - * Resizes an icon drawable to the correct icon size. - */ - public Drawable resizeIconDrawable(Drawable icon) { - icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx); - return icon; - } - /** * Prints out out state for debugging. */ diff --git a/src/com/android/launcher3/PendingAppWidgetHostView.java b/src/com/android/launcher3/PendingAppWidgetHostView.java index bf397744b..7c9b76bdf 100644 --- a/src/com/android/launcher3/PendingAppWidgetHostView.java +++ b/src/com/android/launcher3/PendingAppWidgetHostView.java @@ -36,6 +36,8 @@ import android.view.ContextThemeWrapper; import android.view.View; import android.view.View.OnClickListener; +import com.android.launcher3.graphics.DrawableFactory; + public class PendingAppWidgetHostView extends LauncherAppWidgetHostView implements OnClickListener { private static final float SETUP_ICON_SIZE_FACTOR = 2f / 5; private static final float MIN_SATUNATION = 0.7f; @@ -132,13 +134,14 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView implemen // 1) App icon in the center // 2) Preload icon in the center // 3) Setup icon in the center and app icon in the top right corner. + DrawableFactory drawableFactory = DrawableFactory.get(getContext()); if (mDisabledForSafeMode) { - FastBitmapDrawable disabledIcon = mLauncher.createIconDrawable(mIcon); + FastBitmapDrawable disabledIcon = drawableFactory.newIcon(mIcon, mInfo); disabledIcon.setIsDisabled(true); mCenterDrawable = disabledIcon; mSettingIconDrawable = null; } else if (isReadyForClickSetup()) { - mCenterDrawable = new FastBitmapDrawable(mIcon); + mCenterDrawable = drawableFactory.newIcon(mIcon, mInfo); mSettingIconDrawable = getResources().getDrawable(R.drawable.ic_setting).mutate(); updateSettingColor(); @@ -148,7 +151,7 @@ public class PendingAppWidgetHostView extends LauncherAppWidgetHostView implemen sPreloaderTheme.applyStyle(R.style.PreloadIcon, true); } - FastBitmapDrawable drawable = mLauncher.createIconDrawable(mIcon); + FastBitmapDrawable drawable = drawableFactory.newIcon(mIcon, mInfo); mCenterDrawable = new PreloadIconDrawable(drawable, sPreloaderTheme); mCenterDrawable.setCallback(this); mSettingIconDrawable = null; diff --git a/src/com/android/launcher3/folder/FolderIcon.java b/src/com/android/launcher3/folder/FolderIcon.java index a29a94659..37453232b 100644 --- a/src/com/android/launcher3/folder/FolderIcon.java +++ b/src/com/android/launcher3/folder/FolderIcon.java @@ -459,10 +459,7 @@ public class FolderIcon extends FrameLayout implements FolderListener { d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize); if (d instanceof FastBitmapDrawable) { FastBitmapDrawable fd = (FastBitmapDrawable) d; - float oldBrightness = fd.getBrightness(); - fd.setBrightness(params.overlayAlpha); - d.draw(canvas); - fd.setBrightness(oldBrightness); + fd.drawWithBrightness(canvas, params.overlayAlpha); } else { d.setColorFilter(Color.argb((int) (params.overlayAlpha * 255), 255, 255, 255), PorterDuff.Mode.SRC_ATOP); diff --git a/src/com/android/launcher3/graphics/DrawableFactory.java b/src/com/android/launcher3/graphics/DrawableFactory.java new file mode 100644 index 000000000..2926a29d6 --- /dev/null +++ b/src/com/android/launcher3/graphics/DrawableFactory.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.launcher3.graphics; + +import android.content.Context; +import android.graphics.Bitmap; +import android.text.TextUtils; + +import com.android.launcher3.FastBitmapDrawable; +import com.android.launcher3.ItemInfo; +import com.android.launcher3.R; + +import java.lang.reflect.InvocationTargetException; + +/** + * Factory for creating new drawables. + */ +public class DrawableFactory { + + private static DrawableFactory sInstance; + private static final Object LOCK = new Object(); + + public static DrawableFactory get(Context context) { + synchronized (LOCK) { + if (sInstance == null) { + context = context.getApplicationContext(); + sInstance = loadByName(context.getString(R.string.drawable_factory_class), context); + } + return sInstance; + } + } + + public static DrawableFactory loadByName(String className, Context context) { + if (!TextUtils.isEmpty(className)) { + try { + Class cls = Class.forName(className); + return (DrawableFactory) + cls.getDeclaredConstructor(Context.class).newInstance(context); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException + | ClassCastException | NoSuchMethodException | InvocationTargetException e) { + return new DrawableFactory(); + } + } + return new DrawableFactory(); + } + + /** + * Returns a FastBitmapDrawable with the icon. + */ + public FastBitmapDrawable newIcon(Bitmap icon, ItemInfo info) { + FastBitmapDrawable d = new FastBitmapDrawable(icon); + d.setFilterBitmap(true); + return d; + } +} -- cgit v1.2.3