Adding an overridable DrawableFactory to allow creating custom icon drawables

> Adding ItemInfo as a parameter for creating drawable

Change-Id: I793acb0381d2b8df4db0a08317dddf1464788ebc
This commit is contained in:
Sunny Goyal
2016-11-12 09:58:29 -08:00
parent 4633be64e8
commit 55cb70bf70
8 changed files with 100 additions and 48 deletions
+3
View File
@@ -79,6 +79,9 @@
<!-- Name of an icon provider class. -->
<string name="icon_provider_class" translatable="false"></string>
<!-- Name of a drawable factory class. -->
<string name="drawable_factory_class" translatable="false"></string>
<!-- Package name of the default wallpaper picker. -->
<string name="wallpaper_picker_package" translatable="false"></string>
+3 -17
View File
@@ -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);
}
@@ -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;
}
+3 -2
View File
@@ -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,
-18
View File
@@ -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.
*/
@@ -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;
@@ -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);
@@ -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;
}
}