blob: 87b3473a18c4bb0054f63a015bad678c34ca69cf [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;
Michael Jurka0280c3b2010-09-17 15:00:07 -070021import android.graphics.Canvas;
Joe Onorato00acb122009-08-04 16:04:30 -040022import android.graphics.Rect;
23import android.graphics.RectF;
Joe Onorato00acb122009-08-04 16:04:30 -040024import android.os.Handler;
Michael Jurka0280c3b2010-09-17 15:00:07 -070025import android.os.IBinder;
Joe Onorato00acb122009-08-04 16:04:30 -040026import android.os.Vibrator;
Joe Onoratoe048e8a2009-09-25 10:39:17 -070027import android.util.DisplayMetrics;
Joe Onorato00acb122009-08-04 16:04:30 -040028import android.util.Log;
Joe Onorato00acb122009-08-04 16:04:30 -040029import android.view.KeyEvent;
30import android.view.MotionEvent;
Michael Jurka0280c3b2010-09-17 15:00:07 -070031import android.view.View;
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
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;
51 private static final int SCROLL_ZONE = 20;
52 private static final int VIBRATE_DURATION = 35;
53
54 private static final boolean PROFILE_DRAWING_DURING_DRAG = false;
55
56 private static final int SCROLL_OUTSIDE_ZONE = 0;
57 private static final int SCROLL_WAITING_IN_ZONE = 1;
58
59 private static final int SCROLL_LEFT = 0;
60 private static final int SCROLL_RIGHT = 1;
61
62 private Context mContext;
63 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. */
74 private float mMotionDownX;
75
76 /** Y coordinate of the down event. */
77 private float mMotionDownY;
78
Joe Onoratoe048e8a2009-09-25 10:39:17 -070079 /** Info about the screen for clamping. */
80 private DisplayMetrics mDisplayMetrics = new DisplayMetrics();
81
Joe Onorato00acb122009-08-04 16:04:30 -040082 /** Original view that is being dragged. */
83 private View mOriginator;
84
Joe Onorato00acb122009-08-04 16:04:30 -040085 /** X offset from the upper-left corner of the cell to where we touched. */
86 private float mTouchOffsetX;
87
88 /** Y offset from the upper-left corner of the cell to where we touched. */
89 private float mTouchOffsetY;
90
91 /** Where the drag originated */
92 private DragSource mDragSource;
93
94 /** The data associated with the object being dragged */
95 private Object mDragInfo;
96
97 /** The view that moves around while you drag. */
98 private DragView mDragView;
99
100 /** Who can receive drop events */
101 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
102
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700103 private ArrayList<DragListener> mListeners = new ArrayList<DragListener>();
Joe Onorato00acb122009-08-04 16:04:30 -0400104
105 /** The window token used as the parent for the DragView. */
106 private IBinder mWindowToken;
107
108 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
109 private View mScrollView;
110
Romain Guyea3763c2010-01-11 18:02:04 -0800111 private View mMoveTarget;
112
Joe Onorato00acb122009-08-04 16:04:30 -0400113 private DragScroller mDragScroller;
114 private int mScrollState = SCROLL_OUTSIDE_ZONE;
115 private ScrollRunnable mScrollRunnable = new ScrollRunnable();
116
117 private RectF mDeleteRegion;
118 private DropTarget mLastDropTarget;
119
120 private InputMethodManager mInputMethodManager;
121
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 /**
123 * Interface to receive notifications when a drag starts or stops
124 */
125 interface DragListener {
126
127 /**
128 * A drag has begun
129 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 * @param source An object representing where the drag originated
131 * @param info The data associated with the object that is being dragged
132 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
133 * or {@link DragController#DRAG_ACTION_COPY}
134 */
Joe Onorato5162ea92009-09-03 09:39:42 -0700135 void onDragStart(DragSource source, Object info, int dragAction);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700138 * The drag has ended
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 */
140 void onDragEnd();
141 }
142
143 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400144 * Used to create a new DragLayer from XML.
145 *
146 * @param context The application's context.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 */
Joe Onorato00acb122009-08-04 16:04:30 -0400148 public DragController(Context context) {
149 mContext = context;
150 mHandler = new Handler();
151 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
153 /**
Joe Onorato5162ea92009-09-03 09:39:42 -0700154 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700155 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 * @param v The view that is being dragged
157 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800158 * @param dragInfo The data associated with the object that is being dragged
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
160 * {@link #DRAG_ACTION_COPY}
161 */
Joe Onorato00acb122009-08-04 16:04:30 -0400162 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700163 startDrag(v, source, dragInfo, dragAction, null);
164 }
165
166 /**
167 * Starts a drag.
168 *
169 * @param v The view that is being dragged
170 * @param source An object representing where the drag originated
171 * @param dragInfo The data associated with the object that is being dragged
172 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
173 * {@link #DRAG_ACTION_COPY}
174 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
175 * Makes dragging feel more precise, e.g. you can clip out a transparent border
176 */
177 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction,
178 Rect dragRegion) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700179 mOriginator = v;
180
181 Bitmap b = getViewBitmap(v);
182
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400183 if (b == null) {
184 // out of memory?
185 return;
186 }
187
Joe Onorato5162ea92009-09-03 09:39:42 -0700188 int[] loc = mCoordinatesTemp;
189 v.getLocationOnScreen(loc);
190 int screenX = loc[0];
191 int screenY = loc[1];
192
193 startDrag(b, screenX, screenY, 0, 0, b.getWidth(), b.getHeight(),
Michael Jurkaa63c4522010-08-19 13:52:27 -0700194 source, dragInfo, dragAction, dragRegion);
Joe Onorato5162ea92009-09-03 09:39:42 -0700195
196 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
224 startDrag(bmp, screenX, screenY, 0, 0, bmp.getWidth(), bmp.getHeight(),
225 source, dragInfo, dragAction, dragRegion);
226
227 if (dragAction == DRAG_ACTION_MOVE) {
228 v.setVisibility(View.GONE);
229 }
230 }
231
232 /**
233 * Starts a drag.
234 *
Joe Onorato5162ea92009-09-03 09:39:42 -0700235 * @param b The bitmap to display as the drag image. It will be re-scaled to the
236 * enlarged size.
237 * @param screenX The x position on screen of the left-top of the bitmap.
238 * @param screenY The y position on screen of the left-top of the bitmap.
239 * @param textureLeft The left edge of the region inside b to use.
240 * @param textureTop The top edge of the region inside b to use.
241 * @param textureWidth The width of the region inside b to use.
242 * @param textureHeight The height of the region inside b to use.
243 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800244 * @param dragInfo The data associated with the object that is being dragged
Joe Onorato5162ea92009-09-03 09:39:42 -0700245 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
246 * {@link #DRAG_ACTION_COPY}
247 */
248 public void startDrag(Bitmap b, int screenX, int screenY,
249 int textureLeft, int textureTop, int textureWidth, int textureHeight,
250 DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700251 startDrag(b, screenX, screenY, textureLeft, textureTop, textureWidth, textureHeight,
252 source, dragInfo, dragAction, null);
253 }
254
255 /**
256 * Starts a drag.
257 *
258 * @param b The bitmap to display as the drag image. It will be re-scaled to the
259 * enlarged size.
260 * @param screenX The x position on screen of the left-top of the bitmap.
261 * @param screenY The y position on screen of the left-top of the bitmap.
262 * @param textureLeft The left edge of the region inside b to use.
263 * @param textureTop The top edge of the region inside b to use.
264 * @param textureWidth The width of the region inside b to use.
265 * @param textureHeight The height of the region inside b to use.
266 * @param source An object representing where the drag originated
267 * @param dragInfo The data associated with the object that is being dragged
268 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
269 * {@link #DRAG_ACTION_COPY}
270 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
271 * Makes dragging feel more precise, e.g. you can clip out a transparent border
272 */
273 public void startDrag(Bitmap b, int screenX, int screenY,
274 int textureLeft, int textureTop, int textureWidth, int textureHeight,
275 DragSource source, Object dragInfo, int dragAction, Rect dragRegion) {
Joe Onorato00acb122009-08-04 16:04:30 -0400276 if (PROFILE_DRAWING_DURING_DRAG) {
277 android.os.Debug.startMethodTracing("Launcher");
278 }
279
280 // Hide soft keyboard, if visible
281 if (mInputMethodManager == null) {
282 mInputMethodManager = (InputMethodManager)
283 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
284 }
285 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
286
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700287 for (DragListener listener : mListeners) {
288 listener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400289 }
290
Joe Onorato00acb122009-08-04 16:04:30 -0400291 int registrationX = ((int)mMotionDownX) - screenX;
292 int registrationY = ((int)mMotionDownY) - screenY;
293
Michael Jurkaa63c4522010-08-19 13:52:27 -0700294 final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left;
295 final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top;
296 mTouchOffsetX = mMotionDownX - screenX - dragRegionLeft;
297 mTouchOffsetY = mMotionDownY - screenY - dragRegionTop;
Joe Onorato00acb122009-08-04 16:04:30 -0400298
299 mDragging = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400300 mDragSource = source;
301 mDragInfo = dragInfo;
302
303 mVibrator.vibrate(VIBRATE_DURATION);
304
Joe Onorato5162ea92009-09-03 09:39:42 -0700305 DragView dragView = mDragView = new DragView(mContext, b, registrationX, registrationY,
306 textureLeft, textureTop, textureWidth, textureHeight);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700307
308 if (dragRegion != null) {
309 dragView.setDragRegion(dragRegionLeft, dragRegion.top,
310 dragRegion.right - dragRegionLeft, dragRegion.bottom - dragRegionTop);
311 }
312
Joe Onorato00acb122009-08-04 16:04:30 -0400313 dragView.show(mWindowToken, (int)mMotionDownX, (int)mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400314 }
315
316 /**
317 * Draw the view into a bitmap.
318 */
319 private Bitmap getViewBitmap(View v) {
320 v.clearFocus();
321 v.setPressed(false);
322
323 boolean willNotCache = v.willNotCacheDrawing();
324 v.setWillNotCacheDrawing(false);
325
326 // Reset the drawing cache background color to fully transparent
327 // for the duration of this operation
328 int color = v.getDrawingCacheBackgroundColor();
329 v.setDrawingCacheBackgroundColor(0);
330
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();
345 v.setWillNotCacheDrawing(willNotCache);
346 v.setDrawingCacheBackgroundColor(color);
347
348 return bitmap;
349 }
350
351 /**
352 * Call this from a drag source view like this:
353 *
354 * <pre>
355 * @Override
356 * public boolean dispatchKeyEvent(KeyEvent event) {
357 * return mDragController.dispatchKeyEvent(this, event)
358 * || super.dispatchKeyEvent(event);
359 * </pre>
360 */
Romain Guyea3763c2010-01-11 18:02:04 -0800361 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato00acb122009-08-04 16:04:30 -0400362 public boolean dispatchKeyEvent(KeyEvent event) {
363 return mDragging;
364 }
365
Joe Onorato24b6fd82009-11-12 13:47:09 -0800366 /**
367 * Stop dragging without dropping.
368 */
369 public void cancelDrag() {
370 endDrag();
371 }
372
Joe Onorato00acb122009-08-04 16:04:30 -0400373 private void endDrag() {
374 if (mDragging) {
375 mDragging = false;
376 if (mOriginator != null) {
377 mOriginator.setVisibility(View.VISIBLE);
378 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700379 for (DragListener listener : mListeners) {
380 listener.onDragEnd();
Joe Onorato00acb122009-08-04 16:04:30 -0400381 }
382 if (mDragView != null) {
383 mDragView.remove();
384 mDragView = null;
385 }
Joe Onorato00acb122009-08-04 16:04:30 -0400386 }
387 }
388
389 /**
390 * Call this from a drag source view.
391 */
392 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400393 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800394 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400395 + mDragging);
396 }
Joe Onorato00acb122009-08-04 16:04:30 -0400397 final int action = ev.getAction();
398
Joe Onorato87467d32009-11-08 14:36:43 -0500399 if (action == MotionEvent.ACTION_DOWN) {
400 recordScreenSize();
401 }
402
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700403 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
404 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400405
406 switch (action) {
407 case MotionEvent.ACTION_MOVE:
408 break;
409
410 case MotionEvent.ACTION_DOWN:
411 // Remember location of down touch
412 mMotionDownX = screenX;
413 mMotionDownY = screenY;
414 mLastDropTarget = null;
415 break;
416
417 case MotionEvent.ACTION_CANCEL:
418 case MotionEvent.ACTION_UP:
419 if (mDragging) {
420 drop(screenX, screenY);
421 }
422 endDrag();
423 break;
424 }
425
426 return mDragging;
427 }
428
429 /**
Romain Guyea3763c2010-01-11 18:02:04 -0800430 * Sets the view that should handle move events.
431 */
432 void setMoveTarget(View view) {
433 mMoveTarget = view;
434 }
435
436 public boolean dispatchUnhandledMove(View focused, int direction) {
437 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
438 }
439
440 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400441 * Call this from a drag source view.
442 */
443 public boolean onTouchEvent(MotionEvent ev) {
444 View scrollView = mScrollView;
445
446 if (!mDragging) {
447 return false;
448 }
449
450 final int action = ev.getAction();
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700451 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
452 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400453
454 switch (action) {
455 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400456 // Remember where the motion event started
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700457 mMotionDownX = screenX;
458 mMotionDownY = screenY;
Joe Onorato00acb122009-08-04 16:04:30 -0400459
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700460 if ((screenX < SCROLL_ZONE) || (screenX > scrollView.getWidth() - SCROLL_ZONE)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400461 mScrollState = SCROLL_WAITING_IN_ZONE;
462 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
463 } else {
464 mScrollState = SCROLL_OUTSIDE_ZONE;
465 }
466
467 break;
468 case MotionEvent.ACTION_MOVE:
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700469 // Update the drag view. Don't use the clamped pos here so the dragging looks
470 // like it goes off screen a little, intead of bumping up against the edge.
Joe Onorato00acb122009-08-04 16:04:30 -0400471 mDragView.move((int)ev.getRawX(), (int)ev.getRawY());
472
473 // Drop on someone?
474 final int[] coordinates = mCoordinatesTemp;
Romain Guyea3763c2010-01-11 18:02:04 -0800475 DropTarget dropTarget = findDropTarget(screenX, screenY, coordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400476 if (dropTarget != null) {
Patrick Dubroyc1701ad2010-07-16 15:43:04 -0700477 DropTarget delegate = dropTarget.getDropTargetDelegate(
478 mDragSource, coordinates[0], coordinates[1],
479 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
480 if (delegate != null) {
481 dropTarget = delegate;
482 }
483
Joe Onorato00acb122009-08-04 16:04:30 -0400484 if (mLastDropTarget == dropTarget) {
485 dropTarget.onDragOver(mDragSource, coordinates[0], coordinates[1],
486 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
487 } else {
488 if (mLastDropTarget != null) {
489 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
490 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
491 }
492 dropTarget.onDragEnter(mDragSource, coordinates[0], coordinates[1],
493 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
494 }
495 } else {
496 if (mLastDropTarget != null) {
497 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
498 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
499 }
500 }
501 mLastDropTarget = dropTarget;
502
503 // Scroll, maybe, but not if we're in the delete region.
504 boolean inDeleteRegion = false;
505 if (mDeleteRegion != null) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700506 inDeleteRegion = mDeleteRegion.contains(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400507 }
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700508 if (!inDeleteRegion && screenX < SCROLL_ZONE) {
Joe Onorato00acb122009-08-04 16:04:30 -0400509 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
510 mScrollState = SCROLL_WAITING_IN_ZONE;
511 mScrollRunnable.setDirection(SCROLL_LEFT);
512 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
513 }
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700514 } else if (!inDeleteRegion && screenX > scrollView.getWidth() - SCROLL_ZONE) {
Joe Onorato00acb122009-08-04 16:04:30 -0400515 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
516 mScrollState = SCROLL_WAITING_IN_ZONE;
517 mScrollRunnable.setDirection(SCROLL_RIGHT);
518 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
519 }
520 } else {
521 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
522 mScrollState = SCROLL_OUTSIDE_ZONE;
523 mScrollRunnable.setDirection(SCROLL_RIGHT);
524 mHandler.removeCallbacks(mScrollRunnable);
525 }
526 }
527
528 break;
529 case MotionEvent.ACTION_UP:
530 mHandler.removeCallbacks(mScrollRunnable);
531 if (mDragging) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700532 drop(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400533 }
534 endDrag();
535
536 break;
537 case MotionEvent.ACTION_CANCEL:
Joe Onorato24b6fd82009-11-12 13:47:09 -0800538 cancelDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400539 }
540
541 return true;
542 }
543
544 private boolean drop(float x, float y) {
545 final int[] coordinates = mCoordinatesTemp;
546 DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
547
548 if (dropTarget != null) {
549 dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
550 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
551 if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1],
552 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) {
553 dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1],
554 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
555 mDragSource.onDropCompleted((View) dropTarget, true);
556 return true;
557 } else {
558 mDragSource.onDropCompleted((View) dropTarget, false);
559 return true;
560 }
561 }
562 return false;
563 }
564
565 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
566 final Rect r = mRectTemp;
567
568 final ArrayList<DropTarget> dropTargets = mDropTargets;
569 final int count = dropTargets.size();
570 for (int i=count-1; i>=0; i--) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700571 DropTarget target = dropTargets.get(i);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700572 if (!target.isDropEnabled())
573 continue;
574
Joe Onorato00acb122009-08-04 16:04:30 -0400575 target.getHitRect(r);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700576
577 // Convert the hit rect to screen coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400578 target.getLocationOnScreen(dropCoordinates);
579 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
Patrick Dubroy440c3602010-07-13 17:50:32 -0700580
Joe Onorato00acb122009-08-04 16:04:30 -0400581 if (r.contains(x, y)) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700582 DropTarget delegate = target.getDropTargetDelegate(mDragSource,
583 x, y, (int)mTouchOffsetX, (int)mTouchOffsetY, mDragView, mDragInfo);
584 if (delegate != null) {
585 target = delegate;
586 target.getLocationOnScreen(dropCoordinates);
587 }
588
589 // Make dropCoordinates relative to the DropTarget
Joe Onorato00acb122009-08-04 16:04:30 -0400590 dropCoordinates[0] = x - dropCoordinates[0];
591 dropCoordinates[1] = y - dropCoordinates[1];
Patrick Dubroy440c3602010-07-13 17:50:32 -0700592
Joe Onorato00acb122009-08-04 16:04:30 -0400593 return target;
594 }
595 }
596 return null;
597 }
598
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700599 /**
600 * Get the screen size so we can clamp events to the screen size so even if
601 * you drag off the edge of the screen, we find something.
602 */
603 private void recordScreenSize() {
604 ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
605 .getDefaultDisplay().getMetrics(mDisplayMetrics);
606 }
607
608 /**
609 * Clamp val to be &gt;= min and &lt; max.
610 */
611 private static int clamp(int val, int min, int max) {
612 if (val < min) {
613 return min;
614 } else if (val >= max) {
615 return max - 1;
616 } else {
617 return val;
618 }
619 }
620
Joe Onorato00acb122009-08-04 16:04:30 -0400621 public void setDragScoller(DragScroller scroller) {
622 mDragScroller = scroller;
623 }
624
625 public void setWindowToken(IBinder token) {
626 mWindowToken = token;
627 }
628
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 /**
630 * Sets the drag listner which will be notified when a drag starts or ends.
631 */
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700632 public void addDragListener(DragListener l) {
633 mListeners.add(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400634 }
635
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 /**
637 * Remove a previously installed drag listener.
638 */
Joe Onorato00acb122009-08-04 16:04:30 -0400639 public void removeDragListener(DragListener l) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700640 mListeners.remove(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400641 }
642
643 /**
644 * Add a DropTarget to the list of potential places to receive drop events.
645 */
646 public void addDropTarget(DropTarget target) {
647 mDropTargets.add(target);
648 }
649
650 /**
651 * Don't send drop events to <em>target</em> any more.
652 */
653 public void removeDropTarget(DropTarget target) {
654 mDropTargets.remove(target);
655 }
656
657 /**
658 * Set which view scrolls for touch events near the edge of the screen.
659 */
660 public void setScrollView(View v) {
661 mScrollView = v;
662 }
663
664 /**
665 * Specifies the delete region. We won't scroll on touch events over the delete region.
666 *
667 * @param region The rectangle in screen coordinates of the delete region.
668 */
669 void setDeleteRegion(RectF region) {
670 mDeleteRegion = region;
671 }
672
673 private class ScrollRunnable implements Runnable {
674 private int mDirection;
675
676 ScrollRunnable() {
677 }
678
679 public void run() {
680 if (mDragScroller != null) {
681 if (mDirection == SCROLL_LEFT) {
682 mDragScroller.scrollLeft();
683 } else {
684 mDragScroller.scrollRight();
685 }
686 mScrollState = SCROLL_OUTSIDE_ZONE;
687 }
688 }
689
690 void setDirection(int direction) {
691 mDirection = direction;
692 }
693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694}