Fixing drag and drop issues with AppsCustomize pane.

- Fixing issue with no-more-space error not showing
- Returning to first page of AllApps when leaving and coming back

Change-Id: If3fb1ba1abae9d453bb71214e44170df91a52189
diff --git a/src/com/android/launcher2/AppsCustomizePagedView.java b/src/com/android/launcher2/AppsCustomizePagedView.java
index f0a712a..5f86f7e 100644
--- a/src/com/android/launcher2/AppsCustomizePagedView.java
+++ b/src/com/android/launcher2/AppsCustomizePagedView.java
@@ -362,14 +362,16 @@
     protected boolean beginDragging(View v) {
         if (!super.beginDragging(v)) return false;
 
+        // Hide the pane so that the user can drop onto the workspace, we must do this first,
+        // due to how the drop target layout is computed when we start dragging to the workspace.
+        mLauncher.showWorkspace(true);
+
         if (v instanceof PagedViewIcon) {
             beginDraggingApplication(v);
         } else if (v instanceof PagedViewWidget) {
             beginDraggingWidget(v);
         }
 
-        // Hide the pane so that the user can drop onto the workspace
-        mLauncher.showWorkspace(true);
         return true;
     }
     private void endDragging(boolean success) {
@@ -401,6 +403,27 @@
     @Override
     public void onDropCompleted(View target, Object dragInfo, boolean success) {
         endDragging(success);
+
+        // Display an error message if the drag failed due to there not being enough space on the
+        // target layout we were dropping on.
+        if (!success) {
+            boolean showOutOfSpaceMessage = false;
+            if (target instanceof Workspace) {
+                int currentScreen = mLauncher.getCurrentWorkspaceScreen();
+                Workspace workspace = (Workspace) target;
+                CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
+                ItemInfo itemInfo = (ItemInfo) dragInfo;
+                if (layout != null) {
+                    layout.calculateSpans(itemInfo);
+                    showOutOfSpaceMessage =
+                            !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
+                }
+            }
+            // TODO-APPS_CUSTOMIZE: We need to handle this for folders as well later.
+            if (showOutOfSpaceMessage) {
+                mLauncher.showOutOfSpaceMessage();
+            }
+        }
     }
 
     public void setContentType(ContentType type) {
@@ -717,8 +740,15 @@
     }
     @Override
     public void reset() {
-        setCurrentPage(0);
-        invalidatePageData();
+        if (mContentType != ContentType.Applications) {
+            // Reset to the first page of the Apps pane
+            AppsCustomizeTabHost tabs = (AppsCustomizeTabHost)
+                    mLauncher.findViewById(R.id.apps_customize_pane);
+            tabs.setCurrentTabByTag(tabs.getTabTagForContentType(ContentType.Applications));
+        } else {
+            setCurrentPage(0);
+            invalidatePageData();
+        }
     }
     @Override
     public void dumpState() {
diff --git a/src/com/android/launcher2/AppsCustomizeTabHost.java b/src/com/android/launcher2/AppsCustomizeTabHost.java
index 5820ce7..15920c3 100644
--- a/src/com/android/launcher2/AppsCustomizeTabHost.java
+++ b/src/com/android/launcher2/AppsCustomizeTabHost.java
@@ -96,6 +96,18 @@
     }
 
     /**
+     * Returns the tab tag for a given content type.
+     */
+    public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
+        if (type == AppsCustomizePagedView.ContentType.Applications) {
+            return APPS_TAB_TAG;
+        } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
+            return WIDGETS_TAB_TAG;
+        }
+        return APPS_TAB_TAG;
+    }
+
+    /**
      * Disable focus on anything under this view in the hierarchy if we are not visible.
      */
     @Override
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index dc22d84..2d15ffa 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -2391,7 +2391,9 @@
         if (source != this) {
             final int[] touchXY = new int[] { (int) mDragViewVisualCenter[0],
                     (int) mDragViewVisualCenter[1] };
-            if ((mIsSmall || mIsInUnshrinkAnimation) && !mLauncher.isAllAppsVisible()) {
+            if (LauncherApplication.isScreenXLarge()
+                    && (mIsSmall || mIsInUnshrinkAnimation)
+                    && !mLauncher.isAllAppsVisible()) {
                 // When the workspace is shrunk and the drop comes from customize, don't actually
                 // add the item to the screen -- customize will do this itself
                 ((ItemInfo) dragInfo).dropPos = touchXY;
@@ -2893,7 +2895,7 @@
                 mLastDragYOffset = yOffset;
                 layout = findMatchingPageForDragOver(dragView, left, top, xOffset, yOffset);
 
-                if (layout != mDragTargetLayout) {
+                if (layout != null && layout != mDragTargetLayout) {
                     if (mDragTargetLayout != null) {
                         mDragTargetLayout.setIsDragOverlapping(false);
                         mSpringLoadedDragController.onDragExit();