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/print_db.py b/print_db.py
index 6caa7bb..05237d0 100755
--- a/print_db.py
+++ b/print_db.py
@@ -34,6 +34,10 @@
   shutil.rmtree(DIR, True)
   os.makedirs(DIR)
 
+def adb_root_remount():
+  os.system("adb root")
+  os.system("adb remount")
+
 def pull_file(fn):
   print "pull_file: " + fn
   rv = os.system("adb pull"
@@ -52,6 +56,15 @@
     rows.append(row)
   return columns,rows
 
+def get_screens(conn):
+  c = conn.cursor()
+  c.execute("SELECT * FROM workspaceScreens")
+  columns = [d[0] for d in c.description]
+  rows = []
+  for row in c:
+    rows.append(row)
+  return columns,rows
+
 def print_intent(out, id, i, cell):
   if cell:
     out.write("""<span class="intent" title="%s">shortcut</span>""" % (
@@ -127,13 +140,21 @@
       out.write("<b>unknown type: %d</b>" % itemType)
     out.write("</td>\n")
 
+def render_screen_info(out, screen):
+  out.write("<tr>")
+  out.write("<td>%s</td>" % (screen["_id"]))
+  out.write("<td>%s</td>" % (screen["screenRank"]))
+  out.write("</tr>")
+
 def process_file(fn):
   global SCREENS, COLUMNS, ROWS, HOTSEAT_SIZE
   print "process_file: " + fn
   conn = sqlite3.connect(fn)
   columns,rows = get_favorites(conn)
+  screenCols, screenRows = get_screens(conn)
 
   data = [dict(zip(columns,row)) for row in rows]
+  screenData = [dict(zip(screenCols, screenRow)) for screenRow in screenRows]
 
   # Calculate the proper number of screens, columns, and rows in this db
   screensIdMap = []
@@ -198,6 +219,14 @@
   out.write("""</table>
 """)
 
+  # Screens
+  out.write("<br/><b>Screens</b><br/>\n")
+  out.write("<table class=layout border=1 cellspacing=0 cellpadding=4>\n")
+  out.write("<tr><td>Screen ID</td><td>Rank</td></tr>\n")
+  for screen in screenData:
+    render_screen_info(out, screen)
+  out.write("</table>\n")
+
   # Hotseat
   hotseat = []
   for i in range(0, HOTSEAT_SIZE):
@@ -274,6 +303,7 @@
 def main(argv):
   if len(argv) == 1 or (len(argv) == 2 and updateDeviceClassConstants(argv[1])):
     make_dir()
+    adb_root_remount()
     pull_file(AUTO_FILE)
     process_file(AUTO_FILE)
   elif len(argv) == 2 or (len(argv) == 3 and updateDeviceClassConstants(argv[2])):
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.