blob: 2a29c33f35d06e6ad70670f4eda15cd7a72e45bd [file] [log] [blame]
Michael Jurka72b079e2010-12-10 01:03:53 -08001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Michael Jurka72b079e2010-12-10 01:03:53 -080018
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.MotionEvent;
22import 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 */
34public abstract class PagedViewWithDraggableItems extends PagedView
35 implements View.OnLongClickListener, View.OnTouchListener {
36 private View mLastTouchedItem;
37 private boolean mIsDragging;
Winson Chung94569f42011-01-17 14:09:17 -080038 private boolean mIsDragEnabled;
Michael Jurka72b079e2010-12-10 01:03:53 -080039 private float mDragSlopeThreshold;
Adam Cohenfc53cd22011-07-20 15:45:11 -070040 private Launcher mLauncher;
Michael Jurka72b079e2010-12-10 01:03:53 -080041
42 public PagedViewWithDraggableItems(Context context) {
Adam Cohenfc53cd22011-07-20 15:45:11 -070043 this(context, null);
Michael Jurka72b079e2010-12-10 01:03:53 -080044 }
45
46 public PagedViewWithDraggableItems(Context context, AttributeSet attrs) {
Adam Cohenfc53cd22011-07-20 15:45:11 -070047 this(context, attrs, 0);
Michael Jurka72b079e2010-12-10 01:03:53 -080048 }
49
50 public PagedViewWithDraggableItems(Context context, AttributeSet attrs, int defStyle) {
51 super(context, attrs, defStyle);
Adam Cohenfc53cd22011-07-20 15:45:11 -070052 mLauncher = (Launcher) context;
Michael Jurka72b079e2010-12-10 01:03:53 -080053 }
54
55 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -080056 boolean wasDragging = mIsDragging;
Michael Jurka72b079e2010-12-10 01:03:53 -080057 mIsDragging = true;
Winson Chung304dcde2011-01-07 11:17:23 -080058 return !wasDragging;
Michael Jurka72b079e2010-12-10 01:03:53 -080059 }
60
Winson Chung7d1fcbc2011-01-04 10:22:20 -080061 protected void cancelDragging() {
62 mIsDragging = false;
63 mLastTouchedItem = null;
Winson Chung94569f42011-01-17 14:09:17 -080064 mIsDragEnabled = false;
Winson Chung7d1fcbc2011-01-04 10:22:20 -080065 }
66
67 private void handleTouchEvent(MotionEvent ev) {
Michael Jurka72b079e2010-12-10 01:03:53 -080068 final int action = ev.getAction();
69 switch (action & MotionEvent.ACTION_MASK) {
70 case MotionEvent.ACTION_DOWN:
Winson Chung7d1fcbc2011-01-04 10:22:20 -080071 cancelDragging();
Winson Chung94569f42011-01-17 14:09:17 -080072 mIsDragEnabled = true;
Michael Jurka72b079e2010-12-10 01:03:53 -080073 break;
74 case MotionEvent.ACTION_MOVE:
Winson Chung94569f42011-01-17 14:09:17 -080075 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging && mIsDragEnabled) {
Michael Jurka72b079e2010-12-10 01:03:53 -080076 determineDraggingStart(ev);
77 }
78 break;
79 }
Winson Chung7d1fcbc2011-01-04 10:22:20 -080080 }
81
82 @Override
83 public boolean onInterceptTouchEvent(MotionEvent ev) {
84 handleTouchEvent(ev);
Michael Jurka72b079e2010-12-10 01:03:53 -080085 return super.onInterceptTouchEvent(ev);
86 }
87
88 @Override
Winson Chung7d1fcbc2011-01-04 10:22:20 -080089 public boolean onTouchEvent(MotionEvent ev) {
90 handleTouchEvent(ev);
91 return super.onTouchEvent(ev);
92 }
93
94 @Override
Michael Jurka72b079e2010-12-10 01:03:53 -080095 public boolean onTouch(View v, MotionEvent event) {
96 mLastTouchedItem = v;
Winson Chung94569f42011-01-17 14:09:17 -080097 mIsDragEnabled = true;
Michael Jurka72b079e2010-12-10 01:03:53 -080098 return false;
99 }
100
101 @Override
Michael Jurka72b079e2010-12-10 01:03:53 -0800102 public boolean onLongClick(View v) {
103 // Return early if this is not initiated from a touch
104 if (!v.isInTouchMode()) return false;
105 // Return early if we are still animating the pages
106 if (mNextPage != INVALID_PAGE) return false;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700107 // When we have exited all apps or are in transition, disregard long clicks
Winson Chungc93e5ae2012-07-23 20:48:26 -0700108 if (!mLauncher.isAllAppsVisible() ||
Adam Cohenfc53cd22011-07-20 15:45:11 -0700109 mLauncher.getWorkspace().isSwitchingState()) return false;
Winson Chung36a62fe2012-05-06 18:04:42 -0700110 // Return if global dragging is not enabled
111 if (!mLauncher.isDraggingEnabled()) return false;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700112
Michael Jurka72b079e2010-12-10 01:03:53 -0800113 return beginDragging(v);
114 }
115
Michael Jurka72b079e2010-12-10 01:03:53 -0800116 /*
117 * Determines if we should change the touch state to start scrolling after the
118 * user moves their touch point too far.
119 */
120 protected void determineScrollingStart(MotionEvent ev) {
121 if (!mIsDragging) super.determineScrollingStart(ev);
122 }
123
124 /*
125 * Determines if we should change the touch state to start dragging after the
126 * user moves their touch point far enough.
127 */
128 protected void determineDraggingStart(MotionEvent ev) {
129 /*
130 * Locally do absolute value. mLastMotionX is set to the y value
131 * of the down event.
132 */
133 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
134 final float x = ev.getX(pointerIndex);
135 final float y = ev.getY(pointerIndex);
136 final int xDiff = (int) Math.abs(x - mLastMotionX);
137 final int yDiff = (int) Math.abs(y - mLastMotionY);
138
139 final int touchSlop = mTouchSlop;
140 boolean yMoved = yDiff > touchSlop;
141 boolean isUpwardMotion = (yDiff / (float) xDiff) > mDragSlopeThreshold;
142
143 if (isUpwardMotion && yMoved && mLastTouchedItem != null) {
144 // Drag if the user moved far enough along the Y axis
145 beginDragging(mLastTouchedItem);
146
147 // Cancel any pending long press
148 if (mAllowLongPress) {
149 mAllowLongPress = false;
150 // Try canceling the long press. It could also have been scheduled
151 // by a distant descendant, so use the mAllowLongPress flag to block
152 // everything
153 final View currentPage = getPageAt(mCurrentPage);
154 if (currentPage != null) {
155 currentPage.cancelLongPress();
156 }
157 }
158 }
159 }
160
161 public void setDragSlopeThreshold(float dragSlopeThreshold) {
162 mDragSlopeThreshold = dragSlopeThreshold;
163 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -0800164
165 @Override
166 protected void onDetachedFromWindow() {
Winson Chung94569f42011-01-17 14:09:17 -0800167 cancelDragging();
Patrick Dubroy2313eff2011-01-11 20:01:31 -0800168 super.onDetachedFromWindow();
169 }
Michael Jurka72b079e2010-12-10 01:03:53 -0800170}