From 7426c42ce01e132781faa68941d79d23cd7fdf1e Mon Sep 17 00:00:00 2001 From: Michael Jurka Date: Thu, 11 Nov 2010 15:23:47 -0800 Subject: Allow dragging items off customization tray Long press is no longer required Change-Id: Ie122e298ab8782522b650551b8004c53b514bdd1 --- src/com/android/launcher2/CustomizePagedView.java | 100 +++++++++++++++++++++- src/com/android/launcher2/PagedView.java | 8 +- 2 files changed, 103 insertions(+), 5 deletions(-) (limited to 'src/com') diff --git a/src/com/android/launcher2/CustomizePagedView.java b/src/com/android/launcher2/CustomizePagedView.java index 6dfa4e535..a7293c88b 100644 --- a/src/com/android/launcher2/CustomizePagedView.java +++ b/src/com/android/launcher2/CustomizePagedView.java @@ -44,6 +44,7 @@ import android.view.Gravity; 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 android.widget.TextView; 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 @@ public class CustomizePagedView extends PagedView 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 @@ public class CustomizePagedView extends PagedView 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 @@ public class CustomizePagedView extends PagedView 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 @@ public class CustomizePagedView extends PagedView 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 @@ public class CustomizePagedView extends PagedView info.activityInfo.name); icon.setTag(createItemInfo); icon.setOnClickListener(this); + icon.setOnTouchListener(this); icon.setOnLongClickListener(this); break; default: @@ -774,6 +871,7 @@ public class CustomizePagedView extends PagedView 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 caa1e1298..d4dffe6b0 100644 --- a/src/com/android/launcher2/PagedView.java +++ b/src/com/android/launcher2/PagedView.java @@ -74,8 +74,8 @@ public abstract class PagedView extends ViewGroup { 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 @@ public abstract class PagedView extends ViewGroup { 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; -- cgit v1.2.3