blob: b45603094b9dc78fb21edcd01ac2354bcfbdad2e [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
Adam Cohen120980b2010-12-08 11:05:37 -080019import java.util.ArrayList;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -070020
Joe Onorato00acb122009-08-04 16:04:30 -040021import android.content.Context;
22import android.graphics.Bitmap;
Joe Onorato00acb122009-08-04 16:04:30 -040023import android.graphics.Rect;
24import android.graphics.RectF;
Joe Onorato00acb122009-08-04 16:04:30 -040025import android.os.Handler;
Michael Jurka0280c3b2010-09-17 15:00:07 -070026import android.os.IBinder;
Joe Onorato00acb122009-08-04 16:04:30 -040027import android.os.Vibrator;
Joe Onoratoe048e8a2009-09-25 10:39:17 -070028import android.util.DisplayMetrics;
Joe Onorato00acb122009-08-04 16:04:30 -040029import android.util.Log;
Joe Onorato00acb122009-08-04 16:04:30 -040030import android.view.KeyEvent;
31import android.view.MotionEvent;
Michael Jurka0280c3b2010-09-17 15:00:07 -070032import android.view.View;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -070033import android.view.ViewConfiguration;
Joe Onoratoe048e8a2009-09-25 10:39:17 -070034import android.view.WindowManager;
Joe Onorato00acb122009-08-04 16:04:30 -040035import android.view.inputmethod.InputMethodManager;
Joe Onorato00acb122009-08-04 16:04:30 -040036
Adam Cohen120980b2010-12-08 11:05:37 -080037import com.android.launcher.R;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038
39/**
Joe Onorato00acb122009-08-04 16:04:30 -040040 * Class for initiating a drag within a view or across multiple views.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041 */
Joe Onorato00acb122009-08-04 16:04:30 -040042public class DragController {
Romain Guyea3763c2010-01-11 18:02:04 -080043 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato2e5c4322009-10-06 12:34:42 -070044 private static final String TAG = "Launcher.DragController";
45
Joe Onorato00acb122009-08-04 16:04:30 -040046 /** Indicates the drag is a move. */
47 public static int DRAG_ACTION_MOVE = 0;
48
49 /** Indicates the drag is a copy. */
50 public static int DRAG_ACTION_COPY = 1;
51
52 private static final int SCROLL_DELAY = 600;
Joe Onorato00acb122009-08-04 16:04:30 -040053 private static final int VIBRATE_DURATION = 35;
54
55 private static final boolean PROFILE_DRAWING_DURING_DRAG = false;
56
57 private static final int SCROLL_OUTSIDE_ZONE = 0;
58 private static final int SCROLL_WAITING_IN_ZONE = 1;
59
Patrick Dubroy54fa3b92010-11-17 12:18:45 -080060 static final int SCROLL_NONE = -1;
Patrick Dubroy1262e362010-10-06 15:49:50 -070061 static final int SCROLL_LEFT = 0;
62 static final int SCROLL_RIGHT = 1;
Joe Onorato00acb122009-08-04 16:04:30 -040063
64 private Context mContext;
65 private Handler mHandler;
66 private final Vibrator mVibrator = new Vibrator();
67
68 // temporaries to avoid gc thrash
69 private Rect mRectTemp = new Rect();
70 private final int[] mCoordinatesTemp = new int[2];
71
72 /** Whether or not we're dragging. */
73 private boolean mDragging;
74
75 /** X coordinate of the down event. */
76 private float mMotionDownX;
77
78 /** Y coordinate of the down event. */
79 private float mMotionDownY;
80
Joe Onoratoe048e8a2009-09-25 10:39:17 -070081 /** Info about the screen for clamping. */
82 private DisplayMetrics mDisplayMetrics = new DisplayMetrics();
83
Joe Onorato00acb122009-08-04 16:04:30 -040084 /** Original view that is being dragged. */
85 private View mOriginator;
86
Joe Onorato00acb122009-08-04 16:04:30 -040087 /** X offset from the upper-left corner of the cell to where we touched. */
88 private float mTouchOffsetX;
89
90 /** Y offset from the upper-left corner of the cell to where we touched. */
91 private float mTouchOffsetY;
92
Joe Onorato658db742010-09-29 11:40:39 -070093 /** the area at the edge of the screen that makes the workspace go left
94 * or right while you're dragging.
95 */
96 private int mScrollZone;
97
Joe Onorato00acb122009-08-04 16:04:30 -040098 /** Where the drag originated */
99 private DragSource mDragSource;
100
101 /** The data associated with the object being dragged */
102 private Object mDragInfo;
103
104 /** The view that moves around while you drag. */
105 private DragView mDragView;
106
107 /** Who can receive drop events */
108 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
109
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700110 private ArrayList<DragListener> mListeners = new ArrayList<DragListener>();
Joe Onorato00acb122009-08-04 16:04:30 -0400111
112 /** The window token used as the parent for the DragView. */
113 private IBinder mWindowToken;
114
115 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
116 private View mScrollView;
117
Romain Guyea3763c2010-01-11 18:02:04 -0800118 private View mMoveTarget;
119
Joe Onorato00acb122009-08-04 16:04:30 -0400120 private DragScroller mDragScroller;
121 private int mScrollState = SCROLL_OUTSIDE_ZONE;
122 private ScrollRunnable mScrollRunnable = new ScrollRunnable();
123
124 private RectF mDeleteRegion;
125 private DropTarget mLastDropTarget;
126
127 private InputMethodManager mInputMethodManager;
128
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700129 private int mLastTouch[] = new int[2];
130 private int mDistanceSinceScroll = 0;
131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 /**
133 * Interface to receive notifications when a drag starts or stops
134 */
135 interface DragListener {
136
137 /**
138 * A drag has begun
139 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 * @param source An object representing where the drag originated
141 * @param info The data associated with the object that is being dragged
142 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
143 * or {@link DragController#DRAG_ACTION_COPY}
144 */
Joe Onorato5162ea92009-09-03 09:39:42 -0700145 void onDragStart(DragSource source, Object info, int dragAction);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
147 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700148 * The drag has ended
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 */
150 void onDragEnd();
151 }
152
153 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400154 * Used to create a new DragLayer from XML.
155 *
156 * @param context The application's context.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 */
Joe Onorato00acb122009-08-04 16:04:30 -0400158 public DragController(Context context) {
159 mContext = context;
160 mHandler = new Handler();
Joe Onorato658db742010-09-29 11:40:39 -0700161 mScrollZone = context.getResources().getDimensionPixelSize(R.dimen.scroll_zone);
Joe Onorato00acb122009-08-04 16:04:30 -0400162 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Patrick Dubroy1262e362010-10-06 15:49:50 -0700164 public boolean dragging() {
165 return mDragging;
166 }
167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 /**
Joe Onorato5162ea92009-09-03 09:39:42 -0700169 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700170 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 * @param v The view that is being dragged
172 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800173 * @param dragInfo The data associated with the object that is being dragged
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
175 * {@link #DRAG_ACTION_COPY}
176 */
Joe Onorato00acb122009-08-04 16:04:30 -0400177 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700178 startDrag(v, source, dragInfo, dragAction, null);
179 }
180
181 /**
182 * Starts a drag.
183 *
184 * @param v The view that is being dragged
185 * @param source An object representing where the drag originated
186 * @param dragInfo The data associated with the object that is being dragged
187 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
188 * {@link #DRAG_ACTION_COPY}
189 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
190 * Makes dragging feel more precise, e.g. you can clip out a transparent border
191 */
192 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction,
193 Rect dragRegion) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700194 mOriginator = v;
195
196 Bitmap b = getViewBitmap(v);
197
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400198 if (b == null) {
199 // out of memory?
200 return;
201 }
202
Joe Onorato5162ea92009-09-03 09:39:42 -0700203 int[] loc = mCoordinatesTemp;
204 v.getLocationOnScreen(loc);
205 int screenX = loc[0];
206 int screenY = loc[1];
207
208 startDrag(b, screenX, screenY, 0, 0, b.getWidth(), b.getHeight(),
Michael Jurkaa63c4522010-08-19 13:52:27 -0700209 source, dragInfo, dragAction, dragRegion);
Joe Onorato5162ea92009-09-03 09:39:42 -0700210
211 b.recycle();
212
213 if (dragAction == DRAG_ACTION_MOVE) {
214 v.setVisibility(View.GONE);
215 }
216 }
217
218 /**
219 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700220 *
Winson Chunge3193b92010-09-10 11:44:42 -0700221 * @param v The view that is being dragged
222 * @param bmp The bitmap that represents the view being dragged
223 * @param source An object representing where the drag originated
224 * @param dragInfo The data associated with the object that is being dragged
225 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
226 * {@link #DRAG_ACTION_COPY}
227 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
228 * Makes dragging feel more precise, e.g. you can clip out a transparent border
229 */
230 public void startDrag(View v, Bitmap bmp, DragSource source, Object dragInfo, int dragAction,
231 Rect dragRegion) {
232 mOriginator = v;
233
234 int[] loc = mCoordinatesTemp;
235 v.getLocationOnScreen(loc);
236 int screenX = loc[0];
237 int screenY = loc[1];
238
239 startDrag(bmp, screenX, screenY, 0, 0, bmp.getWidth(), bmp.getHeight(),
240 source, dragInfo, dragAction, dragRegion);
241
242 if (dragAction == DRAG_ACTION_MOVE) {
243 v.setVisibility(View.GONE);
244 }
245 }
246
247 /**
248 * Starts a drag.
249 *
Joe Onorato5162ea92009-09-03 09:39:42 -0700250 * @param b The bitmap to display as the drag image. It will be re-scaled to the
251 * enlarged size.
252 * @param screenX The x position on screen of the left-top of the bitmap.
253 * @param screenY The y position on screen of the left-top of the bitmap.
254 * @param textureLeft The left edge of the region inside b to use.
255 * @param textureTop The top edge of the region inside b to use.
256 * @param textureWidth The width of the region inside b to use.
257 * @param textureHeight The height of the region inside b to use.
258 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800259 * @param dragInfo The data associated with the object that is being dragged
Joe Onorato5162ea92009-09-03 09:39:42 -0700260 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
261 * {@link #DRAG_ACTION_COPY}
262 */
263 public void startDrag(Bitmap b, int screenX, int screenY,
264 int textureLeft, int textureTop, int textureWidth, int textureHeight,
265 DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700266 startDrag(b, screenX, screenY, textureLeft, textureTop, textureWidth, textureHeight,
267 source, dragInfo, dragAction, null);
268 }
269
270 /**
271 * Starts a drag.
272 *
273 * @param b The bitmap to display as the drag image. It will be re-scaled to the
274 * enlarged size.
275 * @param screenX The x position on screen of the left-top of the bitmap.
276 * @param screenY The y position on screen of the left-top of the bitmap.
277 * @param textureLeft The left edge of the region inside b to use.
278 * @param textureTop The top edge of the region inside b to use.
279 * @param textureWidth The width of the region inside b to use.
280 * @param textureHeight The height of the region inside b to use.
281 * @param source An object representing where the drag originated
282 * @param dragInfo The data associated with the object that is being dragged
283 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
284 * {@link #DRAG_ACTION_COPY}
285 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
286 * Makes dragging feel more precise, e.g. you can clip out a transparent border
287 */
288 public void startDrag(Bitmap b, int screenX, int screenY,
289 int textureLeft, int textureTop, int textureWidth, int textureHeight,
290 DragSource source, Object dragInfo, int dragAction, Rect dragRegion) {
Joe Onorato00acb122009-08-04 16:04:30 -0400291 if (PROFILE_DRAWING_DURING_DRAG) {
292 android.os.Debug.startMethodTracing("Launcher");
293 }
294
295 // Hide soft keyboard, if visible
296 if (mInputMethodManager == null) {
297 mInputMethodManager = (InputMethodManager)
298 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
299 }
300 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
301
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700302 for (DragListener listener : mListeners) {
303 listener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400304 }
305
Joe Onorato00acb122009-08-04 16:04:30 -0400306 int registrationX = ((int)mMotionDownX) - screenX;
307 int registrationY = ((int)mMotionDownY) - screenY;
308
Michael Jurkaa63c4522010-08-19 13:52:27 -0700309 final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left;
310 final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top;
311 mTouchOffsetX = mMotionDownX - screenX - dragRegionLeft;
312 mTouchOffsetY = mMotionDownY - screenY - dragRegionTop;
Joe Onorato00acb122009-08-04 16:04:30 -0400313
314 mDragging = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400315 mDragSource = source;
316 mDragInfo = dragInfo;
317
318 mVibrator.vibrate(VIBRATE_DURATION);
319
Joe Onorato5162ea92009-09-03 09:39:42 -0700320 DragView dragView = mDragView = new DragView(mContext, b, registrationX, registrationY,
321 textureLeft, textureTop, textureWidth, textureHeight);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700322
Patrick Dubroya669d792010-11-23 14:40:33 -0800323 final DragSource dragSource = source;
324 dragView.setOnDrawRunnable(new Runnable() {
325 public void run() {
326 dragSource.onDragViewVisible();
327 };
328 });
329
Michael Jurkaa63c4522010-08-19 13:52:27 -0700330 if (dragRegion != null) {
331 dragView.setDragRegion(dragRegionLeft, dragRegion.top,
332 dragRegion.right - dragRegionLeft, dragRegion.bottom - dragRegionTop);
333 }
334
Joe Onorato00acb122009-08-04 16:04:30 -0400335 dragView.show(mWindowToken, (int)mMotionDownX, (int)mMotionDownY);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700336
337 handleMoveEvent((int) mMotionDownX, (int) mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400338 }
339
340 /**
341 * Draw the view into a bitmap.
342 */
Adam Cohen120980b2010-12-08 11:05:37 -0800343 Bitmap getViewBitmap(View v) {
Joe Onorato00acb122009-08-04 16:04:30 -0400344 v.clearFocus();
345 v.setPressed(false);
346
347 boolean willNotCache = v.willNotCacheDrawing();
348 v.setWillNotCacheDrawing(false);
349
350 // Reset the drawing cache background color to fully transparent
351 // for the duration of this operation
352 int color = v.getDrawingCacheBackgroundColor();
353 v.setDrawingCacheBackgroundColor(0);
Adam Cohen120980b2010-12-08 11:05:37 -0800354 float alpha = v.getAlpha();
355 v.setAlpha(1.0f);
Joe Onorato00acb122009-08-04 16:04:30 -0400356
357 if (color != 0) {
358 v.destroyDrawingCache();
359 }
360 v.buildDrawingCache();
361 Bitmap cacheBitmap = v.getDrawingCache();
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400362 if (cacheBitmap == null) {
363 Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
364 return null;
365 }
Joe Onorato00acb122009-08-04 16:04:30 -0400366
367 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
368
369 // Restore the view
370 v.destroyDrawingCache();
Adam Cohen120980b2010-12-08 11:05:37 -0800371 v.setAlpha(alpha);
Joe Onorato00acb122009-08-04 16:04:30 -0400372 v.setWillNotCacheDrawing(willNotCache);
373 v.setDrawingCacheBackgroundColor(color);
374
375 return bitmap;
376 }
377
378 /**
379 * Call this from a drag source view like this:
380 *
381 * <pre>
382 * @Override
383 * public boolean dispatchKeyEvent(KeyEvent event) {
384 * return mDragController.dispatchKeyEvent(this, event)
385 * || super.dispatchKeyEvent(event);
386 * </pre>
387 */
Romain Guyea3763c2010-01-11 18:02:04 -0800388 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato00acb122009-08-04 16:04:30 -0400389 public boolean dispatchKeyEvent(KeyEvent event) {
390 return mDragging;
391 }
392
Winson Chung304dcde2011-01-07 11:17:23 -0800393 public boolean isDragging() {
394 return mDragging;
395 }
396
Joe Onorato24b6fd82009-11-12 13:47:09 -0800397 /**
398 * Stop dragging without dropping.
399 */
400 public void cancelDrag() {
Winson Chung621e6402011-01-04 16:03:57 -0800401 if (mDragging) {
402 mDragSource.onDropCompleted(null, false);
403 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800404 endDrag();
405 }
406
Joe Onorato00acb122009-08-04 16:04:30 -0400407 private void endDrag() {
408 if (mDragging) {
409 mDragging = false;
410 if (mOriginator != null) {
411 mOriginator.setVisibility(View.VISIBLE);
412 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700413 for (DragListener listener : mListeners) {
414 listener.onDragEnd();
Joe Onorato00acb122009-08-04 16:04:30 -0400415 }
416 if (mDragView != null) {
417 mDragView.remove();
418 mDragView = null;
419 }
Joe Onorato00acb122009-08-04 16:04:30 -0400420 }
421 }
422
423 /**
424 * Call this from a drag source view.
425 */
426 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400427 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800428 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400429 + mDragging);
430 }
Joe Onorato00acb122009-08-04 16:04:30 -0400431 final int action = ev.getAction();
432
Joe Onorato87467d32009-11-08 14:36:43 -0500433 if (action == MotionEvent.ACTION_DOWN) {
434 recordScreenSize();
435 }
436
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700437 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
438 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400439
440 switch (action) {
441 case MotionEvent.ACTION_MOVE:
442 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400443 case MotionEvent.ACTION_DOWN:
444 // Remember location of down touch
445 mMotionDownX = screenX;
446 mMotionDownY = screenY;
447 mLastDropTarget = null;
448 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400449 case MotionEvent.ACTION_UP:
450 if (mDragging) {
451 drop(screenX, screenY);
452 }
453 endDrag();
454 break;
Winson Chung621e6402011-01-04 16:03:57 -0800455 case MotionEvent.ACTION_CANCEL:
456 cancelDrag();
457 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400458 }
459
460 return mDragging;
461 }
462
463 /**
Romain Guyea3763c2010-01-11 18:02:04 -0800464 * Sets the view that should handle move events.
465 */
466 void setMoveTarget(View view) {
467 mMoveTarget = view;
468 }
469
470 public boolean dispatchUnhandledMove(View focused, int direction) {
471 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
472 }
473
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700474 private void handleMoveEvent(int x, int y) {
475 mDragView.move(x, y);
476
477 // Drop on someone?
478 final int[] coordinates = mCoordinatesTemp;
479 DropTarget dropTarget = findDropTarget(x, y, coordinates);
480 if (dropTarget != null) {
481 DropTarget delegate = dropTarget.getDropTargetDelegate(
482 mDragSource, coordinates[0], coordinates[1],
483 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
484 if (delegate != null) {
485 dropTarget = delegate;
486 }
487
488 if (mLastDropTarget != dropTarget) {
489 if (mLastDropTarget != null) {
490 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
491 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
492 }
493 dropTarget.onDragEnter(mDragSource, coordinates[0], coordinates[1],
494 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
495 }
496 dropTarget.onDragOver(mDragSource, coordinates[0], coordinates[1],
497 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
498 } else {
499 if (mLastDropTarget != null) {
500 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
501 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
502 }
503 }
504 mLastDropTarget = dropTarget;
505
506 // Scroll, maybe, but not if we're in the delete region.
507 boolean inDeleteRegion = false;
508 if (mDeleteRegion != null) {
509 inDeleteRegion = mDeleteRegion.contains(x, y);
510 }
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700511
512 // After a scroll, the touch point will still be in the scroll region.
513 // Rather than scrolling immediately, require a bit of twiddling to scroll again
514 final int slop = ViewConfiguration.get(mContext).getScaledWindowTouchSlop();
515 mDistanceSinceScroll +=
516 Math.sqrt(Math.pow(mLastTouch[0] - x, 2) + Math.pow(mLastTouch[1] - y, 2));
517 mLastTouch[0] = x;
518 mLastTouch[1] = y;
519
Joe Onorato658db742010-09-29 11:40:39 -0700520 if (!inDeleteRegion && x < mScrollZone) {
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700521 if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700522 mScrollState = SCROLL_WAITING_IN_ZONE;
523 mScrollRunnable.setDirection(SCROLL_LEFT);
524 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700525 mDragScroller.onEnterScrollArea(SCROLL_LEFT);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700526 }
Joe Onorato658db742010-09-29 11:40:39 -0700527 } else if (!inDeleteRegion && x > mScrollView.getWidth() - mScrollZone) {
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700528 if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700529 mScrollState = SCROLL_WAITING_IN_ZONE;
530 mScrollRunnable.setDirection(SCROLL_RIGHT);
531 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700532 mDragScroller.onEnterScrollArea(SCROLL_RIGHT);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700533 }
534 } else {
535 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
536 mScrollState = SCROLL_OUTSIDE_ZONE;
537 mScrollRunnable.setDirection(SCROLL_RIGHT);
538 mHandler.removeCallbacks(mScrollRunnable);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700539 mDragScroller.onExitScrollArea();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700540 }
541 }
542 }
543
Romain Guyea3763c2010-01-11 18:02:04 -0800544 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400545 * Call this from a drag source view.
546 */
547 public boolean onTouchEvent(MotionEvent ev) {
Joe Onorato00acb122009-08-04 16:04:30 -0400548 if (!mDragging) {
549 return false;
550 }
551
552 final int action = ev.getAction();
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700553 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
554 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400555
556 switch (action) {
557 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400558 // Remember where the motion event started
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700559 mMotionDownX = screenX;
560 mMotionDownY = screenY;
Joe Onorato00acb122009-08-04 16:04:30 -0400561
Joe Onorato658db742010-09-29 11:40:39 -0700562 if ((screenX < mScrollZone) || (screenX > mScrollView.getWidth() - mScrollZone)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400563 mScrollState = SCROLL_WAITING_IN_ZONE;
564 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
565 } else {
566 mScrollState = SCROLL_OUTSIDE_ZONE;
567 }
Joe Onorato00acb122009-08-04 16:04:30 -0400568 break;
569 case MotionEvent.ACTION_MOVE:
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700570 handleMoveEvent(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400571 break;
572 case MotionEvent.ACTION_UP:
573 mHandler.removeCallbacks(mScrollRunnable);
574 if (mDragging) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700575 drop(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400576 }
577 endDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400578 break;
579 case MotionEvent.ACTION_CANCEL:
Joe Onorato24b6fd82009-11-12 13:47:09 -0800580 cancelDrag();
Winson Chung621e6402011-01-04 16:03:57 -0800581 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400582 }
583
584 return true;
585 }
586
587 private boolean drop(float x, float y) {
588 final int[] coordinates = mCoordinatesTemp;
589 DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
590
591 if (dropTarget != null) {
592 dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
593 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
594 if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1],
595 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) {
596 dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1],
597 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
598 mDragSource.onDropCompleted((View) dropTarget, true);
599 return true;
600 } else {
601 mDragSource.onDropCompleted((View) dropTarget, false);
602 return true;
603 }
Winson Chunga34abf82010-11-12 12:10:35 -0800604 } else {
605 mDragSource.onDropCompleted(null, false);
Joe Onorato00acb122009-08-04 16:04:30 -0400606 }
607 return false;
608 }
609
610 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
611 final Rect r = mRectTemp;
612
613 final ArrayList<DropTarget> dropTargets = mDropTargets;
614 final int count = dropTargets.size();
615 for (int i=count-1; i>=0; i--) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700616 DropTarget target = dropTargets.get(i);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700617 if (!target.isDropEnabled())
618 continue;
619
Joe Onorato00acb122009-08-04 16:04:30 -0400620 target.getHitRect(r);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700621
622 // Convert the hit rect to screen coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400623 target.getLocationOnScreen(dropCoordinates);
624 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
Patrick Dubroy440c3602010-07-13 17:50:32 -0700625
Joe Onorato00acb122009-08-04 16:04:30 -0400626 if (r.contains(x, y)) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700627 DropTarget delegate = target.getDropTargetDelegate(mDragSource,
628 x, y, (int)mTouchOffsetX, (int)mTouchOffsetY, mDragView, mDragInfo);
629 if (delegate != null) {
630 target = delegate;
631 target.getLocationOnScreen(dropCoordinates);
632 }
633
634 // Make dropCoordinates relative to the DropTarget
Joe Onorato00acb122009-08-04 16:04:30 -0400635 dropCoordinates[0] = x - dropCoordinates[0];
636 dropCoordinates[1] = y - dropCoordinates[1];
Patrick Dubroy440c3602010-07-13 17:50:32 -0700637
Joe Onorato00acb122009-08-04 16:04:30 -0400638 return target;
639 }
640 }
641 return null;
642 }
643
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700644 /**
645 * Get the screen size so we can clamp events to the screen size so even if
646 * you drag off the edge of the screen, we find something.
647 */
648 private void recordScreenSize() {
649 ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
650 .getDefaultDisplay().getMetrics(mDisplayMetrics);
651 }
652
653 /**
654 * Clamp val to be &gt;= min and &lt; max.
655 */
656 private static int clamp(int val, int min, int max) {
657 if (val < min) {
658 return min;
659 } else if (val >= max) {
660 return max - 1;
661 } else {
662 return val;
663 }
664 }
665
Joe Onorato00acb122009-08-04 16:04:30 -0400666 public void setDragScoller(DragScroller scroller) {
667 mDragScroller = scroller;
668 }
669
670 public void setWindowToken(IBinder token) {
671 mWindowToken = token;
672 }
673
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 /**
675 * Sets the drag listner which will be notified when a drag starts or ends.
676 */
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700677 public void addDragListener(DragListener l) {
678 mListeners.add(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400679 }
680
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 /**
682 * Remove a previously installed drag listener.
683 */
Joe Onorato00acb122009-08-04 16:04:30 -0400684 public void removeDragListener(DragListener l) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700685 mListeners.remove(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400686 }
687
688 /**
689 * Add a DropTarget to the list of potential places to receive drop events.
690 */
691 public void addDropTarget(DropTarget target) {
692 mDropTargets.add(target);
693 }
694
695 /**
696 * Don't send drop events to <em>target</em> any more.
697 */
698 public void removeDropTarget(DropTarget target) {
699 mDropTargets.remove(target);
700 }
701
702 /**
703 * Set which view scrolls for touch events near the edge of the screen.
704 */
705 public void setScrollView(View v) {
706 mScrollView = v;
707 }
708
709 /**
710 * Specifies the delete region. We won't scroll on touch events over the delete region.
711 *
712 * @param region The rectangle in screen coordinates of the delete region.
713 */
714 void setDeleteRegion(RectF region) {
715 mDeleteRegion = region;
716 }
717
718 private class ScrollRunnable implements Runnable {
719 private int mDirection;
720
721 ScrollRunnable() {
722 }
723
724 public void run() {
725 if (mDragScroller != null) {
726 if (mDirection == SCROLL_LEFT) {
727 mDragScroller.scrollLeft();
728 } else {
729 mDragScroller.scrollRight();
730 }
731 mScrollState = SCROLL_OUTSIDE_ZONE;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700732 mDistanceSinceScroll = 0;
733 mDragScroller.onExitScrollArea();
Joe Onorato00acb122009-08-04 16:04:30 -0400734 }
735 }
736
737 void setDirection(int direction) {
738 mDirection = direction;
739 }
740 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741}