blob: 286e2fe6d01ccc3c5b028a9a1448bd6d48c91fc1 [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;
23import android.graphics.RectF;
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;
Michael Jurka0280c3b2010-09-17 15:00:07 -070030import android.view.View;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -070031import android.view.ViewConfiguration;
Joe Onorato00acb122009-08-04 16:04:30 -040032import android.view.inputmethod.InputMethodManager;
Joe Onorato00acb122009-08-04 16:04:30 -040033
Adam Cohen120980b2010-12-08 11:05:37 -080034import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070035
36import java.util.ArrayList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037
38/**
Joe Onorato00acb122009-08-04 16:04:30 -040039 * Class for initiating a drag within a view or across multiple views.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040 */
Joe Onorato00acb122009-08-04 16:04:30 -040041public class DragController {
Romain Guyea3763c2010-01-11 18:02:04 -080042 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato2e5c4322009-10-06 12:34:42 -070043 private static final String TAG = "Launcher.DragController";
44
Joe Onorato00acb122009-08-04 16:04:30 -040045 /** Indicates the drag is a move. */
46 public static int DRAG_ACTION_MOVE = 0;
47
48 /** Indicates the drag is a copy. */
49 public static int DRAG_ACTION_COPY = 1;
50
51 private static final int SCROLL_DELAY = 600;
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 Chungb8c69f32011-10-19 21:36:08 -0700188 startDrag(b, dragLayerX, dragLayerY, source, dragInfo, dragAction, null, dragRegion);
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,
209 Rect dragRegion) {
Winson Chunge3193b92010-09-10 11:44:42 -0700210 int[] loc = mCoordinatesTemp;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700211 mLauncher.getDragLayer().getLocationInDragLayer(v, loc);
212 int dragLayerX = loc[0];
213 int dragLayerY = loc[1];
Winson Chunge3193b92010-09-10 11:44:42 -0700214
Winson Chungb8c69f32011-10-19 21:36:08 -0700215 startDrag(bmp, dragLayerX, dragLayerY, source, dragInfo, dragAction, null, dragRegion);
Winson Chunge3193b92010-09-10 11:44:42 -0700216
217 if (dragAction == DRAG_ACTION_MOVE) {
218 v.setVisibility(View.GONE);
219 }
220 }
221
222 /**
223 * Starts a drag.
224 *
Joe Onorato5162ea92009-09-03 09:39:42 -0700225 * @param b The bitmap to display as the drag image. It will be re-scaled to the
226 * enlarged size.
Adam Cohen8dfcba42011-07-07 16:38:18 -0700227 * @param dragLayerX The x position in the DragLayer of the left-top of the bitmap.
228 * @param dragLayerY The y position in the DragLayer of the left-top of the bitmap.
Joe Onorato5162ea92009-09-03 09:39:42 -0700229 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800230 * @param dragInfo The data associated with the object that is being dragged
Joe Onorato5162ea92009-09-03 09:39:42 -0700231 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
232 * {@link #DRAG_ACTION_COPY}
233 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700234 public void startDrag(Bitmap b, int dragLayerX, int dragLayerY,
Joe Onorato5162ea92009-09-03 09:39:42 -0700235 DragSource source, Object dragInfo, int dragAction) {
Winson Chungb8c69f32011-10-19 21:36:08 -0700236 startDrag(b, dragLayerX, dragLayerY, source, dragInfo, dragAction, null, null);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700237 }
238
239 /**
240 * Starts a drag.
241 *
242 * @param b The bitmap to display as the drag image. It will be re-scaled to the
243 * enlarged size.
Adam Cohen8dfcba42011-07-07 16:38:18 -0700244 * @param dragLayerX The x position in the DragLayer of the left-top of the bitmap.
245 * @param dragLayerY The y position in the DragLayer of the left-top of the bitmap.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700246 * @param source An object representing where the drag originated
247 * @param dragInfo The data associated with the object that is being dragged
248 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
249 * {@link #DRAG_ACTION_COPY}
250 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
251 * Makes dragging feel more precise, e.g. you can clip out a transparent border
252 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700253 public void startDrag(Bitmap b, int dragLayerX, int dragLayerY,
Winson Chungb8c69f32011-10-19 21:36:08 -0700254 DragSource source, Object dragInfo, int dragAction, Point dragOffset, Rect dragRegion) {
Joe Onorato00acb122009-08-04 16:04:30 -0400255 if (PROFILE_DRAWING_DURING_DRAG) {
256 android.os.Debug.startMethodTracing("Launcher");
257 }
258
259 // Hide soft keyboard, if visible
260 if (mInputMethodManager == null) {
261 mInputMethodManager = (InputMethodManager)
Adam Cohen8dfcba42011-07-07 16:38:18 -0700262 mLauncher.getSystemService(Context.INPUT_METHOD_SERVICE);
Joe Onorato00acb122009-08-04 16:04:30 -0400263 }
264 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
265
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700266 for (DragListener listener : mListeners) {
267 listener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400268 }
269
Adam Cohen8dfcba42011-07-07 16:38:18 -0700270 final int registrationX = mMotionDownX - dragLayerX;
271 final int registrationY = mMotionDownY - dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400272
Michael Jurkaa63c4522010-08-19 13:52:27 -0700273 final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left;
274 final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top;
Adam Cohene3e27a82011-04-15 12:07:39 -0700275
Joe Onorato00acb122009-08-04 16:04:30 -0400276 mDragging = true;
Adam Cohencb3382b2011-05-24 14:07:08 -0700277
Adam Cohen9932a9b2011-08-02 22:14:07 -0700278 mDragObject = new DropTarget.DragObject();
279
Adam Cohenbfbfd262011-06-13 16:55:12 -0700280 mDragObject.dragComplete = false;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700281 mDragObject.xOffset = mMotionDownX - (dragLayerX + dragRegionLeft);
282 mDragObject.yOffset = mMotionDownY - (dragLayerY + dragRegionTop);
Adam Cohencb3382b2011-05-24 14:07:08 -0700283 mDragObject.dragSource = source;
284 mDragObject.dragInfo = dragInfo;
Joe Onorato00acb122009-08-04 16:04:30 -0400285
286 mVibrator.vibrate(VIBRATE_DURATION);
287
Adam Cohen8dfcba42011-07-07 16:38:18 -0700288 final DragView dragView = mDragObject.dragView = new DragView(mLauncher, b, registrationX,
Adam Cohencb3382b2011-05-24 14:07:08 -0700289 registrationY, 0, 0, b.getWidth(), b.getHeight());
Michael Jurkaa63c4522010-08-19 13:52:27 -0700290
Winson Chungb8c69f32011-10-19 21:36:08 -0700291 if (dragOffset != null) {
292 dragView.setDragVisualizeOffset(new Point(dragOffset));
293 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700294 if (dragRegion != null) {
Adam Cohene3e27a82011-04-15 12:07:39 -0700295 dragView.setDragRegion(new Rect(dragRegion));
Michael Jurkaa63c4522010-08-19 13:52:27 -0700296 }
297
Adam Cohen8dfcba42011-07-07 16:38:18 -0700298 dragView.show(mMotionDownX, mMotionDownY);
299 handleMoveEvent(mMotionDownX, mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400300 }
301
302 /**
303 * Draw the view into a bitmap.
304 */
Adam Cohen120980b2010-12-08 11:05:37 -0800305 Bitmap getViewBitmap(View v) {
Joe Onorato00acb122009-08-04 16:04:30 -0400306 v.clearFocus();
307 v.setPressed(false);
308
309 boolean willNotCache = v.willNotCacheDrawing();
310 v.setWillNotCacheDrawing(false);
311
312 // Reset the drawing cache background color to fully transparent
313 // for the duration of this operation
314 int color = v.getDrawingCacheBackgroundColor();
315 v.setDrawingCacheBackgroundColor(0);
Adam Cohen120980b2010-12-08 11:05:37 -0800316 float alpha = v.getAlpha();
317 v.setAlpha(1.0f);
Joe Onorato00acb122009-08-04 16:04:30 -0400318
319 if (color != 0) {
320 v.destroyDrawingCache();
321 }
322 v.buildDrawingCache();
323 Bitmap cacheBitmap = v.getDrawingCache();
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400324 if (cacheBitmap == null) {
325 Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
326 return null;
327 }
Joe Onorato00acb122009-08-04 16:04:30 -0400328
329 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
330
331 // Restore the view
332 v.destroyDrawingCache();
Adam Cohen120980b2010-12-08 11:05:37 -0800333 v.setAlpha(alpha);
Joe Onorato00acb122009-08-04 16:04:30 -0400334 v.setWillNotCacheDrawing(willNotCache);
335 v.setDrawingCacheBackgroundColor(color);
336
337 return bitmap;
338 }
339
340 /**
341 * Call this from a drag source view like this:
342 *
343 * <pre>
344 * @Override
345 * public boolean dispatchKeyEvent(KeyEvent event) {
346 * return mDragController.dispatchKeyEvent(this, event)
347 * || super.dispatchKeyEvent(event);
348 * </pre>
349 */
Romain Guyea3763c2010-01-11 18:02:04 -0800350 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato00acb122009-08-04 16:04:30 -0400351 public boolean dispatchKeyEvent(KeyEvent event) {
352 return mDragging;
353 }
354
Winson Chung304dcde2011-01-07 11:17:23 -0800355 public boolean isDragging() {
356 return mDragging;
357 }
358
Joe Onorato24b6fd82009-11-12 13:47:09 -0800359 /**
360 * Stop dragging without dropping.
361 */
362 public void cancelDrag() {
Winson Chung621e6402011-01-04 16:03:57 -0800363 if (mDragging) {
Winson Chungc07918d2011-07-01 15:35:26 -0700364 if (mLastDropTarget != null) {
365 mLastDropTarget.onDragExit(mDragObject);
366 }
Adam Cohen36cc09b2011-09-29 17:33:15 -0700367 mDragObject.cancelled = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700368 mDragObject.dragComplete = true;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700369 mDragObject.dragSource.onDropCompleted(null, mDragObject, 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;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700392 for (DragListener listener : mListeners) {
393 listener.onDragEnd();
Joe Onorato00acb122009-08-04 16:04:30 -0400394 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700395 if (mDragObject.dragView != null) {
396 mDragObject.dragView.remove();
397 mDragObject.dragView = null;
Joe Onorato00acb122009-08-04 16:04:30 -0400398 }
Joe Onorato00acb122009-08-04 16:04:30 -0400399 }
400 }
401
402 /**
Winson Chung273c1022011-07-11 13:40:52 -0700403 * Clamps the position to the drag layer bounds.
404 */
405 private int[] getClampedDragLayerPos(float x, float y) {
406 mLauncher.getDragLayer().getLocalVisibleRect(mDragLayerRect);
407 mTmpPoint[0] = (int) Math.max(mDragLayerRect.left, Math.min(x, mDragLayerRect.right - 1));
408 mTmpPoint[1] = (int) Math.max(mDragLayerRect.top, Math.min(y, mDragLayerRect.bottom - 1));
409 return mTmpPoint;
410 }
411
412 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400413 * Call this from a drag source view.
414 */
415 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400416 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800417 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400418 + mDragging);
419 }
Joe Onorato00acb122009-08-04 16:04:30 -0400420 final int action = ev.getAction();
421
Winson Chung273c1022011-07-11 13:40:52 -0700422 final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY());
423 final int dragLayerX = dragLayerPos[0];
424 final int dragLayerY = dragLayerPos[1];
Joe Onorato00acb122009-08-04 16:04:30 -0400425
426 switch (action) {
427 case MotionEvent.ACTION_MOVE:
428 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400429 case MotionEvent.ACTION_DOWN:
430 // Remember location of down touch
Adam Cohen8dfcba42011-07-07 16:38:18 -0700431 mMotionDownX = dragLayerX;
432 mMotionDownY = dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400433 mLastDropTarget = null;
434 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400435 case MotionEvent.ACTION_UP:
436 if (mDragging) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700437 drop(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400438 }
439 endDrag();
440 break;
Winson Chung621e6402011-01-04 16:03:57 -0800441 case MotionEvent.ACTION_CANCEL:
442 cancelDrag();
443 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400444 }
445
446 return mDragging;
447 }
448
449 /**
Romain Guyea3763c2010-01-11 18:02:04 -0800450 * Sets the view that should handle move events.
451 */
452 void setMoveTarget(View view) {
453 mMoveTarget = view;
454 }
455
456 public boolean dispatchUnhandledMove(View focused, int direction) {
457 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
458 }
459
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700460 private void handleMoveEvent(int x, int y) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700461 mDragObject.dragView.move(x, y);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700462
463 // Drop on someone?
464 final int[] coordinates = mCoordinatesTemp;
465 DropTarget dropTarget = findDropTarget(x, y, coordinates);
Adam Cohencb3382b2011-05-24 14:07:08 -0700466 mDragObject.x = coordinates[0];
467 mDragObject.y = coordinates[1];
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700468 if (dropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700469 DropTarget delegate = dropTarget.getDropTargetDelegate(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700470 if (delegate != null) {
471 dropTarget = delegate;
472 }
473
474 if (mLastDropTarget != dropTarget) {
475 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700476 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700477 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700478 dropTarget.onDragEnter(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700479 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700480 dropTarget.onDragOver(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700481 } else {
482 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700483 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700484 }
485 }
486 mLastDropTarget = dropTarget;
487
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700488 // After a scroll, the touch point will still be in the scroll region.
489 // Rather than scrolling immediately, require a bit of twiddling to scroll again
Adam Cohen8dfcba42011-07-07 16:38:18 -0700490 final int slop = ViewConfiguration.get(mLauncher).getScaledWindowTouchSlop();
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700491 mDistanceSinceScroll +=
492 Math.sqrt(Math.pow(mLastTouch[0] - x, 2) + Math.pow(mLastTouch[1] - y, 2));
493 mLastTouch[0] = x;
494 mLastTouch[1] = y;
495
Winson Chung3f4e1422011-11-17 14:58:51 -0800496 if (x < mScrollZone) {
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700497 if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) {
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);
501 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
502 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700503 }
Winson Chung3f4e1422011-11-17 14:58:51 -0800504 } else if (x > mScrollView.getWidth() - mScrollZone) {
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700505 if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) {
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);
509 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
510 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700511 }
512 } else {
513 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
514 mScrollState = SCROLL_OUTSIDE_ZONE;
515 mScrollRunnable.setDirection(SCROLL_RIGHT);
516 mHandler.removeCallbacks(mScrollRunnable);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700517 mDragScroller.onExitScrollArea();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700518 }
519 }
520 }
521
Romain Guyea3763c2010-01-11 18:02:04 -0800522 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400523 * Call this from a drag source view.
524 */
525 public boolean onTouchEvent(MotionEvent ev) {
Joe Onorato00acb122009-08-04 16:04:30 -0400526 if (!mDragging) {
527 return false;
528 }
529
530 final int action = ev.getAction();
Winson Chung273c1022011-07-11 13:40:52 -0700531 final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY());
532 final int dragLayerX = dragLayerPos[0];
533 final int dragLayerY = dragLayerPos[1];
Joe Onorato00acb122009-08-04 16:04:30 -0400534
535 switch (action) {
536 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400537 // Remember where the motion event started
Adam Cohen8dfcba42011-07-07 16:38:18 -0700538 mMotionDownX = dragLayerX;
539 mMotionDownY = dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400540
Adam Cohen8dfcba42011-07-07 16:38:18 -0700541 if ((dragLayerX < mScrollZone) || (dragLayerX > mScrollView.getWidth() - mScrollZone)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400542 mScrollState = SCROLL_WAITING_IN_ZONE;
543 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
544 } else {
545 mScrollState = SCROLL_OUTSIDE_ZONE;
546 }
Joe Onorato00acb122009-08-04 16:04:30 -0400547 break;
548 case MotionEvent.ACTION_MOVE:
Adam Cohen8dfcba42011-07-07 16:38:18 -0700549 handleMoveEvent(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400550 break;
551 case MotionEvent.ACTION_UP:
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800552 // Ensure that we've processed a move event at the current pointer location.
Adam Cohen8dfcba42011-07-07 16:38:18 -0700553 handleMoveEvent(dragLayerX, dragLayerY);
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800554
Joe Onorato00acb122009-08-04 16:04:30 -0400555 mHandler.removeCallbacks(mScrollRunnable);
556 if (mDragging) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700557 drop(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400558 }
559 endDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400560 break;
561 case MotionEvent.ACTION_CANCEL:
Joe Onorato24b6fd82009-11-12 13:47:09 -0800562 cancelDrag();
Winson Chung621e6402011-01-04 16:03:57 -0800563 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400564 }
565
566 return true;
567 }
568
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800569 private void drop(float x, float y) {
Joe Onorato00acb122009-08-04 16:04:30 -0400570 final int[] coordinates = mCoordinatesTemp;
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800571 final DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400572
Adam Cohencb3382b2011-05-24 14:07:08 -0700573 mDragObject.x = coordinates[0];
574 mDragObject.y = coordinates[1];
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800575 boolean accepted = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400576 if (dropTarget != null) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700577 mDragObject.dragComplete = true;
Adam Cohencb3382b2011-05-24 14:07:08 -0700578 dropTarget.onDragExit(mDragObject);
579 if (dropTarget.acceptDrop(mDragObject)) {
580 dropTarget.onDrop(mDragObject);
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800581 accepted = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400582 }
583 }
Adam Cohenc0dcf592011-06-01 15:30:43 -0700584 mDragObject.dragSource.onDropCompleted((View) dropTarget, mDragObject, accepted);
Joe Onorato00acb122009-08-04 16:04:30 -0400585 }
586
587 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
588 final Rect r = mRectTemp;
589
590 final ArrayList<DropTarget> dropTargets = mDropTargets;
591 final int count = dropTargets.size();
592 for (int i=count-1; i>=0; i--) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700593 DropTarget target = dropTargets.get(i);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700594 if (!target.isDropEnabled())
595 continue;
596
Joe Onorato00acb122009-08-04 16:04:30 -0400597 target.getHitRect(r);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700598
Adam Cohen8dfcba42011-07-07 16:38:18 -0700599 // Convert the hit rect to DragLayer coordinates
600 target.getLocationInDragLayer(dropCoordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400601 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
Patrick Dubroy440c3602010-07-13 17:50:32 -0700602
Adam Cohencb3382b2011-05-24 14:07:08 -0700603 mDragObject.x = x;
604 mDragObject.y = y;
Joe Onorato00acb122009-08-04 16:04:30 -0400605 if (r.contains(x, y)) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700606 DropTarget delegate = target.getDropTargetDelegate(mDragObject);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700607 if (delegate != null) {
608 target = delegate;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700609 target.getLocationInDragLayer(dropCoordinates);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700610 }
611
612 // Make dropCoordinates relative to the DropTarget
Joe Onorato00acb122009-08-04 16:04:30 -0400613 dropCoordinates[0] = x - dropCoordinates[0];
614 dropCoordinates[1] = y - dropCoordinates[1];
Patrick Dubroy440c3602010-07-13 17:50:32 -0700615
Joe Onorato00acb122009-08-04 16:04:30 -0400616 return target;
617 }
618 }
619 return null;
620 }
621
622 public void setDragScoller(DragScroller scroller) {
623 mDragScroller = scroller;
624 }
625
626 public void setWindowToken(IBinder token) {
627 mWindowToken = token;
628 }
629
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 /**
631 * Sets the drag listner which will be notified when a drag starts or ends.
632 */
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700633 public void addDragListener(DragListener l) {
634 mListeners.add(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400635 }
636
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 /**
638 * Remove a previously installed drag listener.
639 */
Joe Onorato00acb122009-08-04 16:04:30 -0400640 public void removeDragListener(DragListener l) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700641 mListeners.remove(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400642 }
643
644 /**
645 * Add a DropTarget to the list of potential places to receive drop events.
646 */
647 public void addDropTarget(DropTarget target) {
648 mDropTargets.add(target);
649 }
650
651 /**
652 * Don't send drop events to <em>target</em> any more.
653 */
654 public void removeDropTarget(DropTarget target) {
655 mDropTargets.remove(target);
656 }
657
658 /**
659 * Set which view scrolls for touch events near the edge of the screen.
660 */
661 public void setScrollView(View v) {
662 mScrollView = v;
663 }
664
Patrick Dubroy5f445422011-02-18 14:35:21 -0800665 DragView getDragView() {
Adam Cohencb3382b2011-05-24 14:07:08 -0700666 return mDragObject.dragView;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800667 }
668
Joe Onorato00acb122009-08-04 16:04:30 -0400669 private class ScrollRunnable implements Runnable {
670 private int mDirection;
671
672 ScrollRunnable() {
673 }
674
675 public void run() {
676 if (mDragScroller != null) {
677 if (mDirection == SCROLL_LEFT) {
678 mDragScroller.scrollLeft();
679 } else {
680 mDragScroller.scrollRight();
681 }
682 mScrollState = SCROLL_OUTSIDE_ZONE;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700683 mDistanceSinceScroll = 0;
684 mDragScroller.onExitScrollArea();
Joe Onorato00acb122009-08-04 16:04:30 -0400685 }
686 }
687
688 void setDirection(int direction) {
689 mDirection = direction;
690 }
691 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692}