blob: 3f722921a06205b86db94a222360e0e43c1b521d [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
17package com.android.launcher2;
18
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;
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) {
Winson Chung304dcde2011-01-07 11:17:23 -080053 boolean wasDragging = mIsDragging;
Michael Jurka72b079e2010-12-10 01:03:53 -080054 mIsDragging = true;
Winson Chung304dcde2011-01-07 11:17:23 -080055 return !wasDragging;
Michael Jurka72b079e2010-12-10 01:03:53 -080056 }
57
Winson Chung7d1fcbc2011-01-04 10:22:20 -080058 protected void cancelDragging() {
59 mIsDragging = false;
60 mLastTouchedItem = null;
61 }
62
63 private void handleTouchEvent(MotionEvent ev) {
Michael Jurka72b079e2010-12-10 01:03:53 -080064 final int action = ev.getAction();
65 switch (action & MotionEvent.ACTION_MASK) {
66 case MotionEvent.ACTION_DOWN:
Winson Chung7d1fcbc2011-01-04 10:22:20 -080067 cancelDragging();
Michael Jurka72b079e2010-12-10 01:03:53 -080068 break;
69 case MotionEvent.ACTION_MOVE:
70 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) {
71 determineDraggingStart(ev);
72 }
73 break;
74 }
Winson Chung7d1fcbc2011-01-04 10:22:20 -080075 }
76
77 @Override
78 public boolean onInterceptTouchEvent(MotionEvent ev) {
79 handleTouchEvent(ev);
Michael Jurka72b079e2010-12-10 01:03:53 -080080 return super.onInterceptTouchEvent(ev);
81 }
82
83 @Override
Winson Chung7d1fcbc2011-01-04 10:22:20 -080084 public boolean onTouchEvent(MotionEvent ev) {
85 handleTouchEvent(ev);
86 return super.onTouchEvent(ev);
87 }
88
89 @Override
Michael Jurka72b079e2010-12-10 01:03:53 -080090 public boolean onTouch(View v, MotionEvent event) {
91 mLastTouchedItem = v;
92 return false;
93 }
94
95 @Override
Michael Jurka72b079e2010-12-10 01:03:53 -080096 public boolean onLongClick(View v) {
97 // Return early if this is not initiated from a touch
98 if (!v.isInTouchMode()) return false;
99 // Return early if we are still animating the pages
100 if (mNextPage != INVALID_PAGE) return false;
101 return beginDragging(v);
102 }
103
104
105 /*
106 * Determines if we should change the touch state to start scrolling after the
107 * user moves their touch point too far.
108 */
109 protected void determineScrollingStart(MotionEvent ev) {
110 if (!mIsDragging) super.determineScrollingStart(ev);
111 }
112
113 /*
114 * Determines if we should change the touch state to start dragging after the
115 * user moves their touch point far enough.
116 */
117 protected void determineDraggingStart(MotionEvent ev) {
118 /*
119 * Locally do absolute value. mLastMotionX is set to the y value
120 * of the down event.
121 */
122 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
123 final float x = ev.getX(pointerIndex);
124 final float y = ev.getY(pointerIndex);
125 final int xDiff = (int) Math.abs(x - mLastMotionX);
126 final int yDiff = (int) Math.abs(y - mLastMotionY);
127
128 final int touchSlop = mTouchSlop;
129 boolean yMoved = yDiff > touchSlop;
130 boolean isUpwardMotion = (yDiff / (float) xDiff) > mDragSlopeThreshold;
131
132 if (isUpwardMotion && yMoved && mLastTouchedItem != null) {
133 // Drag if the user moved far enough along the Y axis
134 beginDragging(mLastTouchedItem);
135
136 // Cancel any pending long press
137 if (mAllowLongPress) {
138 mAllowLongPress = false;
139 // Try canceling the long press. It could also have been scheduled
140 // by a distant descendant, so use the mAllowLongPress flag to block
141 // everything
142 final View currentPage = getPageAt(mCurrentPage);
143 if (currentPage != null) {
144 currentPage.cancelLongPress();
145 }
146 }
147 }
148 }
149
150 public void setDragSlopeThreshold(float dragSlopeThreshold) {
151 mDragSlopeThreshold = dragSlopeThreshold;
152 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -0800153
154 @Override
155 protected void onDetachedFromWindow() {
156 mLastTouchedItem = null;
157 super.onDetachedFromWindow();
158 }
Michael Jurka72b079e2010-12-10 01:03:53 -0800159}