blob: 5fa7be79837b56a4ebed94e913a459c1eab2c36b [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;
Winson Chung94569f42011-01-17 14:09:17 -080038 private boolean mIsDragEnabled;
Michael Jurka72b079e2010-12-10 01:03:53 -080039 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 Chung304dcde2011-01-07 11:17:23 -080054 boolean wasDragging = mIsDragging;
Michael Jurka72b079e2010-12-10 01:03:53 -080055 mIsDragging = true;
Winson Chung304dcde2011-01-07 11:17:23 -080056 return !wasDragging;
Michael Jurka72b079e2010-12-10 01:03:53 -080057 }
58
Winson Chung7d1fcbc2011-01-04 10:22:20 -080059 protected void cancelDragging() {
60 mIsDragging = false;
61 mLastTouchedItem = null;
Winson Chung94569f42011-01-17 14:09:17 -080062 mIsDragEnabled = false;
Winson Chung7d1fcbc2011-01-04 10:22:20 -080063 }
64
65 private void handleTouchEvent(MotionEvent ev) {
Michael Jurka72b079e2010-12-10 01:03:53 -080066 final int action = ev.getAction();
67 switch (action & MotionEvent.ACTION_MASK) {
68 case MotionEvent.ACTION_DOWN:
Winson Chung7d1fcbc2011-01-04 10:22:20 -080069 cancelDragging();
Winson Chung94569f42011-01-17 14:09:17 -080070 mIsDragEnabled = true;
Michael Jurka72b079e2010-12-10 01:03:53 -080071 break;
72 case MotionEvent.ACTION_MOVE:
Winson Chung94569f42011-01-17 14:09:17 -080073 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging && mIsDragEnabled) {
Michael Jurka72b079e2010-12-10 01:03:53 -080074 determineDraggingStart(ev);
75 }
76 break;
77 }
Winson Chung7d1fcbc2011-01-04 10:22:20 -080078 }
79
80 @Override
81 public boolean onInterceptTouchEvent(MotionEvent ev) {
82 handleTouchEvent(ev);
Michael Jurka72b079e2010-12-10 01:03:53 -080083 return super.onInterceptTouchEvent(ev);
84 }
85
86 @Override
Winson Chung7d1fcbc2011-01-04 10:22:20 -080087 public boolean onTouchEvent(MotionEvent ev) {
88 handleTouchEvent(ev);
89 return super.onTouchEvent(ev);
90 }
91
92 @Override
Michael Jurka72b079e2010-12-10 01:03:53 -080093 public boolean onTouch(View v, MotionEvent event) {
94 mLastTouchedItem = v;
Winson Chung94569f42011-01-17 14:09:17 -080095 mIsDragEnabled = true;
Michael Jurka72b079e2010-12-10 01:03:53 -080096 return false;
97 }
98
99 @Override
Michael Jurka72b079e2010-12-10 01:03:53 -0800100 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 Dubroy2313eff2011-01-11 20:01:31 -0800157
158 @Override
159 protected void onDetachedFromWindow() {
Winson Chung94569f42011-01-17 14:09:17 -0800160 cancelDragging();
Patrick Dubroy2313eff2011-01-11 20:01:31 -0800161 super.onDetachedFromWindow();
162 }
Michael Jurka72b079e2010-12-10 01:03:53 -0800163}