blob: ee054abcc1c579b2e7d51b12fcd59d1194da6c56 [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
104 private RectF mDeleteRegion;
105 private DropTarget mLastDropTarget;
106
107 private InputMethodManager mInputMethodManager;
108
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700109 private int mLastTouch[] = new int[2];
110 private int mDistanceSinceScroll = 0;
111
Winson Chung273c1022011-07-11 13:40:52 -0700112 private int mTmpPoint[] = new int[2];
113 private Rect mDragLayerRect = new Rect();
114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 /**
116 * Interface to receive notifications when a drag starts or stops
117 */
118 interface DragListener {
119
120 /**
121 * A drag has begun
122 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 * @param source An object representing where the drag originated
124 * @param info The data associated with the object that is being dragged
125 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
126 * or {@link DragController#DRAG_ACTION_COPY}
127 */
Joe Onorato5162ea92009-09-03 09:39:42 -0700128 void onDragStart(DragSource source, Object info, int dragAction);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
130 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700131 * The drag has ended
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 */
133 void onDragEnd();
134 }
135
136 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400137 * Used to create a new DragLayer from XML.
138 *
139 * @param context The application's context.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700141 public DragController(Launcher launcher) {
142 mLauncher = launcher;
Joe Onorato00acb122009-08-04 16:04:30 -0400143 mHandler = new Handler();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700144 mScrollZone = launcher.getResources().getDimensionPixelSize(R.dimen.scroll_zone);
Joe Onorato00acb122009-08-04 16:04:30 -0400145 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Patrick Dubroy1262e362010-10-06 15:49:50 -0700147 public boolean dragging() {
148 return mDragging;
149 }
150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 /**
Joe Onorato5162ea92009-09-03 09:39:42 -0700152 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700153 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 * @param v The view that is being dragged
155 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800156 * @param dragInfo The data associated with the object that is being dragged
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
158 * {@link #DRAG_ACTION_COPY}
159 */
Joe Onorato00acb122009-08-04 16:04:30 -0400160 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700161 startDrag(v, source, dragInfo, dragAction, null);
162 }
163
164 /**
165 * Starts a drag.
166 *
167 * @param v The view that is being dragged
168 * @param source An object representing where the drag originated
169 * @param dragInfo The data associated with the object that is being dragged
170 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
171 * {@link #DRAG_ACTION_COPY}
172 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
173 * Makes dragging feel more precise, e.g. you can clip out a transparent border
174 */
175 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction,
176 Rect dragRegion) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700177 Bitmap b = getViewBitmap(v);
178
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400179 if (b == null) {
180 // out of memory?
181 return;
182 }
183
Joe Onorato5162ea92009-09-03 09:39:42 -0700184 int[] loc = mCoordinatesTemp;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700185 mLauncher.getDragLayer().getLocationInDragLayer(v, loc);
186 int dragLayerX = loc[0];
187 int dragLayerY = loc[1];
Joe Onorato5162ea92009-09-03 09:39:42 -0700188
Winson Chungb8c69f32011-10-19 21:36:08 -0700189 startDrag(b, dragLayerX, dragLayerY, source, dragInfo, dragAction, null, dragRegion);
Joe Onorato5162ea92009-09-03 09:39:42 -0700190 b.recycle();
191
192 if (dragAction == DRAG_ACTION_MOVE) {
193 v.setVisibility(View.GONE);
194 }
195 }
196
197 /**
198 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700199 *
Winson Chunge3193b92010-09-10 11:44:42 -0700200 * @param v The view that is being dragged
201 * @param bmp The bitmap that represents the view being dragged
202 * @param source An object representing where the drag originated
203 * @param dragInfo The data associated with the object that is being dragged
204 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
205 * {@link #DRAG_ACTION_COPY}
206 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
207 * Makes dragging feel more precise, e.g. you can clip out a transparent border
208 */
209 public void startDrag(View v, Bitmap bmp, DragSource source, Object dragInfo, int dragAction,
210 Rect dragRegion) {
Winson Chunge3193b92010-09-10 11:44:42 -0700211 int[] loc = mCoordinatesTemp;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700212 mLauncher.getDragLayer().getLocationInDragLayer(v, loc);
213 int dragLayerX = loc[0];
214 int dragLayerY = loc[1];
Winson Chunge3193b92010-09-10 11:44:42 -0700215
Winson Chungb8c69f32011-10-19 21:36:08 -0700216 startDrag(bmp, dragLayerX, dragLayerY, source, dragInfo, dragAction, null, dragRegion);
Winson Chunge3193b92010-09-10 11:44:42 -0700217
218 if (dragAction == DRAG_ACTION_MOVE) {
219 v.setVisibility(View.GONE);
220 }
221 }
222
223 /**
224 * Starts a drag.
225 *
Joe Onorato5162ea92009-09-03 09:39:42 -0700226 * @param b The bitmap to display as the drag image. It will be re-scaled to the
227 * enlarged size.
Adam Cohen8dfcba42011-07-07 16:38:18 -0700228 * @param dragLayerX The x position in the DragLayer of the left-top of the bitmap.
229 * @param dragLayerY The y position in the DragLayer of the left-top of the bitmap.
Joe Onorato5162ea92009-09-03 09:39:42 -0700230 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800231 * @param dragInfo The data associated with the object that is being dragged
Joe Onorato5162ea92009-09-03 09:39:42 -0700232 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
233 * {@link #DRAG_ACTION_COPY}
234 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700235 public void startDrag(Bitmap b, int dragLayerX, int dragLayerY,
Joe Onorato5162ea92009-09-03 09:39:42 -0700236 DragSource source, Object dragInfo, int dragAction) {
Winson Chungb8c69f32011-10-19 21:36:08 -0700237 startDrag(b, dragLayerX, dragLayerY, source, dragInfo, dragAction, null, null);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700238 }
239
240 /**
241 * Starts a drag.
242 *
243 * @param b The bitmap to display as the drag image. It will be re-scaled to the
244 * enlarged size.
Adam Cohen8dfcba42011-07-07 16:38:18 -0700245 * @param dragLayerX The x position in the DragLayer of the left-top of the bitmap.
246 * @param dragLayerY The y position in the DragLayer of the left-top of the bitmap.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700247 * @param source An object representing where the drag originated
248 * @param dragInfo The data associated with the object that is being dragged
249 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
250 * {@link #DRAG_ACTION_COPY}
251 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
252 * Makes dragging feel more precise, e.g. you can clip out a transparent border
253 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700254 public void startDrag(Bitmap b, int dragLayerX, int dragLayerY,
Winson Chungb8c69f32011-10-19 21:36:08 -0700255 DragSource source, Object dragInfo, int dragAction, Point dragOffset, Rect dragRegion) {
Joe Onorato00acb122009-08-04 16:04:30 -0400256 if (PROFILE_DRAWING_DURING_DRAG) {
257 android.os.Debug.startMethodTracing("Launcher");
258 }
259
260 // Hide soft keyboard, if visible
261 if (mInputMethodManager == null) {
262 mInputMethodManager = (InputMethodManager)
Adam Cohen8dfcba42011-07-07 16:38:18 -0700263 mLauncher.getSystemService(Context.INPUT_METHOD_SERVICE);
Joe Onorato00acb122009-08-04 16:04:30 -0400264 }
265 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
266
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700267 for (DragListener listener : mListeners) {
268 listener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400269 }
270
Adam Cohen8dfcba42011-07-07 16:38:18 -0700271 final int registrationX = mMotionDownX - dragLayerX;
272 final int registrationY = mMotionDownY - dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400273
Michael Jurkaa63c4522010-08-19 13:52:27 -0700274 final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left;
275 final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top;
Adam Cohene3e27a82011-04-15 12:07:39 -0700276
Joe Onorato00acb122009-08-04 16:04:30 -0400277 mDragging = true;
Adam Cohencb3382b2011-05-24 14:07:08 -0700278
Adam Cohen9932a9b2011-08-02 22:14:07 -0700279 mDragObject = new DropTarget.DragObject();
280
Adam Cohenbfbfd262011-06-13 16:55:12 -0700281 mDragObject.dragComplete = false;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700282 mDragObject.xOffset = mMotionDownX - (dragLayerX + dragRegionLeft);
283 mDragObject.yOffset = mMotionDownY - (dragLayerY + dragRegionTop);
Adam Cohencb3382b2011-05-24 14:07:08 -0700284 mDragObject.dragSource = source;
285 mDragObject.dragInfo = dragInfo;
Joe Onorato00acb122009-08-04 16:04:30 -0400286
287 mVibrator.vibrate(VIBRATE_DURATION);
288
Adam Cohen8dfcba42011-07-07 16:38:18 -0700289 final DragView dragView = mDragObject.dragView = new DragView(mLauncher, b, registrationX,
Adam Cohencb3382b2011-05-24 14:07:08 -0700290 registrationY, 0, 0, b.getWidth(), b.getHeight());
Michael Jurkaa63c4522010-08-19 13:52:27 -0700291
Winson Chungb8c69f32011-10-19 21:36:08 -0700292 if (dragOffset != null) {
293 dragView.setDragVisualizeOffset(new Point(dragOffset));
294 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700295 if (dragRegion != null) {
Adam Cohene3e27a82011-04-15 12:07:39 -0700296 dragView.setDragRegion(new Rect(dragRegion));
Michael Jurkaa63c4522010-08-19 13:52:27 -0700297 }
298
Adam Cohen8dfcba42011-07-07 16:38:18 -0700299 dragView.show(mMotionDownX, mMotionDownY);
300 handleMoveEvent(mMotionDownX, mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400301 }
302
303 /**
304 * Draw the view into a bitmap.
305 */
Adam Cohen120980b2010-12-08 11:05:37 -0800306 Bitmap getViewBitmap(View v) {
Joe Onorato00acb122009-08-04 16:04:30 -0400307 v.clearFocus();
308 v.setPressed(false);
309
310 boolean willNotCache = v.willNotCacheDrawing();
311 v.setWillNotCacheDrawing(false);
312
313 // Reset the drawing cache background color to fully transparent
314 // for the duration of this operation
315 int color = v.getDrawingCacheBackgroundColor();
316 v.setDrawingCacheBackgroundColor(0);
Adam Cohen120980b2010-12-08 11:05:37 -0800317 float alpha = v.getAlpha();
318 v.setAlpha(1.0f);
Joe Onorato00acb122009-08-04 16:04:30 -0400319
320 if (color != 0) {
321 v.destroyDrawingCache();
322 }
323 v.buildDrawingCache();
324 Bitmap cacheBitmap = v.getDrawingCache();
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400325 if (cacheBitmap == null) {
326 Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
327 return null;
328 }
Joe Onorato00acb122009-08-04 16:04:30 -0400329
330 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
331
332 // Restore the view
333 v.destroyDrawingCache();
Adam Cohen120980b2010-12-08 11:05:37 -0800334 v.setAlpha(alpha);
Joe Onorato00acb122009-08-04 16:04:30 -0400335 v.setWillNotCacheDrawing(willNotCache);
336 v.setDrawingCacheBackgroundColor(color);
337
338 return bitmap;
339 }
340
341 /**
342 * Call this from a drag source view like this:
343 *
344 * <pre>
345 * @Override
346 * public boolean dispatchKeyEvent(KeyEvent event) {
347 * return mDragController.dispatchKeyEvent(this, event)
348 * || super.dispatchKeyEvent(event);
349 * </pre>
350 */
Romain Guyea3763c2010-01-11 18:02:04 -0800351 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato00acb122009-08-04 16:04:30 -0400352 public boolean dispatchKeyEvent(KeyEvent event) {
353 return mDragging;
354 }
355
Winson Chung304dcde2011-01-07 11:17:23 -0800356 public boolean isDragging() {
357 return mDragging;
358 }
359
Joe Onorato24b6fd82009-11-12 13:47:09 -0800360 /**
361 * Stop dragging without dropping.
362 */
363 public void cancelDrag() {
Winson Chung621e6402011-01-04 16:03:57 -0800364 if (mDragging) {
Winson Chungc07918d2011-07-01 15:35:26 -0700365 if (mLastDropTarget != null) {
366 mLastDropTarget.onDragExit(mDragObject);
367 }
Adam Cohen36cc09b2011-09-29 17:33:15 -0700368 mDragObject.cancelled = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700369 mDragObject.dragComplete = true;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700370 mDragObject.dragSource.onDropCompleted(null, mDragObject, false);
Winson Chung621e6402011-01-04 16:03:57 -0800371 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800372 endDrag();
373 }
Winson Chunga1820962011-10-03 16:31:06 -0700374 public void onAppsRemoved(ArrayList<ApplicationInfo> apps, Context context) {
375 // Cancel the current drag if we are removing an app that we are dragging
376 if (mDragObject != null) {
377 Object rawDragInfo = mDragObject.dragInfo;
378 if (rawDragInfo instanceof ShortcutInfo) {
379 ShortcutInfo dragInfo = (ShortcutInfo) rawDragInfo;
380 for (ApplicationInfo info : apps) {
381 if (dragInfo.intent.getComponent().equals(info.intent.getComponent())) {
382 cancelDrag();
383 return;
384 }
385 }
386 }
387 }
388 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800389
Joe Onorato00acb122009-08-04 16:04:30 -0400390 private void endDrag() {
391 if (mDragging) {
392 mDragging = false;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700393 for (DragListener listener : mListeners) {
394 listener.onDragEnd();
Joe Onorato00acb122009-08-04 16:04:30 -0400395 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700396 if (mDragObject.dragView != null) {
397 mDragObject.dragView.remove();
398 mDragObject.dragView = null;
Joe Onorato00acb122009-08-04 16:04:30 -0400399 }
Joe Onorato00acb122009-08-04 16:04:30 -0400400 }
401 }
402
403 /**
Winson Chung273c1022011-07-11 13:40:52 -0700404 * Clamps the position to the drag layer bounds.
405 */
406 private int[] getClampedDragLayerPos(float x, float y) {
407 mLauncher.getDragLayer().getLocalVisibleRect(mDragLayerRect);
408 mTmpPoint[0] = (int) Math.max(mDragLayerRect.left, Math.min(x, mDragLayerRect.right - 1));
409 mTmpPoint[1] = (int) Math.max(mDragLayerRect.top, Math.min(y, mDragLayerRect.bottom - 1));
410 return mTmpPoint;
411 }
412
413 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400414 * Call this from a drag source view.
415 */
416 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400417 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800418 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400419 + mDragging);
420 }
Joe Onorato00acb122009-08-04 16:04:30 -0400421 final int action = ev.getAction();
422
Winson Chung273c1022011-07-11 13:40:52 -0700423 final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY());
424 final int dragLayerX = dragLayerPos[0];
425 final int dragLayerY = dragLayerPos[1];
Joe Onorato00acb122009-08-04 16:04:30 -0400426
427 switch (action) {
428 case MotionEvent.ACTION_MOVE:
429 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400430 case MotionEvent.ACTION_DOWN:
431 // Remember location of down touch
Adam Cohen8dfcba42011-07-07 16:38:18 -0700432 mMotionDownX = dragLayerX;
433 mMotionDownY = dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400434 mLastDropTarget = null;
435 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400436 case MotionEvent.ACTION_UP:
437 if (mDragging) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700438 drop(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400439 }
440 endDrag();
441 break;
Winson Chung621e6402011-01-04 16:03:57 -0800442 case MotionEvent.ACTION_CANCEL:
443 cancelDrag();
444 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400445 }
446
447 return mDragging;
448 }
449
450 /**
Romain Guyea3763c2010-01-11 18:02:04 -0800451 * Sets the view that should handle move events.
452 */
453 void setMoveTarget(View view) {
454 mMoveTarget = view;
455 }
456
457 public boolean dispatchUnhandledMove(View focused, int direction) {
458 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
459 }
460
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700461 private void handleMoveEvent(int x, int y) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700462 mDragObject.dragView.move(x, y);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700463
464 // Drop on someone?
465 final int[] coordinates = mCoordinatesTemp;
466 DropTarget dropTarget = findDropTarget(x, y, coordinates);
Adam Cohencb3382b2011-05-24 14:07:08 -0700467 mDragObject.x = coordinates[0];
468 mDragObject.y = coordinates[1];
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700469 if (dropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700470 DropTarget delegate = dropTarget.getDropTargetDelegate(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700471 if (delegate != null) {
472 dropTarget = delegate;
473 }
474
475 if (mLastDropTarget != dropTarget) {
476 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700477 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700478 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700479 dropTarget.onDragEnter(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700480 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700481 dropTarget.onDragOver(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700482 } else {
483 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700484 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700485 }
486 }
487 mLastDropTarget = dropTarget;
488
489 // Scroll, maybe, but not if we're in the delete region.
490 boolean inDeleteRegion = false;
491 if (mDeleteRegion != null) {
492 inDeleteRegion = mDeleteRegion.contains(x, y);
493 }
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700494
495 // After a scroll, the touch point will still be in the scroll region.
496 // Rather than scrolling immediately, require a bit of twiddling to scroll again
Adam Cohen8dfcba42011-07-07 16:38:18 -0700497 final int slop = ViewConfiguration.get(mLauncher).getScaledWindowTouchSlop();
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700498 mDistanceSinceScroll +=
499 Math.sqrt(Math.pow(mLastTouch[0] - x, 2) + Math.pow(mLastTouch[1] - y, 2));
500 mLastTouch[0] = x;
501 mLastTouch[1] = y;
502
Joe Onorato658db742010-09-29 11:40:39 -0700503 if (!inDeleteRegion && x < mScrollZone) {
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700504 if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700505 mScrollState = SCROLL_WAITING_IN_ZONE;
Winson Chung3e0839e2011-10-03 15:15:18 -0700506 if (mDragScroller.onEnterScrollArea(x, y, SCROLL_LEFT)) {
507 mScrollRunnable.setDirection(SCROLL_LEFT);
508 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
509 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700510 }
Joe Onorato658db742010-09-29 11:40:39 -0700511 } else if (!inDeleteRegion && x > mScrollView.getWidth() - mScrollZone) {
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700512 if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700513 mScrollState = SCROLL_WAITING_IN_ZONE;
Winson Chung3e0839e2011-10-03 15:15:18 -0700514 if (mDragScroller.onEnterScrollArea(x, y, SCROLL_RIGHT)) {
515 mScrollRunnable.setDirection(SCROLL_RIGHT);
516 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
517 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700518 }
519 } else {
520 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
521 mScrollState = SCROLL_OUTSIDE_ZONE;
522 mScrollRunnable.setDirection(SCROLL_RIGHT);
523 mHandler.removeCallbacks(mScrollRunnable);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700524 mDragScroller.onExitScrollArea();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700525 }
526 }
527 }
528
Romain Guyea3763c2010-01-11 18:02:04 -0800529 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400530 * Call this from a drag source view.
531 */
532 public boolean onTouchEvent(MotionEvent ev) {
Joe Onorato00acb122009-08-04 16:04:30 -0400533 if (!mDragging) {
534 return false;
535 }
536
537 final int action = ev.getAction();
Winson Chung273c1022011-07-11 13:40:52 -0700538 final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY());
539 final int dragLayerX = dragLayerPos[0];
540 final int dragLayerY = dragLayerPos[1];
Joe Onorato00acb122009-08-04 16:04:30 -0400541
542 switch (action) {
543 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400544 // Remember where the motion event started
Adam Cohen8dfcba42011-07-07 16:38:18 -0700545 mMotionDownX = dragLayerX;
546 mMotionDownY = dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400547
Adam Cohen8dfcba42011-07-07 16:38:18 -0700548 if ((dragLayerX < mScrollZone) || (dragLayerX > mScrollView.getWidth() - mScrollZone)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400549 mScrollState = SCROLL_WAITING_IN_ZONE;
550 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
551 } else {
552 mScrollState = SCROLL_OUTSIDE_ZONE;
553 }
Joe Onorato00acb122009-08-04 16:04:30 -0400554 break;
555 case MotionEvent.ACTION_MOVE:
Adam Cohen8dfcba42011-07-07 16:38:18 -0700556 handleMoveEvent(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400557 break;
558 case MotionEvent.ACTION_UP:
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800559 // Ensure that we've processed a move event at the current pointer location.
Adam Cohen8dfcba42011-07-07 16:38:18 -0700560 handleMoveEvent(dragLayerX, dragLayerY);
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800561
Joe Onorato00acb122009-08-04 16:04:30 -0400562 mHandler.removeCallbacks(mScrollRunnable);
563 if (mDragging) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700564 drop(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400565 }
566 endDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400567 break;
568 case MotionEvent.ACTION_CANCEL:
Joe Onorato24b6fd82009-11-12 13:47:09 -0800569 cancelDrag();
Winson Chung621e6402011-01-04 16:03:57 -0800570 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400571 }
572
573 return true;
574 }
575
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800576 private void drop(float x, float y) {
Joe Onorato00acb122009-08-04 16:04:30 -0400577 final int[] coordinates = mCoordinatesTemp;
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800578 final DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400579
Adam Cohencb3382b2011-05-24 14:07:08 -0700580 mDragObject.x = coordinates[0];
581 mDragObject.y = coordinates[1];
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800582 boolean accepted = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400583 if (dropTarget != null) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700584 mDragObject.dragComplete = true;
Adam Cohencb3382b2011-05-24 14:07:08 -0700585 dropTarget.onDragExit(mDragObject);
586 if (dropTarget.acceptDrop(mDragObject)) {
587 dropTarget.onDrop(mDragObject);
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800588 accepted = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400589 }
590 }
Adam Cohenc0dcf592011-06-01 15:30:43 -0700591 mDragObject.dragSource.onDropCompleted((View) dropTarget, mDragObject, accepted);
Joe Onorato00acb122009-08-04 16:04:30 -0400592 }
593
594 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
595 final Rect r = mRectTemp;
596
597 final ArrayList<DropTarget> dropTargets = mDropTargets;
598 final int count = dropTargets.size();
599 for (int i=count-1; i>=0; i--) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700600 DropTarget target = dropTargets.get(i);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700601 if (!target.isDropEnabled())
602 continue;
603
Joe Onorato00acb122009-08-04 16:04:30 -0400604 target.getHitRect(r);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700605
Adam Cohen8dfcba42011-07-07 16:38:18 -0700606 // Convert the hit rect to DragLayer coordinates
607 target.getLocationInDragLayer(dropCoordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400608 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
Patrick Dubroy440c3602010-07-13 17:50:32 -0700609
Adam Cohencb3382b2011-05-24 14:07:08 -0700610 mDragObject.x = x;
611 mDragObject.y = y;
Joe Onorato00acb122009-08-04 16:04:30 -0400612 if (r.contains(x, y)) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700613 DropTarget delegate = target.getDropTargetDelegate(mDragObject);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700614 if (delegate != null) {
615 target = delegate;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700616 target.getLocationInDragLayer(dropCoordinates);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700617 }
618
619 // Make dropCoordinates relative to the DropTarget
Joe Onorato00acb122009-08-04 16:04:30 -0400620 dropCoordinates[0] = x - dropCoordinates[0];
621 dropCoordinates[1] = y - dropCoordinates[1];
Patrick Dubroy440c3602010-07-13 17:50:32 -0700622
Joe Onorato00acb122009-08-04 16:04:30 -0400623 return target;
624 }
625 }
626 return null;
627 }
628
629 public void setDragScoller(DragScroller scroller) {
630 mDragScroller = scroller;
631 }
632
633 public void setWindowToken(IBinder token) {
634 mWindowToken = token;
635 }
636
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 /**
638 * Sets the drag listner which will be notified when a drag starts or ends.
639 */
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700640 public void addDragListener(DragListener l) {
641 mListeners.add(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400642 }
643
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 /**
645 * Remove a previously installed drag listener.
646 */
Joe Onorato00acb122009-08-04 16:04:30 -0400647 public void removeDragListener(DragListener l) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700648 mListeners.remove(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400649 }
650
651 /**
652 * Add a DropTarget to the list of potential places to receive drop events.
653 */
654 public void addDropTarget(DropTarget target) {
655 mDropTargets.add(target);
656 }
657
658 /**
659 * Don't send drop events to <em>target</em> any more.
660 */
661 public void removeDropTarget(DropTarget target) {
662 mDropTargets.remove(target);
663 }
664
665 /**
666 * Set which view scrolls for touch events near the edge of the screen.
667 */
668 public void setScrollView(View v) {
669 mScrollView = v;
670 }
671
672 /**
673 * Specifies the delete region. We won't scroll on touch events over the delete region.
674 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700675 * @param region The rectangle in DragLayer coordinates of the delete region.
Joe Onorato00acb122009-08-04 16:04:30 -0400676 */
677 void setDeleteRegion(RectF region) {
678 mDeleteRegion = region;
679 }
680
Patrick Dubroy5f445422011-02-18 14:35:21 -0800681 DragView getDragView() {
Adam Cohencb3382b2011-05-24 14:07:08 -0700682 return mDragObject.dragView;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800683 }
684
Joe Onorato00acb122009-08-04 16:04:30 -0400685 private class ScrollRunnable implements Runnable {
686 private int mDirection;
687
688 ScrollRunnable() {
689 }
690
691 public void run() {
692 if (mDragScroller != null) {
693 if (mDirection == SCROLL_LEFT) {
694 mDragScroller.scrollLeft();
695 } else {
696 mDragScroller.scrollRight();
697 }
698 mScrollState = SCROLL_OUTSIDE_ZONE;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700699 mDistanceSinceScroll = 0;
700 mDragScroller.onExitScrollArea();
Joe Onorato00acb122009-08-04 16:04:30 -0400701 }
702 }
703
704 void setDirection(int direction) {
705 mDirection = direction;
706 }
707 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708}