Allow dragging items off customization tray

Long press is no longer required

Change-Id: Ie122e298ab8782522b650551b8004c53b514bdd1
diff --git a/res/values-xlarge/config.xml b/res/values-xlarge/config.xml
index a54528e..9c01e3c 100644
--- a/res/values-xlarge/config.xml
+++ b/res/values-xlarge/config.xml
@@ -49,4 +49,8 @@
     <integer name="config_screenOnDropScaleDownDuration">200</integer>
     <integer name="config_screenOnDropAlphaFadeDelay">350</integer>
     <integer name="config_screenOnDropAlphaFadeDuration">50</integer>
+
+    <!-- The slope, in percent, of the drag movement needed to drag an item out of the customization
+         drawer (y / x * 100%)  -->
+    <integer name="config_customizationDrawerDragSlopeThreshold">150</integer>
 </resources>
diff --git a/src/com/android/launcher2/CustomizePagedView.java b/src/com/android/launcher2/CustomizePagedView.java
index 6dfa4e5..a7293c8 100644
--- a/src/com/android/launcher2/CustomizePagedView.java
+++ b/src/com/android/launcher2/CustomizePagedView.java
@@ -44,6 +44,7 @@
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuItem;
+import android.view.MotionEvent;
 import android.view.View;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
@@ -52,7 +53,7 @@
 import com.android.launcher.R;
 
 public class CustomizePagedView extends PagedView
-    implements View.OnLongClickListener, View.OnClickListener,
+    implements View.OnLongClickListener, View.OnClickListener, View.OnTouchListener,
                 DragSource, ActionMode.Callback {
 
     public enum CustomizationType {
@@ -65,9 +66,12 @@
     private static final String TAG = "CustomizeWorkspace";
     private static final boolean DEBUG = false;
 
+    private View mLastTouchedItem;
     private Launcher mLauncher;
     private DragController mDragController;
     private PackageManager mPackageManager;
+    private boolean mIsDragging;
+    private float mDragSlopeThreshold;
 
     private CustomizationType mCustomizationType;
 
@@ -132,6 +136,10 @@
         mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
         mInflater = LayoutInflater.from(context);
 
+        final Resources r = context.getResources();
+        mDragSlopeThreshold =
+            r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f;
+
         setVisibility(View.GONE);
         setSoundEffectsEnabled(false);
         setupWorkspaceLayout();
@@ -373,11 +381,98 @@
         if (!v.isInTouchMode()) return false;
         // Return early if we are still animating the pages
         if (mNextPage != INVALID_PAGE) return false;
+        return beginDragging(v);
+    }
 
+    @Override
+    public boolean onTouch(View v, MotionEvent event) {
+        mLastTouchedItem = v;
+        return false;
+    }
+
+    /*
+     * Determines if we should change the touch state to start scrolling after the
+     * user moves their touch point too far.
+     */
+    protected void determineScrollingStart(MotionEvent ev) {
+        if (!mIsDragging) super.determineScrollingStart(ev);
+    }
+
+    /*
+     * Determines if we should change the touch state to start dragging after the
+     * user moves their touch point far enough.
+     */
+    protected void determineDraggingStart(MotionEvent ev) {
+        /*
+         * Locally do absolute value. mLastMotionX is set to the y value
+         * of the down event.
+         */
+        final int pointerIndex = ev.findPointerIndex(mActivePointerId);
+        final float x = ev.getX(pointerIndex);
+        final float y = ev.getY(pointerIndex);
+        final int xDiff = (int) Math.abs(x - mLastMotionX);
+        final int yDiff = (int) Math.abs(y - mLastMotionY);
+
+        final int touchSlop = mTouchSlop;
+        boolean yMoved = yDiff > touchSlop;
+        boolean isUpwardMotion = (yDiff / (float) xDiff) > mDragSlopeThreshold;
+
+        if (isUpwardMotion && yMoved) {
+            // Drag if the user moved far enough along the Y axis
+            beginDragging(mLastTouchedItem);
+
+            // Cancel any pending longpress
+            if (mAllowLongPress) {
+                mAllowLongPress = false;
+                // Try canceling the long press. It could also have been scheduled
+                // by a distant descendant, so use the mAllowLongPress flag to block
+                // everything
+                final View currentPage = getPageAt(mCurrentPage);
+                if (currentPage != null) {
+                    currentPage.cancelLongPress();
+                }
+            }
+        }
+    }
+
+    @Override
+    public boolean onInterceptTouchEvent(MotionEvent ev) {
+        final int action = ev.getAction();
+        switch (action & MotionEvent.ACTION_MASK) {
+            case MotionEvent.ACTION_DOWN:
+                mIsDragging = false;
+                break;
+            case MotionEvent.ACTION_MOVE:
+                if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) {
+                    determineDraggingStart(ev);
+                }
+                break;
+        }
+        return super.onInterceptTouchEvent(ev);
+    }
+
+    @Override
+    public boolean onTouchEvent(MotionEvent ev) {
+        final int action = ev.getAction();
+        switch (action & MotionEvent.ACTION_MASK) {
+            case MotionEvent.ACTION_DOWN:
+                mIsDragging = false;
+                break;
+            case MotionEvent.ACTION_MOVE:
+                if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) {
+                    determineDraggingStart(ev);
+                }
+                break;
+        }
+        return super.onTouchEvent(ev);
+    }
+
+    private boolean beginDragging(View v) {
         // End the current choice mode before we start dragging anything
         if (isChoiceMode(CHOICE_MODE_SINGLE)) {
             endChoiceMode();
         }
+        mIsDragging = true;
 
         PendingAddItemInfo createItemInfo;
         switch (mCustomizationType) {
@@ -626,6 +721,7 @@
                     R.layout.customize_paged_view_widget, layout, false);
             l.setTag(createItemInfo);
             l.setOnClickListener(this);
+            l.setOnTouchListener(this);
             l.setOnLongClickListener(this);
 
             final Drawable icon = getWidgetPreview(info);
@@ -729,6 +825,7 @@
                         info.activityInfo.name);
                 icon.setTag(createItemInfo);
                 icon.setOnClickListener(this);
+                icon.setOnTouchListener(this);
                 icon.setOnLongClickListener(this);
                 break;
             default:
@@ -774,6 +871,7 @@
                     R.layout.all_apps_paged_view_application, layout, false);
             icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
             icon.setOnClickListener(this);
+            icon.setOnTouchListener(this);
             icon.setOnLongClickListener(this);
 
             final int index = i - startIndex;
diff --git a/src/com/android/launcher2/PagedView.java b/src/com/android/launcher2/PagedView.java
index caa1e12..d4dffe6 100644
--- a/src/com/android/launcher2/PagedView.java
+++ b/src/com/android/launcher2/PagedView.java
@@ -74,8 +74,8 @@
     private VelocityTracker mVelocityTracker;
 
     private float mDownMotionX;
-    private float mLastMotionX;
-    private float mLastMotionY;
+    protected float mLastMotionX;
+    protected float mLastMotionY;
     private int mLastScreenCenter = -1;
 
     protected final static int TOUCH_STATE_REST = 0;
@@ -88,9 +88,9 @@
 
     protected OnLongClickListener mLongClickListener;
 
-    private boolean mAllowLongPress = true;
+    protected boolean mAllowLongPress = true;
 
-    private int mTouchSlop;
+    protected int mTouchSlop;
     private int mPagingTouchSlop;
     private int mMaximumVelocity;
     protected int mPageSpacing;