New launcher2 icon migration algorithm.

The user will be able to request "icon migration", which is
not a direct mapping of the old workspace, but rather
follows this heuristic for bringing the user's favorite
icons (by dint of their existence on the workspace) into
Launcher3:

Workspace shortcuts are placed in lexicographic order on the workspace
starting at screen 0 (leaving the bottom row of screen 0 empty to make
sure there's room to move things around). Folders are preserved and
their contents sorted. Duplicate icons (that is, shortcuts
with the same intent, pursuant to some cleanups) are removed.

Hotseat icons are migrated in their original place, unless their new
location is not accommodated by the hotseat (i.e. the L3 hotseat is
too small on this device), in which case they're treated like any
other shortcut and tossed into the workspace.

To test, turn on Launcher.ENABLE_DEBUG_INTENTS and then:

$ adb shell am broadcast -a com.android.launcher3.action.DELETE_DATABASE
$ adb shell am broadcast -a com.android.launcher3.action.MIGRATE_DATABASE

Bug: 12416411
Change-Id: Ia5c56f36c11455867ea20a39f70210f595020a87
diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java
index b2cfb24..460ab5d 100644
--- a/src/com/android/launcher3/LauncherModel.java
+++ b/src/com/android/launcher3/LauncherModel.java
@@ -75,6 +75,10 @@
     // false = strew non-workspace apps across the workspace on upgrade
     public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
 
+    public static final int LOADER_FLAG_NONE = 0;
+    public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
+    public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
+
     private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
     private static final long INVALID_SCREEN_ID = -1L;
     private final boolean mAppsCanBeOnRemoveableStorage;
@@ -1193,6 +1197,10 @@
     }
 
     public void startLoader(boolean isLaunching, int synchronousBindPage) {
+        startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
+    }
+
+    public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
         synchronized (mLock) {
             if (DEBUG_LOADERS) {
                 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
@@ -1207,7 +1215,7 @@
                 // If there is already one running, tell it to stop.
                 // also, don't downgrade isLaunching if we're already running
                 isLaunching = isLaunching || stopLoaderLocked();
-                mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
+                mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
                 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
                         && mAllAppsLoaded && mWorkspaceLoaded) {
                     mLoaderTask.runBindSynchronousPage(synchronousBindPage);
@@ -1298,13 +1306,15 @@
         private boolean mIsLoadingAndBindingWorkspace;
         private boolean mStopped;
         private boolean mLoadAndBindStepFinished;
+        private int mFlags;
 
         private HashMap<Object, CharSequence> mLabelCache;
 
-        LoaderTask(Context context, boolean isLaunching) {
+        LoaderTask(Context context, boolean isLaunching, int flags) {
             mContext = context;
             mIsLaunching = isLaunching;
             mLabelCache = new HashMap<Object, CharSequence>();
+            mFlags = flags;
         }
 
         boolean isLaunching() {
@@ -1651,7 +1661,7 @@
             }
         }
 
-        /** Returns whether this is an upgradge path */
+        /** Returns whether this is an upgrade path */
         private boolean loadWorkspace() {
             // Log to disk
             Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
@@ -1669,8 +1679,20 @@
             int countX = (int) grid.numColumns;
             int countY = (int) grid.numRows;
 
-            // Make sure the default workspace is loaded, if needed
-            LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
+            if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
+                Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
+                LauncherAppState.getLauncherProvider().deleteDatabase();
+            }
+
+            if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
+                // append the user's Launcher2 shortcuts
+                Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
+                LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
+            } else {
+                // Make sure the default workspace is loaded
+                Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
+                LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
+            }
 
             // Check if we need to do any upgrade-path logic
             // (Includes having just imported default favorites)