Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.util.AttributeSet; |
| 21 | import android.view.MotionEvent; |
| 22 | import android.view.View; |
| 23 | |
| 24 | |
| 25 | /* Class that does most of the work of enabling dragging items out of a PagedView by performing a |
| 26 | * vertical drag. Used by both CustomizePagedView and AllAppsPagedView. |
| 27 | * Subclasses must do the following: |
| 28 | * * call setDragSlopeThreshold after making an instance of the PagedViewWithDraggableItems |
| 29 | * * call child.setOnLongClickListener(this) and child.setOnTouchListener(this) on all children |
| 30 | * (good place to do it is in syncPageItems) |
| 31 | * * override beginDragging(View) (but be careful to call super.beginDragging(View) |
| 32 | * |
| 33 | */ |
| 34 | public abstract class PagedViewWithDraggableItems extends PagedView |
| 35 | implements View.OnLongClickListener, View.OnTouchListener { |
| 36 | private View mLastTouchedItem; |
| 37 | private boolean mIsDragging; |
| 38 | private float mDragSlopeThreshold; |
| 39 | |
| 40 | public PagedViewWithDraggableItems(Context context) { |
| 41 | super(context, null); |
| 42 | } |
| 43 | |
| 44 | public PagedViewWithDraggableItems(Context context, AttributeSet attrs) { |
| 45 | super(context, attrs, 0); |
| 46 | } |
| 47 | |
| 48 | public PagedViewWithDraggableItems(Context context, AttributeSet attrs, int defStyle) { |
| 49 | super(context, attrs, defStyle); |
| 50 | } |
| 51 | |
| 52 | protected boolean beginDragging(View v) { |
| 53 | mIsDragging = true; |
| 54 | return false; |
| 55 | } |
| 56 | |
Winson Chung | 7d1fcbc | 2011-01-04 10:22:20 -0800 | [diff] [blame^] | 57 | protected void cancelDragging() { |
| 58 | mIsDragging = false; |
| 59 | mLastTouchedItem = null; |
| 60 | } |
| 61 | |
| 62 | private void handleTouchEvent(MotionEvent ev) { |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 63 | final int action = ev.getAction(); |
| 64 | switch (action & MotionEvent.ACTION_MASK) { |
| 65 | case MotionEvent.ACTION_DOWN: |
Winson Chung | 7d1fcbc | 2011-01-04 10:22:20 -0800 | [diff] [blame^] | 66 | cancelDragging(); |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 67 | break; |
| 68 | case MotionEvent.ACTION_MOVE: |
| 69 | if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) { |
| 70 | determineDraggingStart(ev); |
| 71 | } |
| 72 | break; |
| 73 | } |
Winson Chung | 7d1fcbc | 2011-01-04 10:22:20 -0800 | [diff] [blame^] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 78 | handleTouchEvent(ev); |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 79 | return super.onInterceptTouchEvent(ev); |
| 80 | } |
| 81 | |
| 82 | @Override |
Winson Chung | 7d1fcbc | 2011-01-04 10:22:20 -0800 | [diff] [blame^] | 83 | public boolean onTouchEvent(MotionEvent ev) { |
| 84 | handleTouchEvent(ev); |
| 85 | return super.onTouchEvent(ev); |
| 86 | } |
| 87 | |
| 88 | @Override |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 89 | public boolean onTouch(View v, MotionEvent event) { |
| 90 | mLastTouchedItem = v; |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | @Override |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 95 | public boolean onLongClick(View v) { |
| 96 | // Return early if this is not initiated from a touch |
| 97 | if (!v.isInTouchMode()) return false; |
| 98 | // Return early if we are still animating the pages |
| 99 | if (mNextPage != INVALID_PAGE) return false; |
| 100 | return beginDragging(v); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /* |
| 105 | * Determines if we should change the touch state to start scrolling after the |
| 106 | * user moves their touch point too far. |
| 107 | */ |
| 108 | protected void determineScrollingStart(MotionEvent ev) { |
| 109 | if (!mIsDragging) super.determineScrollingStart(ev); |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Determines if we should change the touch state to start dragging after the |
| 114 | * user moves their touch point far enough. |
| 115 | */ |
| 116 | protected void determineDraggingStart(MotionEvent ev) { |
| 117 | /* |
| 118 | * Locally do absolute value. mLastMotionX is set to the y value |
| 119 | * of the down event. |
| 120 | */ |
| 121 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 122 | final float x = ev.getX(pointerIndex); |
| 123 | final float y = ev.getY(pointerIndex); |
| 124 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
| 125 | final int yDiff = (int) Math.abs(y - mLastMotionY); |
| 126 | |
| 127 | final int touchSlop = mTouchSlop; |
| 128 | boolean yMoved = yDiff > touchSlop; |
| 129 | boolean isUpwardMotion = (yDiff / (float) xDiff) > mDragSlopeThreshold; |
| 130 | |
| 131 | if (isUpwardMotion && yMoved && mLastTouchedItem != null) { |
| 132 | // Drag if the user moved far enough along the Y axis |
| 133 | beginDragging(mLastTouchedItem); |
| 134 | |
| 135 | // Cancel any pending long press |
| 136 | if (mAllowLongPress) { |
| 137 | mAllowLongPress = false; |
| 138 | // Try canceling the long press. It could also have been scheduled |
| 139 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 140 | // everything |
| 141 | final View currentPage = getPageAt(mCurrentPage); |
| 142 | if (currentPage != null) { |
| 143 | currentPage.cancelLongPress(); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | public void setDragSlopeThreshold(float dragSlopeThreshold) { |
| 150 | mDragSlopeThreshold = dragSlopeThreshold; |
| 151 | } |
| 152 | } |