ignore drop position when dragging items to small screens

- adding plumbing for future support for spring-loaded adding of items
- also, additional minor code cleanup

Change-Id: Idb313d4cd125b4f0b315a845dc8fb853cb48885e
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index 232fef4..2a2d364 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -1101,7 +1101,7 @@
         // supporting spring-loaded mini-screens; however, leaving the ability to directly place
         // a widget on the home screen in case we want to add it in the future
         int[] touchXY = null;
-        if (mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
+        if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
             touchXY = mAddDropPosition;
         }
         boolean findNearestVacantAreaFailed = false;
@@ -1114,15 +1114,12 @@
             findNearestVacantAreaFailed = (result == null);
             foundCellSpan = !findNearestVacantAreaFailed;
         } else {
-            if (mAddIntersectCellX != -1 && mAddIntersectCellY != -1) {
-                // if we long pressed on an empty cell to bring up a menu,
-                // make sure we intersect the empty cell
-                foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
-                        mAddIntersectCellX, mAddIntersectCellY);
-            } else {
-                // if we went through the menu -> add, just find any spot
-                foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
-            }
+            // if we long pressed on an empty cell to bring up a menu,
+            // make sure we intersect the empty cell
+            // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
+            // findCellForSpanThatIntersects will just ignore them
+            foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
+                    mAddIntersectCellX, mAddIntersectCellY);
         }
 
         if (!foundCellSpan) {
@@ -2778,7 +2775,9 @@
     }
 
     void showWorkspace(boolean animated, CellLayout layout) {
-        if (layout != null && animated) {
+        if (layout != null) {
+            // always animated, but that's ok since we never specify a layout and
+            // want no animation
             mWorkspace.unshrink(layout);
         } else {
             mWorkspace.unshrink(animated);
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index bfe5718..d99e9af 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -1168,24 +1168,14 @@
         updateWhichPagesAcceptDrops(mShrunkenState);
     }
 
-    // We call this when we trigger an unshrink by clicking on the CellLayout cl
-    public void unshrink(CellLayout clThatWasClicked) {
-        int newCurrentPage = mCurrentPage;
-        final int screenCount = getChildCount();
-        for (int i = 0; i < screenCount; i++) {
-            if (getChildAt(i) == clThatWasClicked) {
-                newCurrentPage = i;
-            }
-        }
-        unshrink(newCurrentPage);
-    }
-
     @Override
     protected boolean handlePagingClicks() {
         return true;
     }
 
-    private void unshrink(int newCurrentPage) {
+    // We call this when we trigger an unshrink by clicking on the CellLayout cl
+    public void unshrink(CellLayout clThatWasClicked) {
+        int newCurrentPage = indexOfChild(clThatWasClicked);
         if (mIsSmall) {
             int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
             int delta = newX - mScrollX;
@@ -1528,7 +1518,13 @@
         }
 
         if (source != this) {
-            onDropExternal(originX, originY, dragInfo, mDragTargetLayout);
+            if (mIsSmall) {
+                // if we drag and drop to small screens, don't pass the touch x/y coords (when we
+                // enable spring-loaded adding, however, we do want to pass the touch x/y coords)
+                onDropExternal(-1, -1, dragInfo, mDragTargetLayout);
+            } else {
+                onDropExternal(originX, originY, dragInfo, mDragTargetLayout);
+            }
         } else if (mDragInfo != null) {
             final View cell = mDragInfo.cell;
             CellLayout dropTargetLayout = mDragTargetLayout;
@@ -2045,6 +2041,7 @@
             PendingAddItemInfo info = (PendingAddItemInfo) dragInfo;
             // When dragging and dropping from customization tray, we deal with creating
             // widgets/shortcuts/folders in a slightly different way
+            // Only set touchXY if you are supporting spring loaded adding of items
             int[] touchXY = new int[2];
             touchXY[0] = x;
             touchXY[1] = y;
@@ -2063,38 +2060,37 @@
             }
             cellLayout.onDragExit();
             cellLayout.animateDrop();
-            return;
-        }
-
-        // This is for other drag/drop cases, like dragging from All Apps
-        ItemInfo info = (ItemInfo) dragInfo;
-
-        View view = null;
-
-        switch (info.itemType) {
-        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
-        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
-            if (info.container == NO_ID && info instanceof ApplicationInfo) {
-                // Came from all apps -- make a copy
-                info = new ShortcutInfo((ApplicationInfo) info);
-            }
-            view = mLauncher.createShortcut(R.layout.application, cellLayout,
-                    (ShortcutInfo) info);
-            break;
-        case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
-            view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher,
-                    cellLayout, (UserFolderInfo) info, mIconCache);
-            break;
-        default:
-            throw new IllegalStateException("Unknown item type: " + info.itemType);
-        }
-
-        // If the view is null, it has already been added.
-        if (view == null) {
-            cellLayout.onDragExit();
         } else {
-            mTargetCell = new int[]{x, y};
-            cellLayout.findCellForSpan(mTargetCell, 1, 1);
+            // This is for other drag/drop cases, like dragging from All Apps
+            ItemInfo info = (ItemInfo) dragInfo;
+
+            View view = null;
+
+            switch (info.itemType) {
+            case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
+            case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
+                if (info.container == NO_ID && info instanceof ApplicationInfo) {
+                    // Came from all apps -- make a copy
+                    info = new ShortcutInfo((ApplicationInfo) info);
+                }
+                view = mLauncher.createShortcut(R.layout.application, cellLayout,
+                        (ShortcutInfo) info);
+                break;
+            case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
+                view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher,
+                        cellLayout, (UserFolderInfo) info, mIconCache);
+                break;
+            default:
+                throw new IllegalStateException("Unknown item type: " + info.itemType);
+            }
+
+            mTargetCell = new int[2];
+            if (x != -1 && y != -1) {
+                // when dragging and dropping, just find the closest free spot
+                cellLayout.findNearestVacantArea(x, y, 1, 1, mTargetCell);
+            } else {
+                cellLayout.findCellForSpan(mTargetCell, 1, 1);
+            }
             addInScreen(view, indexOfChild(cellLayout), mTargetCell[0],
                     mTargetCell[1], info.spanX, info.spanY, insertAtFirst);
             cellLayout.onDropChild(view);