Normalizing icons in recents view

Change-Id: I1d95c1abf158044ca5666473f976b49f7997ca27
diff --git a/quickstep/libs/sysui_shared.jar b/quickstep/libs/sysui_shared.jar
index d1ac936..d5859a7 100644
--- a/quickstep/libs/sysui_shared.jar
+++ b/quickstep/libs/sysui_shared.jar
Binary files differ
diff --git a/quickstep/src/com/android/quickstep/NormalizedIconLoader.java b/quickstep/src/com/android/quickstep/NormalizedIconLoader.java
new file mode 100644
index 0000000..431fb30
--- /dev/null
+++ b/quickstep/src/com/android/quickstep/NormalizedIconLoader.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2018 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.quickstep;
+
+import android.annotation.TargetApi;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.pm.ActivityInfo;
+import android.content.res.Resources;
+import android.graphics.drawable.Drawable;
+import android.os.Build;
+import android.os.Build.VERSION_CODES;
+import android.os.UserHandle;
+import android.util.LruCache;
+import android.util.SparseArray;
+
+import com.android.launcher3.FastBitmapDrawable;
+import com.android.launcher3.graphics.BitmapInfo;
+import com.android.launcher3.graphics.LauncherIcons;
+import com.android.systemui.shared.recents.model.IconLoader;
+import com.android.systemui.shared.recents.model.TaskKeyLruCache;
+
+/**
+ * Extension of {@link IconLoader} with icon normalization support
+ */
+@TargetApi(Build.VERSION_CODES.O)
+public class NormalizedIconLoader extends IconLoader {
+
+    private final SparseArray<BitmapInfo> mDefaultIcons = new SparseArray<>();
+    private LauncherIcons mLauncherIcons;
+
+    public NormalizedIconLoader(Context context, TaskKeyLruCache<Drawable> iconCache,
+            LruCache<ComponentName, ActivityInfo> activityInfoCache) {
+        super(context, iconCache, activityInfoCache);
+    }
+
+    @Override
+    public Drawable getDefaultIcon(int userId) {
+        synchronized (mDefaultIcons) {
+            BitmapInfo info = mDefaultIcons.get(userId);
+            if (info == null) {
+                info = getBitmapInfo(Resources.getSystem()
+                        .getDrawable(android.R.drawable.sym_def_app_icon), userId);
+                mDefaultIcons.put(userId, info);
+            }
+
+            return new FastBitmapDrawable(info);
+        }
+    }
+
+    @Override
+    protected Drawable createBadgedDrawable(Drawable drawable, int userId) {
+        return new FastBitmapDrawable(getBitmapInfo(drawable, userId));
+    }
+
+    private synchronized BitmapInfo getBitmapInfo(Drawable drawable, int userId) {
+        if (mLauncherIcons == null) {
+            mLauncherIcons = LauncherIcons.obtain(mContext);
+        }
+
+        // User version code O, so that the icon is always wrapped in an adaptive icon container.
+        return mLauncherIcons.createBadgedIconBitmap(drawable, UserHandle.of(userId),
+                Build.VERSION_CODES.O);
+    }
+
+    @Override
+    protected Drawable getBadgedActivityIcon(ActivityInfo activityInfo, int userId) {
+        return createBadgedDrawable(
+                activityInfo.loadUnbadgedIcon(mContext.getPackageManager()), userId);
+    }
+}
diff --git a/quickstep/src/com/android/quickstep/RecentsModel.java b/quickstep/src/com/android/quickstep/RecentsModel.java
index d10c5a6..182803e 100644
--- a/quickstep/src/com/android/quickstep/RecentsModel.java
+++ b/quickstep/src/com/android/quickstep/RecentsModel.java
@@ -16,18 +16,24 @@
 package com.android.quickstep;
 
 import android.annotation.TargetApi;
+import android.content.ComponentName;
 import android.content.Context;
+import android.content.pm.ActivityInfo;
 import android.content.res.Resources;
+import android.graphics.drawable.Drawable;
 import android.os.Build;
 import android.os.Looper;
 import android.os.UserHandle;
+import android.util.LruCache;
 
 import com.android.launcher3.MainThreadExecutor;
 import com.android.launcher3.R;
 import com.android.systemui.shared.recents.ISystemUiProxy;
+import com.android.systemui.shared.recents.model.IconLoader;
 import com.android.systemui.shared.recents.model.RecentsTaskLoadPlan;
 import com.android.systemui.shared.recents.model.RecentsTaskLoadPlan.PreloadOptions;
 import com.android.systemui.shared.recents.model.RecentsTaskLoader;
+import com.android.systemui.shared.recents.model.TaskKeyLruCache;
 import com.android.systemui.shared.system.ActivityManagerWrapper;
 import com.android.systemui.shared.system.BackgroundExecutor;
 import com.android.systemui.shared.system.TaskStackChangeListener;
@@ -75,7 +81,15 @@
         Resources res = context.getResources();
         mRecentsTaskLoader = new RecentsTaskLoader(mContext,
                 res.getInteger(R.integer.config_recentsMaxThumbnailCacheSize),
-                res.getInteger(R.integer.config_recentsMaxIconCacheSize), 0);
+                res.getInteger(R.integer.config_recentsMaxIconCacheSize), 0) {
+
+            @Override
+            protected IconLoader createNewIconLoader(Context context,
+                    TaskKeyLruCache<Drawable> iconCache,
+                    LruCache<ComponentName, ActivityInfo> activityInfoCache) {
+                return new NormalizedIconLoader(context, iconCache, activityInfoCache);
+            }
+        };
         mRecentsTaskLoader.startLoader(mContext);
 
         mMainThreadExecutor = new MainThreadExecutor();
diff --git a/src/com/android/launcher3/FastBitmapDrawable.java b/src/com/android/launcher3/FastBitmapDrawable.java
index bd19dfa..c4ec8c9 100644
--- a/src/com/android/launcher3/FastBitmapDrawable.java
+++ b/src/com/android/launcher3/FastBitmapDrawable.java
@@ -29,6 +29,8 @@
 import android.graphics.PorterDuff;
 import android.graphics.PorterDuffColorFilter;
 import android.graphics.drawable.Drawable;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
 import android.util.Property;
 import android.util.SparseArray;
 
@@ -304,4 +306,30 @@
         }
         invalidateSelf();
     }
+
+    @Override
+    public ConstantState getConstantState() {
+        return new MyConstantState(mBitmap, mIconColor);
+    }
+
+    private static class MyConstantState extends ConstantState {
+        private final Bitmap mBitmap;
+        private final int mIconColor;
+
+
+        public MyConstantState(Bitmap bitmap, int color) {
+            mBitmap = bitmap;
+            mIconColor = color;
+        }
+
+        @Override
+        public Drawable newDrawable() {
+            return new FastBitmapDrawable(mBitmap, mIconColor);
+        }
+
+        @Override
+        public int getChangingConfigurations() {
+            return 0;
+        }
+    }
 }