blob: eba89e5369d36bd6b54cfac10c86fdeb27a7400d [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato00acb122009-08-04 16:04:30 -040019import android.content.Context;
20import android.graphics.Bitmap;
Winson Chungb8c69f32011-10-19 21:36:08 -070021import android.graphics.Point;
Winson Chung043f2af2012-03-01 16:09:54 -080022import android.graphics.PointF;
Joe Onorato00acb122009-08-04 16:04:30 -040023import android.graphics.Rect;
Joe Onorato00acb122009-08-04 16:04:30 -040024import android.os.Handler;
Michael Jurka0280c3b2010-09-17 15:00:07 -070025import android.os.IBinder;
Joe Onorato00acb122009-08-04 16:04:30 -040026import android.os.Vibrator;
Joe Onorato00acb122009-08-04 16:04:30 -040027import android.util.Log;
Joe Onorato00acb122009-08-04 16:04:30 -040028import android.view.KeyEvent;
29import android.view.MotionEvent;
Winson Chung043f2af2012-03-01 16:09:54 -080030import android.view.VelocityTracker;
Michael Jurka0280c3b2010-09-17 15:00:07 -070031import android.view.View;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -070032import android.view.ViewConfiguration;
Joe Onorato00acb122009-08-04 16:04:30 -040033import android.view.inputmethod.InputMethodManager;
Joe Onorato00acb122009-08-04 16:04:30 -040034
Adam Cohen120980b2010-12-08 11:05:37 -080035import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070036
37import java.util.ArrayList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038
39/**
Joe Onorato00acb122009-08-04 16:04:30 -040040 * Class for initiating a drag within a view or across multiple views.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041 */
Joe Onorato00acb122009-08-04 16:04:30 -040042public class DragController {
Romain Guyea3763c2010-01-11 18:02:04 -080043 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato2e5c4322009-10-06 12:34:42 -070044 private static final String TAG = "Launcher.DragController";
45
Joe Onorato00acb122009-08-04 16:04:30 -040046 /** Indicates the drag is a move. */
47 public static int DRAG_ACTION_MOVE = 0;
48
49 /** Indicates the drag is a copy. */
50 public static int DRAG_ACTION_COPY = 1;
51
Winson Chungaa15ffe2012-01-18 15:45:28 -080052 private static final int SCROLL_DELAY = 500;
53 private static final int RESCROLL_DELAY = 750;
Winson Chung61b0c692012-02-23 16:31:13 -080054 private static final int VIBRATE_DURATION = 15;
Joe Onorato00acb122009-08-04 16:04:30 -040055
56 private static final boolean PROFILE_DRAWING_DURING_DRAG = false;
57
58 private static final int SCROLL_OUTSIDE_ZONE = 0;
59 private static final int SCROLL_WAITING_IN_ZONE = 1;
60
Patrick Dubroy54fa3b92010-11-17 12:18:45 -080061 static final int SCROLL_NONE = -1;
Patrick Dubroy1262e362010-10-06 15:49:50 -070062 static final int SCROLL_LEFT = 0;
63 static final int SCROLL_RIGHT = 1;
Joe Onorato00acb122009-08-04 16:04:30 -040064
Winson Chung043f2af2012-03-01 16:09:54 -080065 private static final float MAX_FLING_DEGREES = 35f;
66 private static final int FLING_TO_DELETE_THRESHOLD_Y_VELOCITY = -1400;
67
Adam Cohen8dfcba42011-07-07 16:38:18 -070068 private Launcher mLauncher;
Joe Onorato00acb122009-08-04 16:04:30 -040069 private Handler mHandler;
70 private final Vibrator mVibrator = new Vibrator();
71
72 // temporaries to avoid gc thrash
73 private Rect mRectTemp = new Rect();
74 private final int[] mCoordinatesTemp = new int[2];
75
76 /** Whether or not we're dragging. */
77 private boolean mDragging;
78
79 /** X coordinate of the down event. */
Adam Cohene3e27a82011-04-15 12:07:39 -070080 private int mMotionDownX;
Joe Onorato00acb122009-08-04 16:04:30 -040081
82 /** Y coordinate of the down event. */
Adam Cohene3e27a82011-04-15 12:07:39 -070083 private int mMotionDownY;
Joe Onorato00acb122009-08-04 16:04:30 -040084
Joe Onorato658db742010-09-29 11:40:39 -070085 /** the area at the edge of the screen that makes the workspace go left
86 * or right while you're dragging.
87 */
88 private int mScrollZone;
89
Adam Cohen9932a9b2011-08-02 22:14:07 -070090 private DropTarget.DragObject mDragObject;
Joe Onorato00acb122009-08-04 16:04:30 -040091
92 /** Who can receive drop events */
93 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
Patrick Dubroy4ed62782010-08-17 15:11:18 -070094 private ArrayList<DragListener> mListeners = new ArrayList<DragListener>();
Winson Chung043f2af2012-03-01 16:09:54 -080095 private DropTarget mFlingToDeleteDropTarget;
Joe Onorato00acb122009-08-04 16:04:30 -040096
97 /** The window token used as the parent for the DragView. */
98 private IBinder mWindowToken;
99
100 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
101 private View mScrollView;
102
Romain Guyea3763c2010-01-11 18:02:04 -0800103 private View mMoveTarget;
104
Joe Onorato00acb122009-08-04 16:04:30 -0400105 private DragScroller mDragScroller;
106 private int mScrollState = SCROLL_OUTSIDE_ZONE;
107 private ScrollRunnable mScrollRunnable = new ScrollRunnable();
108
Joe Onorato00acb122009-08-04 16:04:30 -0400109 private DropTarget mLastDropTarget;
110
111 private InputMethodManager mInputMethodManager;
112
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700113 private int mLastTouch[] = new int[2];
114 private int mDistanceSinceScroll = 0;
115
Winson Chung273c1022011-07-11 13:40:52 -0700116 private int mTmpPoint[] = new int[2];
117 private Rect mDragLayerRect = new Rect();
118
Winson Chung043f2af2012-03-01 16:09:54 -0800119 protected int mFlingToDeleteThresholdVelocity;
120 private VelocityTracker mVelocityTracker;
121
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 /**
123 * Interface to receive notifications when a drag starts or stops
124 */
125 interface DragListener {
126
127 /**
128 * A drag has begun
129 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 * @param source An object representing where the drag originated
131 * @param info The data associated with the object that is being dragged
132 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
133 * or {@link DragController#DRAG_ACTION_COPY}
134 */
Joe Onorato5162ea92009-09-03 09:39:42 -0700135 void onDragStart(DragSource source, Object info, int dragAction);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700138 * The drag has ended
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 */
140 void onDragEnd();
141 }
142
143 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400144 * Used to create a new DragLayer from XML.
145 *
146 * @param context The application's context.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700148 public DragController(Launcher launcher) {
149 mLauncher = launcher;
Joe Onorato00acb122009-08-04 16:04:30 -0400150 mHandler = new Handler();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700151 mScrollZone = launcher.getResources().getDimensionPixelSize(R.dimen.scroll_zone);
Winson Chung043f2af2012-03-01 16:09:54 -0800152 mVelocityTracker = VelocityTracker.obtain();
153
154 float density = launcher.getResources().getDisplayMetrics().density;
155 mFlingToDeleteThresholdVelocity = (int) (FLING_TO_DELETE_THRESHOLD_Y_VELOCITY * density);
Joe Onorato00acb122009-08-04 16:04:30 -0400156 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Patrick Dubroy1262e362010-10-06 15:49:50 -0700158 public boolean dragging() {
159 return mDragging;
160 }
161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 /**
Joe Onorato5162ea92009-09-03 09:39:42 -0700163 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700164 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 * @param v The view that is being dragged
166 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800167 * @param dragInfo The data associated with the object that is being dragged
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
169 * {@link #DRAG_ACTION_COPY}
170 */
Joe Onorato00acb122009-08-04 16:04:30 -0400171 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700172 startDrag(v, source, dragInfo, dragAction, null);
173 }
174
175 /**
176 * Starts a drag.
177 *
178 * @param v The view that is being dragged
179 * @param source An object representing where the drag originated
180 * @param dragInfo The data associated with the object that is being dragged
181 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
182 * {@link #DRAG_ACTION_COPY}
183 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
184 * Makes dragging feel more precise, e.g. you can clip out a transparent border
185 */
186 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction,
187 Rect dragRegion) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700188 Bitmap b = getViewBitmap(v);
189
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400190 if (b == null) {
191 // out of memory?
192 return;
193 }
194
Joe Onorato5162ea92009-09-03 09:39:42 -0700195 int[] loc = mCoordinatesTemp;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700196 mLauncher.getDragLayer().getLocationInDragLayer(v, loc);
197 int dragLayerX = loc[0];
198 int dragLayerY = loc[1];
Joe Onorato5162ea92009-09-03 09:39:42 -0700199
Winson Chung72d59842012-02-22 13:51:36 -0800200 startDrag(b, dragLayerX, dragLayerY, source, dragInfo, dragAction, null, dragRegion, 1f);
Joe Onorato5162ea92009-09-03 09:39:42 -0700201 b.recycle();
202
203 if (dragAction == DRAG_ACTION_MOVE) {
204 v.setVisibility(View.GONE);
205 }
206 }
207
208 /**
209 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700210 *
Winson Chunge3193b92010-09-10 11:44:42 -0700211 * @param v The view that is being dragged
212 * @param bmp The bitmap that represents the view being dragged
213 * @param source An object representing where the drag originated
214 * @param dragInfo The data associated with the object that is being dragged
215 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
216 * {@link #DRAG_ACTION_COPY}
217 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
218 * Makes dragging feel more precise, e.g. you can clip out a transparent border
219 */
220 public void startDrag(View v, Bitmap bmp, DragSource source, Object dragInfo, int dragAction,
Winson Chung72d59842012-02-22 13:51:36 -0800221 Rect dragRegion, float initialDragViewScale) {
Winson Chunge3193b92010-09-10 11:44:42 -0700222 int[] loc = mCoordinatesTemp;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700223 mLauncher.getDragLayer().getLocationInDragLayer(v, loc);
Winson Chung72d59842012-02-22 13:51:36 -0800224 int dragLayerX = loc[0] + v.getPaddingLeft() +
225 (int) ((initialDragViewScale * bmp.getWidth() - bmp.getWidth()) / 2);
226 int dragLayerY = loc[1] + v.getPaddingTop() +
227 (int) ((initialDragViewScale * bmp.getHeight() - bmp.getHeight()) / 2);
Winson Chunge3193b92010-09-10 11:44:42 -0700228
Winson Chung72d59842012-02-22 13:51:36 -0800229 startDrag(bmp, dragLayerX, dragLayerY, source, dragInfo, dragAction, null, dragRegion,
230 initialDragViewScale);
Winson Chunge3193b92010-09-10 11:44:42 -0700231
232 if (dragAction == DRAG_ACTION_MOVE) {
233 v.setVisibility(View.GONE);
234 }
235 }
236
237 /**
238 * Starts a drag.
239 *
Joe Onorato5162ea92009-09-03 09:39:42 -0700240 * @param b The bitmap to display as the drag image. It will be re-scaled to the
241 * enlarged size.
Adam Cohen8dfcba42011-07-07 16:38:18 -0700242 * @param dragLayerX The x position in the DragLayer of the left-top of the bitmap.
243 * @param dragLayerY The y position in the DragLayer of the left-top of the bitmap.
Joe Onorato5162ea92009-09-03 09:39:42 -0700244 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800245 * @param dragInfo The data associated with the object that is being dragged
Joe Onorato5162ea92009-09-03 09:39:42 -0700246 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
247 * {@link #DRAG_ACTION_COPY}
Michael Jurkaa63c4522010-08-19 13:52:27 -0700248 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
249 * Makes dragging feel more precise, e.g. you can clip out a transparent border
250 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700251 public void startDrag(Bitmap b, int dragLayerX, int dragLayerY,
Winson Chung72d59842012-02-22 13:51:36 -0800252 DragSource source, Object dragInfo, int dragAction, Point dragOffset, Rect dragRegion,
253 float initialDragViewScale) {
Joe Onorato00acb122009-08-04 16:04:30 -0400254 if (PROFILE_DRAWING_DURING_DRAG) {
255 android.os.Debug.startMethodTracing("Launcher");
256 }
257
258 // Hide soft keyboard, if visible
259 if (mInputMethodManager == null) {
260 mInputMethodManager = (InputMethodManager)
Adam Cohen8dfcba42011-07-07 16:38:18 -0700261 mLauncher.getSystemService(Context.INPUT_METHOD_SERVICE);
Joe Onorato00acb122009-08-04 16:04:30 -0400262 }
263 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
264
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700265 for (DragListener listener : mListeners) {
266 listener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400267 }
268
Adam Cohen8dfcba42011-07-07 16:38:18 -0700269 final int registrationX = mMotionDownX - dragLayerX;
270 final int registrationY = mMotionDownY - dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400271
Michael Jurkaa63c4522010-08-19 13:52:27 -0700272 final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left;
273 final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top;
Adam Cohene3e27a82011-04-15 12:07:39 -0700274
Joe Onorato00acb122009-08-04 16:04:30 -0400275 mDragging = true;
Adam Cohencb3382b2011-05-24 14:07:08 -0700276
Adam Cohen9932a9b2011-08-02 22:14:07 -0700277 mDragObject = new DropTarget.DragObject();
278
Adam Cohenbfbfd262011-06-13 16:55:12 -0700279 mDragObject.dragComplete = false;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700280 mDragObject.xOffset = mMotionDownX - (dragLayerX + dragRegionLeft);
281 mDragObject.yOffset = mMotionDownY - (dragLayerY + dragRegionTop);
Adam Cohencb3382b2011-05-24 14:07:08 -0700282 mDragObject.dragSource = source;
283 mDragObject.dragInfo = dragInfo;
Joe Onorato00acb122009-08-04 16:04:30 -0400284
285 mVibrator.vibrate(VIBRATE_DURATION);
286
Adam Cohen8dfcba42011-07-07 16:38:18 -0700287 final DragView dragView = mDragObject.dragView = new DragView(mLauncher, b, registrationX,
Winson Chung72d59842012-02-22 13:51:36 -0800288 registrationY, 0, 0, b.getWidth(), b.getHeight(), initialDragViewScale);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700289
Winson Chungb8c69f32011-10-19 21:36:08 -0700290 if (dragOffset != null) {
291 dragView.setDragVisualizeOffset(new Point(dragOffset));
292 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700293 if (dragRegion != null) {
Adam Cohene3e27a82011-04-15 12:07:39 -0700294 dragView.setDragRegion(new Rect(dragRegion));
Michael Jurkaa63c4522010-08-19 13:52:27 -0700295 }
296
Adam Cohen8dfcba42011-07-07 16:38:18 -0700297 dragView.show(mMotionDownX, mMotionDownY);
298 handleMoveEvent(mMotionDownX, mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400299 }
300
301 /**
302 * Draw the view into a bitmap.
303 */
Adam Cohen120980b2010-12-08 11:05:37 -0800304 Bitmap getViewBitmap(View v) {
Joe Onorato00acb122009-08-04 16:04:30 -0400305 v.clearFocus();
306 v.setPressed(false);
307
308 boolean willNotCache = v.willNotCacheDrawing();
309 v.setWillNotCacheDrawing(false);
310
311 // Reset the drawing cache background color to fully transparent
312 // for the duration of this operation
313 int color = v.getDrawingCacheBackgroundColor();
314 v.setDrawingCacheBackgroundColor(0);
Adam Cohen120980b2010-12-08 11:05:37 -0800315 float alpha = v.getAlpha();
316 v.setAlpha(1.0f);
Joe Onorato00acb122009-08-04 16:04:30 -0400317
318 if (color != 0) {
319 v.destroyDrawingCache();
320 }
321 v.buildDrawingCache();
322 Bitmap cacheBitmap = v.getDrawingCache();
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400323 if (cacheBitmap == null) {
324 Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
325 return null;
326 }
Joe Onorato00acb122009-08-04 16:04:30 -0400327
328 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
329
330 // Restore the view
331 v.destroyDrawingCache();
Adam Cohen120980b2010-12-08 11:05:37 -0800332 v.setAlpha(alpha);
Joe Onorato00acb122009-08-04 16:04:30 -0400333 v.setWillNotCacheDrawing(willNotCache);
334 v.setDrawingCacheBackgroundColor(color);
335
336 return bitmap;
337 }
338
339 /**
340 * Call this from a drag source view like this:
341 *
342 * <pre>
343 * @Override
344 * public boolean dispatchKeyEvent(KeyEvent event) {
345 * return mDragController.dispatchKeyEvent(this, event)
346 * || super.dispatchKeyEvent(event);
347 * </pre>
348 */
Romain Guyea3763c2010-01-11 18:02:04 -0800349 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato00acb122009-08-04 16:04:30 -0400350 public boolean dispatchKeyEvent(KeyEvent event) {
351 return mDragging;
352 }
353
Winson Chung304dcde2011-01-07 11:17:23 -0800354 public boolean isDragging() {
355 return mDragging;
356 }
357
Joe Onorato24b6fd82009-11-12 13:47:09 -0800358 /**
359 * Stop dragging without dropping.
360 */
361 public void cancelDrag() {
Winson Chung621e6402011-01-04 16:03:57 -0800362 if (mDragging) {
Winson Chungc07918d2011-07-01 15:35:26 -0700363 if (mLastDropTarget != null) {
364 mLastDropTarget.onDragExit(mDragObject);
365 }
Winson Chung41bb19d2012-03-05 18:36:46 -0800366 mDragObject.deferDragViewCleanupPostAnimation = false;
Adam Cohen36cc09b2011-09-29 17:33:15 -0700367 mDragObject.cancelled = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700368 mDragObject.dragComplete = true;
Winson Chunga48487a2012-03-20 16:19:37 -0700369 mDragObject.dragSource.onDropCompleted(null, mDragObject, false, false);
Winson Chung621e6402011-01-04 16:03:57 -0800370 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800371 endDrag();
372 }
Winson Chunga1820962011-10-03 16:31:06 -0700373 public void onAppsRemoved(ArrayList<ApplicationInfo> apps, Context context) {
374 // Cancel the current drag if we are removing an app that we are dragging
375 if (mDragObject != null) {
376 Object rawDragInfo = mDragObject.dragInfo;
377 if (rawDragInfo instanceof ShortcutInfo) {
378 ShortcutInfo dragInfo = (ShortcutInfo) rawDragInfo;
379 for (ApplicationInfo info : apps) {
380 if (dragInfo.intent.getComponent().equals(info.intent.getComponent())) {
381 cancelDrag();
382 return;
383 }
384 }
385 }
386 }
387 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800388
Joe Onorato00acb122009-08-04 16:04:30 -0400389 private void endDrag() {
390 if (mDragging) {
391 mDragging = false;
Winson Chungaa15ffe2012-01-18 15:45:28 -0800392 clearScrollRunnable();
Winson Chung043f2af2012-03-01 16:09:54 -0800393 boolean isDeferred = false;
Adam Cohencb3382b2011-05-24 14:07:08 -0700394 if (mDragObject.dragView != null) {
Winson Chung043f2af2012-03-01 16:09:54 -0800395 isDeferred = mDragObject.deferDragViewCleanupPostAnimation;
396 if (!isDeferred) {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800397 mDragObject.dragView.remove();
398 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700399 mDragObject.dragView = null;
Joe Onorato00acb122009-08-04 16:04:30 -0400400 }
Winson Chung043f2af2012-03-01 16:09:54 -0800401
402 // Only end the drag if we are not deferred
403 if (!isDeferred) {
404 for (DragListener listener : mListeners) {
405 listener.onDragEnd();
406 }
407 }
408 }
409
410 releaseVelocityTracker();
411 }
412
413 /**
414 * This only gets called as a result of drag view cleanup being deferred in endDrag();
415 */
416 void onDeferredEndDrag(DragView dragView) {
417 dragView.remove();
418
419 // If we skipped calling onDragEnd() before, do it now
420 for (DragListener listener : mListeners) {
421 listener.onDragEnd();
Joe Onorato00acb122009-08-04 16:04:30 -0400422 }
423 }
424
Winson Chunga48487a2012-03-20 16:19:37 -0700425 void onDeferredEndFling(DropTarget.DragObject d) {
426 d.dragSource.onFlingToDeleteCompleted();
427 }
428
Joe Onorato00acb122009-08-04 16:04:30 -0400429 /**
Winson Chung273c1022011-07-11 13:40:52 -0700430 * Clamps the position to the drag layer bounds.
431 */
432 private int[] getClampedDragLayerPos(float x, float y) {
433 mLauncher.getDragLayer().getLocalVisibleRect(mDragLayerRect);
434 mTmpPoint[0] = (int) Math.max(mDragLayerRect.left, Math.min(x, mDragLayerRect.right - 1));
435 mTmpPoint[1] = (int) Math.max(mDragLayerRect.top, Math.min(y, mDragLayerRect.bottom - 1));
436 return mTmpPoint;
437 }
438
439 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400440 * Call this from a drag source view.
441 */
442 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400443 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800444 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400445 + mDragging);
446 }
Joe Onorato00acb122009-08-04 16:04:30 -0400447
Winson Chung043f2af2012-03-01 16:09:54 -0800448 // Update the velocity tracker
449 acquireVelocityTrackerAndAddMovement(ev);
450
451 final int action = ev.getAction();
Winson Chung273c1022011-07-11 13:40:52 -0700452 final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY());
453 final int dragLayerX = dragLayerPos[0];
454 final int dragLayerY = dragLayerPos[1];
Joe Onorato00acb122009-08-04 16:04:30 -0400455
456 switch (action) {
457 case MotionEvent.ACTION_MOVE:
458 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400459 case MotionEvent.ACTION_DOWN:
460 // Remember location of down touch
Adam Cohen8dfcba42011-07-07 16:38:18 -0700461 mMotionDownX = dragLayerX;
462 mMotionDownY = dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400463 mLastDropTarget = null;
464 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400465 case MotionEvent.ACTION_UP:
466 if (mDragging) {
Winson Chung043f2af2012-03-01 16:09:54 -0800467 PointF vec = isFlingingToDelete(mDragObject.dragSource);
468 if (vec != null) {
469 dropOnFlingToDeleteTarget(dragLayerX, dragLayerY, vec);
470 } else {
471 drop(dragLayerX, dragLayerY);
472 }
Joe Onorato00acb122009-08-04 16:04:30 -0400473 }
474 endDrag();
475 break;
Winson Chung621e6402011-01-04 16:03:57 -0800476 case MotionEvent.ACTION_CANCEL:
477 cancelDrag();
478 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400479 }
480
481 return mDragging;
482 }
483
484 /**
Romain Guyea3763c2010-01-11 18:02:04 -0800485 * Sets the view that should handle move events.
486 */
487 void setMoveTarget(View view) {
488 mMoveTarget = view;
489 }
490
491 public boolean dispatchUnhandledMove(View focused, int direction) {
492 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
493 }
494
Winson Chungaa15ffe2012-01-18 15:45:28 -0800495 private void clearScrollRunnable() {
496 mHandler.removeCallbacks(mScrollRunnable);
497 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
498 mScrollState = SCROLL_OUTSIDE_ZONE;
499 mScrollRunnable.setDirection(SCROLL_RIGHT);
500 mDragScroller.onExitScrollArea();
501 }
502 }
503
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700504 private void handleMoveEvent(int x, int y) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700505 mDragObject.dragView.move(x, y);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700506
507 // Drop on someone?
508 final int[] coordinates = mCoordinatesTemp;
509 DropTarget dropTarget = findDropTarget(x, y, coordinates);
Adam Cohencb3382b2011-05-24 14:07:08 -0700510 mDragObject.x = coordinates[0];
511 mDragObject.y = coordinates[1];
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700512 if (dropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700513 DropTarget delegate = dropTarget.getDropTargetDelegate(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700514 if (delegate != null) {
515 dropTarget = delegate;
516 }
517
518 if (mLastDropTarget != dropTarget) {
519 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700520 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700521 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700522 dropTarget.onDragEnter(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700523 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700524 dropTarget.onDragOver(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700525 } else {
526 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700527 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700528 }
529 }
530 mLastDropTarget = dropTarget;
531
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700532 // After a scroll, the touch point will still be in the scroll region.
533 // Rather than scrolling immediately, require a bit of twiddling to scroll again
Adam Cohen8dfcba42011-07-07 16:38:18 -0700534 final int slop = ViewConfiguration.get(mLauncher).getScaledWindowTouchSlop();
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700535 mDistanceSinceScroll +=
536 Math.sqrt(Math.pow(mLastTouch[0] - x, 2) + Math.pow(mLastTouch[1] - y, 2));
537 mLastTouch[0] = x;
538 mLastTouch[1] = y;
Winson Chungaa15ffe2012-01-18 15:45:28 -0800539 final int delay = mDistanceSinceScroll < slop ? RESCROLL_DELAY : SCROLL_DELAY;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700540
Winson Chung3f4e1422011-11-17 14:58:51 -0800541 if (x < mScrollZone) {
Winson Chungaa15ffe2012-01-18 15:45:28 -0800542 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700543 mScrollState = SCROLL_WAITING_IN_ZONE;
Winson Chung3e0839e2011-10-03 15:15:18 -0700544 if (mDragScroller.onEnterScrollArea(x, y, SCROLL_LEFT)) {
545 mScrollRunnable.setDirection(SCROLL_LEFT);
Winson Chungaa15ffe2012-01-18 15:45:28 -0800546 mHandler.postDelayed(mScrollRunnable, delay);
Winson Chung3e0839e2011-10-03 15:15:18 -0700547 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700548 }
Winson Chung3f4e1422011-11-17 14:58:51 -0800549 } else if (x > mScrollView.getWidth() - mScrollZone) {
Winson Chungaa15ffe2012-01-18 15:45:28 -0800550 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700551 mScrollState = SCROLL_WAITING_IN_ZONE;
Winson Chung3e0839e2011-10-03 15:15:18 -0700552 if (mDragScroller.onEnterScrollArea(x, y, SCROLL_RIGHT)) {
553 mScrollRunnable.setDirection(SCROLL_RIGHT);
Winson Chungaa15ffe2012-01-18 15:45:28 -0800554 mHandler.postDelayed(mScrollRunnable, delay);
Winson Chung3e0839e2011-10-03 15:15:18 -0700555 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700556 }
557 } else {
Winson Chungaa15ffe2012-01-18 15:45:28 -0800558 clearScrollRunnable();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700559 }
560 }
561
Winson Chung3bc21c32012-01-20 13:59:18 -0800562 public void forceMoveEvent() {
563 if (mDragging) {
564 handleMoveEvent(mDragObject.x, mDragObject.y);
565 }
566 }
567
Romain Guyea3763c2010-01-11 18:02:04 -0800568 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400569 * Call this from a drag source view.
570 */
571 public boolean onTouchEvent(MotionEvent ev) {
Joe Onorato00acb122009-08-04 16:04:30 -0400572 if (!mDragging) {
573 return false;
574 }
575
Winson Chung043f2af2012-03-01 16:09:54 -0800576 // Update the velocity tracker
577 acquireVelocityTrackerAndAddMovement(ev);
578
Joe Onorato00acb122009-08-04 16:04:30 -0400579 final int action = ev.getAction();
Winson Chung273c1022011-07-11 13:40:52 -0700580 final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY());
581 final int dragLayerX = dragLayerPos[0];
582 final int dragLayerY = dragLayerPos[1];
Joe Onorato00acb122009-08-04 16:04:30 -0400583
584 switch (action) {
585 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400586 // Remember where the motion event started
Adam Cohen8dfcba42011-07-07 16:38:18 -0700587 mMotionDownX = dragLayerX;
588 mMotionDownY = dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400589
Adam Cohen8dfcba42011-07-07 16:38:18 -0700590 if ((dragLayerX < mScrollZone) || (dragLayerX > mScrollView.getWidth() - mScrollZone)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400591 mScrollState = SCROLL_WAITING_IN_ZONE;
592 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
593 } else {
594 mScrollState = SCROLL_OUTSIDE_ZONE;
595 }
Joe Onorato00acb122009-08-04 16:04:30 -0400596 break;
597 case MotionEvent.ACTION_MOVE:
Adam Cohen8dfcba42011-07-07 16:38:18 -0700598 handleMoveEvent(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400599 break;
600 case MotionEvent.ACTION_UP:
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800601 // Ensure that we've processed a move event at the current pointer location.
Adam Cohen8dfcba42011-07-07 16:38:18 -0700602 handleMoveEvent(dragLayerX, dragLayerY);
Winson Chung3bc21c32012-01-20 13:59:18 -0800603 mHandler.removeCallbacks(mScrollRunnable);
Winson Chung043f2af2012-03-01 16:09:54 -0800604
Joe Onorato00acb122009-08-04 16:04:30 -0400605 if (mDragging) {
Winson Chung043f2af2012-03-01 16:09:54 -0800606 PointF vec = isFlingingToDelete(mDragObject.dragSource);
607 if (vec != null) {
608 dropOnFlingToDeleteTarget(dragLayerX, dragLayerY, vec);
609 } else {
610 drop(dragLayerX, dragLayerY);
611 }
Joe Onorato00acb122009-08-04 16:04:30 -0400612 }
613 endDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400614 break;
615 case MotionEvent.ACTION_CANCEL:
Winson Chung3bc21c32012-01-20 13:59:18 -0800616 mHandler.removeCallbacks(mScrollRunnable);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800617 cancelDrag();
Winson Chung621e6402011-01-04 16:03:57 -0800618 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400619 }
620
621 return true;
622 }
623
Winson Chung043f2af2012-03-01 16:09:54 -0800624 /**
625 * Determines whether the user flung the current item to delete it.
626 *
627 * @return the vector at which the item was flung, or null if no fling was detected.
628 */
629 private PointF isFlingingToDelete(DragSource source) {
630 if (mFlingToDeleteDropTarget == null) return null;
631 if (!source.supportsFlingToDelete()) return null;
632
633 ViewConfiguration config = ViewConfiguration.get(mLauncher);
634 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
635
636 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
637 // Do a quick dot product test to ensure that we are flinging upwards
638 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
639 mVelocityTracker.getYVelocity());
640 PointF upVec = new PointF(0f, -1f);
641 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
642 (vel.length() * upVec.length()));
643 if (theta <= Math.toRadians(MAX_FLING_DEGREES)) {
644 return vel;
645 }
646 }
647 return null;
648 }
649
650 private void dropOnFlingToDeleteTarget(float x, float y, PointF vel) {
651 final int[] coordinates = mCoordinatesTemp;
652
653 mDragObject.x = coordinates[0];
654 mDragObject.y = coordinates[1];
Winson Chung043f2af2012-03-01 16:09:54 -0800655
656 // Clean up dragging on the target if it's not the current fling delete target otherwise,
657 // start dragging to it.
658 if (mLastDropTarget != null && mFlingToDeleteDropTarget != mLastDropTarget) {
659 mLastDropTarget.onDragExit(mDragObject);
660 }
661
662 // Drop onto the fling-to-delete target
663 boolean accepted = false;
664 mFlingToDeleteDropTarget.onDragEnter(mDragObject);
Winson Chung232decb2012-03-28 15:09:05 -0700665 // We must set dragComplete to true _only_ after we "enter" the fling-to-delete target for
666 // "drop"
667 mDragObject.dragComplete = true;
Winson Chung043f2af2012-03-01 16:09:54 -0800668 mFlingToDeleteDropTarget.onDragExit(mDragObject);
669 if (mFlingToDeleteDropTarget.acceptDrop(mDragObject)) {
670 mFlingToDeleteDropTarget.onFlingToDelete(mDragObject, mDragObject.x, mDragObject.y,
671 vel);
672 accepted = true;
673 }
Winson Chunga48487a2012-03-20 16:19:37 -0700674 mDragObject.dragSource.onDropCompleted((View) mFlingToDeleteDropTarget, mDragObject, true,
Winson Chung043f2af2012-03-01 16:09:54 -0800675 accepted);
676 }
677
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800678 private void drop(float x, float y) {
Joe Onorato00acb122009-08-04 16:04:30 -0400679 final int[] coordinates = mCoordinatesTemp;
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800680 final DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400681
Adam Cohencb3382b2011-05-24 14:07:08 -0700682 mDragObject.x = coordinates[0];
683 mDragObject.y = coordinates[1];
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800684 boolean accepted = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400685 if (dropTarget != null) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700686 mDragObject.dragComplete = true;
Adam Cohencb3382b2011-05-24 14:07:08 -0700687 dropTarget.onDragExit(mDragObject);
688 if (dropTarget.acceptDrop(mDragObject)) {
689 dropTarget.onDrop(mDragObject);
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800690 accepted = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400691 }
692 }
Winson Chunga48487a2012-03-20 16:19:37 -0700693 mDragObject.dragSource.onDropCompleted((View) dropTarget, mDragObject, false, accepted);
Joe Onorato00acb122009-08-04 16:04:30 -0400694 }
695
696 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
697 final Rect r = mRectTemp;
698
699 final ArrayList<DropTarget> dropTargets = mDropTargets;
700 final int count = dropTargets.size();
701 for (int i=count-1; i>=0; i--) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700702 DropTarget target = dropTargets.get(i);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700703 if (!target.isDropEnabled())
704 continue;
705
Joe Onorato00acb122009-08-04 16:04:30 -0400706 target.getHitRect(r);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700707
Adam Cohen8dfcba42011-07-07 16:38:18 -0700708 // Convert the hit rect to DragLayer coordinates
709 target.getLocationInDragLayer(dropCoordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400710 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
Patrick Dubroy440c3602010-07-13 17:50:32 -0700711
Adam Cohencb3382b2011-05-24 14:07:08 -0700712 mDragObject.x = x;
713 mDragObject.y = y;
Joe Onorato00acb122009-08-04 16:04:30 -0400714 if (r.contains(x, y)) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700715 DropTarget delegate = target.getDropTargetDelegate(mDragObject);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700716 if (delegate != null) {
717 target = delegate;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700718 target.getLocationInDragLayer(dropCoordinates);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700719 }
720
721 // Make dropCoordinates relative to the DropTarget
Joe Onorato00acb122009-08-04 16:04:30 -0400722 dropCoordinates[0] = x - dropCoordinates[0];
723 dropCoordinates[1] = y - dropCoordinates[1];
Patrick Dubroy440c3602010-07-13 17:50:32 -0700724
Joe Onorato00acb122009-08-04 16:04:30 -0400725 return target;
726 }
727 }
728 return null;
729 }
730
731 public void setDragScoller(DragScroller scroller) {
732 mDragScroller = scroller;
733 }
734
735 public void setWindowToken(IBinder token) {
736 mWindowToken = token;
737 }
738
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 /**
740 * Sets the drag listner which will be notified when a drag starts or ends.
741 */
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700742 public void addDragListener(DragListener l) {
743 mListeners.add(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400744 }
745
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 /**
747 * Remove a previously installed drag listener.
748 */
Joe Onorato00acb122009-08-04 16:04:30 -0400749 public void removeDragListener(DragListener l) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700750 mListeners.remove(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400751 }
752
753 /**
754 * Add a DropTarget to the list of potential places to receive drop events.
755 */
756 public void addDropTarget(DropTarget target) {
757 mDropTargets.add(target);
758 }
759
760 /**
761 * Don't send drop events to <em>target</em> any more.
762 */
763 public void removeDropTarget(DropTarget target) {
764 mDropTargets.remove(target);
765 }
766
767 /**
Winson Chung043f2af2012-03-01 16:09:54 -0800768 * Sets the current fling-to-delete drop target.
769 */
770 public void setFlingToDeleteDropTarget(DropTarget target) {
771 mFlingToDeleteDropTarget = target;
772 }
773
774 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
775 if (mVelocityTracker == null) {
776 mVelocityTracker = VelocityTracker.obtain();
777 }
778 mVelocityTracker.addMovement(ev);
779 }
780
781 private void releaseVelocityTracker() {
782 if (mVelocityTracker != null) {
783 mVelocityTracker.recycle();
784 mVelocityTracker = null;
785 }
786 }
787
788 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400789 * Set which view scrolls for touch events near the edge of the screen.
790 */
791 public void setScrollView(View v) {
792 mScrollView = v;
793 }
794
Patrick Dubroy5f445422011-02-18 14:35:21 -0800795 DragView getDragView() {
Adam Cohencb3382b2011-05-24 14:07:08 -0700796 return mDragObject.dragView;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800797 }
798
Joe Onorato00acb122009-08-04 16:04:30 -0400799 private class ScrollRunnable implements Runnable {
800 private int mDirection;
801
802 ScrollRunnable() {
803 }
804
805 public void run() {
806 if (mDragScroller != null) {
807 if (mDirection == SCROLL_LEFT) {
808 mDragScroller.scrollLeft();
809 } else {
810 mDragScroller.scrollRight();
811 }
812 mScrollState = SCROLL_OUTSIDE_ZONE;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700813 mDistanceSinceScroll = 0;
814 mDragScroller.onExitScrollArea();
Winson Chungaa15ffe2012-01-18 15:45:28 -0800815
816 if (isDragging()) {
817 // Force an update so that we can requeue the scroller if necessary
Winson Chung3bc21c32012-01-20 13:59:18 -0800818 forceMoveEvent();
Winson Chungaa15ffe2012-01-18 15:45:28 -0800819 }
Joe Onorato00acb122009-08-04 16:04:30 -0400820 }
821 }
822
823 void setDirection(int direction) {
824 mDirection = direction;
825 }
826 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827}