blob: 5aecede0ecfc4966b9209af0353253a49c6fa5b1 [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 Onoratoe048e8a2009-09-25 10:39:17 -070026import android.util.DisplayMetrics;
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 Onoratoe048e8a2009-09-25 10:39:17 -070032import android.view.WindowManager;
Joe Onorato00acb122009-08-04 16:04:30 -040033import android.view.inputmethod.InputMethodManager;
Joe Onorato00acb122009-08-04 16:04:30 -040034
Adam Cohen120980b2010-12-08 11:05:37 -080035import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070036
37import java.util.ArrayList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038
39/**
Joe Onorato00acb122009-08-04 16:04:30 -040040 * Class for initiating a drag within a view or across multiple views.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041 */
Joe Onorato00acb122009-08-04 16:04:30 -040042public class DragController {
Romain Guyea3763c2010-01-11 18:02:04 -080043 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato2e5c4322009-10-06 12:34:42 -070044 private static final String TAG = "Launcher.DragController";
45
Joe Onorato00acb122009-08-04 16:04:30 -040046 /** Indicates the drag is a move. */
47 public static int DRAG_ACTION_MOVE = 0;
48
49 /** Indicates the drag is a copy. */
50 public static int DRAG_ACTION_COPY = 1;
51
52 private static final int SCROLL_DELAY = 600;
Joe Onorato00acb122009-08-04 16:04:30 -040053 private static final int VIBRATE_DURATION = 35;
54
55 private static final boolean PROFILE_DRAWING_DURING_DRAG = false;
56
57 private static final int SCROLL_OUTSIDE_ZONE = 0;
58 private static final int SCROLL_WAITING_IN_ZONE = 1;
59
Patrick Dubroy54fa3b92010-11-17 12:18:45 -080060 static final int SCROLL_NONE = -1;
Patrick Dubroy1262e362010-10-06 15:49:50 -070061 static final int SCROLL_LEFT = 0;
62 static final int SCROLL_RIGHT = 1;
Joe Onorato00acb122009-08-04 16:04:30 -040063
Adam Cohen8dfcba42011-07-07 16:38:18 -070064 private Launcher mLauncher;
Joe Onorato00acb122009-08-04 16:04:30 -040065 private Handler mHandler;
66 private final Vibrator mVibrator = new Vibrator();
67
68 // temporaries to avoid gc thrash
69 private Rect mRectTemp = new Rect();
70 private final int[] mCoordinatesTemp = new int[2];
71
72 /** Whether or not we're dragging. */
73 private boolean mDragging;
74
75 /** X coordinate of the down event. */
Adam Cohene3e27a82011-04-15 12:07:39 -070076 private int mMotionDownX;
Joe Onorato00acb122009-08-04 16:04:30 -040077
78 /** Y coordinate of the down event. */
Adam Cohene3e27a82011-04-15 12:07:39 -070079 private int mMotionDownY;
Joe Onorato00acb122009-08-04 16:04:30 -040080
Joe Onorato658db742010-09-29 11:40:39 -070081 /** the area at the edge of the screen that makes the workspace go left
82 * or right while you're dragging.
83 */
84 private int mScrollZone;
85
Adam Cohencb3382b2011-05-24 14:07:08 -070086 private DropTarget.DragObject mDragObject = new DropTarget.DragObject();
Joe Onorato00acb122009-08-04 16:04:30 -040087
88 /** Who can receive drop events */
89 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
90
Patrick Dubroy4ed62782010-08-17 15:11:18 -070091 private ArrayList<DragListener> mListeners = new ArrayList<DragListener>();
Joe Onorato00acb122009-08-04 16:04:30 -040092
93 /** The window token used as the parent for the DragView. */
94 private IBinder mWindowToken;
95
96 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
97 private View mScrollView;
98
Romain Guyea3763c2010-01-11 18:02:04 -080099 private View mMoveTarget;
100
Joe Onorato00acb122009-08-04 16:04:30 -0400101 private DragScroller mDragScroller;
102 private int mScrollState = SCROLL_OUTSIDE_ZONE;
103 private ScrollRunnable mScrollRunnable = new ScrollRunnable();
104
105 private RectF mDeleteRegion;
106 private DropTarget mLastDropTarget;
107
108 private InputMethodManager mInputMethodManager;
109
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700110 private int mLastTouch[] = new int[2];
111 private int mDistanceSinceScroll = 0;
112
Winson Chung273c1022011-07-11 13:40:52 -0700113 private int mTmpPoint[] = new int[2];
114 private Rect mDragLayerRect = new Rect();
115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 /**
117 * Interface to receive notifications when a drag starts or stops
118 */
119 interface DragListener {
120
121 /**
122 * A drag has begun
123 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 * @param source An object representing where the drag originated
125 * @param info The data associated with the object that is being dragged
126 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
127 * or {@link DragController#DRAG_ACTION_COPY}
128 */
Joe Onorato5162ea92009-09-03 09:39:42 -0700129 void onDragStart(DragSource source, Object info, int dragAction);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
131 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700132 * The drag has ended
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 */
134 void onDragEnd();
135 }
136
137 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400138 * Used to create a new DragLayer from XML.
139 *
140 * @param context The application's context.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700142 public DragController(Launcher launcher) {
143 mLauncher = launcher;
Joe Onorato00acb122009-08-04 16:04:30 -0400144 mHandler = new Handler();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700145 mScrollZone = launcher.getResources().getDimensionPixelSize(R.dimen.scroll_zone);
Joe Onorato00acb122009-08-04 16:04:30 -0400146 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Patrick Dubroy1262e362010-10-06 15:49:50 -0700148 public boolean dragging() {
149 return mDragging;
150 }
151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 /**
Joe Onorato5162ea92009-09-03 09:39:42 -0700153 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700154 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 * @param v The view that is being dragged
156 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800157 * @param dragInfo The data associated with the object that is being dragged
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
159 * {@link #DRAG_ACTION_COPY}
160 */
Joe Onorato00acb122009-08-04 16:04:30 -0400161 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700162 startDrag(v, source, dragInfo, dragAction, null);
163 }
164
165 /**
166 * Starts a drag.
167 *
168 * @param v The view that is being dragged
169 * @param source An object representing where the drag originated
170 * @param dragInfo The data associated with the object that is being dragged
171 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
172 * {@link #DRAG_ACTION_COPY}
173 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
174 * Makes dragging feel more precise, e.g. you can clip out a transparent border
175 */
176 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction,
177 Rect dragRegion) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700178 Bitmap b = getViewBitmap(v);
179
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400180 if (b == null) {
181 // out of memory?
182 return;
183 }
184
Joe Onorato5162ea92009-09-03 09:39:42 -0700185 int[] loc = mCoordinatesTemp;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700186 mLauncher.getDragLayer().getLocationInDragLayer(v, loc);
187 int dragLayerX = loc[0];
188 int dragLayerY = loc[1];
Joe Onorato5162ea92009-09-03 09:39:42 -0700189
Adam Cohen8dfcba42011-07-07 16:38:18 -0700190 startDrag(b, dragLayerX, dragLayerY, source, dragInfo, dragAction, dragRegion);
Joe Onorato5162ea92009-09-03 09:39:42 -0700191 b.recycle();
192
193 if (dragAction == DRAG_ACTION_MOVE) {
194 v.setVisibility(View.GONE);
195 }
196 }
197
198 /**
199 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700200 *
Winson Chunge3193b92010-09-10 11:44:42 -0700201 * @param v The view that is being dragged
202 * @param bmp The bitmap that represents the view being dragged
203 * @param source An object representing where the drag originated
204 * @param dragInfo The data associated with the object that is being dragged
205 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
206 * {@link #DRAG_ACTION_COPY}
207 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
208 * Makes dragging feel more precise, e.g. you can clip out a transparent border
209 */
210 public void startDrag(View v, Bitmap bmp, DragSource source, Object dragInfo, int dragAction,
211 Rect dragRegion) {
Winson Chunge3193b92010-09-10 11:44:42 -0700212 int[] loc = mCoordinatesTemp;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700213 mLauncher.getDragLayer().getLocationInDragLayer(v, loc);
214 int dragLayerX = loc[0];
215 int dragLayerY = loc[1];
Winson Chunge3193b92010-09-10 11:44:42 -0700216
Adam Cohen8dfcba42011-07-07 16:38:18 -0700217 startDrag(bmp, dragLayerX, dragLayerY, source, dragInfo, dragAction, dragRegion);
Winson Chunge3193b92010-09-10 11:44:42 -0700218
219 if (dragAction == DRAG_ACTION_MOVE) {
220 v.setVisibility(View.GONE);
221 }
222 }
223
224 /**
225 * Starts a drag.
226 *
Joe Onorato5162ea92009-09-03 09:39:42 -0700227 * @param b The bitmap to display as the drag image. It will be re-scaled to the
228 * enlarged size.
Adam Cohen8dfcba42011-07-07 16:38:18 -0700229 * @param dragLayerX The x position in the DragLayer of the left-top of the bitmap.
230 * @param dragLayerY The y position in the DragLayer of the left-top of the bitmap.
Joe Onorato5162ea92009-09-03 09:39:42 -0700231 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800232 * @param dragInfo The data associated with the object that is being dragged
Joe Onorato5162ea92009-09-03 09:39:42 -0700233 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
234 * {@link #DRAG_ACTION_COPY}
235 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700236 public void startDrag(Bitmap b, int dragLayerX, int dragLayerY,
Joe Onorato5162ea92009-09-03 09:39:42 -0700237 DragSource source, Object dragInfo, int dragAction) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700238 startDrag(b, dragLayerX, dragLayerY, source, dragInfo, dragAction, null);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700239 }
240
241 /**
242 * Starts a drag.
243 *
244 * @param b The bitmap to display as the drag image. It will be re-scaled to the
245 * enlarged size.
Adam Cohen8dfcba42011-07-07 16:38:18 -0700246 * @param dragLayerX The x position in the DragLayer of the left-top of the bitmap.
247 * @param dragLayerY The y position in the DragLayer of the left-top of the bitmap.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700248 * @param source An object representing where the drag originated
249 * @param dragInfo The data associated with the object that is being dragged
250 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
251 * {@link #DRAG_ACTION_COPY}
252 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
253 * Makes dragging feel more precise, e.g. you can clip out a transparent border
254 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700255 public void startDrag(Bitmap b, int dragLayerX, int dragLayerY,
Michael Jurkaa63c4522010-08-19 13:52:27 -0700256 DragSource source, Object dragInfo, int dragAction, Rect dragRegion) {
Joe Onorato00acb122009-08-04 16:04:30 -0400257 if (PROFILE_DRAWING_DURING_DRAG) {
258 android.os.Debug.startMethodTracing("Launcher");
259 }
260
261 // Hide soft keyboard, if visible
262 if (mInputMethodManager == null) {
263 mInputMethodManager = (InputMethodManager)
Adam Cohen8dfcba42011-07-07 16:38:18 -0700264 mLauncher.getSystemService(Context.INPUT_METHOD_SERVICE);
Joe Onorato00acb122009-08-04 16:04:30 -0400265 }
266 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
267
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700268 for (DragListener listener : mListeners) {
269 listener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400270 }
271
Adam Cohen8dfcba42011-07-07 16:38:18 -0700272 final int registrationX = mMotionDownX - dragLayerX;
273 final int registrationY = mMotionDownY - dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400274
Michael Jurkaa63c4522010-08-19 13:52:27 -0700275 final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left;
276 final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top;
Adam Cohene3e27a82011-04-15 12:07:39 -0700277
Joe Onorato00acb122009-08-04 16:04:30 -0400278 mDragging = true;
Adam Cohencb3382b2011-05-24 14:07:08 -0700279
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
Patrick Dubroya669d792010-11-23 14:40:33 -0800291 final DragSource dragSource = source;
292 dragView.setOnDrawRunnable(new Runnable() {
293 public void run() {
294 dragSource.onDragViewVisible();
295 };
296 });
297
Michael Jurkaa63c4522010-08-19 13:52:27 -0700298 if (dragRegion != null) {
Adam Cohene3e27a82011-04-15 12:07:39 -0700299 dragView.setDragRegion(new Rect(dragRegion));
Michael Jurkaa63c4522010-08-19 13:52:27 -0700300 }
301
Adam Cohen8dfcba42011-07-07 16:38:18 -0700302 dragView.show(mMotionDownX, mMotionDownY);
303 handleMoveEvent(mMotionDownX, mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400304 }
305
306 /**
307 * Draw the view into a bitmap.
308 */
Adam Cohen120980b2010-12-08 11:05:37 -0800309 Bitmap getViewBitmap(View v) {
Joe Onorato00acb122009-08-04 16:04:30 -0400310 v.clearFocus();
311 v.setPressed(false);
312
313 boolean willNotCache = v.willNotCacheDrawing();
314 v.setWillNotCacheDrawing(false);
315
316 // Reset the drawing cache background color to fully transparent
317 // for the duration of this operation
318 int color = v.getDrawingCacheBackgroundColor();
319 v.setDrawingCacheBackgroundColor(0);
Adam Cohen120980b2010-12-08 11:05:37 -0800320 float alpha = v.getAlpha();
321 v.setAlpha(1.0f);
Joe Onorato00acb122009-08-04 16:04:30 -0400322
323 if (color != 0) {
324 v.destroyDrawingCache();
325 }
326 v.buildDrawingCache();
327 Bitmap cacheBitmap = v.getDrawingCache();
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400328 if (cacheBitmap == null) {
329 Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
330 return null;
331 }
Joe Onorato00acb122009-08-04 16:04:30 -0400332
333 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
334
335 // Restore the view
336 v.destroyDrawingCache();
Adam Cohen120980b2010-12-08 11:05:37 -0800337 v.setAlpha(alpha);
Joe Onorato00acb122009-08-04 16:04:30 -0400338 v.setWillNotCacheDrawing(willNotCache);
339 v.setDrawingCacheBackgroundColor(color);
340
341 return bitmap;
342 }
343
344 /**
345 * Call this from a drag source view like this:
346 *
347 * <pre>
348 * @Override
349 * public boolean dispatchKeyEvent(KeyEvent event) {
350 * return mDragController.dispatchKeyEvent(this, event)
351 * || super.dispatchKeyEvent(event);
352 * </pre>
353 */
Romain Guyea3763c2010-01-11 18:02:04 -0800354 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato00acb122009-08-04 16:04:30 -0400355 public boolean dispatchKeyEvent(KeyEvent event) {
356 return mDragging;
357 }
358
Winson Chung304dcde2011-01-07 11:17:23 -0800359 public boolean isDragging() {
360 return mDragging;
361 }
362
Joe Onorato24b6fd82009-11-12 13:47:09 -0800363 /**
364 * Stop dragging without dropping.
365 */
366 public void cancelDrag() {
Winson Chung621e6402011-01-04 16:03:57 -0800367 if (mDragging) {
Winson Chungc07918d2011-07-01 15:35:26 -0700368 if (mLastDropTarget != null) {
369 mLastDropTarget.onDragExit(mDragObject);
370 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700371 mDragObject.dragComplete = true;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700372 mDragObject.dragSource.onDropCompleted(null, mDragObject, false);
Winson Chung621e6402011-01-04 16:03:57 -0800373 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800374 endDrag();
375 }
376
Joe Onorato00acb122009-08-04 16:04:30 -0400377 private void endDrag() {
378 if (mDragging) {
379 mDragging = false;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700380 for (DragListener listener : mListeners) {
381 listener.onDragEnd();
Joe Onorato00acb122009-08-04 16:04:30 -0400382 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700383 if (mDragObject.dragView != null) {
384 mDragObject.dragView.remove();
385 mDragObject.dragView = null;
Joe Onorato00acb122009-08-04 16:04:30 -0400386 }
Joe Onorato00acb122009-08-04 16:04:30 -0400387 }
388 }
389
390 /**
Winson Chung273c1022011-07-11 13:40:52 -0700391 * Clamps the position to the drag layer bounds.
392 */
393 private int[] getClampedDragLayerPos(float x, float y) {
394 mLauncher.getDragLayer().getLocalVisibleRect(mDragLayerRect);
395 mTmpPoint[0] = (int) Math.max(mDragLayerRect.left, Math.min(x, mDragLayerRect.right - 1));
396 mTmpPoint[1] = (int) Math.max(mDragLayerRect.top, Math.min(y, mDragLayerRect.bottom - 1));
397 return mTmpPoint;
398 }
399
400 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400401 * Call this from a drag source view.
402 */
403 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400404 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800405 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400406 + mDragging);
407 }
Joe Onorato00acb122009-08-04 16:04:30 -0400408 final int action = ev.getAction();
409
Winson Chung273c1022011-07-11 13:40:52 -0700410 final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY());
411 final int dragLayerX = dragLayerPos[0];
412 final int dragLayerY = dragLayerPos[1];
Joe Onorato00acb122009-08-04 16:04:30 -0400413
414 switch (action) {
415 case MotionEvent.ACTION_MOVE:
416 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400417 case MotionEvent.ACTION_DOWN:
418 // Remember location of down touch
Adam Cohen8dfcba42011-07-07 16:38:18 -0700419 mMotionDownX = dragLayerX;
420 mMotionDownY = dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400421 mLastDropTarget = null;
422 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400423 case MotionEvent.ACTION_UP:
424 if (mDragging) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700425 drop(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400426 }
427 endDrag();
428 break;
Winson Chung621e6402011-01-04 16:03:57 -0800429 case MotionEvent.ACTION_CANCEL:
430 cancelDrag();
431 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400432 }
433
434 return mDragging;
435 }
436
437 /**
Romain Guyea3763c2010-01-11 18:02:04 -0800438 * Sets the view that should handle move events.
439 */
440 void setMoveTarget(View view) {
441 mMoveTarget = view;
442 }
443
444 public boolean dispatchUnhandledMove(View focused, int direction) {
445 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
446 }
447
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700448 private void handleMoveEvent(int x, int y) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700449 mDragObject.dragView.move(x, y);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700450
451 // Drop on someone?
452 final int[] coordinates = mCoordinatesTemp;
453 DropTarget dropTarget = findDropTarget(x, y, coordinates);
Adam Cohencb3382b2011-05-24 14:07:08 -0700454 mDragObject.x = coordinates[0];
455 mDragObject.y = coordinates[1];
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700456 if (dropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700457 DropTarget delegate = dropTarget.getDropTargetDelegate(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700458 if (delegate != null) {
459 dropTarget = delegate;
460 }
461
462 if (mLastDropTarget != dropTarget) {
463 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700464 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700465 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700466 dropTarget.onDragEnter(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700467 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700468 dropTarget.onDragOver(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700469 } else {
470 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700471 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700472 }
473 }
474 mLastDropTarget = dropTarget;
475
476 // Scroll, maybe, but not if we're in the delete region.
477 boolean inDeleteRegion = false;
478 if (mDeleteRegion != null) {
479 inDeleteRegion = mDeleteRegion.contains(x, y);
480 }
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700481
482 // After a scroll, the touch point will still be in the scroll region.
483 // Rather than scrolling immediately, require a bit of twiddling to scroll again
Adam Cohen8dfcba42011-07-07 16:38:18 -0700484 final int slop = ViewConfiguration.get(mLauncher).getScaledWindowTouchSlop();
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700485 mDistanceSinceScroll +=
486 Math.sqrt(Math.pow(mLastTouch[0] - x, 2) + Math.pow(mLastTouch[1] - y, 2));
487 mLastTouch[0] = x;
488 mLastTouch[1] = y;
489
Joe Onorato658db742010-09-29 11:40:39 -0700490 if (!inDeleteRegion && x < mScrollZone) {
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700491 if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700492 mScrollState = SCROLL_WAITING_IN_ZONE;
493 mScrollRunnable.setDirection(SCROLL_LEFT);
494 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700495 mDragScroller.onEnterScrollArea(SCROLL_LEFT);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700496 }
Joe Onorato658db742010-09-29 11:40:39 -0700497 } else if (!inDeleteRegion && x > mScrollView.getWidth() - mScrollZone) {
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700498 if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700499 mScrollState = SCROLL_WAITING_IN_ZONE;
500 mScrollRunnable.setDirection(SCROLL_RIGHT);
501 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700502 mDragScroller.onEnterScrollArea(SCROLL_RIGHT);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700503 }
504 } else {
505 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
506 mScrollState = SCROLL_OUTSIDE_ZONE;
507 mScrollRunnable.setDirection(SCROLL_RIGHT);
508 mHandler.removeCallbacks(mScrollRunnable);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700509 mDragScroller.onExitScrollArea();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700510 }
511 }
512 }
513
Romain Guyea3763c2010-01-11 18:02:04 -0800514 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400515 * Call this from a drag source view.
516 */
517 public boolean onTouchEvent(MotionEvent ev) {
Joe Onorato00acb122009-08-04 16:04:30 -0400518 if (!mDragging) {
519 return false;
520 }
521
522 final int action = ev.getAction();
Winson Chung273c1022011-07-11 13:40:52 -0700523 final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY());
524 final int dragLayerX = dragLayerPos[0];
525 final int dragLayerY = dragLayerPos[1];
Joe Onorato00acb122009-08-04 16:04:30 -0400526
527 switch (action) {
528 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400529 // Remember where the motion event started
Adam Cohen8dfcba42011-07-07 16:38:18 -0700530 mMotionDownX = dragLayerX;
531 mMotionDownY = dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400532
Adam Cohen8dfcba42011-07-07 16:38:18 -0700533 if ((dragLayerX < mScrollZone) || (dragLayerX > mScrollView.getWidth() - mScrollZone)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400534 mScrollState = SCROLL_WAITING_IN_ZONE;
535 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
536 } else {
537 mScrollState = SCROLL_OUTSIDE_ZONE;
538 }
Joe Onorato00acb122009-08-04 16:04:30 -0400539 break;
540 case MotionEvent.ACTION_MOVE:
Adam Cohen8dfcba42011-07-07 16:38:18 -0700541 handleMoveEvent(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400542 break;
543 case MotionEvent.ACTION_UP:
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800544 // Ensure that we've processed a move event at the current pointer location.
Adam Cohen8dfcba42011-07-07 16:38:18 -0700545 handleMoveEvent(dragLayerX, dragLayerY);
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800546
Joe Onorato00acb122009-08-04 16:04:30 -0400547 mHandler.removeCallbacks(mScrollRunnable);
548 if (mDragging) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700549 drop(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400550 }
551 endDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400552 break;
553 case MotionEvent.ACTION_CANCEL:
Joe Onorato24b6fd82009-11-12 13:47:09 -0800554 cancelDrag();
Winson Chung621e6402011-01-04 16:03:57 -0800555 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400556 }
557
558 return true;
559 }
560
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800561 private void drop(float x, float y) {
Joe Onorato00acb122009-08-04 16:04:30 -0400562 final int[] coordinates = mCoordinatesTemp;
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800563 final DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400564
Adam Cohencb3382b2011-05-24 14:07:08 -0700565 mDragObject.x = coordinates[0];
566 mDragObject.y = coordinates[1];
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800567 boolean accepted = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400568 if (dropTarget != null) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700569 mDragObject.dragComplete = true;
Adam Cohencb3382b2011-05-24 14:07:08 -0700570 dropTarget.onDragExit(mDragObject);
571 if (dropTarget.acceptDrop(mDragObject)) {
572 dropTarget.onDrop(mDragObject);
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800573 accepted = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400574 }
575 }
Adam Cohenc0dcf592011-06-01 15:30:43 -0700576 mDragObject.dragSource.onDropCompleted((View) dropTarget, mDragObject, accepted);
Joe Onorato00acb122009-08-04 16:04:30 -0400577 }
578
579 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
580 final Rect r = mRectTemp;
581
582 final ArrayList<DropTarget> dropTargets = mDropTargets;
583 final int count = dropTargets.size();
584 for (int i=count-1; i>=0; i--) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700585 DropTarget target = dropTargets.get(i);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700586 if (!target.isDropEnabled())
587 continue;
588
Joe Onorato00acb122009-08-04 16:04:30 -0400589 target.getHitRect(r);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700590
Adam Cohen8dfcba42011-07-07 16:38:18 -0700591 // Convert the hit rect to DragLayer coordinates
592 target.getLocationInDragLayer(dropCoordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400593 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
Patrick Dubroy440c3602010-07-13 17:50:32 -0700594
Adam Cohencb3382b2011-05-24 14:07:08 -0700595 mDragObject.x = x;
596 mDragObject.y = y;
Joe Onorato00acb122009-08-04 16:04:30 -0400597 if (r.contains(x, y)) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700598 DropTarget delegate = target.getDropTargetDelegate(mDragObject);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700599 if (delegate != null) {
600 target = delegate;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700601 target.getLocationInDragLayer(dropCoordinates);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700602 }
603
604 // Make dropCoordinates relative to the DropTarget
Joe Onorato00acb122009-08-04 16:04:30 -0400605 dropCoordinates[0] = x - dropCoordinates[0];
606 dropCoordinates[1] = y - dropCoordinates[1];
Patrick Dubroy440c3602010-07-13 17:50:32 -0700607
Joe Onorato00acb122009-08-04 16:04:30 -0400608 return target;
609 }
610 }
611 return null;
612 }
613
614 public void setDragScoller(DragScroller scroller) {
615 mDragScroller = scroller;
616 }
617
618 public void setWindowToken(IBinder token) {
619 mWindowToken = token;
620 }
621
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 /**
623 * Sets the drag listner which will be notified when a drag starts or ends.
624 */
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700625 public void addDragListener(DragListener l) {
626 mListeners.add(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400627 }
628
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 /**
630 * Remove a previously installed drag listener.
631 */
Joe Onorato00acb122009-08-04 16:04:30 -0400632 public void removeDragListener(DragListener l) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700633 mListeners.remove(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400634 }
635
636 /**
637 * Add a DropTarget to the list of potential places to receive drop events.
638 */
639 public void addDropTarget(DropTarget target) {
640 mDropTargets.add(target);
641 }
642
643 /**
644 * Don't send drop events to <em>target</em> any more.
645 */
646 public void removeDropTarget(DropTarget target) {
647 mDropTargets.remove(target);
648 }
649
650 /**
651 * Set which view scrolls for touch events near the edge of the screen.
652 */
653 public void setScrollView(View v) {
654 mScrollView = v;
655 }
656
657 /**
658 * Specifies the delete region. We won't scroll on touch events over the delete region.
659 *
Adam Cohen8dfcba42011-07-07 16:38:18 -0700660 * @param region The rectangle in DragLayer coordinates of the delete region.
Joe Onorato00acb122009-08-04 16:04:30 -0400661 */
662 void setDeleteRegion(RectF region) {
663 mDeleteRegion = region;
664 }
665
Patrick Dubroy5f445422011-02-18 14:35:21 -0800666 DragView getDragView() {
Adam Cohencb3382b2011-05-24 14:07:08 -0700667 return mDragObject.dragView;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800668 }
669
Joe Onorato00acb122009-08-04 16:04:30 -0400670 private class ScrollRunnable implements Runnable {
671 private int mDirection;
672
673 ScrollRunnable() {
674 }
675
676 public void run() {
677 if (mDragScroller != null) {
678 if (mDirection == SCROLL_LEFT) {
679 mDragScroller.scrollLeft();
680 } else {
681 mDragScroller.scrollRight();
682 }
683 mScrollState = SCROLL_OUTSIDE_ZONE;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700684 mDistanceSinceScroll = 0;
685 mDragScroller.onExitScrollArea();
Joe Onorato00acb122009-08-04 16:04:30 -0400686 }
687 }
688
689 void setDirection(int direction) {
690 mDirection = direction;
691 }
692 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693}