Fixing slow binding when returning to Launcher

Bug: 8978842
Bug: 8660324

Change-Id: Idfa37c05ed299faa465ea66de4b43d30da77ecbc
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index 473a3d1..94a0bca 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -126,7 +126,7 @@
     static final boolean PROFILE_STARTUP = false;
     static final boolean DEBUG_WIDGETS = false;
     static final boolean DEBUG_STRICT_MODE = false;
-    static final boolean DEBUG_RESUME_TIME = true;
+    static final boolean DEBUG_RESUME_TIME = false;
 
     private static final int MENU_GROUP_WALLPAPER = 1;
     private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
@@ -392,7 +392,8 @@
 
         // Update customization drawer _after_ restoring the states
         if (mAppsCustomizeContent != null) {
-            mAppsCustomizeContent.onPackagesUpdated();
+            mAppsCustomizeContent.onPackagesUpdated(
+                LauncherModel.getSortedWidgetsAndShortcuts(this));
         }
 
         if (PROFILE_STARTUP) {
@@ -759,19 +760,28 @@
             mRestoring = false;
             mOnResumeNeedsLoad = false;
         }
-        // We might have postponed some bind calls until onResume (see waitUntilResume) --
-        // execute them here
-        long startTimeCallbacks = 0;
-        if (DEBUG_RESUME_TIME) {
-            startTimeCallbacks = System.currentTimeMillis();
-        }
-        for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
-            mOnResumeCallbacks.get(i).run();
-        }
-        mOnResumeCallbacks.clear();
-        if (DEBUG_RESUME_TIME) {
-            Log.d(TAG, "Time spent processing callbacks in onResume: " +
-                (System.currentTimeMillis() - startTimeCallbacks));
+        if (mOnResumeCallbacks.size() > 0) {
+            // We might have postponed some bind calls until onResume (see waitUntilResume) --
+            // execute them here
+            long startTimeCallbacks = 0;
+            if (DEBUG_RESUME_TIME) {
+                startTimeCallbacks = System.currentTimeMillis();
+            }
+
+            if (mAppsCustomizeContent != null) {
+                mAppsCustomizeContent.setBulkBind(true);
+            }
+            for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
+                mOnResumeCallbacks.get(i).run();
+            }
+            if (mAppsCustomizeContent != null) {
+                mAppsCustomizeContent.setBulkBind(false);
+            }
+            mOnResumeCallbacks.clear();
+            if (DEBUG_RESUME_TIME) {
+                Log.d(TAG, "Time spent processing callbacks in onResume: " +
+                    (System.currentTimeMillis() - startTimeCallbacks));
+            }
         }
 
         // Reset the pressed state of icons that were locked in the press state while activities
@@ -3308,9 +3318,13 @@
      * @return true if we are currently paused.  The caller might be able to
      * skip some work in that case since we will come back again.
      */
-    private boolean waitUntilResume(Runnable run) {
+    private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
         if (mPaused) {
             Log.i(TAG, "Deferring update until onResume");
+            if (deletePreviousRunnables) {
+                while (mOnResumeCallbacks.remove(run)) {
+                }
+            }
             mOnResumeCallbacks.add(run);
             return true;
         } else {
@@ -3318,6 +3332,10 @@
         }
     }
 
+    private boolean waitUntilResume(Runnable run) {
+        return waitUntilResume(run, false);
+    }
+
     /**
      * If the activity is currently paused, signal that we need to re-run the loader
      * in onResume.
@@ -3761,17 +3779,23 @@
     /**
      * A number of packages were updated.
      */
-    public void bindPackagesUpdated() {
-        if (waitUntilResume(new Runnable() {
+
+    private ArrayList<Object> mWidgetsAndShortcuts;
+    private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
             public void run() {
-                bindPackagesUpdated();
+                bindPackagesUpdated(mWidgetsAndShortcuts);
+                mWidgetsAndShortcuts = null;
             }
-        })) {
+        };
+
+    public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
+        if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
+            mWidgetsAndShortcuts = widgetsAndShortcuts;
             return;
         }
 
         if (mAppsCustomizeContent != null) {
-            mAppsCustomizeContent.onPackagesUpdated();
+            mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
         }
     }