From ea383c312c57a65234bd7734903913aefa779184 Mon Sep 17 00:00:00 2001 From: Hyunyoung Song Date: Wed, 18 Jan 2017 02:43:17 -0800 Subject: [PATCH] Changes required to use MaskableIconDrawable inside Launcher3 This CL will bake the legacy icons inside a grey background Note: There will be follow up CL to change the color and shadow of the legacy icon. The idea of this CL is to first test if MaskableIconDrawable methods and class work properly. Bug: 32063838 Bug: 33553066 Change-Id: I3d02e619fcb0cd9879baa22a025b5d95c829f26c --- res/values/colors.xml | 1 + .../launcher3/graphics/LauncherIcons.java | 29 +++++++++++++++---- .../launcher3/util/SQLiteCacheHelper.java | 9 ++++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/res/values/colors.xml b/res/values/colors.xml index a7b507c268..19bf51af8d 100644 --- a/res/values/colors.xml +++ b/res/values/colors.xml @@ -52,4 +52,5 @@ #FF37474F #757575 + #FFE0E0E0 diff --git a/src/com/android/launcher3/graphics/LauncherIcons.java b/src/com/android/launcher3/graphics/LauncherIcons.java index 2ae7a4af49..472b9135b6 100644 --- a/src/com/android/launcher3/graphics/LauncherIcons.java +++ b/src/com/android/launcher3/graphics/LauncherIcons.java @@ -29,10 +29,13 @@ import android.graphics.PaintFlagsDrawFilter; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.PaintDrawable; +import android.graphics.drawable.ScaleDrawable; import android.os.Process; import android.os.UserHandle; +import android.view.Gravity; import com.android.launcher3.AppInfo; import com.android.launcher3.IconCache; @@ -45,12 +48,12 @@ import com.android.launcher3.model.PackageItemInfo; import com.android.launcher3.shortcuts.DeepShortcutManager; import com.android.launcher3.shortcuts.ShortcutInfoCompat; -import java.lang.reflect.Method; - /** * Helper methods for generating various launcher icons */ public class LauncherIcons { + // TODO b/33553066 use the constant defined in MaskableIconDrawable + private static final float LEGACY_ICON_SCALE = .7f * .6667f; private static final Rect sOldBounds = new Rect(); private static final Canvas sCanvas = new Canvas(); @@ -168,7 +171,7 @@ public class LauncherIcons { * @param scale the scale to apply before drawing {@param icon} on the canvas */ public static Bitmap createIconBitmap(Drawable icon, Context context, float scale) { - icon = castToMaskableIconDrawable(icon); + icon = wrapToMaskableIconDrawable(context, icon); synchronized (sCanvas) { final int iconBitmapSize = LauncherAppState.getIDP(context).iconBitmapSize; @@ -224,17 +227,31 @@ public class LauncherIcons { } } - static Drawable castToMaskableIconDrawable(Drawable drawable) { + /** + * If the platform is running O but the app is not providing MaskableIconDrawable, then + * shrink the legacy icon and set it as foreground. Use color drawable as background to + * create MaskableIconDrawable. + */ + static Drawable wrapToMaskableIconDrawable(Context context, Drawable drawable) { if (!(ProviderConfig.IS_DOGFOOD_BUILD && Utilities.isAtLeastO())) { return drawable; } + int color = context.getResources().getColor(R.color.legacy_icon_background); + ColorDrawable colorDrawable = new ColorDrawable(color); + ScaleDrawable scaleDrawable = new ScaleDrawable(drawable, + Gravity.CENTER, LEGACY_ICON_SCALE, LEGACY_ICON_SCALE); + scaleDrawable.setLevel(1); try { Class clazz = Class.forName("android.graphics.drawable.MaskableIconDrawable"); - Method method = clazz.getDeclaredMethod("wrap", Drawable.class); - return (Drawable) method.invoke(null, drawable); + if (!clazz.isAssignableFrom(drawable.getClass())){ + + return (Drawable) clazz.getConstructor(Drawable.class, Drawable.class) + .newInstance(colorDrawable, scaleDrawable); + } } catch (Exception e) { return drawable; } + return drawable; } public static Bitmap createShortcutIcon(ShortcutInfoCompat shortcutInfo, Context context) { diff --git a/src/com/android/launcher3/util/SQLiteCacheHelper.java b/src/com/android/launcher3/util/SQLiteCacheHelper.java index d1cfe4264a..9aabfeb256 100644 --- a/src/com/android/launcher3/util/SQLiteCacheHelper.java +++ b/src/com/android/launcher3/util/SQLiteCacheHelper.java @@ -9,6 +9,9 @@ import android.database.sqlite.SQLiteFullException; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; +import com.android.launcher3.Utilities; +import com.android.launcher3.config.ProviderConfig; + /** * An extension of {@link SQLiteOpenHelper} with utility methods for a single table cache DB. * Any exception during write operations are ignored, and any version change causes a DB reset. @@ -16,12 +19,18 @@ import android.util.Log; public abstract class SQLiteCacheHelper { private static final String TAG = "SQLiteCacheHelper"; + private static final boolean NO_ICON_CACHE = ProviderConfig.IS_DOGFOOD_BUILD && + Utilities.isPropertyEnabled("MEMORY_ONLY_ICON_CACHE"); + private final String mTableName; private final MySQLiteOpenHelper mOpenHelper; private boolean mIgnoreWrites; public SQLiteCacheHelper(Context context, String name, int version, String tableName) { + if (NO_ICON_CACHE) { + name = null; + } mTableName = tableName; mOpenHelper = new MySQLiteOpenHelper(context, name, version);