Fixing a couple bugs, allapps clicks and weird crash

-> issue 5021897 (when this was fixed, exposed more bugginess that is now fixed)
-> issue 5038392

Change-Id: I49a4e9fca154f75ea22ad2c462641747536102ce
diff --git a/src/com/android/launcher2/AppsCustomizePagedView.java b/src/com/android/launcher2/AppsCustomizePagedView.java
index c127ecd..ca11c1e 100644
--- a/src/com/android/launcher2/AppsCustomizePagedView.java
+++ b/src/com/android/launcher2/AppsCustomizePagedView.java
@@ -350,6 +350,10 @@
 
     @Override
     public void onClick(View v) {
+        // When we have exited all apps or are in transition, disregard clicks
+        if (!mLauncher.isAllAppsCustomizeOpen() ||
+                mLauncher.getWorkspace().isSwitchingState()) return;
+
         if (v instanceof PagedViewIcon) {
             // Animate some feedback to the click
             final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
@@ -459,11 +463,6 @@
 
     }
 
-    /*
-     * DragSource implementation
-     */
-    @Override
-    public void onDragViewVisible() {}
     @Override
     public void onDropCompleted(View target, DragObject d, boolean success) {
         endDragging(target, success);
diff --git a/src/com/android/launcher2/CellLayout.java b/src/com/android/launcher2/CellLayout.java
index bd2a949..de9c188 100644
--- a/src/com/android/launcher2/CellLayout.java
+++ b/src/com/android/launcher2/CellLayout.java
@@ -1454,23 +1454,12 @@
     void onDropChild(View child) {
         if (child != null) {
             LayoutParams lp = (LayoutParams) child.getLayoutParams();
-            lp.isDragging = false;
             lp.dropped = true;
             child.requestLayout();
         }
     }
 
     /**
-     * Start dragging the specified child
-     *
-     * @param child The child that is being dragged
-     */
-    void onDragChild(View child) {
-        LayoutParams lp = (LayoutParams) child.getLayoutParams();
-        lp.isDragging = true;
-    }
-
-    /**
      * Computes a bounding rectangle for a range of cells
      *
      * @param cellX X coordinate of upper left corner expressed as a cell position
@@ -1755,11 +1744,6 @@
          */
         public boolean isLockedToGrid = true;
 
-        /**
-         * Is this item currently being dragged
-         */
-        public boolean isDragging;
-
         // X coordinate of the view in the layout.
         @ViewDebug.ExportedProperty
         int x;
diff --git a/src/com/android/launcher2/DragController.java b/src/com/android/launcher2/DragController.java
index 5aecede..eb0db69 100644
--- a/src/com/android/launcher2/DragController.java
+++ b/src/com/android/launcher2/DragController.java
@@ -23,13 +23,11 @@
 import android.os.Handler;
 import android.os.IBinder;
 import android.os.Vibrator;
-import android.util.DisplayMetrics;
 import android.util.Log;
 import android.view.KeyEvent;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.ViewConfiguration;
-import android.view.WindowManager;
 import android.view.inputmethod.InputMethodManager;
 
 import com.android.launcher.R;
@@ -288,13 +286,6 @@
         final DragView dragView = mDragObject.dragView = new DragView(mLauncher, b, registrationX,
                 registrationY, 0, 0, b.getWidth(), b.getHeight());
 
-        final DragSource dragSource = source;
-        dragView.setOnDrawRunnable(new Runnable() {
-            public void run() {
-                dragSource.onDragViewVisible();
-            };
-        });
-
         if (dragRegion != null) {
             dragView.setDragRegion(new Rect(dragRegion));
         }
diff --git a/src/com/android/launcher2/DragLayer.java b/src/com/android/launcher2/DragLayer.java
index 8563be9..809a651 100644
--- a/src/com/android/launcher2/DragLayer.java
+++ b/src/com/android/launcher2/DragLayer.java
@@ -215,8 +215,8 @@
             pt[1] += view.getTop() - view.getScrollY();
             viewParent = view.getParent();
         }
-        coord[0] = (int) pt[0];
-        coord[1] = (int) pt[1];
+        coord[0] = (int) Math.round(pt[0]);
+        coord[1] = (int) Math.round(pt[1]);
         return scale;
     }
 
diff --git a/src/com/android/launcher2/DragSource.java b/src/com/android/launcher2/DragSource.java
index 2a8f263..06f5ee1 100644
--- a/src/com/android/launcher2/DragSource.java
+++ b/src/com/android/launcher2/DragSource.java
@@ -25,11 +25,5 @@
  *
  */
 public interface DragSource {
-    /**
-     * Callback from the DragController when it begins drawing the drag view.
-     * This allows the DragSource to dim or hide the original view.
-     */
-    void onDragViewVisible();
-
     void onDropCompleted(View target, DragObject d, boolean success);
 }
diff --git a/src/com/android/launcher2/DragView.java b/src/com/android/launcher2/DragView.java
index 1c75e47..386cb55 100644
--- a/src/com/android/launcher2/DragView.java
+++ b/src/com/android/launcher2/DragView.java
@@ -41,6 +41,7 @@
 
     private Rect mDragRegion = null;
     private DragLayer mDragLayer = null;
+    private boolean mHasDrawn = false;
 
     ValueAnimator mAnim;
     private float mOffsetX = 0.0f;
@@ -49,13 +50,6 @@
     private DragLayer.LayoutParams mLayoutParams;
 
     /**
-     * A callback to be called the first time this view is drawn.
-     * This allows the originator of the drag to dim or hide the original view as soon
-     * as the DragView is drawn.
-     */
-    private Runnable mOnDrawRunnable = null;
-
-    /**
      * Construct the drag view.
      * <p>
      * The registration point is the point inside our view that the touch events should
@@ -125,10 +119,6 @@
         return mOffsetY;
     }
 
-    public void setOnDrawRunnable(Runnable r) {
-        mOnDrawRunnable = r;
-    }
-
     public int getDragRegionLeft() {
         return mDragRegion.left;
     }
@@ -168,14 +158,7 @@
             canvas.drawRect(0, 0, getWidth(), getHeight(), p);
         }
 
-        // Call the callback if we haven't already been detached
-        if (getParent() != null) {
-            if (mOnDrawRunnable != null) {
-                mOnDrawRunnable.run();
-                mOnDrawRunnable = null;
-            }
-        }
-
+        mHasDrawn = true;
         canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
     }
 
@@ -184,6 +167,10 @@
         invalidate();
     }
 
+    public boolean hasDrawn() {
+        return mHasDrawn;
+    }
+
     @Override
     public void setAlpha(float alpha) {
         super.setAlpha(alpha);
diff --git a/src/com/android/launcher2/Folder.java b/src/com/android/launcher2/Folder.java
index 96cd22b..d3b5d41 100644
--- a/src/com/android/launcher2/Folder.java
+++ b/src/com/android/launcher2/Folder.java
@@ -214,15 +214,13 @@
             mEmptyCell[0] = item.cellX;
             mEmptyCell[1] = item.cellY;
             mCurrentDragView = v;
+
+            mContent.removeView(mCurrentDragView);
+            mInfo.remove(mCurrentDragInfo);
         }
         return true;
     }
 
-    public void onDragViewVisible() {
-        mContent.removeView(mCurrentDragView);
-        mInfo.remove(mCurrentDragInfo);
-    }
-
     public boolean isEditingName() {
         return mIsEditingName;
     }
@@ -890,7 +888,11 @@
             si.cellX = lp.cellX = mEmptyCell[0];
             si.cellX = lp.cellY = mEmptyCell[1];
             mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
-            mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
+            if (d.dragView.hasDrawn()) {
+                mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
+            } else {
+                mCurrentDragView.setVisibility(VISIBLE);
+            }
             mItemsInvalidated = true;
             setupContentDimension(getItemCount());
             mSuppressOnAdd = true;
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index 90bd151..0abdec0 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -2418,6 +2418,10 @@
         // Otherwise, we are not in spring loaded mode, so don't do anything.
     }
 
+    public boolean isAllAppsCustomizeOpen() {
+        return mState == State.APPS_CUSTOMIZE;
+    }
+
     /**
      * Shows the hotseat area.
      */
diff --git a/src/com/android/launcher2/PagedViewWithDraggableItems.java b/src/com/android/launcher2/PagedViewWithDraggableItems.java
index 5fa7be7..287a065 100644
--- a/src/com/android/launcher2/PagedViewWithDraggableItems.java
+++ b/src/com/android/launcher2/PagedViewWithDraggableItems.java
@@ -37,17 +37,19 @@
     private boolean mIsDragging;
     private boolean mIsDragEnabled;
     private float mDragSlopeThreshold;
+    private Launcher mLauncher;
 
     public PagedViewWithDraggableItems(Context context) {
-        super(context, null);
+        this(context, null);
     }
 
     public PagedViewWithDraggableItems(Context context, AttributeSet attrs) {
-        super(context, attrs, 0);
+        this(context, attrs, 0);
     }
 
     public PagedViewWithDraggableItems(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
+        mLauncher = (Launcher) context;
     }
 
     protected boolean beginDragging(View v) {
@@ -102,10 +104,13 @@
         if (!v.isInTouchMode()) return false;
         // Return early if we are still animating the pages
         if (mNextPage != INVALID_PAGE) return false;
+        // When we have exited all apps or are in transition, disregard long clicks
+        if (!mLauncher.isAllAppsCustomizeOpen() ||
+                mLauncher.getWorkspace().isSwitchingState()) return false;
+
         return beginDragging(v);
     }
 
-
     /*
      * Determines if we should change the touch state to start scrolling after the
      * user moves their touch point too far.
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index d40d77f..5309423 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -539,6 +539,10 @@
         return (isSmall() || mIsSwitchingState);
     }
 
+    public boolean isSwitchingState() {
+        return mIsSwitchingState;
+    }
+
     protected void onWindowVisibilityChanged (int visibility) {
         mLauncher.onWindowVisibilityChanged(visibility);
     }
@@ -1972,9 +1976,7 @@
         }
 
         mDragInfo = cellInfo;
-
-        CellLayout current = getParentCellLayoutForView(cellInfo.cell);
-        current.onDragChild(child);
+        child.setVisibility(GONE);
 
         child.clearFocus();
         child.setPressed(false);
@@ -2001,8 +2003,7 @@
 
         mLauncher.getDragLayer().getLocationInDragLayer(child, mTempXY);
         final int dragLayerX = (int) mTempXY[0] + (child.getWidth() - bmpWidth) / 2;
-        int dragLayerY = (int) mTempXY[1] + (child.getHeight() - bmpHeight) / 2;
-        dragLayerY -= (child.getHeight() - b.getHeight()) / 2;
+        int dragLayerY = mTempXY[1] - bitmapPadding / 2;
 
         Rect dragRect = null;
         if (child instanceof BubbleTextView) {
@@ -2011,6 +2012,7 @@
             int left = (bmpWidth - iconSize) / 2;
             int right = left + iconSize;
             int bottom = top + iconSize;
+            dragLayerY += top;
             dragRect = new Rect(left, top, right, bottom);
         } else if (child instanceof FolderIcon) {
             int previewSize = getResources().getDimensionPixelSize(R.dimen.folder_preview_size);
@@ -2310,8 +2312,12 @@
                 }
             };
             mAnimatingViewIntoPlace = true;
-            mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, cell,
-                    disableHardwareLayersRunnable);
+            if (d.dragView.hasDrawn()) {
+                mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, cell,
+                        disableHardwareLayersRunnable);
+            } else {
+                cell.setVisibility(VISIBLE);
+            }
             parent.onDropChild(cell);
         }
     }
@@ -3092,11 +3098,6 @@
         mDragInfo = null;
     }
 
-    @Override
-    public void onDragViewVisible() {
-        ((View) mDragInfo.cell).setVisibility(View.GONE);
-    }
-
     public boolean isDropEnabled() {
         return true;
     }