Removing workspace screens on load and ensuring we don't bind on new apps until returning to Launcher.

- Updating print_db to print workspace screens

Change-Id: Id477f5a5649c0d3b7ab0cce5ed99ca0a519d7706
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index c29e74f..6bd66d4 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -3518,14 +3518,10 @@
         // from scratch again
         mBindOnResumeCallbacks.clear();
 
-        final Workspace workspace = mWorkspace;
+        // Clear the workspace because it's going to be rebound
         mWorkspace.clearDropTargets();
-        int count = workspace.getChildCount();
-        for (int i = 0; i < count; i++) {
-            // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
-            final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
-            layoutParent.removeAllViewsInLayout();
-        }
+        mWorkspace.removeAllViews();
+
         mWidgetsToAdvance.clear();
         if (mHotseat != null) {
             mHotseat.resetLayout();
@@ -3571,6 +3567,35 @@
         }
     }
 
+    public void bindAppsAdded(final ArrayList<Long> newScreens,
+                              final ArrayList<ItemInfo> addNotAnimated,
+                              final ArrayList<ItemInfo> addAnimated) {
+        Runnable r = new Runnable() {
+            public void run() {
+                bindAppsAdded(newScreens, addNotAnimated, addAnimated);
+            }
+        };
+        if (waitUntilResume(r)) {
+            return;
+        }
+
+        Log.w(TAG, "10249126 - bindAppsAdded(" + newScreens.size() + ")");
+
+        // Add the new screens
+        bindAddScreens(newScreens);
+
+        // We add the items without animation on non-visible pages, and with
+        // animations on the new page (which we will try and snap to).
+        if (!addNotAnimated.isEmpty()) {
+            bindItems(addNotAnimated, 0,
+                    addNotAnimated.size(), false);
+        }
+        if (!addAnimated.isEmpty()) {
+            bindItems(addAnimated, 0,
+                    addAnimated.size(), true);
+        }
+    }
+
     /**
      * Bind the items start-end from the list.
      *
@@ -3578,11 +3603,12 @@
      */
     public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
                           final boolean forceAnimateIcons) {
-        if (waitUntilResume(new Runnable() {
-                public void run() {
-                    bindItems(shortcuts, start, end, forceAnimateIcons);
-                }
-            })) {
+        Runnable r = new Runnable() {
+            public void run() {
+                bindItems(shortcuts, start, end, forceAnimateIcons);
+            }
+        };
+        if (waitUntilResume(r)) {
             return;
         }
 
@@ -3666,11 +3692,12 @@
      * Implementation of the method from LauncherModel.Callbacks.
      */
     public void bindFolders(final HashMap<Long, FolderInfo> folders) {
-        if (waitUntilResume(new Runnable() {
-                public void run() {
-                    bindFolders(folders);
-                }
-            })) {
+        Runnable r = new Runnable() {
+            public void run() {
+                bindFolders(folders);
+            }
+        };
+        if (waitUntilResume(r)) {
             return;
         }
         sFolders.clear();
@@ -3683,11 +3710,12 @@
      * Implementation of the method from LauncherModel.Callbacks.
      */
     public void bindAppWidget(final LauncherAppWidgetInfo item) {
-        if (waitUntilResume(new Runnable() {
-                public void run() {
-                    bindAppWidget(item);
-                }
-            })) {
+        Runnable r = new Runnable() {
+            public void run() {
+                bindAppWidget(item);
+            }
+        };
+        if (waitUntilResume(r)) {
             return;
         }
 
@@ -3730,11 +3758,12 @@
      * Implementation of the method from LauncherModel.Callbacks.
      */
     public void finishBindingItems(final boolean upgradePath) {
-        if (waitUntilResume(new Runnable() {
-                public void run() {
-                    finishBindingItems(upgradePath);
-                }
-            })) {
+        Runnable r = new Runnable() {
+            public void run() {
+                finishBindingItems(upgradePath);
+            }
+        };
+        if (waitUntilResume(r)) {
             return;
         }
         if (mSavedState != null) {
@@ -3820,11 +3849,12 @@
      * Implementation of the method from LauncherModel.Callbacks.
      */
     public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
-        if (waitUntilResume(new Runnable() {
-                public void run() {
-                    bindAppsUpdated(apps);
-                }
-            })) {
+        Runnable r = new Runnable() {
+            public void run() {
+                bindAppsUpdated(apps);
+            }
+        };
+        if (waitUntilResume(r)) {
             return;
         }
 
@@ -3845,11 +3875,12 @@
     public void bindComponentsRemoved(final ArrayList<String> packageNames,
                                       final ArrayList<ApplicationInfo> appInfos,
                                       final boolean packageRemoved) {
-        if (waitUntilResume(new Runnable() {
+        Runnable r = new Runnable() {
             public void run() {
                 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
             }
-        })) {
+        };
+        if (waitUntilResume(r)) {
             return;
         }
 
diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java
index cd37a16..50e84b7 100644
--- a/src/com/android/launcher3/LauncherModel.java
+++ b/src/com/android/launcher3/LauncherModel.java
@@ -156,6 +156,9 @@
         public void finishBindingItems(boolean upgradePath);
         public void bindAppWidget(LauncherAppWidgetInfo info);
         public void bindAllApplications(ArrayList<ApplicationInfo> apps);
+        public void bindAppsAdded(ArrayList<Long> newScreens,
+                                  ArrayList<ItemInfo> addNotAnimated,
+                                  ArrayList<ItemInfo> addAnimated);
         public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
         public void bindComponentsRemoved(ArrayList<String> packageNames,
                         ArrayList<ApplicationInfo> appInfos,
@@ -275,6 +278,7 @@
     }
     public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
                                     final Callbacks callbacks) {
+        Log.w(TAG, "10249126 - addAndBindAddedApps()");
         if (added.isEmpty()) {
             throw new RuntimeException("EMPTY ADDED ARRAY?");
         }
@@ -354,6 +358,8 @@
                     }
                 }
 
+                Log.w(TAG, "10249126 - addAndBindAddedApps - updateWorkspaceScreenOrder(" + workspaceScreens.size() + ")");
+
                 // Update the workspace screens
                 updateWorkspaceScreenOrder(context, workspaceScreens);
 
@@ -362,8 +368,6 @@
                         public void run() {
                             Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
                             if (callbacks == cb && cb != null) {
-                                callbacks.bindAddScreens(addedWorkspaceScreensFinal);
-
                                 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
                                 long lastScreenId = info.screenId;
                                 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
@@ -375,16 +379,8 @@
                                         addNotAnimated.add(i);
                                     }
                                 }
-                                // We add the items without animation on non-visible pages, and with
-                                // animations on the new page (which we will try and snap to).
-                                if (!addNotAnimated.isEmpty()) {
-                                    callbacks.bindItems(addNotAnimated, 0,
-                                            addNotAnimated.size(), false);
-                                }
-                                if (!addAnimated.isEmpty()) {
-                                    callbacks.bindItems(addAnimated, 0,
-                                            addAnimated.size(), true);
-                                }
+                                callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
+                                        addNotAnimated, addAnimated);
                             }
                         }
                     });
@@ -1905,6 +1901,7 @@
                 }
 
                 if (loadedOldDb) {
+                    Log.w(TAG, "10249126 - loadWorkspace - loadedOldDb");
                     long maxScreenId = 0;
                     // If we're importing we use the old screen order.
                     for (ItemInfo item: sBgItemsIdMap.values()) {
@@ -1931,6 +1928,7 @@
                     LauncherAppState app = LauncherAppState.getInstance();
                     app.getLauncherProvider().updateMaxItemId(maxItemId);
                 } else {
+                    Log.w(TAG, "10249126 - loadWorkspace - !loadedOldDb");
                     TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
                     for (Integer i : orderedScreens.keySet()) {
                         sBgWorkspaceScreens.add(orderedScreens.get(i));
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index 77f726a..7d3a830 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -1718,6 +1718,8 @@
             CellLayout cl = ((CellLayout) getChildAt(i));
             mScreenOrder.add(getIdForScreen(cl));
         }
+
+        Log.w(TAG, "10249126 - onEndReordering()");
         mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder);
 
         // Re-enable auto layout transitions for page deletion.