blob: 5b1b20a6841ec5429c4e47d46a633c72b21afab8 [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
64 private Context mContext;
65 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 Onoratoe048e8a2009-09-25 10:39:17 -070081 /** Info about the screen for clamping. */
82 private DisplayMetrics mDisplayMetrics = new DisplayMetrics();
83
Joe Onorato00acb122009-08-04 16:04:30 -040084 /** Original view that is being dragged. */
85 private View mOriginator;
86
Joe Onorato658db742010-09-29 11:40:39 -070087 /** the area at the edge of the screen that makes the workspace go left
88 * or right while you're dragging.
89 */
90 private int mScrollZone;
91
Adam Cohencb3382b2011-05-24 14:07:08 -070092 private DropTarget.DragObject mDragObject = new DropTarget.DragObject();
Joe Onorato00acb122009-08-04 16:04:30 -040093
94 /** Who can receive drop events */
95 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
96
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097 private ArrayList<DragListener> mListeners = new ArrayList<DragListener>();
Joe Onorato00acb122009-08-04 16:04:30 -040098
99 /** The window token used as the parent for the DragView. */
100 private IBinder mWindowToken;
101
102 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
103 private View mScrollView;
104
Romain Guyea3763c2010-01-11 18:02:04 -0800105 private View mMoveTarget;
106
Joe Onorato00acb122009-08-04 16:04:30 -0400107 private DragScroller mDragScroller;
108 private int mScrollState = SCROLL_OUTSIDE_ZONE;
109 private ScrollRunnable mScrollRunnable = new ScrollRunnable();
110
111 private RectF mDeleteRegion;
112 private DropTarget mLastDropTarget;
113
114 private InputMethodManager mInputMethodManager;
115
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700116 private int mLastTouch[] = new int[2];
117 private int mDistanceSinceScroll = 0;
118
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 /**
120 * Interface to receive notifications when a drag starts or stops
121 */
122 interface DragListener {
123
124 /**
125 * A drag has begun
126 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 * @param source An object representing where the drag originated
128 * @param info The data associated with the object that is being dragged
129 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
130 * or {@link DragController#DRAG_ACTION_COPY}
131 */
Joe Onorato5162ea92009-09-03 09:39:42 -0700132 void onDragStart(DragSource source, Object info, int dragAction);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
134 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700135 * The drag has ended
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 */
137 void onDragEnd();
138 }
139
140 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400141 * Used to create a new DragLayer from XML.
142 *
143 * @param context The application's context.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 */
Joe Onorato00acb122009-08-04 16:04:30 -0400145 public DragController(Context context) {
146 mContext = context;
147 mHandler = new Handler();
Joe Onorato658db742010-09-29 11:40:39 -0700148 mScrollZone = context.getResources().getDimensionPixelSize(R.dimen.scroll_zone);
Joe Onorato00acb122009-08-04 16:04:30 -0400149 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Patrick Dubroy1262e362010-10-06 15:49:50 -0700151 public boolean dragging() {
152 return mDragging;
153 }
154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 /**
Joe Onorato5162ea92009-09-03 09:39:42 -0700156 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700157 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 * @param v The view that is being dragged
159 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800160 * @param dragInfo The data associated with the object that is being dragged
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
162 * {@link #DRAG_ACTION_COPY}
163 */
Joe Onorato00acb122009-08-04 16:04:30 -0400164 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700165 startDrag(v, source, dragInfo, dragAction, null);
166 }
167
168 /**
169 * Starts a drag.
170 *
171 * @param v The view that is being dragged
172 * @param source An object representing where the drag originated
173 * @param dragInfo The data associated with the object that is being dragged
174 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
175 * {@link #DRAG_ACTION_COPY}
176 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
177 * Makes dragging feel more precise, e.g. you can clip out a transparent border
178 */
179 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction,
180 Rect dragRegion) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700181 mOriginator = v;
182
183 Bitmap b = getViewBitmap(v);
184
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400185 if (b == null) {
186 // out of memory?
187 return;
188 }
189
Joe Onorato5162ea92009-09-03 09:39:42 -0700190 int[] loc = mCoordinatesTemp;
191 v.getLocationOnScreen(loc);
192 int screenX = loc[0];
193 int screenY = loc[1];
194
Patrick Dubroy5f445422011-02-18 14:35:21 -0800195 startDrag(b, screenX, screenY, source, dragInfo, dragAction, dragRegion);
Joe Onorato5162ea92009-09-03 09:39:42 -0700196 b.recycle();
197
198 if (dragAction == DRAG_ACTION_MOVE) {
199 v.setVisibility(View.GONE);
200 }
201 }
202
203 /**
204 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700205 *
Winson Chunge3193b92010-09-10 11:44:42 -0700206 * @param v The view that is being dragged
207 * @param bmp The bitmap that represents the view being dragged
208 * @param source An object representing where the drag originated
209 * @param dragInfo The data associated with the object that is being dragged
210 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
211 * {@link #DRAG_ACTION_COPY}
212 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
213 * Makes dragging feel more precise, e.g. you can clip out a transparent border
214 */
215 public void startDrag(View v, Bitmap bmp, DragSource source, Object dragInfo, int dragAction,
216 Rect dragRegion) {
217 mOriginator = v;
218
219 int[] loc = mCoordinatesTemp;
220 v.getLocationOnScreen(loc);
221 int screenX = loc[0];
222 int screenY = loc[1];
223
Patrick Dubroy5f445422011-02-18 14:35:21 -0800224 startDrag(bmp, screenX, screenY, source, dragInfo, dragAction, dragRegion);
Winson Chunge3193b92010-09-10 11:44:42 -0700225
226 if (dragAction == DRAG_ACTION_MOVE) {
227 v.setVisibility(View.GONE);
228 }
229 }
230
231 /**
232 * Starts a drag.
233 *
Joe Onorato5162ea92009-09-03 09:39:42 -0700234 * @param b The bitmap to display as the drag image. It will be re-scaled to the
235 * enlarged size.
236 * @param screenX The x position on screen of the left-top of the bitmap.
237 * @param screenY The y position on screen of the left-top of the bitmap.
Joe Onorato5162ea92009-09-03 09:39:42 -0700238 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800239 * @param dragInfo The data associated with the object that is being dragged
Joe Onorato5162ea92009-09-03 09:39:42 -0700240 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
241 * {@link #DRAG_ACTION_COPY}
242 */
243 public void startDrag(Bitmap b, int screenX, int screenY,
Joe Onorato5162ea92009-09-03 09:39:42 -0700244 DragSource source, Object dragInfo, int dragAction) {
Patrick Dubroy5f445422011-02-18 14:35:21 -0800245 startDrag(b, screenX, screenY, source, dragInfo, dragAction, null);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700246 }
247
248 /**
249 * Starts a drag.
250 *
251 * @param b The bitmap to display as the drag image. It will be re-scaled to the
252 * enlarged size.
253 * @param screenX The x position on screen of the left-top of the bitmap.
254 * @param screenY The y position on screen of the left-top of the bitmap.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700255 * @param source An object representing where the drag originated
256 * @param dragInfo The data associated with the object that is being dragged
257 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
258 * {@link #DRAG_ACTION_COPY}
259 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
260 * Makes dragging feel more precise, e.g. you can clip out a transparent border
261 */
262 public void startDrag(Bitmap b, int screenX, int screenY,
Michael Jurkaa63c4522010-08-19 13:52:27 -0700263 DragSource source, Object dragInfo, int dragAction, Rect dragRegion) {
Joe Onorato00acb122009-08-04 16:04:30 -0400264 if (PROFILE_DRAWING_DURING_DRAG) {
265 android.os.Debug.startMethodTracing("Launcher");
266 }
267
268 // Hide soft keyboard, if visible
269 if (mInputMethodManager == null) {
270 mInputMethodManager = (InputMethodManager)
271 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
272 }
273 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
274
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700275 for (DragListener listener : mListeners) {
276 listener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400277 }
278
Adam Cohene3e27a82011-04-15 12:07:39 -0700279 final int registrationX = ((int)mMotionDownX) - screenX;
280 final int registrationY = ((int)mMotionDownY) - screenY;
Joe Onorato00acb122009-08-04 16:04:30 -0400281
Michael Jurkaa63c4522010-08-19 13:52:27 -0700282 final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left;
283 final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top;
Adam Cohene3e27a82011-04-15 12:07:39 -0700284
Joe Onorato00acb122009-08-04 16:04:30 -0400285 mDragging = true;
Adam Cohencb3382b2011-05-24 14:07:08 -0700286
Adam Cohenbfbfd262011-06-13 16:55:12 -0700287 mDragObject.dragComplete = false;
Adam Cohencb3382b2011-05-24 14:07:08 -0700288 mDragObject.xOffset = mMotionDownX - (screenX + dragRegionLeft);
289 mDragObject.yOffset = mMotionDownY - (screenY + dragRegionTop);
290 mDragObject.dragSource = source;
291 mDragObject.dragInfo = dragInfo;
Joe Onorato00acb122009-08-04 16:04:30 -0400292
293 mVibrator.vibrate(VIBRATE_DURATION);
294
Adam Cohencb3382b2011-05-24 14:07:08 -0700295 final DragView dragView = mDragObject.dragView = new DragView(mContext, b, registrationX,
296 registrationY, 0, 0, b.getWidth(), b.getHeight());
Michael Jurkaa63c4522010-08-19 13:52:27 -0700297
Patrick Dubroya669d792010-11-23 14:40:33 -0800298 final DragSource dragSource = source;
299 dragView.setOnDrawRunnable(new Runnable() {
300 public void run() {
301 dragSource.onDragViewVisible();
302 };
303 });
304
Michael Jurkaa63c4522010-08-19 13:52:27 -0700305 if (dragRegion != null) {
Adam Cohene3e27a82011-04-15 12:07:39 -0700306 dragView.setDragRegion(new Rect(dragRegion));
Michael Jurkaa63c4522010-08-19 13:52:27 -0700307 }
308
Joe Onorato00acb122009-08-04 16:04:30 -0400309 dragView.show(mWindowToken, (int)mMotionDownX, (int)mMotionDownY);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700310
311 handleMoveEvent((int) mMotionDownX, (int) mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400312 }
313
314 /**
315 * Draw the view into a bitmap.
316 */
Adam Cohen120980b2010-12-08 11:05:37 -0800317 Bitmap getViewBitmap(View v) {
Joe Onorato00acb122009-08-04 16:04:30 -0400318 v.clearFocus();
319 v.setPressed(false);
320
321 boolean willNotCache = v.willNotCacheDrawing();
322 v.setWillNotCacheDrawing(false);
323
324 // Reset the drawing cache background color to fully transparent
325 // for the duration of this operation
326 int color = v.getDrawingCacheBackgroundColor();
327 v.setDrawingCacheBackgroundColor(0);
Adam Cohen120980b2010-12-08 11:05:37 -0800328 float alpha = v.getAlpha();
329 v.setAlpha(1.0f);
Joe Onorato00acb122009-08-04 16:04:30 -0400330
331 if (color != 0) {
332 v.destroyDrawingCache();
333 }
334 v.buildDrawingCache();
335 Bitmap cacheBitmap = v.getDrawingCache();
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400336 if (cacheBitmap == null) {
337 Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
338 return null;
339 }
Joe Onorato00acb122009-08-04 16:04:30 -0400340
341 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
342
343 // Restore the view
344 v.destroyDrawingCache();
Adam Cohen120980b2010-12-08 11:05:37 -0800345 v.setAlpha(alpha);
Joe Onorato00acb122009-08-04 16:04:30 -0400346 v.setWillNotCacheDrawing(willNotCache);
347 v.setDrawingCacheBackgroundColor(color);
348
349 return bitmap;
350 }
351
352 /**
353 * Call this from a drag source view like this:
354 *
355 * <pre>
356 * @Override
357 * public boolean dispatchKeyEvent(KeyEvent event) {
358 * return mDragController.dispatchKeyEvent(this, event)
359 * || super.dispatchKeyEvent(event);
360 * </pre>
361 */
Romain Guyea3763c2010-01-11 18:02:04 -0800362 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato00acb122009-08-04 16:04:30 -0400363 public boolean dispatchKeyEvent(KeyEvent event) {
364 return mDragging;
365 }
366
Winson Chung304dcde2011-01-07 11:17:23 -0800367 public boolean isDragging() {
368 return mDragging;
369 }
370
Joe Onorato24b6fd82009-11-12 13:47:09 -0800371 /**
372 * Stop dragging without dropping.
373 */
374 public void cancelDrag() {
Winson Chung621e6402011-01-04 16:03:57 -0800375 if (mDragging) {
Patrick Dubroye3887cc2011-01-20 10:43:40 -0800376 // Should we also be calling onDragExit() here?
Adam Cohenbfbfd262011-06-13 16:55:12 -0700377 mDragObject.dragComplete = true;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700378 mDragObject.dragSource.onDropCompleted(null, mDragObject, false);
Winson Chung621e6402011-01-04 16:03:57 -0800379 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800380 endDrag();
381 }
382
Joe Onorato00acb122009-08-04 16:04:30 -0400383 private void endDrag() {
384 if (mDragging) {
385 mDragging = false;
386 if (mOriginator != null) {
387 mOriginator.setVisibility(View.VISIBLE);
388 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700389 for (DragListener listener : mListeners) {
390 listener.onDragEnd();
Joe Onorato00acb122009-08-04 16:04:30 -0400391 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700392 if (mDragObject.dragView != null) {
393 mDragObject.dragView.remove();
394 mDragObject.dragView = null;
Joe Onorato00acb122009-08-04 16:04:30 -0400395 }
Joe Onorato00acb122009-08-04 16:04:30 -0400396 }
397 }
398
399 /**
400 * Call this from a drag source view.
401 */
402 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400403 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800404 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400405 + mDragging);
406 }
Joe Onorato00acb122009-08-04 16:04:30 -0400407 final int action = ev.getAction();
408
Joe Onorato87467d32009-11-08 14:36:43 -0500409 if (action == MotionEvent.ACTION_DOWN) {
410 recordScreenSize();
411 }
412
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700413 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
414 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400415
416 switch (action) {
417 case MotionEvent.ACTION_MOVE:
418 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400419 case MotionEvent.ACTION_DOWN:
420 // Remember location of down touch
421 mMotionDownX = screenX;
422 mMotionDownY = screenY;
423 mLastDropTarget = null;
424 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400425 case MotionEvent.ACTION_UP:
426 if (mDragging) {
427 drop(screenX, screenY);
428 }
429 endDrag();
430 break;
Winson Chung621e6402011-01-04 16:03:57 -0800431 case MotionEvent.ACTION_CANCEL:
432 cancelDrag();
433 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400434 }
435
436 return mDragging;
437 }
438
439 /**
Romain Guyea3763c2010-01-11 18:02:04 -0800440 * Sets the view that should handle move events.
441 */
442 void setMoveTarget(View view) {
443 mMoveTarget = view;
444 }
445
446 public boolean dispatchUnhandledMove(View focused, int direction) {
447 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
448 }
449
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700450 private void handleMoveEvent(int x, int y) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700451 mDragObject.dragView.move(x, y);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700452
453 // Drop on someone?
454 final int[] coordinates = mCoordinatesTemp;
455 DropTarget dropTarget = findDropTarget(x, y, coordinates);
Adam Cohencb3382b2011-05-24 14:07:08 -0700456 mDragObject.x = coordinates[0];
457 mDragObject.y = coordinates[1];
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700458 if (dropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700459 DropTarget delegate = dropTarget.getDropTargetDelegate(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700460 if (delegate != null) {
461 dropTarget = delegate;
462 }
463
464 if (mLastDropTarget != dropTarget) {
465 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700466 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700467 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700468 dropTarget.onDragEnter(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700469 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700470 dropTarget.onDragOver(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700471 } else {
472 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700473 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700474 }
475 }
476 mLastDropTarget = dropTarget;
477
478 // Scroll, maybe, but not if we're in the delete region.
479 boolean inDeleteRegion = false;
480 if (mDeleteRegion != null) {
481 inDeleteRegion = mDeleteRegion.contains(x, y);
482 }
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700483
484 // After a scroll, the touch point will still be in the scroll region.
485 // Rather than scrolling immediately, require a bit of twiddling to scroll again
486 final int slop = ViewConfiguration.get(mContext).getScaledWindowTouchSlop();
487 mDistanceSinceScroll +=
488 Math.sqrt(Math.pow(mLastTouch[0] - x, 2) + Math.pow(mLastTouch[1] - y, 2));
489 mLastTouch[0] = x;
490 mLastTouch[1] = y;
491
Joe Onorato658db742010-09-29 11:40:39 -0700492 if (!inDeleteRegion && x < mScrollZone) {
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700493 if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700494 mScrollState = SCROLL_WAITING_IN_ZONE;
495 mScrollRunnable.setDirection(SCROLL_LEFT);
496 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700497 mDragScroller.onEnterScrollArea(SCROLL_LEFT);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700498 }
Joe Onorato658db742010-09-29 11:40:39 -0700499 } else if (!inDeleteRegion && x > mScrollView.getWidth() - mScrollZone) {
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700500 if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700501 mScrollState = SCROLL_WAITING_IN_ZONE;
502 mScrollRunnable.setDirection(SCROLL_RIGHT);
503 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700504 mDragScroller.onEnterScrollArea(SCROLL_RIGHT);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700505 }
506 } else {
507 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
508 mScrollState = SCROLL_OUTSIDE_ZONE;
509 mScrollRunnable.setDirection(SCROLL_RIGHT);
510 mHandler.removeCallbacks(mScrollRunnable);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700511 mDragScroller.onExitScrollArea();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700512 }
513 }
514 }
515
Romain Guyea3763c2010-01-11 18:02:04 -0800516 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400517 * Call this from a drag source view.
518 */
519 public boolean onTouchEvent(MotionEvent ev) {
Joe Onorato00acb122009-08-04 16:04:30 -0400520 if (!mDragging) {
521 return false;
522 }
523
524 final int action = ev.getAction();
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700525 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
526 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400527
528 switch (action) {
529 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400530 // Remember where the motion event started
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700531 mMotionDownX = screenX;
532 mMotionDownY = screenY;
Joe Onorato00acb122009-08-04 16:04:30 -0400533
Joe Onorato658db742010-09-29 11:40:39 -0700534 if ((screenX < mScrollZone) || (screenX > mScrollView.getWidth() - mScrollZone)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400535 mScrollState = SCROLL_WAITING_IN_ZONE;
536 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
537 } else {
538 mScrollState = SCROLL_OUTSIDE_ZONE;
539 }
Joe Onorato00acb122009-08-04 16:04:30 -0400540 break;
541 case MotionEvent.ACTION_MOVE:
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700542 handleMoveEvent(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400543 break;
544 case MotionEvent.ACTION_UP:
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800545 // Ensure that we've processed a move event at the current pointer location.
546 handleMoveEvent(screenX, screenY);
547
Joe Onorato00acb122009-08-04 16:04:30 -0400548 mHandler.removeCallbacks(mScrollRunnable);
549 if (mDragging) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700550 drop(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400551 }
552 endDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400553 break;
554 case MotionEvent.ACTION_CANCEL:
Joe Onorato24b6fd82009-11-12 13:47:09 -0800555 cancelDrag();
Winson Chung621e6402011-01-04 16:03:57 -0800556 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400557 }
558
559 return true;
560 }
561
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800562 private void drop(float x, float y) {
Joe Onorato00acb122009-08-04 16:04:30 -0400563 final int[] coordinates = mCoordinatesTemp;
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800564 final DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400565
Adam Cohencb3382b2011-05-24 14:07:08 -0700566 mDragObject.x = coordinates[0];
567 mDragObject.y = coordinates[1];
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800568 boolean accepted = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400569 if (dropTarget != null) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700570 mDragObject.dragComplete = true;
Adam Cohencb3382b2011-05-24 14:07:08 -0700571 dropTarget.onDragExit(mDragObject);
572 if (dropTarget.acceptDrop(mDragObject)) {
573 dropTarget.onDrop(mDragObject);
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800574 accepted = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400575 }
576 }
Adam Cohenc0dcf592011-06-01 15:30:43 -0700577 mDragObject.dragSource.onDropCompleted((View) dropTarget, mDragObject, accepted);
Joe Onorato00acb122009-08-04 16:04:30 -0400578 }
579
580 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
581 final Rect r = mRectTemp;
582
583 final ArrayList<DropTarget> dropTargets = mDropTargets;
584 final int count = dropTargets.size();
585 for (int i=count-1; i>=0; i--) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700586 DropTarget target = dropTargets.get(i);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700587 if (!target.isDropEnabled())
588 continue;
589
Joe Onorato00acb122009-08-04 16:04:30 -0400590 target.getHitRect(r);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700591
592 // Convert the hit rect to screen coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400593 target.getLocationOnScreen(dropCoordinates);
594 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
Patrick Dubroy440c3602010-07-13 17:50:32 -0700595
Adam Cohencb3382b2011-05-24 14:07:08 -0700596 mDragObject.x = x;
597 mDragObject.y = y;
Joe Onorato00acb122009-08-04 16:04:30 -0400598 if (r.contains(x, y)) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700599 DropTarget delegate = target.getDropTargetDelegate(mDragObject);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700600 if (delegate != null) {
601 target = delegate;
602 target.getLocationOnScreen(dropCoordinates);
603 }
604
605 // Make dropCoordinates relative to the DropTarget
Joe Onorato00acb122009-08-04 16:04:30 -0400606 dropCoordinates[0] = x - dropCoordinates[0];
607 dropCoordinates[1] = y - dropCoordinates[1];
Patrick Dubroy440c3602010-07-13 17:50:32 -0700608
Joe Onorato00acb122009-08-04 16:04:30 -0400609 return target;
610 }
611 }
612 return null;
613 }
614
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700615 /**
616 * Get the screen size so we can clamp events to the screen size so even if
617 * you drag off the edge of the screen, we find something.
618 */
619 private void recordScreenSize() {
620 ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
621 .getDefaultDisplay().getMetrics(mDisplayMetrics);
622 }
623
624 /**
625 * Clamp val to be &gt;= min and &lt; max.
626 */
627 private static int clamp(int val, int min, int max) {
628 if (val < min) {
629 return min;
630 } else if (val >= max) {
631 return max - 1;
632 } else {
633 return val;
634 }
635 }
636
Joe Onorato00acb122009-08-04 16:04:30 -0400637 public void setDragScoller(DragScroller scroller) {
638 mDragScroller = scroller;
639 }
640
641 public void setWindowToken(IBinder token) {
642 mWindowToken = token;
643 }
644
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 /**
646 * Sets the drag listner which will be notified when a drag starts or ends.
647 */
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700648 public void addDragListener(DragListener l) {
649 mListeners.add(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400650 }
651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 /**
653 * Remove a previously installed drag listener.
654 */
Joe Onorato00acb122009-08-04 16:04:30 -0400655 public void removeDragListener(DragListener l) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700656 mListeners.remove(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400657 }
658
659 /**
660 * Add a DropTarget to the list of potential places to receive drop events.
661 */
662 public void addDropTarget(DropTarget target) {
663 mDropTargets.add(target);
664 }
665
666 /**
667 * Don't send drop events to <em>target</em> any more.
668 */
669 public void removeDropTarget(DropTarget target) {
670 mDropTargets.remove(target);
671 }
672
673 /**
674 * Set which view scrolls for touch events near the edge of the screen.
675 */
676 public void setScrollView(View v) {
677 mScrollView = v;
678 }
679
680 /**
681 * Specifies the delete region. We won't scroll on touch events over the delete region.
682 *
683 * @param region The rectangle in screen coordinates of the delete region.
684 */
685 void setDeleteRegion(RectF region) {
686 mDeleteRegion = region;
687 }
688
Patrick Dubroy5f445422011-02-18 14:35:21 -0800689 DragView getDragView() {
Adam Cohencb3382b2011-05-24 14:07:08 -0700690 return mDragObject.dragView;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800691 }
692
Joe Onorato00acb122009-08-04 16:04:30 -0400693 private class ScrollRunnable implements Runnable {
694 private int mDirection;
695
696 ScrollRunnable() {
697 }
698
699 public void run() {
700 if (mDragScroller != null) {
701 if (mDirection == SCROLL_LEFT) {
702 mDragScroller.scrollLeft();
703 } else {
704 mDragScroller.scrollRight();
705 }
706 mScrollState = SCROLL_OUTSIDE_ZONE;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700707 mDistanceSinceScroll = 0;
708 mDragScroller.onExitScrollArea();
Joe Onorato00acb122009-08-04 16:04:30 -0400709 }
710 }
711
712 void setDirection(int direction) {
713 mDirection = direction;
714 }
715 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716}