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; |
Winson Chung | 94569f4 | 2011-01-17 14:09:17 -0800 | [diff] [blame] | 38 | private boolean mIsDragEnabled; |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 39 | private float mDragSlopeThreshold; |
| 40 | |
| 41 | public PagedViewWithDraggableItems(Context context) { |
| 42 | super(context, null); |
| 43 | } |
| 44 | |
| 45 | public PagedViewWithDraggableItems(Context context, AttributeSet attrs) { |
| 46 | super(context, attrs, 0); |
| 47 | } |
| 48 | |
| 49 | public PagedViewWithDraggableItems(Context context, AttributeSet attrs, int defStyle) { |
| 50 | super(context, attrs, defStyle); |
| 51 | } |
| 52 | |
| 53 | protected boolean beginDragging(View v) { |
Winson Chung | 304dcde | 2011-01-07 11:17:23 -0800 | [diff] [blame] | 54 | boolean wasDragging = mIsDragging; |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 55 | mIsDragging = true; |
Winson Chung | 304dcde | 2011-01-07 11:17:23 -0800 | [diff] [blame] | 56 | return !wasDragging; |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Winson Chung | 7d1fcbc | 2011-01-04 10:22:20 -0800 | [diff] [blame] | 59 | protected void cancelDragging() { |
| 60 | mIsDragging = false; |
| 61 | mLastTouchedItem = null; |
Winson Chung | 94569f4 | 2011-01-17 14:09:17 -0800 | [diff] [blame] | 62 | mIsDragEnabled = false; |
Winson Chung | 7d1fcbc | 2011-01-04 10:22:20 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | private void handleTouchEvent(MotionEvent ev) { |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 66 | final int action = ev.getAction(); |
| 67 | switch (action & MotionEvent.ACTION_MASK) { |
| 68 | case MotionEvent.ACTION_DOWN: |
Winson Chung | 7d1fcbc | 2011-01-04 10:22:20 -0800 | [diff] [blame] | 69 | cancelDragging(); |
Winson Chung | 94569f4 | 2011-01-17 14:09:17 -0800 | [diff] [blame] | 70 | mIsDragEnabled = true; |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 71 | break; |
| 72 | case MotionEvent.ACTION_MOVE: |
Winson Chung | 94569f4 | 2011-01-17 14:09:17 -0800 | [diff] [blame] | 73 | if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging && mIsDragEnabled) { |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 74 | determineDraggingStart(ev); |
| 75 | } |
| 76 | break; |
| 77 | } |
Winson Chung | 7d1fcbc | 2011-01-04 10:22:20 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 82 | handleTouchEvent(ev); |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 83 | return super.onInterceptTouchEvent(ev); |
| 84 | } |
| 85 | |
| 86 | @Override |
Winson Chung | 7d1fcbc | 2011-01-04 10:22:20 -0800 | [diff] [blame] | 87 | public boolean onTouchEvent(MotionEvent ev) { |
| 88 | handleTouchEvent(ev); |
| 89 | return super.onTouchEvent(ev); |
| 90 | } |
| 91 | |
| 92 | @Override |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 93 | public boolean onTouch(View v, MotionEvent event) { |
| 94 | mLastTouchedItem = v; |
Winson Chung | 94569f4 | 2011-01-17 14:09:17 -0800 | [diff] [blame] | 95 | mIsDragEnabled = true; |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | |
| 99 | @Override |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 100 | public boolean onLongClick(View v) { |
| 101 | // Return early if this is not initiated from a touch |
| 102 | if (!v.isInTouchMode()) return false; |
| 103 | // Return early if we are still animating the pages |
| 104 | if (mNextPage != INVALID_PAGE) return false; |
| 105 | return beginDragging(v); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /* |
| 110 | * Determines if we should change the touch state to start scrolling after the |
| 111 | * user moves their touch point too far. |
| 112 | */ |
| 113 | protected void determineScrollingStart(MotionEvent ev) { |
| 114 | if (!mIsDragging) super.determineScrollingStart(ev); |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Determines if we should change the touch state to start dragging after the |
| 119 | * user moves their touch point far enough. |
| 120 | */ |
| 121 | protected void determineDraggingStart(MotionEvent ev) { |
| 122 | /* |
| 123 | * Locally do absolute value. mLastMotionX is set to the y value |
| 124 | * of the down event. |
| 125 | */ |
| 126 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 127 | final float x = ev.getX(pointerIndex); |
| 128 | final float y = ev.getY(pointerIndex); |
| 129 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
| 130 | final int yDiff = (int) Math.abs(y - mLastMotionY); |
| 131 | |
| 132 | final int touchSlop = mTouchSlop; |
| 133 | boolean yMoved = yDiff > touchSlop; |
| 134 | boolean isUpwardMotion = (yDiff / (float) xDiff) > mDragSlopeThreshold; |
| 135 | |
| 136 | if (isUpwardMotion && yMoved && mLastTouchedItem != null) { |
| 137 | // Drag if the user moved far enough along the Y axis |
| 138 | beginDragging(mLastTouchedItem); |
| 139 | |
| 140 | // Cancel any pending long press |
| 141 | if (mAllowLongPress) { |
| 142 | mAllowLongPress = false; |
| 143 | // Try canceling the long press. It could also have been scheduled |
| 144 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 145 | // everything |
| 146 | final View currentPage = getPageAt(mCurrentPage); |
| 147 | if (currentPage != null) { |
| 148 | currentPage.cancelLongPress(); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | public void setDragSlopeThreshold(float dragSlopeThreshold) { |
| 155 | mDragSlopeThreshold = dragSlopeThreshold; |
| 156 | } |
Patrick Dubroy | 2313eff | 2011-01-11 20:01:31 -0800 | [diff] [blame] | 157 | |
| 158 | @Override |
| 159 | protected void onDetachedFromWindow() { |
Winson Chung | 94569f4 | 2011-01-17 14:09:17 -0800 | [diff] [blame] | 160 | cancelDragging(); |
Patrick Dubroy | 2313eff | 2011-01-11 20:01:31 -0800 | [diff] [blame] | 161 | super.onDetachedFromWindow(); |
| 162 | } |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 163 | } |