blob: d54434002fc058a052c2a5c7e442341064f037fd [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
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
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
159 /**
Joe Onorato5162ea92009-09-03 09:39:42 -0700160 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700161 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 * @param v The view that is being dragged
163 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800164 * @param dragInfo The data associated with the object that is being dragged
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
166 * {@link #DRAG_ACTION_COPY}
167 */
Joe Onorato00acb122009-08-04 16:04:30 -0400168 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700169 startDrag(v, source, dragInfo, dragAction, null);
170 }
171
172 /**
173 * Starts a drag.
174 *
175 * @param v The view that is being dragged
176 * @param source An object representing where the drag originated
177 * @param dragInfo The data associated with the object that is being dragged
178 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
179 * {@link #DRAG_ACTION_COPY}
180 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
181 * Makes dragging feel more precise, e.g. you can clip out a transparent border
182 */
183 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction,
184 Rect dragRegion) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700185 mOriginator = v;
186
187 Bitmap b = getViewBitmap(v);
188
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400189 if (b == null) {
190 // out of memory?
191 return;
192 }
193
Joe Onorato5162ea92009-09-03 09:39:42 -0700194 int[] loc = mCoordinatesTemp;
195 v.getLocationOnScreen(loc);
196 int screenX = loc[0];
197 int screenY = loc[1];
198
199 startDrag(b, screenX, screenY, 0, 0, b.getWidth(), b.getHeight(),
Michael Jurkaa63c4522010-08-19 13:52:27 -0700200 source, dragInfo, dragAction, dragRegion);
Joe Onorato5162ea92009-09-03 09:39:42 -0700201
202 b.recycle();
203
204 if (dragAction == DRAG_ACTION_MOVE) {
205 v.setVisibility(View.GONE);
206 }
207 }
208
209 /**
210 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700211 *
Winson Chunge3193b92010-09-10 11:44:42 -0700212 * @param v The view that is being dragged
213 * @param bmp The bitmap that represents the view being dragged
214 * @param source An object representing where the drag originated
215 * @param dragInfo The data associated with the object that is being dragged
216 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
217 * {@link #DRAG_ACTION_COPY}
218 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
219 * Makes dragging feel more precise, e.g. you can clip out a transparent border
220 */
221 public void startDrag(View v, Bitmap bmp, DragSource source, Object dragInfo, int dragAction,
222 Rect dragRegion) {
223 mOriginator = v;
224
225 int[] loc = mCoordinatesTemp;
226 v.getLocationOnScreen(loc);
227 int screenX = loc[0];
228 int screenY = loc[1];
229
230 startDrag(bmp, screenX, screenY, 0, 0, bmp.getWidth(), bmp.getHeight(),
231 source, dragInfo, dragAction, dragRegion);
232
233 if (dragAction == DRAG_ACTION_MOVE) {
234 v.setVisibility(View.GONE);
235 }
236 }
237
238 /**
239 * Starts a drag.
240 *
Joe Onorato5162ea92009-09-03 09:39:42 -0700241 * @param b The bitmap to display as the drag image. It will be re-scaled to the
242 * enlarged size.
243 * @param screenX The x position on screen of the left-top of the bitmap.
244 * @param screenY The y position on screen of the left-top of the bitmap.
245 * @param textureLeft The left edge of the region inside b to use.
246 * @param textureTop The top edge of the region inside b to use.
247 * @param textureWidth The width of the region inside b to use.
248 * @param textureHeight The height of the region inside b to use.
249 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800250 * @param dragInfo The data associated with the object that is being dragged
Joe Onorato5162ea92009-09-03 09:39:42 -0700251 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
252 * {@link #DRAG_ACTION_COPY}
253 */
254 public void startDrag(Bitmap b, int screenX, int screenY,
255 int textureLeft, int textureTop, int textureWidth, int textureHeight,
256 DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700257 startDrag(b, screenX, screenY, textureLeft, textureTop, textureWidth, textureHeight,
258 source, dragInfo, dragAction, null);
259 }
260
261 /**
262 * Starts a drag.
263 *
264 * @param b The bitmap to display as the drag image. It will be re-scaled to the
265 * enlarged size.
266 * @param screenX The x position on screen of the left-top of the bitmap.
267 * @param screenY The y position on screen of the left-top of the bitmap.
268 * @param textureLeft The left edge of the region inside b to use.
269 * @param textureTop The top edge of the region inside b to use.
270 * @param textureWidth The width of the region inside b to use.
271 * @param textureHeight The height of the region inside b to use.
272 * @param source An object representing where the drag originated
273 * @param dragInfo The data associated with the object that is being dragged
274 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
275 * {@link #DRAG_ACTION_COPY}
276 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
277 * Makes dragging feel more precise, e.g. you can clip out a transparent border
278 */
279 public void startDrag(Bitmap b, int screenX, int screenY,
280 int textureLeft, int textureTop, int textureWidth, int textureHeight,
281 DragSource source, Object dragInfo, int dragAction, Rect dragRegion) {
Joe Onorato00acb122009-08-04 16:04:30 -0400282 if (PROFILE_DRAWING_DURING_DRAG) {
283 android.os.Debug.startMethodTracing("Launcher");
284 }
285
286 // Hide soft keyboard, if visible
287 if (mInputMethodManager == null) {
288 mInputMethodManager = (InputMethodManager)
289 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
290 }
291 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
292
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700293 for (DragListener listener : mListeners) {
294 listener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400295 }
296
Joe Onorato00acb122009-08-04 16:04:30 -0400297 int registrationX = ((int)mMotionDownX) - screenX;
298 int registrationY = ((int)mMotionDownY) - screenY;
299
Michael Jurkaa63c4522010-08-19 13:52:27 -0700300 final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left;
301 final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top;
302 mTouchOffsetX = mMotionDownX - screenX - dragRegionLeft;
303 mTouchOffsetY = mMotionDownY - screenY - dragRegionTop;
Joe Onorato00acb122009-08-04 16:04:30 -0400304
305 mDragging = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400306 mDragSource = source;
307 mDragInfo = dragInfo;
308
309 mVibrator.vibrate(VIBRATE_DURATION);
310
Joe Onorato5162ea92009-09-03 09:39:42 -0700311 DragView dragView = mDragView = new DragView(mContext, b, registrationX, registrationY,
312 textureLeft, textureTop, textureWidth, textureHeight);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700313
314 if (dragRegion != null) {
315 dragView.setDragRegion(dragRegionLeft, dragRegion.top,
316 dragRegion.right - dragRegionLeft, dragRegion.bottom - dragRegionTop);
317 }
318
Joe Onorato00acb122009-08-04 16:04:30 -0400319 dragView.show(mWindowToken, (int)mMotionDownX, (int)mMotionDownY);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700320
321 handleMoveEvent((int) mMotionDownX, (int) mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400322 }
323
324 /**
325 * Draw the view into a bitmap.
326 */
327 private Bitmap getViewBitmap(View v) {
328 v.clearFocus();
329 v.setPressed(false);
330
331 boolean willNotCache = v.willNotCacheDrawing();
332 v.setWillNotCacheDrawing(false);
333
334 // Reset the drawing cache background color to fully transparent
335 // for the duration of this operation
336 int color = v.getDrawingCacheBackgroundColor();
337 v.setDrawingCacheBackgroundColor(0);
338
339 if (color != 0) {
340 v.destroyDrawingCache();
341 }
342 v.buildDrawingCache();
343 Bitmap cacheBitmap = v.getDrawingCache();
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400344 if (cacheBitmap == null) {
345 Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
346 return null;
347 }
Joe Onorato00acb122009-08-04 16:04:30 -0400348
349 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
350
351 // Restore the view
352 v.destroyDrawingCache();
353 v.setWillNotCacheDrawing(willNotCache);
354 v.setDrawingCacheBackgroundColor(color);
355
356 return bitmap;
357 }
358
359 /**
360 * Call this from a drag source view like this:
361 *
362 * <pre>
363 * @Override
364 * public boolean dispatchKeyEvent(KeyEvent event) {
365 * return mDragController.dispatchKeyEvent(this, event)
366 * || super.dispatchKeyEvent(event);
367 * </pre>
368 */
Romain Guyea3763c2010-01-11 18:02:04 -0800369 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato00acb122009-08-04 16:04:30 -0400370 public boolean dispatchKeyEvent(KeyEvent event) {
371 return mDragging;
372 }
373
Joe Onorato24b6fd82009-11-12 13:47:09 -0800374 /**
375 * Stop dragging without dropping.
376 */
377 public void cancelDrag() {
378 endDrag();
379 }
380
Joe Onorato00acb122009-08-04 16:04:30 -0400381 private void endDrag() {
382 if (mDragging) {
383 mDragging = false;
384 if (mOriginator != null) {
385 mOriginator.setVisibility(View.VISIBLE);
386 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700387 for (DragListener listener : mListeners) {
388 listener.onDragEnd();
Joe Onorato00acb122009-08-04 16:04:30 -0400389 }
390 if (mDragView != null) {
391 mDragView.remove();
392 mDragView = null;
393 }
Joe Onorato00acb122009-08-04 16:04:30 -0400394 }
395 }
396
397 /**
398 * Call this from a drag source view.
399 */
400 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400401 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800402 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400403 + mDragging);
404 }
Joe Onorato00acb122009-08-04 16:04:30 -0400405 final int action = ev.getAction();
406
Joe Onorato87467d32009-11-08 14:36:43 -0500407 if (action == MotionEvent.ACTION_DOWN) {
408 recordScreenSize();
409 }
410
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700411 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
412 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400413
414 switch (action) {
415 case MotionEvent.ACTION_MOVE:
416 break;
417
418 case MotionEvent.ACTION_DOWN:
419 // Remember location of down touch
420 mMotionDownX = screenX;
421 mMotionDownY = screenY;
422 mLastDropTarget = null;
423 break;
424
425 case MotionEvent.ACTION_CANCEL:
426 case MotionEvent.ACTION_UP:
427 if (mDragging) {
428 drop(screenX, screenY);
429 }
430 endDrag();
431 break;
432 }
433
434 return mDragging;
435 }
436
437 /**
Romain Guyea3763c2010-01-11 18:02:04 -0800438 * Sets the view that should handle move events.
439 */
440 void setMoveTarget(View view) {
441 mMoveTarget = view;
442 }
443
444 public boolean dispatchUnhandledMove(View focused, int direction) {
445 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
446 }
447
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700448 private void handleMoveEvent(int x, int y) {
449 mDragView.move(x, y);
450
451 // Drop on someone?
452 final int[] coordinates = mCoordinatesTemp;
453 DropTarget dropTarget = findDropTarget(x, y, coordinates);
454 if (dropTarget != null) {
455 DropTarget delegate = dropTarget.getDropTargetDelegate(
456 mDragSource, coordinates[0], coordinates[1],
457 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
458 if (delegate != null) {
459 dropTarget = delegate;
460 }
461
462 if (mLastDropTarget != dropTarget) {
463 if (mLastDropTarget != null) {
464 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
465 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
466 }
467 dropTarget.onDragEnter(mDragSource, coordinates[0], coordinates[1],
468 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
469 }
470 dropTarget.onDragOver(mDragSource, coordinates[0], coordinates[1],
471 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
472 } else {
473 if (mLastDropTarget != null) {
474 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
475 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
476 }
477 }
478 mLastDropTarget = dropTarget;
479
480 // Scroll, maybe, but not if we're in the delete region.
481 boolean inDeleteRegion = false;
482 if (mDeleteRegion != null) {
483 inDeleteRegion = mDeleteRegion.contains(x, y);
484 }
Joe Onorato658db742010-09-29 11:40:39 -0700485 if (!inDeleteRegion && x < mScrollZone) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700486 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
487 mScrollState = SCROLL_WAITING_IN_ZONE;
488 mScrollRunnable.setDirection(SCROLL_LEFT);
489 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
490 }
Joe Onorato658db742010-09-29 11:40:39 -0700491 } else if (!inDeleteRegion && x > mScrollView.getWidth() - mScrollZone) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700492 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
493 mScrollState = SCROLL_WAITING_IN_ZONE;
494 mScrollRunnable.setDirection(SCROLL_RIGHT);
495 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
496 }
497 } else {
498 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
499 mScrollState = SCROLL_OUTSIDE_ZONE;
500 mScrollRunnable.setDirection(SCROLL_RIGHT);
501 mHandler.removeCallbacks(mScrollRunnable);
502 }
503 }
504 }
505
Romain Guyea3763c2010-01-11 18:02:04 -0800506 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400507 * Call this from a drag source view.
508 */
509 public boolean onTouchEvent(MotionEvent ev) {
Joe Onorato00acb122009-08-04 16:04:30 -0400510 if (!mDragging) {
511 return false;
512 }
513
514 final int action = ev.getAction();
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700515 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
516 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400517
518 switch (action) {
519 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400520 // Remember where the motion event started
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700521 mMotionDownX = screenX;
522 mMotionDownY = screenY;
Joe Onorato00acb122009-08-04 16:04:30 -0400523
Joe Onorato658db742010-09-29 11:40:39 -0700524 if ((screenX < mScrollZone) || (screenX > mScrollView.getWidth() - mScrollZone)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400525 mScrollState = SCROLL_WAITING_IN_ZONE;
526 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
527 } else {
528 mScrollState = SCROLL_OUTSIDE_ZONE;
529 }
Joe Onorato00acb122009-08-04 16:04:30 -0400530 break;
531 case MotionEvent.ACTION_MOVE:
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700532 handleMoveEvent(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400533 break;
534 case MotionEvent.ACTION_UP:
535 mHandler.removeCallbacks(mScrollRunnable);
536 if (mDragging) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700537 drop(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400538 }
539 endDrag();
540
541 break;
542 case MotionEvent.ACTION_CANCEL:
Joe Onorato24b6fd82009-11-12 13:47:09 -0800543 cancelDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400544 }
545
546 return true;
547 }
548
549 private boolean drop(float x, float y) {
550 final int[] coordinates = mCoordinatesTemp;
551 DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
552
553 if (dropTarget != null) {
554 dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
555 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
556 if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1],
557 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) {
558 dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1],
559 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
560 mDragSource.onDropCompleted((View) dropTarget, true);
561 return true;
562 } else {
563 mDragSource.onDropCompleted((View) dropTarget, false);
564 return true;
565 }
566 }
567 return false;
568 }
569
570 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
571 final Rect r = mRectTemp;
572
573 final ArrayList<DropTarget> dropTargets = mDropTargets;
574 final int count = dropTargets.size();
575 for (int i=count-1; i>=0; i--) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700576 DropTarget target = dropTargets.get(i);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700577 if (!target.isDropEnabled())
578 continue;
579
Joe Onorato00acb122009-08-04 16:04:30 -0400580 target.getHitRect(r);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700581
582 // Convert the hit rect to screen coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400583 target.getLocationOnScreen(dropCoordinates);
584 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
Patrick Dubroy440c3602010-07-13 17:50:32 -0700585
Joe Onorato00acb122009-08-04 16:04:30 -0400586 if (r.contains(x, y)) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700587 DropTarget delegate = target.getDropTargetDelegate(mDragSource,
588 x, y, (int)mTouchOffsetX, (int)mTouchOffsetY, mDragView, mDragInfo);
589 if (delegate != null) {
590 target = delegate;
591 target.getLocationOnScreen(dropCoordinates);
592 }
593
594 // Make dropCoordinates relative to the DropTarget
Joe Onorato00acb122009-08-04 16:04:30 -0400595 dropCoordinates[0] = x - dropCoordinates[0];
596 dropCoordinates[1] = y - dropCoordinates[1];
Patrick Dubroy440c3602010-07-13 17:50:32 -0700597
Joe Onorato00acb122009-08-04 16:04:30 -0400598 return target;
599 }
600 }
601 return null;
602 }
603
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700604 /**
605 * Get the screen size so we can clamp events to the screen size so even if
606 * you drag off the edge of the screen, we find something.
607 */
608 private void recordScreenSize() {
609 ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
610 .getDefaultDisplay().getMetrics(mDisplayMetrics);
611 }
612
613 /**
614 * Clamp val to be &gt;= min and &lt; max.
615 */
616 private static int clamp(int val, int min, int max) {
617 if (val < min) {
618 return min;
619 } else if (val >= max) {
620 return max - 1;
621 } else {
622 return val;
623 }
624 }
625
Joe Onorato00acb122009-08-04 16:04:30 -0400626 public void setDragScoller(DragScroller scroller) {
627 mDragScroller = scroller;
628 }
629
630 public void setWindowToken(IBinder token) {
631 mWindowToken = token;
632 }
633
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 /**
635 * Sets the drag listner which will be notified when a drag starts or ends.
636 */
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700637 public void addDragListener(DragListener l) {
638 mListeners.add(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400639 }
640
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 /**
642 * Remove a previously installed drag listener.
643 */
Joe Onorato00acb122009-08-04 16:04:30 -0400644 public void removeDragListener(DragListener l) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700645 mListeners.remove(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400646 }
647
648 /**
649 * Add a DropTarget to the list of potential places to receive drop events.
650 */
651 public void addDropTarget(DropTarget target) {
652 mDropTargets.add(target);
653 }
654
655 /**
656 * Don't send drop events to <em>target</em> any more.
657 */
658 public void removeDropTarget(DropTarget target) {
659 mDropTargets.remove(target);
660 }
661
662 /**
663 * Set which view scrolls for touch events near the edge of the screen.
664 */
665 public void setScrollView(View v) {
666 mScrollView = v;
667 }
668
669 /**
670 * Specifies the delete region. We won't scroll on touch events over the delete region.
671 *
672 * @param region The rectangle in screen coordinates of the delete region.
673 */
674 void setDeleteRegion(RectF region) {
675 mDeleteRegion = region;
676 }
677
678 private class ScrollRunnable implements Runnable {
679 private int mDirection;
680
681 ScrollRunnable() {
682 }
683
684 public void run() {
685 if (mDragScroller != null) {
686 if (mDirection == SCROLL_LEFT) {
687 mDragScroller.scrollLeft();
688 } else {
689 mDragScroller.scrollRight();
690 }
691 mScrollState = SCROLL_OUTSIDE_ZONE;
692 }
693 }
694
695 void setDirection(int direction) {
696 mDirection = direction;
697 }
698 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699}