blob: eb0db69ebe77815e15ebaf163e268c65ac54a1bd [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;
Joe Onorato00acb122009-08-04 16:04:30 -040021import android.graphics.Rect;
22import android.graphics.RectF;
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
50 private static final int SCROLL_DELAY = 600;
Joe Onorato00acb122009-08-04 16:04:30 -040051 private static final int VIBRATE_DURATION = 35;
52
53 private static final boolean PROFILE_DRAWING_DURING_DRAG = false;
54
55 private static final int SCROLL_OUTSIDE_ZONE = 0;
56 private static final int SCROLL_WAITING_IN_ZONE = 1;
57
Patrick Dubroy54fa3b92010-11-17 12:18:45 -080058 static final int SCROLL_NONE = -1;
Patrick Dubroy1262e362010-10-06 15:49:50 -070059 static final int SCROLL_LEFT = 0;
60 static final int SCROLL_RIGHT = 1;
Joe Onorato00acb122009-08-04 16:04:30 -040061
Adam Cohen8dfcba42011-07-07 16:38:18 -070062 private Launcher mLauncher;
Joe Onorato00acb122009-08-04 16:04:30 -040063 private Handler mHandler;
64 private final Vibrator mVibrator = new Vibrator();
65
66 // temporaries to avoid gc thrash
67 private Rect mRectTemp = new Rect();
68 private final int[] mCoordinatesTemp = new int[2];
69
70 /** Whether or not we're dragging. */
71 private boolean mDragging;
72
73 /** X coordinate of the down event. */
Adam Cohene3e27a82011-04-15 12:07:39 -070074 private int mMotionDownX;
Joe Onorato00acb122009-08-04 16:04:30 -040075
76 /** Y coordinate of the down event. */
Adam Cohene3e27a82011-04-15 12:07:39 -070077 private int mMotionDownY;
Joe Onorato00acb122009-08-04 16:04:30 -040078
Joe Onorato658db742010-09-29 11:40:39 -070079 /** the area at the edge of the screen that makes the workspace go left
80 * or right while you're dragging.
81 */
82 private int mScrollZone;
83
Adam Cohencb3382b2011-05-24 14:07:08 -070084 private DropTarget.DragObject mDragObject = new DropTarget.DragObject();
Joe Onorato00acb122009-08-04 16:04:30 -040085
86 /** Who can receive drop events */
87 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
88
Patrick Dubroy4ed62782010-08-17 15:11:18 -070089 private ArrayList<DragListener> mListeners = new ArrayList<DragListener>();
Joe Onorato00acb122009-08-04 16:04:30 -040090
91 /** The window token used as the parent for the DragView. */
92 private IBinder mWindowToken;
93
94 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
95 private View mScrollView;
96
Romain Guyea3763c2010-01-11 18:02:04 -080097 private View mMoveTarget;
98
Joe Onorato00acb122009-08-04 16:04:30 -040099 private DragScroller mDragScroller;
100 private int mScrollState = SCROLL_OUTSIDE_ZONE;
101 private ScrollRunnable mScrollRunnable = new ScrollRunnable();
102
103 private RectF mDeleteRegion;
104 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
Adam Cohen8dfcba42011-07-07 16:38:18 -0700188 startDrag(b, dragLayerX, dragLayerY, source, dragInfo, dragAction, 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
Adam Cohen8dfcba42011-07-07 16:38:18 -0700215 startDrag(bmp, dragLayerX, dragLayerY, source, dragInfo, dragAction, 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) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700236 startDrag(b, dragLayerX, dragLayerY, source, dragInfo, dragAction, 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,
Michael Jurkaa63c4522010-08-19 13:52:27 -0700254 DragSource source, Object dragInfo, int dragAction, 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 Cohenbfbfd262011-06-13 16:55:12 -0700278 mDragObject.dragComplete = false;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700279 mDragObject.xOffset = mMotionDownX - (dragLayerX + dragRegionLeft);
280 mDragObject.yOffset = mMotionDownY - (dragLayerY + dragRegionTop);
Adam Cohencb3382b2011-05-24 14:07:08 -0700281 mDragObject.dragSource = source;
282 mDragObject.dragInfo = dragInfo;
Joe Onorato00acb122009-08-04 16:04:30 -0400283
284 mVibrator.vibrate(VIBRATE_DURATION);
285
Adam Cohen8dfcba42011-07-07 16:38:18 -0700286 final DragView dragView = mDragObject.dragView = new DragView(mLauncher, b, registrationX,
Adam Cohencb3382b2011-05-24 14:07:08 -0700287 registrationY, 0, 0, b.getWidth(), b.getHeight());
Michael Jurkaa63c4522010-08-19 13:52:27 -0700288
289 if (dragRegion != null) {
Adam Cohene3e27a82011-04-15 12:07:39 -0700290 dragView.setDragRegion(new Rect(dragRegion));
Michael Jurkaa63c4522010-08-19 13:52:27 -0700291 }
292
Adam Cohen8dfcba42011-07-07 16:38:18 -0700293 dragView.show(mMotionDownX, mMotionDownY);
294 handleMoveEvent(mMotionDownX, mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400295 }
296
297 /**
298 * Draw the view into a bitmap.
299 */
Adam Cohen120980b2010-12-08 11:05:37 -0800300 Bitmap getViewBitmap(View v) {
Joe Onorato00acb122009-08-04 16:04:30 -0400301 v.clearFocus();
302 v.setPressed(false);
303
304 boolean willNotCache = v.willNotCacheDrawing();
305 v.setWillNotCacheDrawing(false);
306
307 // Reset the drawing cache background color to fully transparent
308 // for the duration of this operation
309 int color = v.getDrawingCacheBackgroundColor();
310 v.setDrawingCacheBackgroundColor(0);
Adam Cohen120980b2010-12-08 11:05:37 -0800311 float alpha = v.getAlpha();
312 v.setAlpha(1.0f);
Joe Onorato00acb122009-08-04 16:04:30 -0400313
314 if (color != 0) {
315 v.destroyDrawingCache();
316 }
317 v.buildDrawingCache();
318 Bitmap cacheBitmap = v.getDrawingCache();
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400319 if (cacheBitmap == null) {
320 Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
321 return null;
322 }
Joe Onorato00acb122009-08-04 16:04:30 -0400323
324 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
325
326 // Restore the view
327 v.destroyDrawingCache();
Adam Cohen120980b2010-12-08 11:05:37 -0800328 v.setAlpha(alpha);
Joe Onorato00acb122009-08-04 16:04:30 -0400329 v.setWillNotCacheDrawing(willNotCache);
330 v.setDrawingCacheBackgroundColor(color);
331
332 return bitmap;
333 }
334
335 /**
336 * Call this from a drag source view like this:
337 *
338 * <pre>
339 * @Override
340 * public boolean dispatchKeyEvent(KeyEvent event) {
341 * return mDragController.dispatchKeyEvent(this, event)
342 * || super.dispatchKeyEvent(event);
343 * </pre>
344 */
Romain Guyea3763c2010-01-11 18:02:04 -0800345 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato00acb122009-08-04 16:04:30 -0400346 public boolean dispatchKeyEvent(KeyEvent event) {
347 return mDragging;
348 }
349
Winson Chung304dcde2011-01-07 11:17:23 -0800350 public boolean isDragging() {
351 return mDragging;
352 }
353
Joe Onorato24b6fd82009-11-12 13:47:09 -0800354 /**
355 * Stop dragging without dropping.
356 */
357 public void cancelDrag() {
Winson Chung621e6402011-01-04 16:03:57 -0800358 if (mDragging) {
Winson Chungc07918d2011-07-01 15:35:26 -0700359 if (mLastDropTarget != null) {
360 mLastDropTarget.onDragExit(mDragObject);
361 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700362 mDragObject.dragComplete = true;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700363 mDragObject.dragSource.onDropCompleted(null, mDragObject, false);
Winson Chung621e6402011-01-04 16:03:57 -0800364 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800365 endDrag();
366 }
367
Joe Onorato00acb122009-08-04 16:04:30 -0400368 private void endDrag() {
369 if (mDragging) {
370 mDragging = false;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700371 for (DragListener listener : mListeners) {
372 listener.onDragEnd();
Joe Onorato00acb122009-08-04 16:04:30 -0400373 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700374 if (mDragObject.dragView != null) {
375 mDragObject.dragView.remove();
376 mDragObject.dragView = null;
Joe Onorato00acb122009-08-04 16:04:30 -0400377 }
Joe Onorato00acb122009-08-04 16:04:30 -0400378 }
379 }
380
381 /**
Winson Chung273c1022011-07-11 13:40:52 -0700382 * Clamps the position to the drag layer bounds.
383 */
384 private int[] getClampedDragLayerPos(float x, float y) {
385 mLauncher.getDragLayer().getLocalVisibleRect(mDragLayerRect);
386 mTmpPoint[0] = (int) Math.max(mDragLayerRect.left, Math.min(x, mDragLayerRect.right - 1));
387 mTmpPoint[1] = (int) Math.max(mDragLayerRect.top, Math.min(y, mDragLayerRect.bottom - 1));
388 return mTmpPoint;
389 }
390
391 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400392 * Call this from a drag source view.
393 */
394 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400395 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800396 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400397 + mDragging);
398 }
Joe Onorato00acb122009-08-04 16:04:30 -0400399 final int action = ev.getAction();
400
Winson Chung273c1022011-07-11 13:40:52 -0700401 final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY());
402 final int dragLayerX = dragLayerPos[0];
403 final int dragLayerY = dragLayerPos[1];
Joe Onorato00acb122009-08-04 16:04:30 -0400404
405 switch (action) {
406 case MotionEvent.ACTION_MOVE:
407 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400408 case MotionEvent.ACTION_DOWN:
409 // Remember location of down touch
Adam Cohen8dfcba42011-07-07 16:38:18 -0700410 mMotionDownX = dragLayerX;
411 mMotionDownY = dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400412 mLastDropTarget = null;
413 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400414 case MotionEvent.ACTION_UP:
415 if (mDragging) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700416 drop(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400417 }
418 endDrag();
419 break;
Winson Chung621e6402011-01-04 16:03:57 -0800420 case MotionEvent.ACTION_CANCEL:
421 cancelDrag();
422 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400423 }
424
425 return mDragging;
426 }
427
428 /**
Romain Guyea3763c2010-01-11 18:02:04 -0800429 * Sets the view that should handle move events.
430 */
431 void setMoveTarget(View view) {
432 mMoveTarget = view;
433 }
434
435 public boolean dispatchUnhandledMove(View focused, int direction) {
436 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
437 }
438
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700439 private void handleMoveEvent(int x, int y) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700440 mDragObject.dragView.move(x, y);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700441
442 // Drop on someone?
443 final int[] coordinates = mCoordinatesTemp;
444 DropTarget dropTarget = findDropTarget(x, y, coordinates);
Adam Cohencb3382b2011-05-24 14:07:08 -0700445 mDragObject.x = coordinates[0];
446 mDragObject.y = coordinates[1];
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700447 if (dropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700448 DropTarget delegate = dropTarget.getDropTargetDelegate(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700449 if (delegate != null) {
450 dropTarget = delegate;
451 }
452
453 if (mLastDropTarget != dropTarget) {
454 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700455 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700456 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700457 dropTarget.onDragEnter(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700458 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700459 dropTarget.onDragOver(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700460 } else {
461 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700462 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700463 }
464 }
465 mLastDropTarget = dropTarget;
466
467 // Scroll, maybe, but not if we're in the delete region.
468 boolean inDeleteRegion = false;
469 if (mDeleteRegion != null) {
470 inDeleteRegion = mDeleteRegion.contains(x, y);
471 }
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700472
473 // After a scroll, the touch point will still be in the scroll region.
474 // Rather than scrolling immediately, require a bit of twiddling to scroll again
Adam Cohen8dfcba42011-07-07 16:38:18 -0700475 final int slop = ViewConfiguration.get(mLauncher).getScaledWindowTouchSlop();
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700476 mDistanceSinceScroll +=
477 Math.sqrt(Math.pow(mLastTouch[0] - x, 2) + Math.pow(mLastTouch[1] - y, 2));
478 mLastTouch[0] = x;
479 mLastTouch[1] = y;
480
Joe Onorato658db742010-09-29 11:40:39 -0700481 if (!inDeleteRegion && x < mScrollZone) {
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700482 if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700483 mScrollState = SCROLL_WAITING_IN_ZONE;
484 mScrollRunnable.setDirection(SCROLL_LEFT);
485 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700486 mDragScroller.onEnterScrollArea(SCROLL_LEFT);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700487 }
Joe Onorato658db742010-09-29 11:40:39 -0700488 } else if (!inDeleteRegion && x > mScrollView.getWidth() - mScrollZone) {
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700489 if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700490 mScrollState = SCROLL_WAITING_IN_ZONE;
491 mScrollRunnable.setDirection(SCROLL_RIGHT);
492 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700493 mDragScroller.onEnterScrollArea(SCROLL_RIGHT);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700494 }
495 } else {
496 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
497 mScrollState = SCROLL_OUTSIDE_ZONE;
498 mScrollRunnable.setDirection(SCROLL_RIGHT);
499 mHandler.removeCallbacks(mScrollRunnable);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700500 mDragScroller.onExitScrollArea();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700501 }
502 }
503 }
504
Romain Guyea3763c2010-01-11 18:02:04 -0800505 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400506 * Call this from a drag source view.
507 */
508 public boolean onTouchEvent(MotionEvent ev) {
Joe Onorato00acb122009-08-04 16:04:30 -0400509 if (!mDragging) {
510 return false;
511 }
512
513 final int action = ev.getAction();
Winson Chung273c1022011-07-11 13:40:52 -0700514 final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY());
515 final int dragLayerX = dragLayerPos[0];
516 final int dragLayerY = dragLayerPos[1];
Joe Onorato00acb122009-08-04 16:04:30 -0400517
518 switch (action) {
519 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400520 // Remember where the motion event started
Adam Cohen8dfcba42011-07-07 16:38:18 -0700521 mMotionDownX = dragLayerX;
522 mMotionDownY = dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400523
Adam Cohen8dfcba42011-07-07 16:38:18 -0700524 if ((dragLayerX < mScrollZone) || (dragLayerX > mScrollView.getWidth() - mScrollZone)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400525 mScrollState = SCROLL_WAITING_IN_ZONE;
526 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
527 } else {
528 mScrollState = SCROLL_OUTSIDE_ZONE;
529 }
Joe Onorato00acb122009-08-04 16:04:30 -0400530 break;
531 case MotionEvent.ACTION_MOVE:
Adam Cohen8dfcba42011-07-07 16:38:18 -0700532 handleMoveEvent(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400533 break;
534 case MotionEvent.ACTION_UP:
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800535 // Ensure that we've processed a move event at the current pointer location.
Adam Cohen8dfcba42011-07-07 16:38:18 -0700536 handleMoveEvent(dragLayerX, dragLayerY);
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800537
Joe Onorato00acb122009-08-04 16:04:30 -0400538 mHandler.removeCallbacks(mScrollRunnable);
539 if (mDragging) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700540 drop(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400541 }
542 endDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400543 break;
544 case MotionEvent.ACTION_CANCEL:
Joe Onorato24b6fd82009-11-12 13:47:09 -0800545 cancelDrag();
Winson Chung621e6402011-01-04 16:03:57 -0800546 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400547 }
548
549 return true;
550 }
551
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800552 private void drop(float x, float y) {
Joe Onorato00acb122009-08-04 16:04:30 -0400553 final int[] coordinates = mCoordinatesTemp;
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800554 final DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400555
Adam Cohencb3382b2011-05-24 14:07:08 -0700556 mDragObject.x = coordinates[0];
557 mDragObject.y = coordinates[1];
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800558 boolean accepted = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400559 if (dropTarget != null) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700560 mDragObject.dragComplete = true;
Adam Cohencb3382b2011-05-24 14:07:08 -0700561 dropTarget.onDragExit(mDragObject);
562 if (dropTarget.acceptDrop(mDragObject)) {
563 dropTarget.onDrop(mDragObject);
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800564 accepted = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400565 }
566 }
Adam Cohenc0dcf592011-06-01 15:30:43 -0700567 mDragObject.dragSource.onDropCompleted((View) dropTarget, mDragObject, accepted);
Joe Onorato00acb122009-08-04 16:04:30 -0400568 }
569
570 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
571 final Rect r = mRectTemp;
572
573 final ArrayList<DropTarget> dropTargets = mDropTargets;
574 final int count = dropTargets.size();
575 for (int i=count-1; i>=0; i--) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700576 DropTarget target = dropTargets.get(i);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700577 if (!target.isDropEnabled())
578 continue;
579
Joe Onorato00acb122009-08-04 16:04:30 -0400580 target.getHitRect(r);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700581
Adam Cohen8dfcba42011-07-07 16:38:18 -0700582 // Convert the hit rect to DragLayer coordinates
583 target.getLocationInDragLayer(dropCoordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400584 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
Patrick Dubroy440c3602010-07-13 17:50:32 -0700585
Adam Cohencb3382b2011-05-24 14:07:08 -0700586 mDragObject.x = x;
587 mDragObject.y = y;
Joe Onorato00acb122009-08-04 16:04:30 -0400588 if (r.contains(x, y)) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700589 DropTarget delegate = target.getDropTargetDelegate(mDragObject);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700590 if (delegate != null) {
591 target = delegate;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700592 target.getLocationInDragLayer(dropCoordinates);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700593 }
594
595 // Make dropCoordinates relative to the DropTarget
Joe Onorato00acb122009-08-04 16:04:30 -0400596 dropCoordinates[0] = x - dropCoordinates[0];
597 dropCoordinates[1] = y - dropCoordinates[1];
Patrick Dubroy440c3602010-07-13 17:50:32 -0700598
Joe Onorato00acb122009-08-04 16:04:30 -0400599 return target;
600 }
601 }
602 return null;
603 }
604
605 public void setDragScoller(DragScroller scroller) {
606 mDragScroller = scroller;
607 }
608
609 public void setWindowToken(IBinder token) {
610 mWindowToken = token;
611 }
612
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 /**
614 * Sets the drag listner which will be notified when a drag starts or ends.
615 */
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700616 public void addDragListener(DragListener l) {
617 mListeners.add(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400618 }
619
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 /**
621 * Remove a previously installed drag listener.
622 */
Joe Onorato00acb122009-08-04 16:04:30 -0400623 public void removeDragListener(DragListener l) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700624 mListeners.remove(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400625 }
626
627 /**
628 * Add a DropTarget to the list of potential places to receive drop events.
629 */
630 public void addDropTarget(DropTarget target) {
631 mDropTargets.add(target);
632 }
633
634 /**
635 * Don't send drop events to <em>target</em> any more.
636 */
637 public void removeDropTarget(DropTarget target) {
638 mDropTargets.remove(target);
639 }
640
641 /**
642 * Set which view scrolls for touch events near the edge of the screen.
643 */
644 public void setScrollView(View v) {
645 mScrollView = v;
646 }
647
648 /**
649 * Specifies the delete region. We won't scroll on touch events over the delete region.
650 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700651 * @param region The rectangle in DragLayer coordinates of the delete region.
Joe Onorato00acb122009-08-04 16:04:30 -0400652 */
653 void setDeleteRegion(RectF region) {
654 mDeleteRegion = region;
655 }
656
Patrick Dubroy5f445422011-02-18 14:35:21 -0800657 DragView getDragView() {
Adam Cohencb3382b2011-05-24 14:07:08 -0700658 return mDragObject.dragView;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800659 }
660
Joe Onorato00acb122009-08-04 16:04:30 -0400661 private class ScrollRunnable implements Runnable {
662 private int mDirection;
663
664 ScrollRunnable() {
665 }
666
667 public void run() {
668 if (mDragScroller != null) {
669 if (mDirection == SCROLL_LEFT) {
670 mDragScroller.scrollLeft();
671 } else {
672 mDragScroller.scrollRight();
673 }
674 mScrollState = SCROLL_OUTSIDE_ZONE;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700675 mDistanceSinceScroll = 0;
676 mDragScroller.onExitScrollArea();
Joe Onorato00acb122009-08-04 16:04:30 -0400677 }
678 }
679
680 void setDirection(int direction) {
681 mDirection = direction;
682 }
683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684}