blob: 68dfb35fccd47d618c03b1693b1214f4684bceb8 [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;
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
Joe Onorato658db742010-09-29 11:40:39 -070036import com.android.launcher.R;
37
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038/**
Joe Onorato00acb122009-08-04 16:04:30 -040039 * Class for initiating a drag within a view or across multiple views.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040 */
Joe Onorato00acb122009-08-04 16:04:30 -040041public class DragController {
Romain Guyea3763c2010-01-11 18:02:04 -080042 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato2e5c4322009-10-06 12:34:42 -070043 private static final String TAG = "Launcher.DragController";
44
Joe Onorato00acb122009-08-04 16:04:30 -040045 /** Indicates the drag is a move. */
46 public static int DRAG_ACTION_MOVE = 0;
47
48 /** Indicates the drag is a copy. */
49 public static int DRAG_ACTION_COPY = 1;
50
51 private static final int SCROLL_DELAY = 600;
Joe Onorato00acb122009-08-04 16:04:30 -040052 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
Patrick Dubroy1262e362010-10-06 15:49:50 -070059 static final int SCROLL_LEFT = 0;
60 static final int SCROLL_RIGHT = 1;
Joe Onorato00acb122009-08-04 16:04:30 -040061
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
Joe Onorato658db742010-09-29 11:40:39 -070091 /** the area at the edge of the screen that makes the workspace go left
92 * or right while you're dragging.
93 */
94 private int mScrollZone;
95
Joe Onorato00acb122009-08-04 16:04:30 -040096 /** Where the drag originated */
97 private DragSource mDragSource;
98
99 /** The data associated with the object being dragged */
100 private Object mDragInfo;
101
102 /** The view that moves around while you drag. */
103 private DragView mDragView;
104
105 /** Who can receive drop events */
106 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
107
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700108 private ArrayList<DragListener> mListeners = new ArrayList<DragListener>();
Joe Onorato00acb122009-08-04 16:04:30 -0400109
110 /** The window token used as the parent for the DragView. */
111 private IBinder mWindowToken;
112
113 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
114 private View mScrollView;
115
Romain Guyea3763c2010-01-11 18:02:04 -0800116 private View mMoveTarget;
117
Joe Onorato00acb122009-08-04 16:04:30 -0400118 private DragScroller mDragScroller;
119 private int mScrollState = SCROLL_OUTSIDE_ZONE;
120 private ScrollRunnable mScrollRunnable = new ScrollRunnable();
121
122 private RectF mDeleteRegion;
123 private DropTarget mLastDropTarget;
124
125 private InputMethodManager mInputMethodManager;
126
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 /**
128 * Interface to receive notifications when a drag starts or stops
129 */
130 interface DragListener {
131
132 /**
133 * A drag has begun
134 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 * @param source An object representing where the drag originated
136 * @param info The data associated with the object that is being dragged
137 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
138 * or {@link DragController#DRAG_ACTION_COPY}
139 */
Joe Onorato5162ea92009-09-03 09:39:42 -0700140 void onDragStart(DragSource source, Object info, int dragAction);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
142 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700143 * The drag has ended
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 */
145 void onDragEnd();
146 }
147
148 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400149 * Used to create a new DragLayer from XML.
150 *
151 * @param context The application's context.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 */
Joe Onorato00acb122009-08-04 16:04:30 -0400153 public DragController(Context context) {
154 mContext = context;
155 mHandler = new Handler();
Joe Onorato658db742010-09-29 11:40:39 -0700156 mScrollZone = context.getResources().getDimensionPixelSize(R.dimen.scroll_zone);
Joe Onorato00acb122009-08-04 16:04:30 -0400157 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Patrick Dubroy1262e362010-10-06 15:49:50 -0700159 public boolean dragging() {
160 return mDragging;
161 }
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 /**
Joe Onorato5162ea92009-09-03 09:39:42 -0700164 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700165 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 * @param v The view that is being dragged
167 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800168 * @param dragInfo The data associated with the object that is being dragged
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
170 * {@link #DRAG_ACTION_COPY}
171 */
Joe Onorato00acb122009-08-04 16:04:30 -0400172 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700173 startDrag(v, source, dragInfo, dragAction, null);
174 }
175
176 /**
177 * Starts a drag.
178 *
179 * @param v The view that is being dragged
180 * @param source An object representing where the drag originated
181 * @param dragInfo The data associated with the object that is being dragged
182 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
183 * {@link #DRAG_ACTION_COPY}
184 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
185 * Makes dragging feel more precise, e.g. you can clip out a transparent border
186 */
187 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction,
188 Rect dragRegion) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700189 mOriginator = v;
190
191 Bitmap b = getViewBitmap(v);
192
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400193 if (b == null) {
194 // out of memory?
195 return;
196 }
197
Joe Onorato5162ea92009-09-03 09:39:42 -0700198 int[] loc = mCoordinatesTemp;
199 v.getLocationOnScreen(loc);
200 int screenX = loc[0];
201 int screenY = loc[1];
202
203 startDrag(b, screenX, screenY, 0, 0, b.getWidth(), b.getHeight(),
Michael Jurkaa63c4522010-08-19 13:52:27 -0700204 source, dragInfo, dragAction, dragRegion);
Joe Onorato5162ea92009-09-03 09:39:42 -0700205
206 b.recycle();
207
208 if (dragAction == DRAG_ACTION_MOVE) {
209 v.setVisibility(View.GONE);
210 }
211 }
212
213 /**
214 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700215 *
Winson Chunge3193b92010-09-10 11:44:42 -0700216 * @param v The view that is being dragged
217 * @param bmp The bitmap that represents the view being dragged
218 * @param source An object representing where the drag originated
219 * @param dragInfo The data associated with the object that is being dragged
220 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
221 * {@link #DRAG_ACTION_COPY}
222 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
223 * Makes dragging feel more precise, e.g. you can clip out a transparent border
224 */
225 public void startDrag(View v, Bitmap bmp, DragSource source, Object dragInfo, int dragAction,
226 Rect dragRegion) {
227 mOriginator = v;
228
229 int[] loc = mCoordinatesTemp;
230 v.getLocationOnScreen(loc);
231 int screenX = loc[0];
232 int screenY = loc[1];
233
234 startDrag(bmp, screenX, screenY, 0, 0, bmp.getWidth(), bmp.getHeight(),
235 source, dragInfo, dragAction, dragRegion);
236
237 if (dragAction == DRAG_ACTION_MOVE) {
238 v.setVisibility(View.GONE);
239 }
240 }
241
242 /**
243 * Starts a drag.
244 *
Joe Onorato5162ea92009-09-03 09:39:42 -0700245 * @param b The bitmap to display as the drag image. It will be re-scaled to the
246 * enlarged size.
247 * @param screenX The x position on screen of the left-top of the bitmap.
248 * @param screenY The y position on screen of the left-top of the bitmap.
249 * @param textureLeft The left edge of the region inside b to use.
250 * @param textureTop The top edge of the region inside b to use.
251 * @param textureWidth The width of the region inside b to use.
252 * @param textureHeight The height of the region inside b to use.
253 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800254 * @param dragInfo The data associated with the object that is being dragged
Joe Onorato5162ea92009-09-03 09:39:42 -0700255 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
256 * {@link #DRAG_ACTION_COPY}
257 */
258 public void startDrag(Bitmap b, int screenX, int screenY,
259 int textureLeft, int textureTop, int textureWidth, int textureHeight,
260 DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700261 startDrag(b, screenX, screenY, textureLeft, textureTop, textureWidth, textureHeight,
262 source, dragInfo, dragAction, null);
263 }
264
265 /**
266 * Starts a drag.
267 *
268 * @param b The bitmap to display as the drag image. It will be re-scaled to the
269 * enlarged size.
270 * @param screenX The x position on screen of the left-top of the bitmap.
271 * @param screenY The y position on screen of the left-top of the bitmap.
272 * @param textureLeft The left edge of the region inside b to use.
273 * @param textureTop The top edge of the region inside b to use.
274 * @param textureWidth The width of the region inside b to use.
275 * @param textureHeight The height of the region inside b to use.
276 * @param source An object representing where the drag originated
277 * @param dragInfo The data associated with the object that is being dragged
278 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
279 * {@link #DRAG_ACTION_COPY}
280 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
281 * Makes dragging feel more precise, e.g. you can clip out a transparent border
282 */
283 public void startDrag(Bitmap b, int screenX, int screenY,
284 int textureLeft, int textureTop, int textureWidth, int textureHeight,
285 DragSource source, Object dragInfo, int dragAction, Rect dragRegion) {
Joe Onorato00acb122009-08-04 16:04:30 -0400286 if (PROFILE_DRAWING_DURING_DRAG) {
287 android.os.Debug.startMethodTracing("Launcher");
288 }
289
290 // Hide soft keyboard, if visible
291 if (mInputMethodManager == null) {
292 mInputMethodManager = (InputMethodManager)
293 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
294 }
295 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
296
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700297 for (DragListener listener : mListeners) {
298 listener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400299 }
300
Joe Onorato00acb122009-08-04 16:04:30 -0400301 int registrationX = ((int)mMotionDownX) - screenX;
302 int registrationY = ((int)mMotionDownY) - screenY;
303
Michael Jurkaa63c4522010-08-19 13:52:27 -0700304 final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left;
305 final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top;
306 mTouchOffsetX = mMotionDownX - screenX - dragRegionLeft;
307 mTouchOffsetY = mMotionDownY - screenY - dragRegionTop;
Joe Onorato00acb122009-08-04 16:04:30 -0400308
309 mDragging = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400310 mDragSource = source;
311 mDragInfo = dragInfo;
312
313 mVibrator.vibrate(VIBRATE_DURATION);
314
Joe Onorato5162ea92009-09-03 09:39:42 -0700315 DragView dragView = mDragView = new DragView(mContext, b, registrationX, registrationY,
316 textureLeft, textureTop, textureWidth, textureHeight);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700317
318 if (dragRegion != null) {
319 dragView.setDragRegion(dragRegionLeft, dragRegion.top,
320 dragRegion.right - dragRegionLeft, dragRegion.bottom - dragRegionTop);
321 }
322
Joe Onorato00acb122009-08-04 16:04:30 -0400323 dragView.show(mWindowToken, (int)mMotionDownX, (int)mMotionDownY);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700324
325 handleMoveEvent((int) mMotionDownX, (int) mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400326 }
327
328 /**
329 * Draw the view into a bitmap.
330 */
331 private Bitmap getViewBitmap(View v) {
332 v.clearFocus();
333 v.setPressed(false);
334
335 boolean willNotCache = v.willNotCacheDrawing();
336 v.setWillNotCacheDrawing(false);
337
338 // Reset the drawing cache background color to fully transparent
339 // for the duration of this operation
340 int color = v.getDrawingCacheBackgroundColor();
341 v.setDrawingCacheBackgroundColor(0);
342
343 if (color != 0) {
344 v.destroyDrawingCache();
345 }
346 v.buildDrawingCache();
347 Bitmap cacheBitmap = v.getDrawingCache();
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400348 if (cacheBitmap == null) {
349 Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
350 return null;
351 }
Joe Onorato00acb122009-08-04 16:04:30 -0400352
353 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
354
355 // Restore the view
356 v.destroyDrawingCache();
357 v.setWillNotCacheDrawing(willNotCache);
358 v.setDrawingCacheBackgroundColor(color);
359
360 return bitmap;
361 }
362
363 /**
364 * Call this from a drag source view like this:
365 *
366 * <pre>
367 * @Override
368 * public boolean dispatchKeyEvent(KeyEvent event) {
369 * return mDragController.dispatchKeyEvent(this, event)
370 * || super.dispatchKeyEvent(event);
371 * </pre>
372 */
Romain Guyea3763c2010-01-11 18:02:04 -0800373 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato00acb122009-08-04 16:04:30 -0400374 public boolean dispatchKeyEvent(KeyEvent event) {
375 return mDragging;
376 }
377
Joe Onorato24b6fd82009-11-12 13:47:09 -0800378 /**
379 * Stop dragging without dropping.
380 */
381 public void cancelDrag() {
382 endDrag();
383 }
384
Joe Onorato00acb122009-08-04 16:04:30 -0400385 private void endDrag() {
386 if (mDragging) {
387 mDragging = false;
388 if (mOriginator != null) {
389 mOriginator.setVisibility(View.VISIBLE);
390 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700391 for (DragListener listener : mListeners) {
392 listener.onDragEnd();
Joe Onorato00acb122009-08-04 16:04:30 -0400393 }
394 if (mDragView != null) {
395 mDragView.remove();
396 mDragView = null;
397 }
Joe Onorato00acb122009-08-04 16:04:30 -0400398 }
399 }
400
401 /**
402 * Call this from a drag source view.
403 */
404 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400405 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800406 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400407 + mDragging);
408 }
Joe Onorato00acb122009-08-04 16:04:30 -0400409 final int action = ev.getAction();
410
Joe Onorato87467d32009-11-08 14:36:43 -0500411 if (action == MotionEvent.ACTION_DOWN) {
412 recordScreenSize();
413 }
414
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700415 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
416 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400417
418 switch (action) {
419 case MotionEvent.ACTION_MOVE:
420 break;
421
422 case MotionEvent.ACTION_DOWN:
423 // Remember location of down touch
424 mMotionDownX = screenX;
425 mMotionDownY = screenY;
426 mLastDropTarget = null;
427 break;
428
429 case MotionEvent.ACTION_CANCEL:
430 case MotionEvent.ACTION_UP:
431 if (mDragging) {
432 drop(screenX, screenY);
433 }
434 endDrag();
435 break;
436 }
437
438 return mDragging;
439 }
440
441 /**
Romain Guyea3763c2010-01-11 18:02:04 -0800442 * Sets the view that should handle move events.
443 */
444 void setMoveTarget(View view) {
445 mMoveTarget = view;
446 }
447
448 public boolean dispatchUnhandledMove(View focused, int direction) {
449 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
450 }
451
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700452 private void handleMoveEvent(int x, int y) {
453 mDragView.move(x, y);
454
455 // Drop on someone?
456 final int[] coordinates = mCoordinatesTemp;
457 DropTarget dropTarget = findDropTarget(x, y, coordinates);
458 if (dropTarget != null) {
459 DropTarget delegate = dropTarget.getDropTargetDelegate(
460 mDragSource, coordinates[0], coordinates[1],
461 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
462 if (delegate != null) {
463 dropTarget = delegate;
464 }
465
466 if (mLastDropTarget != dropTarget) {
467 if (mLastDropTarget != null) {
468 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
469 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
470 }
471 dropTarget.onDragEnter(mDragSource, coordinates[0], coordinates[1],
472 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
473 }
474 dropTarget.onDragOver(mDragSource, coordinates[0], coordinates[1],
475 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
476 } else {
477 if (mLastDropTarget != null) {
478 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
479 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
480 }
481 }
482 mLastDropTarget = dropTarget;
483
484 // Scroll, maybe, but not if we're in the delete region.
485 boolean inDeleteRegion = false;
486 if (mDeleteRegion != null) {
487 inDeleteRegion = mDeleteRegion.contains(x, y);
488 }
Joe Onorato658db742010-09-29 11:40:39 -0700489 if (!inDeleteRegion && x < mScrollZone) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700490 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
491 mScrollState = SCROLL_WAITING_IN_ZONE;
492 mScrollRunnable.setDirection(SCROLL_LEFT);
493 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700494 mDragScroller.onEnterScrollArea(SCROLL_LEFT);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700495 }
Joe Onorato658db742010-09-29 11:40:39 -0700496 } else if (!inDeleteRegion && x > mScrollView.getWidth() - mScrollZone) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700497 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
498 mScrollState = SCROLL_WAITING_IN_ZONE;
499 mScrollRunnable.setDirection(SCROLL_RIGHT);
500 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700501 mDragScroller.onEnterScrollArea(SCROLL_RIGHT);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700502 }
503 } else {
504 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
505 mScrollState = SCROLL_OUTSIDE_ZONE;
506 mScrollRunnable.setDirection(SCROLL_RIGHT);
507 mHandler.removeCallbacks(mScrollRunnable);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700508 mDragScroller.onExitScrollArea();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700509 }
510 }
511 }
512
Romain Guyea3763c2010-01-11 18:02:04 -0800513 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400514 * Call this from a drag source view.
515 */
516 public boolean onTouchEvent(MotionEvent ev) {
Joe Onorato00acb122009-08-04 16:04:30 -0400517 if (!mDragging) {
518 return false;
519 }
520
521 final int action = ev.getAction();
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700522 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
523 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400524
525 switch (action) {
526 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400527 // Remember where the motion event started
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700528 mMotionDownX = screenX;
529 mMotionDownY = screenY;
Joe Onorato00acb122009-08-04 16:04:30 -0400530
Joe Onorato658db742010-09-29 11:40:39 -0700531 if ((screenX < mScrollZone) || (screenX > mScrollView.getWidth() - mScrollZone)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400532 mScrollState = SCROLL_WAITING_IN_ZONE;
533 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
534 } else {
535 mScrollState = SCROLL_OUTSIDE_ZONE;
536 }
Joe Onorato00acb122009-08-04 16:04:30 -0400537 break;
538 case MotionEvent.ACTION_MOVE:
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700539 handleMoveEvent(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400540 break;
541 case MotionEvent.ACTION_UP:
542 mHandler.removeCallbacks(mScrollRunnable);
543 if (mDragging) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700544 drop(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400545 }
546 endDrag();
547
548 break;
549 case MotionEvent.ACTION_CANCEL:
Joe Onorato24b6fd82009-11-12 13:47:09 -0800550 cancelDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400551 }
552
553 return true;
554 }
555
556 private boolean drop(float x, float y) {
557 final int[] coordinates = mCoordinatesTemp;
558 DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
559
560 if (dropTarget != null) {
561 dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
562 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
563 if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1],
564 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) {
565 dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1],
566 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
567 mDragSource.onDropCompleted((View) dropTarget, true);
568 return true;
569 } else {
570 mDragSource.onDropCompleted((View) dropTarget, false);
571 return true;
572 }
573 }
574 return false;
575 }
576
577 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
578 final Rect r = mRectTemp;
579
580 final ArrayList<DropTarget> dropTargets = mDropTargets;
581 final int count = dropTargets.size();
582 for (int i=count-1; i>=0; i--) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700583 DropTarget target = dropTargets.get(i);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700584 if (!target.isDropEnabled())
585 continue;
586
Joe Onorato00acb122009-08-04 16:04:30 -0400587 target.getHitRect(r);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700588
589 // Convert the hit rect to screen coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400590 target.getLocationOnScreen(dropCoordinates);
591 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
Patrick Dubroy440c3602010-07-13 17:50:32 -0700592
Joe Onorato00acb122009-08-04 16:04:30 -0400593 if (r.contains(x, y)) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700594 DropTarget delegate = target.getDropTargetDelegate(mDragSource,
595 x, y, (int)mTouchOffsetX, (int)mTouchOffsetY, mDragView, mDragInfo);
596 if (delegate != null) {
597 target = delegate;
598 target.getLocationOnScreen(dropCoordinates);
599 }
600
601 // Make dropCoordinates relative to the DropTarget
Joe Onorato00acb122009-08-04 16:04:30 -0400602 dropCoordinates[0] = x - dropCoordinates[0];
603 dropCoordinates[1] = y - dropCoordinates[1];
Patrick Dubroy440c3602010-07-13 17:50:32 -0700604
Joe Onorato00acb122009-08-04 16:04:30 -0400605 return target;
606 }
607 }
608 return null;
609 }
610
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700611 /**
612 * Get the screen size so we can clamp events to the screen size so even if
613 * you drag off the edge of the screen, we find something.
614 */
615 private void recordScreenSize() {
616 ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
617 .getDefaultDisplay().getMetrics(mDisplayMetrics);
618 }
619
620 /**
621 * Clamp val to be &gt;= min and &lt; max.
622 */
623 private static int clamp(int val, int min, int max) {
624 if (val < min) {
625 return min;
626 } else if (val >= max) {
627 return max - 1;
628 } else {
629 return val;
630 }
631 }
632
Joe Onorato00acb122009-08-04 16:04:30 -0400633 public void setDragScoller(DragScroller scroller) {
634 mDragScroller = scroller;
635 }
636
637 public void setWindowToken(IBinder token) {
638 mWindowToken = token;
639 }
640
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 /**
642 * Sets the drag listner which will be notified when a drag starts or ends.
643 */
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700644 public void addDragListener(DragListener l) {
645 mListeners.add(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400646 }
647
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 /**
649 * Remove a previously installed drag listener.
650 */
Joe Onorato00acb122009-08-04 16:04:30 -0400651 public void removeDragListener(DragListener l) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700652 mListeners.remove(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400653 }
654
655 /**
656 * Add a DropTarget to the list of potential places to receive drop events.
657 */
658 public void addDropTarget(DropTarget target) {
659 mDropTargets.add(target);
660 }
661
662 /**
663 * Don't send drop events to <em>target</em> any more.
664 */
665 public void removeDropTarget(DropTarget target) {
666 mDropTargets.remove(target);
667 }
668
669 /**
670 * Set which view scrolls for touch events near the edge of the screen.
671 */
672 public void setScrollView(View v) {
673 mScrollView = v;
674 }
675
676 /**
677 * Specifies the delete region. We won't scroll on touch events over the delete region.
678 *
679 * @param region The rectangle in screen coordinates of the delete region.
680 */
681 void setDeleteRegion(RectF region) {
682 mDeleteRegion = region;
683 }
684
685 private class ScrollRunnable implements Runnable {
686 private int mDirection;
687
688 ScrollRunnable() {
689 }
690
691 public void run() {
692 if (mDragScroller != null) {
693 if (mDirection == SCROLL_LEFT) {
694 mDragScroller.scrollLeft();
695 } else {
696 mDragScroller.scrollRight();
697 }
698 mScrollState = SCROLL_OUTSIDE_ZONE;
699 }
700 }
701
702 void setDirection(int direction) {
703 mDirection = direction;
704 }
705 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706}