blob: e18470ab237f7aedd2a9676c9c1adf8761c497c6 [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;
23import android.os.IBinder;
24import android.os.Handler;
25import 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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.view.View;
Joe Onorato00acb122009-08-04 16:04:30 -040029import android.view.KeyEvent;
30import android.view.MotionEvent;
Joe Onoratoe048e8a2009-09-25 10:39:17 -070031import android.view.WindowManager;
Joe Onorato00acb122009-08-04 16:04:30 -040032import android.view.inputmethod.InputMethodManager;
Joe Onorato00acb122009-08-04 16:04:30 -040033
34import java.util.ArrayList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035
36/**
Joe Onorato00acb122009-08-04 16:04:30 -040037 * Class for initiating a drag within a view or across multiple views.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038 */
Joe Onorato00acb122009-08-04 16:04:30 -040039public class DragController {
Romain Guyea3763c2010-01-11 18:02:04 -080040 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato2e5c4322009-10-06 12:34:42 -070041 private static final String TAG = "Launcher.DragController";
42
Joe Onorato00acb122009-08-04 16:04:30 -040043 /** Indicates the drag is a move. */
44 public static int DRAG_ACTION_MOVE = 0;
45
46 /** Indicates the drag is a copy. */
47 public static int DRAG_ACTION_COPY = 1;
48
49 private static final int SCROLL_DELAY = 600;
50 private static final int SCROLL_ZONE = 20;
51 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
58 private static final int SCROLL_LEFT = 0;
59 private static final int SCROLL_RIGHT = 1;
60
61 private Context mContext;
62 private Handler mHandler;
63 private final Vibrator mVibrator = new Vibrator();
64
65 // temporaries to avoid gc thrash
66 private Rect mRectTemp = new Rect();
67 private final int[] mCoordinatesTemp = new int[2];
68
69 /** Whether or not we're dragging. */
70 private boolean mDragging;
71
72 /** X coordinate of the down event. */
73 private float mMotionDownX;
74
75 /** Y coordinate of the down event. */
76 private float mMotionDownY;
77
Joe Onoratoe048e8a2009-09-25 10:39:17 -070078 /** Info about the screen for clamping. */
79 private DisplayMetrics mDisplayMetrics = new DisplayMetrics();
80
Joe Onorato00acb122009-08-04 16:04:30 -040081 /** Original view that is being dragged. */
82 private View mOriginator;
83
Joe Onorato00acb122009-08-04 16:04:30 -040084 /** X offset from the upper-left corner of the cell to where we touched. */
85 private float mTouchOffsetX;
86
87 /** Y offset from the upper-left corner of the cell to where we touched. */
88 private float mTouchOffsetY;
89
90 /** Where the drag originated */
91 private DragSource mDragSource;
92
93 /** The data associated with the object being dragged */
94 private Object mDragInfo;
95
96 /** The view that moves around while you drag. */
97 private DragView mDragView;
98
99 /** Who can receive drop events */
100 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
101
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700102 private ArrayList<DragListener> mListeners = new ArrayList<DragListener>();
Joe Onorato00acb122009-08-04 16:04:30 -0400103
104 /** The window token used as the parent for the DragView. */
105 private IBinder mWindowToken;
106
107 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
108 private View mScrollView;
109
Romain Guyea3763c2010-01-11 18:02:04 -0800110 private View mMoveTarget;
111
Joe Onorato00acb122009-08-04 16:04:30 -0400112 private DragScroller mDragScroller;
113 private int mScrollState = SCROLL_OUTSIDE_ZONE;
114 private ScrollRunnable mScrollRunnable = new ScrollRunnable();
115
116 private RectF mDeleteRegion;
117 private DropTarget mLastDropTarget;
118
119 private InputMethodManager mInputMethodManager;
120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 /**
122 * Interface to receive notifications when a drag starts or stops
123 */
124 interface DragListener {
125
126 /**
127 * A drag has begun
128 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 * @param source An object representing where the drag originated
130 * @param info The data associated with the object that is being dragged
131 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
132 * or {@link DragController#DRAG_ACTION_COPY}
133 */
Joe Onorato5162ea92009-09-03 09:39:42 -0700134 void onDragStart(DragSource source, Object info, int dragAction);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
136 /**
137 * The drag has eneded
138 */
139 void onDragEnd();
140 }
141
142 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400143 * Used to create a new DragLayer from XML.
144 *
145 * @param context The application's context.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 */
Joe Onorato00acb122009-08-04 16:04:30 -0400147 public DragController(Context context) {
148 mContext = context;
149 mHandler = new Handler();
150 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
152 /**
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 mOriginator = v;
179
180 Bitmap b = getViewBitmap(v);
181
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400182 if (b == null) {
183 // out of memory?
184 return;
185 }
186
Joe Onorato5162ea92009-09-03 09:39:42 -0700187 int[] loc = mCoordinatesTemp;
188 v.getLocationOnScreen(loc);
189 int screenX = loc[0];
190 int screenY = loc[1];
191
192 startDrag(b, screenX, screenY, 0, 0, b.getWidth(), b.getHeight(),
Michael Jurkaa63c4522010-08-19 13:52:27 -0700193 source, dragInfo, dragAction, dragRegion);
Joe Onorato5162ea92009-09-03 09:39:42 -0700194
195 b.recycle();
196
197 if (dragAction == DRAG_ACTION_MOVE) {
198 v.setVisibility(View.GONE);
199 }
200 }
201
202 /**
203 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700204 *
Joe Onorato5162ea92009-09-03 09:39:42 -0700205 * @param b The bitmap to display as the drag image. It will be re-scaled to the
206 * enlarged size.
207 * @param screenX The x position on screen of the left-top of the bitmap.
208 * @param screenY The y position on screen of the left-top of the bitmap.
209 * @param textureLeft The left edge of the region inside b to use.
210 * @param textureTop The top edge of the region inside b to use.
211 * @param textureWidth The width of the region inside b to use.
212 * @param textureHeight The height of the region inside b to use.
213 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800214 * @param dragInfo The data associated with the object that is being dragged
Joe Onorato5162ea92009-09-03 09:39:42 -0700215 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
216 * {@link #DRAG_ACTION_COPY}
217 */
218 public void startDrag(Bitmap b, int screenX, int screenY,
219 int textureLeft, int textureTop, int textureWidth, int textureHeight,
220 DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700221 startDrag(b, screenX, screenY, textureLeft, textureTop, textureWidth, textureHeight,
222 source, dragInfo, dragAction, null);
223 }
224
225 /**
226 * Starts a drag.
227 *
228 * @param b The bitmap to display as the drag image. It will be re-scaled to the
229 * enlarged size.
230 * @param screenX The x position on screen of the left-top of the bitmap.
231 * @param screenY The y position on screen of the left-top of the bitmap.
232 * @param textureLeft The left edge of the region inside b to use.
233 * @param textureTop The top edge of the region inside b to use.
234 * @param textureWidth The width of the region inside b to use.
235 * @param textureHeight The height of the region inside b to use.
236 * @param source An object representing where the drag originated
237 * @param dragInfo The data associated with the object that is being dragged
238 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
239 * {@link #DRAG_ACTION_COPY}
240 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
241 * Makes dragging feel more precise, e.g. you can clip out a transparent border
242 */
243 public void startDrag(Bitmap b, int screenX, int screenY,
244 int textureLeft, int textureTop, int textureWidth, int textureHeight,
245 DragSource source, Object dragInfo, int dragAction, Rect dragRegion) {
Joe Onorato00acb122009-08-04 16:04:30 -0400246 if (PROFILE_DRAWING_DURING_DRAG) {
247 android.os.Debug.startMethodTracing("Launcher");
248 }
249
250 // Hide soft keyboard, if visible
251 if (mInputMethodManager == null) {
252 mInputMethodManager = (InputMethodManager)
253 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
254 }
255 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
256
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700257 for (DragListener listener : mListeners) {
258 listener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400259 }
260
Joe Onorato00acb122009-08-04 16:04:30 -0400261 int registrationX = ((int)mMotionDownX) - screenX;
262 int registrationY = ((int)mMotionDownY) - screenY;
263
Michael Jurkaa63c4522010-08-19 13:52:27 -0700264 final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left;
265 final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top;
266 mTouchOffsetX = mMotionDownX - screenX - dragRegionLeft;
267 mTouchOffsetY = mMotionDownY - screenY - dragRegionTop;
Joe Onorato00acb122009-08-04 16:04:30 -0400268
269 mDragging = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400270 mDragSource = source;
271 mDragInfo = dragInfo;
272
273 mVibrator.vibrate(VIBRATE_DURATION);
274
Joe Onorato5162ea92009-09-03 09:39:42 -0700275 DragView dragView = mDragView = new DragView(mContext, b, registrationX, registrationY,
276 textureLeft, textureTop, textureWidth, textureHeight);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700277
278 if (dragRegion != null) {
279 dragView.setDragRegion(dragRegionLeft, dragRegion.top,
280 dragRegion.right - dragRegionLeft, dragRegion.bottom - dragRegionTop);
281 }
282
Joe Onorato00acb122009-08-04 16:04:30 -0400283 dragView.show(mWindowToken, (int)mMotionDownX, (int)mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400284 }
285
286 /**
287 * Draw the view into a bitmap.
288 */
289 private Bitmap getViewBitmap(View v) {
290 v.clearFocus();
291 v.setPressed(false);
292
293 boolean willNotCache = v.willNotCacheDrawing();
294 v.setWillNotCacheDrawing(false);
295
296 // Reset the drawing cache background color to fully transparent
297 // for the duration of this operation
298 int color = v.getDrawingCacheBackgroundColor();
299 v.setDrawingCacheBackgroundColor(0);
300
301 if (color != 0) {
302 v.destroyDrawingCache();
303 }
304 v.buildDrawingCache();
305 Bitmap cacheBitmap = v.getDrawingCache();
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400306 if (cacheBitmap == null) {
307 Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
308 return null;
309 }
Joe Onorato00acb122009-08-04 16:04:30 -0400310
311 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
312
313 // Restore the view
314 v.destroyDrawingCache();
315 v.setWillNotCacheDrawing(willNotCache);
316 v.setDrawingCacheBackgroundColor(color);
317
318 return bitmap;
319 }
320
321 /**
322 * Call this from a drag source view like this:
323 *
324 * <pre>
325 * @Override
326 * public boolean dispatchKeyEvent(KeyEvent event) {
327 * return mDragController.dispatchKeyEvent(this, event)
328 * || super.dispatchKeyEvent(event);
329 * </pre>
330 */
Romain Guyea3763c2010-01-11 18:02:04 -0800331 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato00acb122009-08-04 16:04:30 -0400332 public boolean dispatchKeyEvent(KeyEvent event) {
333 return mDragging;
334 }
335
Joe Onorato24b6fd82009-11-12 13:47:09 -0800336 /**
337 * Stop dragging without dropping.
338 */
339 public void cancelDrag() {
340 endDrag();
341 }
342
Joe Onorato00acb122009-08-04 16:04:30 -0400343 private void endDrag() {
344 if (mDragging) {
345 mDragging = false;
346 if (mOriginator != null) {
347 mOriginator.setVisibility(View.VISIBLE);
348 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700349 for (DragListener listener : mListeners) {
350 listener.onDragEnd();
Joe Onorato00acb122009-08-04 16:04:30 -0400351 }
352 if (mDragView != null) {
353 mDragView.remove();
354 mDragView = null;
355 }
Joe Onorato00acb122009-08-04 16:04:30 -0400356 }
357 }
358
359 /**
360 * Call this from a drag source view.
361 */
362 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400363 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800364 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400365 + mDragging);
366 }
Joe Onorato00acb122009-08-04 16:04:30 -0400367 final int action = ev.getAction();
368
Joe Onorato87467d32009-11-08 14:36:43 -0500369 if (action == MotionEvent.ACTION_DOWN) {
370 recordScreenSize();
371 }
372
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700373 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
374 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400375
376 switch (action) {
377 case MotionEvent.ACTION_MOVE:
378 break;
379
380 case MotionEvent.ACTION_DOWN:
381 // Remember location of down touch
382 mMotionDownX = screenX;
383 mMotionDownY = screenY;
384 mLastDropTarget = null;
385 break;
386
387 case MotionEvent.ACTION_CANCEL:
388 case MotionEvent.ACTION_UP:
389 if (mDragging) {
390 drop(screenX, screenY);
391 }
392 endDrag();
393 break;
394 }
395
396 return mDragging;
397 }
398
399 /**
Romain Guyea3763c2010-01-11 18:02:04 -0800400 * Sets the view that should handle move events.
401 */
402 void setMoveTarget(View view) {
403 mMoveTarget = view;
404 }
405
406 public boolean dispatchUnhandledMove(View focused, int direction) {
407 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
408 }
409
410 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400411 * Call this from a drag source view.
412 */
413 public boolean onTouchEvent(MotionEvent ev) {
414 View scrollView = mScrollView;
415
416 if (!mDragging) {
417 return false;
418 }
419
420 final int action = ev.getAction();
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700421 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
422 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400423
424 switch (action) {
425 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400426 // Remember where the motion event started
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700427 mMotionDownX = screenX;
428 mMotionDownY = screenY;
Joe Onorato00acb122009-08-04 16:04:30 -0400429
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700430 if ((screenX < SCROLL_ZONE) || (screenX > scrollView.getWidth() - SCROLL_ZONE)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400431 mScrollState = SCROLL_WAITING_IN_ZONE;
432 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
433 } else {
434 mScrollState = SCROLL_OUTSIDE_ZONE;
435 }
436
437 break;
438 case MotionEvent.ACTION_MOVE:
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700439 // Update the drag view. Don't use the clamped pos here so the dragging looks
440 // like it goes off screen a little, intead of bumping up against the edge.
Joe Onorato00acb122009-08-04 16:04:30 -0400441 mDragView.move((int)ev.getRawX(), (int)ev.getRawY());
442
443 // Drop on someone?
444 final int[] coordinates = mCoordinatesTemp;
Romain Guyea3763c2010-01-11 18:02:04 -0800445 DropTarget dropTarget = findDropTarget(screenX, screenY, coordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400446 if (dropTarget != null) {
Patrick Dubroyc1701ad2010-07-16 15:43:04 -0700447 DropTarget delegate = dropTarget.getDropTargetDelegate(
448 mDragSource, coordinates[0], coordinates[1],
449 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
450 if (delegate != null) {
451 dropTarget = delegate;
452 }
453
Joe Onorato00acb122009-08-04 16:04:30 -0400454 if (mLastDropTarget == dropTarget) {
455 dropTarget.onDragOver(mDragSource, coordinates[0], coordinates[1],
456 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
457 } else {
458 if (mLastDropTarget != null) {
459 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
460 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
461 }
462 dropTarget.onDragEnter(mDragSource, coordinates[0], coordinates[1],
463 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
464 }
465 } else {
466 if (mLastDropTarget != null) {
467 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
468 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
469 }
470 }
471 mLastDropTarget = dropTarget;
472
473 // Scroll, maybe, but not if we're in the delete region.
474 boolean inDeleteRegion = false;
475 if (mDeleteRegion != null) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700476 inDeleteRegion = mDeleteRegion.contains(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400477 }
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700478 if (!inDeleteRegion && screenX < SCROLL_ZONE) {
Joe Onorato00acb122009-08-04 16:04:30 -0400479 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
480 mScrollState = SCROLL_WAITING_IN_ZONE;
481 mScrollRunnable.setDirection(SCROLL_LEFT);
482 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
483 }
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700484 } else if (!inDeleteRegion && screenX > scrollView.getWidth() - SCROLL_ZONE) {
Joe Onorato00acb122009-08-04 16:04:30 -0400485 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
486 mScrollState = SCROLL_WAITING_IN_ZONE;
487 mScrollRunnable.setDirection(SCROLL_RIGHT);
488 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
489 }
490 } else {
491 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
492 mScrollState = SCROLL_OUTSIDE_ZONE;
493 mScrollRunnable.setDirection(SCROLL_RIGHT);
494 mHandler.removeCallbacks(mScrollRunnable);
495 }
496 }
497
498 break;
499 case MotionEvent.ACTION_UP:
500 mHandler.removeCallbacks(mScrollRunnable);
501 if (mDragging) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700502 drop(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400503 }
504 endDrag();
505
506 break;
507 case MotionEvent.ACTION_CANCEL:
Joe Onorato24b6fd82009-11-12 13:47:09 -0800508 cancelDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400509 }
510
511 return true;
512 }
513
514 private boolean drop(float x, float y) {
515 final int[] coordinates = mCoordinatesTemp;
516 DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
517
518 if (dropTarget != null) {
519 dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
520 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
521 if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1],
522 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) {
523 dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1],
524 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
525 mDragSource.onDropCompleted((View) dropTarget, true);
526 return true;
527 } else {
528 mDragSource.onDropCompleted((View) dropTarget, false);
529 return true;
530 }
531 }
532 return false;
533 }
534
535 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
536 final Rect r = mRectTemp;
537
538 final ArrayList<DropTarget> dropTargets = mDropTargets;
539 final int count = dropTargets.size();
540 for (int i=count-1; i>=0; i--) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700541 DropTarget target = dropTargets.get(i);
Joe Onorato00acb122009-08-04 16:04:30 -0400542 target.getHitRect(r);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700543
544 // Convert the hit rect to screen coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400545 target.getLocationOnScreen(dropCoordinates);
546 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
Patrick Dubroy440c3602010-07-13 17:50:32 -0700547
Joe Onorato00acb122009-08-04 16:04:30 -0400548 if (r.contains(x, y)) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700549 DropTarget delegate = target.getDropTargetDelegate(mDragSource,
550 x, y, (int)mTouchOffsetX, (int)mTouchOffsetY, mDragView, mDragInfo);
551 if (delegate != null) {
552 target = delegate;
553 target.getLocationOnScreen(dropCoordinates);
554 }
555
556 // Make dropCoordinates relative to the DropTarget
Joe Onorato00acb122009-08-04 16:04:30 -0400557 dropCoordinates[0] = x - dropCoordinates[0];
558 dropCoordinates[1] = y - dropCoordinates[1];
Patrick Dubroy440c3602010-07-13 17:50:32 -0700559
Joe Onorato00acb122009-08-04 16:04:30 -0400560 return target;
561 }
562 }
563 return null;
564 }
565
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700566 /**
567 * Get the screen size so we can clamp events to the screen size so even if
568 * you drag off the edge of the screen, we find something.
569 */
570 private void recordScreenSize() {
571 ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
572 .getDefaultDisplay().getMetrics(mDisplayMetrics);
573 }
574
575 /**
576 * Clamp val to be &gt;= min and &lt; max.
577 */
578 private static int clamp(int val, int min, int max) {
579 if (val < min) {
580 return min;
581 } else if (val >= max) {
582 return max - 1;
583 } else {
584 return val;
585 }
586 }
587
Joe Onorato00acb122009-08-04 16:04:30 -0400588 public void setDragScoller(DragScroller scroller) {
589 mDragScroller = scroller;
590 }
591
592 public void setWindowToken(IBinder token) {
593 mWindowToken = token;
594 }
595
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 /**
597 * Sets the drag listner which will be notified when a drag starts or ends.
598 */
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700599 public void addDragListener(DragListener l) {
600 mListeners.add(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400601 }
602
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 /**
604 * Remove a previously installed drag listener.
605 */
Joe Onorato00acb122009-08-04 16:04:30 -0400606 public void removeDragListener(DragListener l) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700607 mListeners.remove(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400608 }
609
610 /**
611 * Add a DropTarget to the list of potential places to receive drop events.
612 */
613 public void addDropTarget(DropTarget target) {
614 mDropTargets.add(target);
615 }
616
617 /**
618 * Don't send drop events to <em>target</em> any more.
619 */
620 public void removeDropTarget(DropTarget target) {
621 mDropTargets.remove(target);
622 }
623
624 /**
625 * Set which view scrolls for touch events near the edge of the screen.
626 */
627 public void setScrollView(View v) {
628 mScrollView = v;
629 }
630
631 /**
632 * Specifies the delete region. We won't scroll on touch events over the delete region.
633 *
634 * @param region The rectangle in screen coordinates of the delete region.
635 */
636 void setDeleteRegion(RectF region) {
637 mDeleteRegion = region;
638 }
639
640 private class ScrollRunnable implements Runnable {
641 private int mDirection;
642
643 ScrollRunnable() {
644 }
645
646 public void run() {
647 if (mDragScroller != null) {
648 if (mDirection == SCROLL_LEFT) {
649 mDragScroller.scrollLeft();
650 } else {
651 mDragScroller.scrollRight();
652 }
653 mScrollState = SCROLL_OUTSIDE_ZONE;
654 }
655 }
656
657 void setDirection(int direction) {
658 mDirection = direction;
659 }
660 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661}