Merge "Shifting hotseat icons to aling with all-apps icon, if the hotseat size is different from the source device" into ub-launcher3-burnaby
diff --git a/Android.mk b/Android.mk
index 2e57478..3ff542e 100644
--- a/Android.mk
+++ b/Android.mk
@@ -36,7 +36,7 @@
 
 LOCAL_SDK_VERSION := current
 LOCAL_PACKAGE_NAME := Launcher3
-LOCAL_OVERRIDES_PACKAGES := Launcher2
+LOCAL_OVERRIDES_PACKAGES := Home Launcher2
 
 include $(BUILD_PACKAGE)
 
diff --git a/src/com/android/launcher3/IconCache.java b/src/com/android/launcher3/IconCache.java
index 916418f..ea1c0fd 100644
--- a/src/com/android/launcher3/IconCache.java
+++ b/src/com/android/launcher3/IconCache.java
@@ -540,7 +540,7 @@
             mCache.put(cacheKey, entry);
 
             // Check the DB first.
-            if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
+            if (!getEntryFromDB(cacheKey, entry, useLowResIcon)) {
                 if (info != null) {
                     entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
                 } else {
@@ -579,7 +579,14 @@
             Bitmap icon, CharSequence title) {
         removeFromMemCacheLocked(packageName, user);
 
-        CacheEntry entry = getEntryForPackageLocked(packageName, user, false);
+        ComponentKey cacheKey = getPackageKey(packageName, user);
+        CacheEntry entry = mCache.get(cacheKey);
+
+        // For icon caching, do not go through DB. Just update the in-memory entry.
+        if (entry == null) {
+            entry = new CacheEntry();
+            mCache.put(cacheKey, entry);
+        }
         if (!TextUtils.isEmpty(title)) {
             entry.title = title;
         }
@@ -588,15 +595,18 @@
         }
     }
 
+    private static ComponentKey getPackageKey(String packageName, UserHandleCompat user) {
+        ComponentName cn = new ComponentName(packageName, packageName + EMPTY_CLASS_NAME);
+        return new ComponentKey(cn, user);
+    }
+
     /**
      * Gets an entry for the package, which can be used as a fallback entry for various components.
      * This method is not thread safe, it must be called from a synchronized method.
-     *
      */
     private CacheEntry getEntryForPackageLocked(String packageName, UserHandleCompat user,
             boolean useLowResIcon) {
-        ComponentName cn = new ComponentName(packageName, packageName + EMPTY_CLASS_NAME);
-        ComponentKey cacheKey = new ComponentKey(cn, user);
+        ComponentKey cacheKey = getPackageKey(packageName, user);
         CacheEntry entry = mCache.get(cacheKey);
 
         if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
@@ -604,7 +614,7 @@
             boolean entryUpdated = true;
 
             // Check the DB first.
-            if (!getEntryFromDB(cn, user, entry, useLowResIcon)) {
+            if (!getEntryFromDB(cacheKey, entry, useLowResIcon)) {
                 try {
                     PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
                     ApplicationInfo appInfo = info.applicationInfo;
@@ -622,7 +632,8 @@
                     // package updates.
                     ContentValues values =
                             newContentValues(entry.icon, entry.title.toString(), mPackageBgColor);
-                    addIconToDB(values, cn, info, mUserManager.getSerialNumberForUser(user));
+                    addIconToDB(values, cacheKey.componentName, info,
+                            mUserManager.getSerialNumberForUser(user));
 
                 } catch (NameNotFoundException e) {
                     if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
@@ -649,7 +660,7 @@
      * @param dpi the native density of the icon
      */
     public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
-            long userSerial) {
+            long userSerial, InvariantDeviceProfile idp) {
         // TODO rescale to the correct native DPI
         try {
             PackageManager packageManager = mContext.getPackageManager();
@@ -660,21 +671,22 @@
             // pass
         }
 
-        ContentValues values = newContentValues(icon, label, Color.TRANSPARENT);
+        ContentValues values = newContentValues(
+                Bitmap.createScaledBitmap(icon, idp.iconBitmapSize, idp.iconBitmapSize, true),
+                label, Color.TRANSPARENT);
         values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
         values.put(IconDB.COLUMN_USER, userSerial);
         mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
                 SQLiteDatabase.CONFLICT_REPLACE);
     }
 
-    private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
-            CacheEntry entry, boolean lowRes) {
+    private boolean getEntryFromDB(ComponentKey cacheKey, CacheEntry entry, boolean lowRes) {
         Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
                 new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
                         IconDB.COLUMN_LABEL},
                 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
-                new String[] {component.flattenToString(),
-                    Long.toString(mUserManager.getSerialNumberForUser(user))},
+                new String[] {cacheKey.componentName.flattenToString(),
+                    Long.toString(mUserManager.getSerialNumberForUser(cacheKey.user))},
                 null, null, null);
         try {
             if (c.moveToNext()) {
@@ -685,7 +697,8 @@
                     entry.title = "";
                     entry.contentDescription = "";
                 } else {
-                    entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
+                    entry.contentDescription = mUserManager.getBadgedLabelForUser(
+                            entry.title, cacheKey.user);
                 }
                 return true;
             }
diff --git a/src/com/android/launcher3/LauncherBackupHelper.java b/src/com/android/launcher3/LauncherBackupHelper.java
index 8ac09d3..38a4bde 100644
--- a/src/com/android/launcher3/LauncherBackupHelper.java
+++ b/src/com/android/launcher3/LauncherBackupHelper.java
@@ -610,10 +610,11 @@
         Bitmap icon = BitmapFactory.decodeByteArray(res.data, 0, res.data.length);
         if (icon == null) {
             Log.w(TAG, "failed to unpack icon for " + key.name);
+        } else {
+            if (VERBOSE) Log.v(TAG, "saving restored icon as: " + key.name);
+            mIconCache.preloadIcon(ComponentName.unflattenFromString(key.name), icon, res.dpi,
+                    "" /* label */, mUserSerial, mIdp);
         }
-        if (VERBOSE) Log.v(TAG, "saving restored icon as: " + key.name);
-        mIconCache.preloadIcon(ComponentName.unflattenFromString(key.name), icon, res.dpi,
-                "" /* label */, mUserSerial);
     }
 
     /**
@@ -694,7 +695,7 @@
                 Log.w(TAG, "failed to unpack widget icon for " + key.name);
             } else {
                 mIconCache.preloadIcon(ComponentName.unflattenFromString(widget.provider),
-                        icon, widget.icon.dpi, widget.label, mUserSerial);
+                        icon, widget.icon.dpi, widget.label, mUserSerial, mIdp);
             }
         }
 
diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java
index b60477f..0b67310 100644
--- a/src/com/android/launcher3/LauncherModel.java
+++ b/src/com/android/launcher3/LauncherModel.java
@@ -2163,7 +2163,11 @@
                                         appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
                                                 provider.provider);
 
-                                        int status = restoreStatus;
+                                        // The provider is available. So the widget is either
+                                        // available or not available. We do not need to track
+                                        // any future restore updates.
+                                        int status = restoreStatus &
+                                                ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
                                         if (!wasProviderReady) {
                                             // If provider was not previously ready, update the
                                             // status and UI flag.
@@ -3155,7 +3159,9 @@
                             if (mUser.equals(widgetInfo.user)
                                     && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
                                     && packageSet.contains(widgetInfo.providerName.getPackageName())) {
-                                widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
+                                widgetInfo.restoreStatus &=
+                                        ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
+                                        ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
                                 widgets.add(widgetInfo);
                                 updateItemInDatabase(context, widgetInfo);
                             }