Fixing bugs with new widget adding

-> widget ids were being reused in some cases
-> widgets could be added before springloaded mode was entered

Change-Id: Iaf3bffc49d4ec2de4b63db1b5cfb52b8544e9c2b
diff --git a/src/com/android/launcher2/AppsCustomizePagedView.java b/src/com/android/launcher2/AppsCustomizePagedView.java
index de96eda..91205d0 100644
--- a/src/com/android/launcher2/AppsCustomizePagedView.java
+++ b/src/com/android/launcher2/AppsCustomizePagedView.java
@@ -239,6 +239,7 @@
     static final int WIDGET_INFLATED = 1;
     int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
     int mWidgetLoadingId = -1;
+    PendingAddWidgetInfo mCreateWidgetInfo = null;
 
     public AppsCustomizePagedView(Context context, AttributeSet attrs) {
         super(context, attrs);
@@ -580,13 +581,14 @@
     public void onShortPress(View v) {
         // We are anticipating a long press, and we use this time to load bind and instantiate
         // the widget. This will need to be cleaned up if it turns out no long press occurs.
-        PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
-        loadWidgetInBackground(createWidgetInfo);
+        mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
+        loadWidgetInBackground(mCreateWidgetInfo);
     }
 
     @Override
     public void cleanUpShortPress(View v) {
-        PendingAddWidgetInfo info = (PendingAddWidgetInfo) v.getTag();
+        PendingAddWidgetInfo info = mCreateWidgetInfo;
+        mCreateWidgetInfo = null;
         if (mWidgetCleanupState >= 0 && mWidgetLoadingId != -1) {
             mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
         }
@@ -613,7 +615,9 @@
         Bitmap preview;
         Bitmap outline;
         if (createItemInfo instanceof PendingAddWidgetInfo) {
-            PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
+            PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
+            createItemInfo = createWidgetInfo;
+            mCreateWidgetInfo = null;
             int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null);
             int[] size = mLauncher.getWorkspace().estimateItemSize(spanXY[0],
                     spanXY[1], createWidgetInfo, true);
@@ -677,19 +681,23 @@
         postDelayed(new Runnable() {
             @Override
             public void run() {
-                // Dismiss the cling
-                mLauncher.dismissAllAppsCling(null);
+                // We don't enter spring-loaded mode if the drag has been cancelled
+                if (mLauncher.getDragController().isDragging()) {
+                    // Dismiss the cling
+                    mLauncher.dismissAllAppsCling(null);
 
-                // Reset the alpha on the dragged icon before we drag
-                resetDrawableState();
+                    // Reset the alpha on the dragged icon before we drag
+                    resetDrawableState();
 
-                // Go into spring loaded mode (must happen before we startDrag())
-                mLauncher.enterSpringLoadedDragMode();
+                    // Go into spring loaded mode (must happen before we startDrag())
+                    mLauncher.enterSpringLoadedDragMode();
+                }
             }
         },150);
 
         return true;
     }
+
     private void endDragging(View target, boolean success) {
         mLauncher.getWorkspace().onDragStopped(success);
         if (!success || (target != mLauncher.getWorkspace() &&
@@ -699,7 +707,6 @@
             mLauncher.exitSpringLoadedDragMode();
         }
         mLauncher.unlockScreenOrientationOnLargeUI();
-
     }
 
     @Override
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index 708d5d6..4062145 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -582,6 +582,7 @@
         mWorkspace.animateExternalDrop(mWidgetBeingConfigured, cellLayout,
                 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
                 animationType);
+        mWidgetBeingConfigured = null;
     }
 
     @Override
diff --git a/src/com/android/launcher2/PendingAddItemInfo.java b/src/com/android/launcher2/PendingAddItemInfo.java
index 851dddb..09a8a9e 100644
--- a/src/com/android/launcher2/PendingAddItemInfo.java
+++ b/src/com/android/launcher2/PendingAddItemInfo.java
@@ -57,4 +57,18 @@
             configurationData = data;
         }
     }
-}
\ No newline at end of file
+
+    // Copy constructor
+    public PendingAddWidgetInfo(PendingAddWidgetInfo copy) {
+        minWidth = copy.minWidth;
+        minHeight = copy.minHeight;
+        previewImage = copy.previewImage;
+        icon = copy.icon;
+        info = copy.info;
+        boundWidget = copy.boundWidget;
+        mimeType = copy.mimeType;
+        configurationData = copy.configurationData;
+        componentName = copy.componentName;
+        itemType = copy.itemType;
+    }
+}
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index c8cab16..309fda8 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -1950,7 +1950,7 @@
     }
 
     public boolean transitionStateShouldAllowDrop() {
-        return (!isSwitchingState() || mTransitionProgress > 0.5f);
+        return ((!isSwitchingState() || mTransitionProgress > 0.5f) && mState != State.SMALL);
     }
 
     /**