HACK: Fixing major persistence regression associated with extra screen to the left

-> Also adding ability to delete shortcuts.

Change-Id: I263baf813eae9089f11288494d575a99f0e859c3
diff --git a/src/com/android/launcher3/DeleteDropTarget.java b/src/com/android/launcher3/DeleteDropTarget.java
index a1090dc..0ccdae1 100644
--- a/src/com/android/launcher3/DeleteDropTarget.java
+++ b/src/com/android/launcher3/DeleteDropTarget.java
@@ -128,13 +128,18 @@
 
     @Override
     public boolean acceptDrop(DragObject d) {
-        // We can remove everything including App shortcuts, folders, widgets, etc.
-        if ((d.dragInfo instanceof LauncherAppWidgetInfo) ||
-                (d.dragInfo instanceof PendingAddWidgetInfo)) {
-            return true;
-        } else {
-            return false;
+        return willAcceptDrop(d.dragInfo);
+    }
+
+    public static boolean willAcceptDrop(Object info) {
+        if (info instanceof ItemInfo) {
+            ItemInfo item = (ItemInfo) info;
+            if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET ||
+                    item.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
+                return true;
+            }
         }
+        return false;
     }
 
     @Override
@@ -149,9 +154,7 @@
 
         // If we are dragging an application from AppsCustomize, only show the control if we can
         // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
-
-        if ((info instanceof LauncherAppWidgetInfo) ||
-                (info instanceof PendingAddWidgetInfo)) {
+        if (willAcceptDrop(info)) {
             isVisible = true;
         } else {
             isVisible = false;
diff --git a/src/com/android/launcher3/DragController.java b/src/com/android/launcher3/DragController.java
index e0de5b4..54f7dbc 100644
--- a/src/com/android/launcher3/DragController.java
+++ b/src/com/android/launcher3/DragController.java
@@ -444,6 +444,9 @@
                 mLastTouchUpTime = System.currentTimeMillis();
                 if (mDragging) {
                     PointF vec = isFlingingToDelete(mDragObject.dragSource);
+                    if (!DeleteDropTarget.willAcceptDrop(mDragObject.dragInfo)) {
+                        vec = null;
+                    }
                     if (vec != null) {
                         dropOnFlingToDeleteTarget(dragLayerX, dragLayerY, vec);
                     } else {
@@ -597,8 +600,7 @@
 
             if (mDragging) {
                 PointF vec = isFlingingToDelete(mDragObject.dragSource);
-                if (!(mDragObject.dragInfo instanceof LauncherAppWidgetInfo) &&
-                        !(mDragObject.dragInfo instanceof PendingAddWidgetInfo)) {
+                if (!DeleteDropTarget.willAcceptDrop(mDragObject.dragInfo)) {
                     vec = null;
                 }
                 if (vec != null) {
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 2634e21..7f3c482 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -840,7 +840,7 @@
         mWorkspace.addView(customScreen, 0);
 
         // Ensure that the current page and default page are maintained.
-        mWorkspace.incrementDefaultScreen();
+        mWorkspace.incrementNumScreensToLeft();
         mWorkspace.setCurrentPage(mWorkspace.getCurrentPage() + 1);
     }
 
@@ -1162,7 +1162,8 @@
             return;
         }
 
-        LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
+        int adjustedScreen = screen - getWorkspace().mNumPagesToLeft;
+        LauncherModel.addItemToDatabase(this, info, container, adjustedScreen, cellXY[0], cellXY[1], false);
 
         if (!mRestoring) {
             mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
@@ -1262,8 +1263,9 @@
         launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
         launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
 
+        int adjustedScreen = screen - getWorkspace().mNumPagesToLeft;
         LauncherModel.addItemToDatabase(this, launcherInfo,
-                container, screen, cellXY[0], cellXY[1], false);
+                container, adjustedScreen, cellXY[0], cellXY[1], false);
 
         if (!mRestoring) {
             if (hostView == null) {
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index 41f0242..9a75cc1 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -105,6 +105,7 @@
     private IBinder mWindowToken;
     private static final float WALLPAPER_SCREENS_SPAN = 2f;
 
+    public int mNumPagesToLeft = 0;
     private int mDefaultPage;
 
     /**
@@ -403,8 +404,9 @@
 
     // Just a hack so that if a custom content screen is added to the left, we adjust the
     // default screen accordingly so that it stays the same.
-    void incrementDefaultScreen() {
+    void incrementNumScreensToLeft() {
         mDefaultPage++;
+        mNumPagesToLeft++;
     }
 
     /**
@@ -1963,7 +1965,9 @@
         final int[] cellXY = new int[2];
         target.findCellForSpanThatIntersects(cellXY, 1, 1, intersectX, intersectY);
         addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1, insertAtFirst);
-        LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screen, cellXY[0],
+
+        int adjustedScreen = screen - mNumPagesToLeft;
+        LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, adjustedScreen, cellXY[0],
                 cellXY[1]);
     }
 
@@ -2315,7 +2319,10 @@
                         }
                     }
 
-                    LauncherModel.moveItemInDatabase(mLauncher, info, container, screen, lp.cellX,
+                    //TODO: This is a hack on top of a hack, but items aren't being saved
+                    // to the correct screen due to the extra screen.
+                    int adjustedScreen = screen - mNumPagesToLeft;
+                    LauncherModel.moveItemInDatabase(mLauncher, info, container, adjustedScreen, lp.cellX,
                             lp.cellY);
                 } else {
                     // If we can't find a drop location, we return the item to its original position
@@ -3176,8 +3183,8 @@
             CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams();
             cellLayout.getShortcutsAndWidgets().measureChild(view);
 
-
-            LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screen,
+            int adjustedScreen = screen - mNumPagesToLeft;
+            LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, adjustedScreen,
                     lp.cellX, lp.cellY);
 
             if (d.dragView != null) {
@@ -3424,7 +3431,8 @@
             // Null check required as the AllApps button doesn't have an item info
             if (info != null && info.requiresDbUpdate) {
                 info.requiresDbUpdate = false;
-                LauncherModel.modifyItemInDatabase(mLauncher, info, container, screen, info.cellX,
+                int adjustedScreen = screen - mNumPagesToLeft;
+                LauncherModel.modifyItemInDatabase(mLauncher, info, container, adjustedScreen, info.cellX,
                         info.cellY, info.spanX, info.spanY);
             }
         }