Updating the icon cache after all apps has been bound.

> Updating the cache DB version to reset existing cache.

Bug: 20834835
Change-Id: I298ca9ddcc4dd270b25b767447ecde01ef41a916
diff --git a/src/com/android/launcher3/AllAppsList.java b/src/com/android/launcher3/AllAppsList.java
index dd646bb..3b25dca 100644
--- a/src/com/android/launcher3/AllAppsList.java
+++ b/src/com/android/launcher3/AllAppsList.java
@@ -24,6 +24,7 @@
 import com.android.launcher3.compat.UserHandleCompat;
 
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
 
 
@@ -117,6 +118,16 @@
         }
     }
 
+    public void updateIconsAndLabels(HashSet<String> packages, UserHandleCompat user,
+            ArrayList<AppInfo> outUpdates) {
+        for (AppInfo info : data) {
+            if (info.user.equals(user) && packages.contains(info.componentName.getPackageName())) {
+                mIconCache.updateTitleAndIcon(info);
+                outUpdates.add(info);
+            }
+        }
+    }
+
     /**
      * Add and remove icons for this package which has been updated.
      */
diff --git a/src/com/android/launcher3/IconCache.java b/src/com/android/launcher3/IconCache.java
index fd45714..6c2aa39 100644
--- a/src/com/android/launcher3/IconCache.java
+++ b/src/com/android/launcher3/IconCache.java
@@ -407,6 +407,20 @@
     }
 
     /**
+     * Updates {@param application} only if a valid entry is found.
+     */
+    public synchronized void updateTitleAndIcon(AppInfo application) {
+        CacheEntry entry = cacheLocked(application.componentName, null, application.user,
+                false, application.usingLowResIcon);
+        if (entry.icon != null && !isDefaultIcon(entry.icon, application.user)) {
+            application.title = entry.title;
+            application.iconBitmap = entry.icon;
+            application.contentDescription = entry.contentDescription;
+            application.usingLowResIcon = entry.isLowResIcon;
+        }
+    }
+
+    /**
      * Returns a high res icon for the given intent and user
      */
     public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
@@ -655,7 +669,7 @@
     }
 
     private static final class IconDB extends SQLiteOpenHelper {
-        private final static int DB_VERSION = 3;
+        private final static int DB_VERSION = 4;
 
         private final static String TABLE_NAME = "icons";
         private final static String COLUMN_ROWID = "rowid";
diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java
index 0138a91..ddbae3a 100644
--- a/src/com/android/launcher3/LauncherModel.java
+++ b/src/com/android/launcher3/LauncherModel.java
@@ -2767,6 +2767,7 @@
             }
             if (!mAllAppsLoaded) {
                 loadAllApps();
+                updateAllAppsIconsCache();
                 synchronized (LoaderTask.this) {
                     if (mStopped) {
                         return;
@@ -2821,9 +2822,6 @@
                 return;
             }
 
-            final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
-            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
-
             final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
 
             // Clear the list of apps
@@ -2843,42 +2841,6 @@
                     return;
                 }
 
-                // Update icon cache
-                HashSet<String> updatedPackages = mIconCache.updateDBIcons(user, apps);
-
-                // If any package icon has changed (app was updated while launcher was dead),
-                // update the corresponding shortcuts.
-                if (!updatedPackages.isEmpty()) {
-                    final ArrayList<ShortcutInfo> updates = new ArrayList<ShortcutInfo>();
-                    synchronized (sBgLock) {
-                        for (ItemInfo info : sBgItemsIdMap) {
-                            if (info instanceof ShortcutInfo && user.equals(info.user)
-                                    && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
-                                ShortcutInfo si = (ShortcutInfo) info;
-                                ComponentName cn = si.getTargetComponent();
-                                if (cn != null && updatedPackages.contains(cn.getPackageName())) {
-                                    si.updateIcon(mIconCache);
-                                    updates.add(si);
-                                }
-                            }
-                        }
-                    }
-
-                    if (!updates.isEmpty()) {
-                        final UserHandleCompat userFinal = user;
-                        mHandler.post(new Runnable() {
-
-                            public void run() {
-                                Callbacks cb = getCallback();
-                                if (cb != null) {
-                                    cb.bindShortcutsChanged(
-                                            updates, new ArrayList<ShortcutInfo>(), userFinal);
-                                }
-                            }
-                        });
-                    }
-                }
-
                 // Create the ApplicationInfos
                 for (int i = 0; i < apps.size(); i++) {
                     LauncherActivityInfoCompat app = apps.get(i);
@@ -2929,6 +2891,68 @@
             }
         }
 
+        private void updateAllAppsIconsCache() {
+            final ArrayList<AppInfo> updatedApps = new ArrayList<>();
+
+            for (UserHandleCompat user : mUserManager.getUserProfiles()) {
+                // Query for the set of apps
+                final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
+                // Fail if we don't have any apps
+                // TODO: Fix this. Only fail for the current user.
+                if (apps == null || apps.isEmpty()) {
+                    return;
+                }
+
+                // Update icon cache
+                HashSet<String> updatedPackages = mIconCache.updateDBIcons(user, apps);
+
+                // If any package icon has changed (app was updated while launcher was dead),
+                // update the corresponding shortcuts.
+                if (!updatedPackages.isEmpty()) {
+                    final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
+                    synchronized (sBgLock) {
+                        for (ItemInfo info : sBgItemsIdMap) {
+                            if (info instanceof ShortcutInfo && user.equals(info.user)
+                                    && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
+                                ShortcutInfo si = (ShortcutInfo) info;
+                                ComponentName cn = si.getTargetComponent();
+                                if (cn != null && updatedPackages.contains(cn.getPackageName())) {
+                                    si.updateIcon(mIconCache);
+                                    updatedShortcuts.add(si);
+                                }
+                            }
+                        }
+                        mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
+                    }
+
+                    if (!updatedShortcuts.isEmpty()) {
+                        final UserHandleCompat userFinal = user;
+                        mHandler.post(new Runnable() {
+
+                            public void run() {
+                                Callbacks cb = getCallback();
+                                if (cb != null) {
+                                    cb.bindShortcutsChanged(updatedShortcuts,
+                                            new ArrayList<ShortcutInfo>(), userFinal);
+                                }
+                            }
+                        });
+                    }
+                }
+            }
+            if (!updatedApps.isEmpty()) {
+                mHandler.post(new Runnable() {
+
+                    public void run() {
+                        Callbacks cb = getCallback();
+                        if (cb != null) {
+                            cb.bindAppsUpdated(updatedApps);
+                        }
+                    }
+                });
+            }
+        }
+
         public void dumpState() {
             synchronized (sBgLock) {
                 Log.d(TAG, "mLoaderTask.mContext=" + mContext);