blob: d75c164b30f42ff09aa87934e2ea3eb32c4a55c3 [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;
Joe Onorato00acb122009-08-04 16:04:30 -040022import android.graphics.Rect;
Joe Onorato00acb122009-08-04 16:04:30 -040023import android.os.Handler;
Michael Jurka0280c3b2010-09-17 15:00:07 -070024import android.os.IBinder;
Joe Onorato00acb122009-08-04 16:04:30 -040025import android.os.Vibrator;
Joe Onorato00acb122009-08-04 16:04:30 -040026import android.util.Log;
Joe Onorato00acb122009-08-04 16:04:30 -040027import android.view.KeyEvent;
28import android.view.MotionEvent;
Michael Jurka0280c3b2010-09-17 15:00:07 -070029import android.view.View;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -070030import android.view.ViewConfiguration;
Joe Onorato00acb122009-08-04 16:04:30 -040031import android.view.inputmethod.InputMethodManager;
Joe Onorato00acb122009-08-04 16:04:30 -040032
Adam Cohen120980b2010-12-08 11:05:37 -080033import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070034
35import java.util.ArrayList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036
37/**
Joe Onorato00acb122009-08-04 16:04:30 -040038 * Class for initiating a drag within a view or across multiple views.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039 */
Joe Onorato00acb122009-08-04 16:04:30 -040040public class DragController {
Romain Guyea3763c2010-01-11 18:02:04 -080041 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato2e5c4322009-10-06 12:34:42 -070042 private static final String TAG = "Launcher.DragController";
43
Joe Onorato00acb122009-08-04 16:04:30 -040044 /** Indicates the drag is a move. */
45 public static int DRAG_ACTION_MOVE = 0;
46
47 /** Indicates the drag is a copy. */
48 public static int DRAG_ACTION_COPY = 1;
49
Winson Chungaa15ffe2012-01-18 15:45:28 -080050 private static final int SCROLL_DELAY = 500;
51 private static final int RESCROLL_DELAY = 750;
Joe Onorato00acb122009-08-04 16:04:30 -040052 private static final int VIBRATE_DURATION = 35;
53
54 private static final boolean PROFILE_DRAWING_DURING_DRAG = false;
55
56 private static final int SCROLL_OUTSIDE_ZONE = 0;
57 private static final int SCROLL_WAITING_IN_ZONE = 1;
58
Patrick Dubroy54fa3b92010-11-17 12:18:45 -080059 static final int SCROLL_NONE = -1;
Patrick Dubroy1262e362010-10-06 15:49:50 -070060 static final int SCROLL_LEFT = 0;
61 static final int SCROLL_RIGHT = 1;
Joe Onorato00acb122009-08-04 16:04:30 -040062
Adam Cohen8dfcba42011-07-07 16:38:18 -070063 private Launcher mLauncher;
Joe Onorato00acb122009-08-04 16:04:30 -040064 private Handler mHandler;
65 private final Vibrator mVibrator = new Vibrator();
66
67 // temporaries to avoid gc thrash
68 private Rect mRectTemp = new Rect();
69 private final int[] mCoordinatesTemp = new int[2];
70
71 /** Whether or not we're dragging. */
72 private boolean mDragging;
73
74 /** X coordinate of the down event. */
Adam Cohene3e27a82011-04-15 12:07:39 -070075 private int mMotionDownX;
Joe Onorato00acb122009-08-04 16:04:30 -040076
77 /** Y coordinate of the down event. */
Adam Cohene3e27a82011-04-15 12:07:39 -070078 private int mMotionDownY;
Joe Onorato00acb122009-08-04 16:04:30 -040079
Joe Onorato658db742010-09-29 11:40:39 -070080 /** the area at the edge of the screen that makes the workspace go left
81 * or right while you're dragging.
82 */
83 private int mScrollZone;
84
Adam Cohen9932a9b2011-08-02 22:14:07 -070085 private DropTarget.DragObject mDragObject;
Joe Onorato00acb122009-08-04 16:04:30 -040086
87 /** Who can receive drop events */
88 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
89
Patrick Dubroy4ed62782010-08-17 15:11:18 -070090 private ArrayList<DragListener> mListeners = new ArrayList<DragListener>();
Joe Onorato00acb122009-08-04 16:04:30 -040091
92 /** The window token used as the parent for the DragView. */
93 private IBinder mWindowToken;
94
95 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
96 private View mScrollView;
97
Romain Guyea3763c2010-01-11 18:02:04 -080098 private View mMoveTarget;
99
Joe Onorato00acb122009-08-04 16:04:30 -0400100 private DragScroller mDragScroller;
101 private int mScrollState = SCROLL_OUTSIDE_ZONE;
102 private ScrollRunnable mScrollRunnable = new ScrollRunnable();
103
Joe Onorato00acb122009-08-04 16:04:30 -0400104 private DropTarget mLastDropTarget;
105
106 private InputMethodManager mInputMethodManager;
107
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700108 private int mLastTouch[] = new int[2];
109 private int mDistanceSinceScroll = 0;
110
Winson Chung273c1022011-07-11 13:40:52 -0700111 private int mTmpPoint[] = new int[2];
112 private Rect mDragLayerRect = new Rect();
113
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 /**
115 * Interface to receive notifications when a drag starts or stops
116 */
117 interface DragListener {
118
119 /**
120 * A drag has begun
121 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 * @param source An object representing where the drag originated
123 * @param info The data associated with the object that is being dragged
124 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
125 * or {@link DragController#DRAG_ACTION_COPY}
126 */
Joe Onorato5162ea92009-09-03 09:39:42 -0700127 void onDragStart(DragSource source, Object info, int dragAction);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
129 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700130 * The drag has ended
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 */
132 void onDragEnd();
133 }
134
135 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400136 * Used to create a new DragLayer from XML.
137 *
138 * @param context The application's context.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700140 public DragController(Launcher launcher) {
141 mLauncher = launcher;
Joe Onorato00acb122009-08-04 16:04:30 -0400142 mHandler = new Handler();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700143 mScrollZone = launcher.getResources().getDimensionPixelSize(R.dimen.scroll_zone);
Joe Onorato00acb122009-08-04 16:04:30 -0400144 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Patrick Dubroy1262e362010-10-06 15:49:50 -0700146 public boolean dragging() {
147 return mDragging;
148 }
149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 /**
Joe Onorato5162ea92009-09-03 09:39:42 -0700151 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700152 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 * @param v The view that is being dragged
154 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800155 * @param dragInfo The data associated with the object that is being dragged
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
157 * {@link #DRAG_ACTION_COPY}
158 */
Joe Onorato00acb122009-08-04 16:04:30 -0400159 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700160 startDrag(v, source, dragInfo, dragAction, null);
161 }
162
163 /**
164 * Starts a drag.
165 *
166 * @param v The view that is being dragged
167 * @param source An object representing where the drag originated
168 * @param dragInfo The data associated with the object that is being dragged
169 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
170 * {@link #DRAG_ACTION_COPY}
171 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
172 * Makes dragging feel more precise, e.g. you can clip out a transparent border
173 */
174 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction,
175 Rect dragRegion) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700176 Bitmap b = getViewBitmap(v);
177
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400178 if (b == null) {
179 // out of memory?
180 return;
181 }
182
Joe Onorato5162ea92009-09-03 09:39:42 -0700183 int[] loc = mCoordinatesTemp;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700184 mLauncher.getDragLayer().getLocationInDragLayer(v, loc);
185 int dragLayerX = loc[0];
186 int dragLayerY = loc[1];
Joe Onorato5162ea92009-09-03 09:39:42 -0700187
Winson Chung72d59842012-02-22 13:51:36 -0800188 startDrag(b, dragLayerX, dragLayerY, source, dragInfo, dragAction, null, dragRegion, 1f);
Joe Onorato5162ea92009-09-03 09:39:42 -0700189 b.recycle();
190
191 if (dragAction == DRAG_ACTION_MOVE) {
192 v.setVisibility(View.GONE);
193 }
194 }
195
196 /**
197 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700198 *
Winson Chunge3193b92010-09-10 11:44:42 -0700199 * @param v The view that is being dragged
200 * @param bmp The bitmap that represents the view being dragged
201 * @param source An object representing where the drag originated
202 * @param dragInfo The data associated with the object that is being dragged
203 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
204 * {@link #DRAG_ACTION_COPY}
205 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
206 * Makes dragging feel more precise, e.g. you can clip out a transparent border
207 */
208 public void startDrag(View v, Bitmap bmp, DragSource source, Object dragInfo, int dragAction,
Winson Chung72d59842012-02-22 13:51:36 -0800209 Rect dragRegion, float initialDragViewScale) {
Winson Chunge3193b92010-09-10 11:44:42 -0700210 int[] loc = mCoordinatesTemp;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700211 mLauncher.getDragLayer().getLocationInDragLayer(v, loc);
Winson Chung72d59842012-02-22 13:51:36 -0800212 int dragLayerX = loc[0] + v.getPaddingLeft() +
213 (int) ((initialDragViewScale * bmp.getWidth() - bmp.getWidth()) / 2);
214 int dragLayerY = loc[1] + v.getPaddingTop() +
215 (int) ((initialDragViewScale * bmp.getHeight() - bmp.getHeight()) / 2);
Winson Chunge3193b92010-09-10 11:44:42 -0700216
Winson Chung72d59842012-02-22 13:51:36 -0800217 startDrag(bmp, dragLayerX, dragLayerY, source, dragInfo, dragAction, null, dragRegion,
218 initialDragViewScale);
Winson Chunge3193b92010-09-10 11:44:42 -0700219
220 if (dragAction == DRAG_ACTION_MOVE) {
221 v.setVisibility(View.GONE);
222 }
223 }
224
225 /**
226 * Starts a drag.
227 *
Joe Onorato5162ea92009-09-03 09:39:42 -0700228 * @param b The bitmap to display as the drag image. It will be re-scaled to the
229 * enlarged size.
Adam Cohen8dfcba42011-07-07 16:38:18 -0700230 * @param dragLayerX The x position in the DragLayer of the left-top of the bitmap.
231 * @param dragLayerY The y position in the DragLayer of the left-top of the bitmap.
Joe Onorato5162ea92009-09-03 09:39:42 -0700232 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800233 * @param dragInfo The data associated with the object that is being dragged
Joe Onorato5162ea92009-09-03 09:39:42 -0700234 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
235 * {@link #DRAG_ACTION_COPY}
Michael Jurkaa63c4522010-08-19 13:52:27 -0700236 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
237 * Makes dragging feel more precise, e.g. you can clip out a transparent border
238 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700239 public void startDrag(Bitmap b, int dragLayerX, int dragLayerY,
Winson Chung72d59842012-02-22 13:51:36 -0800240 DragSource source, Object dragInfo, int dragAction, Point dragOffset, Rect dragRegion,
241 float initialDragViewScale) {
Joe Onorato00acb122009-08-04 16:04:30 -0400242 if (PROFILE_DRAWING_DURING_DRAG) {
243 android.os.Debug.startMethodTracing("Launcher");
244 }
245
246 // Hide soft keyboard, if visible
247 if (mInputMethodManager == null) {
248 mInputMethodManager = (InputMethodManager)
Adam Cohen8dfcba42011-07-07 16:38:18 -0700249 mLauncher.getSystemService(Context.INPUT_METHOD_SERVICE);
Joe Onorato00acb122009-08-04 16:04:30 -0400250 }
251 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
252
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700253 for (DragListener listener : mListeners) {
254 listener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400255 }
256
Adam Cohen8dfcba42011-07-07 16:38:18 -0700257 final int registrationX = mMotionDownX - dragLayerX;
258 final int registrationY = mMotionDownY - dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400259
Michael Jurkaa63c4522010-08-19 13:52:27 -0700260 final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left;
261 final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top;
Adam Cohene3e27a82011-04-15 12:07:39 -0700262
Joe Onorato00acb122009-08-04 16:04:30 -0400263 mDragging = true;
Adam Cohencb3382b2011-05-24 14:07:08 -0700264
Adam Cohen9932a9b2011-08-02 22:14:07 -0700265 mDragObject = new DropTarget.DragObject();
266
Adam Cohenbfbfd262011-06-13 16:55:12 -0700267 mDragObject.dragComplete = false;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700268 mDragObject.xOffset = mMotionDownX - (dragLayerX + dragRegionLeft);
269 mDragObject.yOffset = mMotionDownY - (dragLayerY + dragRegionTop);
Adam Cohencb3382b2011-05-24 14:07:08 -0700270 mDragObject.dragSource = source;
271 mDragObject.dragInfo = dragInfo;
Joe Onorato00acb122009-08-04 16:04:30 -0400272
273 mVibrator.vibrate(VIBRATE_DURATION);
274
Adam Cohen8dfcba42011-07-07 16:38:18 -0700275 final DragView dragView = mDragObject.dragView = new DragView(mLauncher, b, registrationX,
Winson Chung72d59842012-02-22 13:51:36 -0800276 registrationY, 0, 0, b.getWidth(), b.getHeight(), initialDragViewScale);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700277
Winson Chungb8c69f32011-10-19 21:36:08 -0700278 if (dragOffset != null) {
279 dragView.setDragVisualizeOffset(new Point(dragOffset));
280 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700281 if (dragRegion != null) {
Adam Cohene3e27a82011-04-15 12:07:39 -0700282 dragView.setDragRegion(new Rect(dragRegion));
Michael Jurkaa63c4522010-08-19 13:52:27 -0700283 }
284
Adam Cohen8dfcba42011-07-07 16:38:18 -0700285 dragView.show(mMotionDownX, mMotionDownY);
286 handleMoveEvent(mMotionDownX, mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400287 }
288
289 /**
290 * Draw the view into a bitmap.
291 */
Adam Cohen120980b2010-12-08 11:05:37 -0800292 Bitmap getViewBitmap(View v) {
Joe Onorato00acb122009-08-04 16:04:30 -0400293 v.clearFocus();
294 v.setPressed(false);
295
296 boolean willNotCache = v.willNotCacheDrawing();
297 v.setWillNotCacheDrawing(false);
298
299 // Reset the drawing cache background color to fully transparent
300 // for the duration of this operation
301 int color = v.getDrawingCacheBackgroundColor();
302 v.setDrawingCacheBackgroundColor(0);
Adam Cohen120980b2010-12-08 11:05:37 -0800303 float alpha = v.getAlpha();
304 v.setAlpha(1.0f);
Joe Onorato00acb122009-08-04 16:04:30 -0400305
306 if (color != 0) {
307 v.destroyDrawingCache();
308 }
309 v.buildDrawingCache();
310 Bitmap cacheBitmap = v.getDrawingCache();
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400311 if (cacheBitmap == null) {
312 Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
313 return null;
314 }
Joe Onorato00acb122009-08-04 16:04:30 -0400315
316 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
317
318 // Restore the view
319 v.destroyDrawingCache();
Adam Cohen120980b2010-12-08 11:05:37 -0800320 v.setAlpha(alpha);
Joe Onorato00acb122009-08-04 16:04:30 -0400321 v.setWillNotCacheDrawing(willNotCache);
322 v.setDrawingCacheBackgroundColor(color);
323
324 return bitmap;
325 }
326
327 /**
328 * Call this from a drag source view like this:
329 *
330 * <pre>
331 * @Override
332 * public boolean dispatchKeyEvent(KeyEvent event) {
333 * return mDragController.dispatchKeyEvent(this, event)
334 * || super.dispatchKeyEvent(event);
335 * </pre>
336 */
Romain Guyea3763c2010-01-11 18:02:04 -0800337 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato00acb122009-08-04 16:04:30 -0400338 public boolean dispatchKeyEvent(KeyEvent event) {
339 return mDragging;
340 }
341
Winson Chung304dcde2011-01-07 11:17:23 -0800342 public boolean isDragging() {
343 return mDragging;
344 }
345
Joe Onorato24b6fd82009-11-12 13:47:09 -0800346 /**
347 * Stop dragging without dropping.
348 */
349 public void cancelDrag() {
Winson Chung621e6402011-01-04 16:03:57 -0800350 if (mDragging) {
Winson Chungc07918d2011-07-01 15:35:26 -0700351 if (mLastDropTarget != null) {
352 mLastDropTarget.onDragExit(mDragObject);
353 }
Adam Cohen36cc09b2011-09-29 17:33:15 -0700354 mDragObject.cancelled = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700355 mDragObject.dragComplete = true;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700356 mDragObject.dragSource.onDropCompleted(null, mDragObject, false);
Winson Chung621e6402011-01-04 16:03:57 -0800357 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800358 endDrag();
359 }
Winson Chunga1820962011-10-03 16:31:06 -0700360 public void onAppsRemoved(ArrayList<ApplicationInfo> apps, Context context) {
361 // Cancel the current drag if we are removing an app that we are dragging
362 if (mDragObject != null) {
363 Object rawDragInfo = mDragObject.dragInfo;
364 if (rawDragInfo instanceof ShortcutInfo) {
365 ShortcutInfo dragInfo = (ShortcutInfo) rawDragInfo;
366 for (ApplicationInfo info : apps) {
367 if (dragInfo.intent.getComponent().equals(info.intent.getComponent())) {
368 cancelDrag();
369 return;
370 }
371 }
372 }
373 }
374 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800375
Joe Onorato00acb122009-08-04 16:04:30 -0400376 private void endDrag() {
377 if (mDragging) {
378 mDragging = false;
Winson Chungaa15ffe2012-01-18 15:45:28 -0800379 clearScrollRunnable();
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700380 for (DragListener listener : mListeners) {
381 listener.onDragEnd();
Joe Onorato00acb122009-08-04 16:04:30 -0400382 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700383 if (mDragObject.dragView != null) {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800384 if (!mDragObject.deferDragViewCleanupPostAnimation) {
385 mDragObject.dragView.remove();
386 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700387 mDragObject.dragView = null;
Joe Onorato00acb122009-08-04 16:04:30 -0400388 }
Joe Onorato00acb122009-08-04 16:04:30 -0400389 }
390 }
391
392 /**
Winson Chung273c1022011-07-11 13:40:52 -0700393 * Clamps the position to the drag layer bounds.
394 */
395 private int[] getClampedDragLayerPos(float x, float y) {
396 mLauncher.getDragLayer().getLocalVisibleRect(mDragLayerRect);
397 mTmpPoint[0] = (int) Math.max(mDragLayerRect.left, Math.min(x, mDragLayerRect.right - 1));
398 mTmpPoint[1] = (int) Math.max(mDragLayerRect.top, Math.min(y, mDragLayerRect.bottom - 1));
399 return mTmpPoint;
400 }
401
402 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400403 * Call this from a drag source view.
404 */
405 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400406 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800407 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400408 + mDragging);
409 }
Joe Onorato00acb122009-08-04 16:04:30 -0400410 final int action = ev.getAction();
411
Winson Chung273c1022011-07-11 13:40:52 -0700412 final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY());
413 final int dragLayerX = dragLayerPos[0];
414 final int dragLayerY = dragLayerPos[1];
Joe Onorato00acb122009-08-04 16:04:30 -0400415
416 switch (action) {
417 case MotionEvent.ACTION_MOVE:
418 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400419 case MotionEvent.ACTION_DOWN:
420 // Remember location of down touch
Adam Cohen8dfcba42011-07-07 16:38:18 -0700421 mMotionDownX = dragLayerX;
422 mMotionDownY = dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400423 mLastDropTarget = null;
424 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400425 case MotionEvent.ACTION_UP:
426 if (mDragging) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700427 drop(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400428 }
429 endDrag();
430 break;
Winson Chung621e6402011-01-04 16:03:57 -0800431 case MotionEvent.ACTION_CANCEL:
432 cancelDrag();
433 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400434 }
435
436 return mDragging;
437 }
438
439 /**
Romain Guyea3763c2010-01-11 18:02:04 -0800440 * Sets the view that should handle move events.
441 */
442 void setMoveTarget(View view) {
443 mMoveTarget = view;
444 }
445
446 public boolean dispatchUnhandledMove(View focused, int direction) {
447 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
448 }
449
Winson Chungaa15ffe2012-01-18 15:45:28 -0800450 private void clearScrollRunnable() {
451 mHandler.removeCallbacks(mScrollRunnable);
452 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
453 mScrollState = SCROLL_OUTSIDE_ZONE;
454 mScrollRunnable.setDirection(SCROLL_RIGHT);
455 mDragScroller.onExitScrollArea();
456 }
457 }
458
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700459 private void handleMoveEvent(int x, int y) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700460 mDragObject.dragView.move(x, y);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700461
462 // Drop on someone?
463 final int[] coordinates = mCoordinatesTemp;
464 DropTarget dropTarget = findDropTarget(x, y, coordinates);
Adam Cohencb3382b2011-05-24 14:07:08 -0700465 mDragObject.x = coordinates[0];
466 mDragObject.y = coordinates[1];
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700467 if (dropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700468 DropTarget delegate = dropTarget.getDropTargetDelegate(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700469 if (delegate != null) {
470 dropTarget = delegate;
471 }
472
473 if (mLastDropTarget != dropTarget) {
474 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700475 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700476 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700477 dropTarget.onDragEnter(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700478 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700479 dropTarget.onDragOver(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700480 } else {
481 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700482 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700483 }
484 }
485 mLastDropTarget = dropTarget;
486
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700487 // After a scroll, the touch point will still be in the scroll region.
488 // Rather than scrolling immediately, require a bit of twiddling to scroll again
Adam Cohen8dfcba42011-07-07 16:38:18 -0700489 final int slop = ViewConfiguration.get(mLauncher).getScaledWindowTouchSlop();
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700490 mDistanceSinceScroll +=
491 Math.sqrt(Math.pow(mLastTouch[0] - x, 2) + Math.pow(mLastTouch[1] - y, 2));
492 mLastTouch[0] = x;
493 mLastTouch[1] = y;
Winson Chungaa15ffe2012-01-18 15:45:28 -0800494 final int delay = mDistanceSinceScroll < slop ? RESCROLL_DELAY : SCROLL_DELAY;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700495
Winson Chung3f4e1422011-11-17 14:58:51 -0800496 if (x < mScrollZone) {
Winson Chungaa15ffe2012-01-18 15:45:28 -0800497 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700498 mScrollState = SCROLL_WAITING_IN_ZONE;
Winson Chung3e0839e2011-10-03 15:15:18 -0700499 if (mDragScroller.onEnterScrollArea(x, y, SCROLL_LEFT)) {
500 mScrollRunnable.setDirection(SCROLL_LEFT);
Winson Chungaa15ffe2012-01-18 15:45:28 -0800501 mHandler.postDelayed(mScrollRunnable, delay);
Winson Chung3e0839e2011-10-03 15:15:18 -0700502 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700503 }
Winson Chung3f4e1422011-11-17 14:58:51 -0800504 } else if (x > mScrollView.getWidth() - mScrollZone) {
Winson Chungaa15ffe2012-01-18 15:45:28 -0800505 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700506 mScrollState = SCROLL_WAITING_IN_ZONE;
Winson Chung3e0839e2011-10-03 15:15:18 -0700507 if (mDragScroller.onEnterScrollArea(x, y, SCROLL_RIGHT)) {
508 mScrollRunnable.setDirection(SCROLL_RIGHT);
Winson Chungaa15ffe2012-01-18 15:45:28 -0800509 mHandler.postDelayed(mScrollRunnable, delay);
Winson Chung3e0839e2011-10-03 15:15:18 -0700510 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700511 }
512 } else {
Winson Chungaa15ffe2012-01-18 15:45:28 -0800513 clearScrollRunnable();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700514 }
515 }
516
Winson Chung3bc21c32012-01-20 13:59:18 -0800517 public void forceMoveEvent() {
518 if (mDragging) {
519 handleMoveEvent(mDragObject.x, mDragObject.y);
520 }
521 }
522
Romain Guyea3763c2010-01-11 18:02:04 -0800523 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400524 * Call this from a drag source view.
525 */
526 public boolean onTouchEvent(MotionEvent ev) {
Joe Onorato00acb122009-08-04 16:04:30 -0400527 if (!mDragging) {
528 return false;
529 }
530
531 final int action = ev.getAction();
Winson Chung273c1022011-07-11 13:40:52 -0700532 final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY());
533 final int dragLayerX = dragLayerPos[0];
534 final int dragLayerY = dragLayerPos[1];
Joe Onorato00acb122009-08-04 16:04:30 -0400535
536 switch (action) {
537 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400538 // Remember where the motion event started
Adam Cohen8dfcba42011-07-07 16:38:18 -0700539 mMotionDownX = dragLayerX;
540 mMotionDownY = dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400541
Adam Cohen8dfcba42011-07-07 16:38:18 -0700542 if ((dragLayerX < mScrollZone) || (dragLayerX > mScrollView.getWidth() - mScrollZone)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400543 mScrollState = SCROLL_WAITING_IN_ZONE;
544 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
545 } else {
546 mScrollState = SCROLL_OUTSIDE_ZONE;
547 }
Joe Onorato00acb122009-08-04 16:04:30 -0400548 break;
549 case MotionEvent.ACTION_MOVE:
Adam Cohen8dfcba42011-07-07 16:38:18 -0700550 handleMoveEvent(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400551 break;
552 case MotionEvent.ACTION_UP:
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800553 // Ensure that we've processed a move event at the current pointer location.
Adam Cohen8dfcba42011-07-07 16:38:18 -0700554 handleMoveEvent(dragLayerX, dragLayerY);
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800555
Winson Chung3bc21c32012-01-20 13:59:18 -0800556 mHandler.removeCallbacks(mScrollRunnable);
Joe Onorato00acb122009-08-04 16:04:30 -0400557 if (mDragging) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700558 drop(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400559 }
560 endDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400561 break;
562 case MotionEvent.ACTION_CANCEL:
Winson Chung3bc21c32012-01-20 13:59:18 -0800563 mHandler.removeCallbacks(mScrollRunnable);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800564 cancelDrag();
Winson Chung621e6402011-01-04 16:03:57 -0800565 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400566 }
567
568 return true;
569 }
570
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800571 private void drop(float x, float y) {
Joe Onorato00acb122009-08-04 16:04:30 -0400572 final int[] coordinates = mCoordinatesTemp;
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800573 final DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400574
Adam Cohencb3382b2011-05-24 14:07:08 -0700575 mDragObject.x = coordinates[0];
576 mDragObject.y = coordinates[1];
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800577 boolean accepted = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400578 if (dropTarget != null) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700579 mDragObject.dragComplete = true;
Adam Cohencb3382b2011-05-24 14:07:08 -0700580 dropTarget.onDragExit(mDragObject);
581 if (dropTarget.acceptDrop(mDragObject)) {
582 dropTarget.onDrop(mDragObject);
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800583 accepted = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400584 }
585 }
Adam Cohenc0dcf592011-06-01 15:30:43 -0700586 mDragObject.dragSource.onDropCompleted((View) dropTarget, mDragObject, accepted);
Joe Onorato00acb122009-08-04 16:04:30 -0400587 }
588
589 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
590 final Rect r = mRectTemp;
591
592 final ArrayList<DropTarget> dropTargets = mDropTargets;
593 final int count = dropTargets.size();
594 for (int i=count-1; i>=0; i--) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700595 DropTarget target = dropTargets.get(i);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700596 if (!target.isDropEnabled())
597 continue;
598
Joe Onorato00acb122009-08-04 16:04:30 -0400599 target.getHitRect(r);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700600
Adam Cohen8dfcba42011-07-07 16:38:18 -0700601 // Convert the hit rect to DragLayer coordinates
602 target.getLocationInDragLayer(dropCoordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400603 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
Patrick Dubroy440c3602010-07-13 17:50:32 -0700604
Adam Cohencb3382b2011-05-24 14:07:08 -0700605 mDragObject.x = x;
606 mDragObject.y = y;
Joe Onorato00acb122009-08-04 16:04:30 -0400607 if (r.contains(x, y)) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700608 DropTarget delegate = target.getDropTargetDelegate(mDragObject);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700609 if (delegate != null) {
610 target = delegate;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700611 target.getLocationInDragLayer(dropCoordinates);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700612 }
613
614 // Make dropCoordinates relative to the DropTarget
Joe Onorato00acb122009-08-04 16:04:30 -0400615 dropCoordinates[0] = x - dropCoordinates[0];
616 dropCoordinates[1] = y - dropCoordinates[1];
Patrick Dubroy440c3602010-07-13 17:50:32 -0700617
Joe Onorato00acb122009-08-04 16:04:30 -0400618 return target;
619 }
620 }
621 return null;
622 }
623
624 public void setDragScoller(DragScroller scroller) {
625 mDragScroller = scroller;
626 }
627
628 public void setWindowToken(IBinder token) {
629 mWindowToken = token;
630 }
631
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 /**
633 * Sets the drag listner which will be notified when a drag starts or ends.
634 */
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700635 public void addDragListener(DragListener l) {
636 mListeners.add(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400637 }
638
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 /**
640 * Remove a previously installed drag listener.
641 */
Joe Onorato00acb122009-08-04 16:04:30 -0400642 public void removeDragListener(DragListener l) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700643 mListeners.remove(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400644 }
645
646 /**
647 * Add a DropTarget to the list of potential places to receive drop events.
648 */
649 public void addDropTarget(DropTarget target) {
650 mDropTargets.add(target);
651 }
652
653 /**
654 * Don't send drop events to <em>target</em> any more.
655 */
656 public void removeDropTarget(DropTarget target) {
657 mDropTargets.remove(target);
658 }
659
660 /**
661 * Set which view scrolls for touch events near the edge of the screen.
662 */
663 public void setScrollView(View v) {
664 mScrollView = v;
665 }
666
Patrick Dubroy5f445422011-02-18 14:35:21 -0800667 DragView getDragView() {
Adam Cohencb3382b2011-05-24 14:07:08 -0700668 return mDragObject.dragView;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800669 }
670
Joe Onorato00acb122009-08-04 16:04:30 -0400671 private class ScrollRunnable implements Runnable {
672 private int mDirection;
673
674 ScrollRunnable() {
675 }
676
677 public void run() {
678 if (mDragScroller != null) {
679 if (mDirection == SCROLL_LEFT) {
680 mDragScroller.scrollLeft();
681 } else {
682 mDragScroller.scrollRight();
683 }
684 mScrollState = SCROLL_OUTSIDE_ZONE;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700685 mDistanceSinceScroll = 0;
686 mDragScroller.onExitScrollArea();
Winson Chungaa15ffe2012-01-18 15:45:28 -0800687
688 if (isDragging()) {
689 // Force an update so that we can requeue the scroller if necessary
Winson Chung3bc21c32012-01-20 13:59:18 -0800690 forceMoveEvent();
Winson Chungaa15ffe2012-01-18 15:45:28 -0800691 }
Joe Onorato00acb122009-08-04 16:04:30 -0400692 }
693 }
694
695 void setDirection(int direction) {
696 mDirection = direction;
697 }
698 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699}