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