blob: 272bf8bfabe46424b647209797093ea1e5b818c2 [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
Patrick Dubroya16fd5a2010-10-07 16:47:28 -070019import com.android.launcher.R;
20
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
37import java.util.ArrayList;
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 Dubroy1262e362010-10-06 15:49:50 -070060 static final int SCROLL_LEFT = 0;
61 static final int SCROLL_RIGHT = 1;
Joe Onorato00acb122009-08-04 16:04:30 -040062
63 private Context mContext;
64 private Handler mHandler;
65 private final Vibrator mVibrator = new Vibrator();
66
67 // temporaries to avoid gc thrash
68 private Rect mRectTemp = new Rect();
69 private final int[] mCoordinatesTemp = new int[2];
70
71 /** Whether or not we're dragging. */
72 private boolean mDragging;
73
74 /** X coordinate of the down event. */
75 private float mMotionDownX;
76
77 /** Y coordinate of the down event. */
78 private float mMotionDownY;
79
Joe Onoratoe048e8a2009-09-25 10:39:17 -070080 /** Info about the screen for clamping. */
81 private DisplayMetrics mDisplayMetrics = new DisplayMetrics();
82
Joe Onorato00acb122009-08-04 16:04:30 -040083 /** Original view that is being dragged. */
84 private View mOriginator;
85
Joe Onorato00acb122009-08-04 16:04:30 -040086 /** X offset from the upper-left corner of the cell to where we touched. */
87 private float mTouchOffsetX;
88
89 /** Y offset from the upper-left corner of the cell to where we touched. */
90 private float mTouchOffsetY;
91
Joe Onorato658db742010-09-29 11:40:39 -070092 /** the area at the edge of the screen that makes the workspace go left
93 * or right while you're dragging.
94 */
95 private int mScrollZone;
96
Joe Onorato00acb122009-08-04 16:04:30 -040097 /** Where the drag originated */
98 private DragSource mDragSource;
99
100 /** The data associated with the object being dragged */
101 private Object mDragInfo;
102
103 /** The view that moves around while you drag. */
104 private DragView mDragView;
105
106 /** Who can receive drop events */
107 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
108
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700109 private ArrayList<DragListener> mListeners = new ArrayList<DragListener>();
Joe Onorato00acb122009-08-04 16:04:30 -0400110
111 /** The window token used as the parent for the DragView. */
112 private IBinder mWindowToken;
113
114 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
115 private View mScrollView;
116
Romain Guyea3763c2010-01-11 18:02:04 -0800117 private View mMoveTarget;
118
Joe Onorato00acb122009-08-04 16:04:30 -0400119 private DragScroller mDragScroller;
120 private int mScrollState = SCROLL_OUTSIDE_ZONE;
121 private ScrollRunnable mScrollRunnable = new ScrollRunnable();
122
123 private RectF mDeleteRegion;
124 private DropTarget mLastDropTarget;
125
126 private InputMethodManager mInputMethodManager;
127
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700128 private int mLastTouch[] = new int[2];
129 private int mDistanceSinceScroll = 0;
130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 /**
132 * Interface to receive notifications when a drag starts or stops
133 */
134 interface DragListener {
135
136 /**
137 * A drag has begun
138 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 * @param source An object representing where the drag originated
140 * @param info The data associated with the object that is being dragged
141 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
142 * or {@link DragController#DRAG_ACTION_COPY}
143 */
Joe Onorato5162ea92009-09-03 09:39:42 -0700144 void onDragStart(DragSource source, Object info, int dragAction);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
146 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700147 * The drag has ended
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 */
149 void onDragEnd();
150 }
151
152 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400153 * Used to create a new DragLayer from XML.
154 *
155 * @param context The application's context.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 */
Joe Onorato00acb122009-08-04 16:04:30 -0400157 public DragController(Context context) {
158 mContext = context;
159 mHandler = new Handler();
Joe Onorato658db742010-09-29 11:40:39 -0700160 mScrollZone = context.getResources().getDimensionPixelSize(R.dimen.scroll_zone);
Joe Onorato00acb122009-08-04 16:04:30 -0400161 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
Patrick Dubroy1262e362010-10-06 15:49:50 -0700163 public boolean dragging() {
164 return mDragging;
165 }
166
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 /**
Joe Onorato5162ea92009-09-03 09:39:42 -0700168 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700169 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 * @param v The view that is being dragged
171 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800172 * @param dragInfo The data associated with the object that is being dragged
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
174 * {@link #DRAG_ACTION_COPY}
175 */
Joe Onorato00acb122009-08-04 16:04:30 -0400176 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700177 startDrag(v, source, dragInfo, dragAction, null);
178 }
179
180 /**
181 * Starts a drag.
182 *
183 * @param v The view that is being dragged
184 * @param source An object representing where the drag originated
185 * @param dragInfo The data associated with the object that is being dragged
186 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
187 * {@link #DRAG_ACTION_COPY}
188 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
189 * Makes dragging feel more precise, e.g. you can clip out a transparent border
190 */
191 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction,
192 Rect dragRegion) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700193 mOriginator = v;
194
195 Bitmap b = getViewBitmap(v);
196
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400197 if (b == null) {
198 // out of memory?
199 return;
200 }
201
Joe Onorato5162ea92009-09-03 09:39:42 -0700202 int[] loc = mCoordinatesTemp;
203 v.getLocationOnScreen(loc);
204 int screenX = loc[0];
205 int screenY = loc[1];
206
207 startDrag(b, screenX, screenY, 0, 0, b.getWidth(), b.getHeight(),
Michael Jurkaa63c4522010-08-19 13:52:27 -0700208 source, dragInfo, dragAction, dragRegion);
Joe Onorato5162ea92009-09-03 09:39:42 -0700209
210 b.recycle();
211
212 if (dragAction == DRAG_ACTION_MOVE) {
213 v.setVisibility(View.GONE);
214 }
215 }
216
217 /**
218 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700219 *
Winson Chunge3193b92010-09-10 11:44:42 -0700220 * @param v The view that is being dragged
221 * @param bmp The bitmap that represents the view being dragged
222 * @param source An object representing where the drag originated
223 * @param dragInfo The data associated with the object that is being dragged
224 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
225 * {@link #DRAG_ACTION_COPY}
226 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
227 * Makes dragging feel more precise, e.g. you can clip out a transparent border
228 */
229 public void startDrag(View v, Bitmap bmp, DragSource source, Object dragInfo, int dragAction,
230 Rect dragRegion) {
231 mOriginator = v;
232
233 int[] loc = mCoordinatesTemp;
234 v.getLocationOnScreen(loc);
235 int screenX = loc[0];
236 int screenY = loc[1];
237
238 startDrag(bmp, screenX, screenY, 0, 0, bmp.getWidth(), bmp.getHeight(),
239 source, dragInfo, dragAction, dragRegion);
240
241 if (dragAction == DRAG_ACTION_MOVE) {
242 v.setVisibility(View.GONE);
243 }
244 }
245
246 /**
247 * Starts a drag.
248 *
Joe Onorato5162ea92009-09-03 09:39:42 -0700249 * @param b The bitmap to display as the drag image. It will be re-scaled to the
250 * enlarged size.
251 * @param screenX The x position on screen of the left-top of the bitmap.
252 * @param screenY The y position on screen of the left-top of the bitmap.
253 * @param textureLeft The left edge of the region inside b to use.
254 * @param textureTop The top edge of the region inside b to use.
255 * @param textureWidth The width of the region inside b to use.
256 * @param textureHeight The height of the region inside b to use.
257 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800258 * @param dragInfo The data associated with the object that is being dragged
Joe Onorato5162ea92009-09-03 09:39:42 -0700259 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
260 * {@link #DRAG_ACTION_COPY}
261 */
262 public void startDrag(Bitmap b, int screenX, int screenY,
263 int textureLeft, int textureTop, int textureWidth, int textureHeight,
264 DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700265 startDrag(b, screenX, screenY, textureLeft, textureTop, textureWidth, textureHeight,
266 source, dragInfo, dragAction, null);
267 }
268
269 /**
270 * Starts a drag.
271 *
272 * @param b The bitmap to display as the drag image. It will be re-scaled to the
273 * enlarged size.
274 * @param screenX The x position on screen of the left-top of the bitmap.
275 * @param screenY The y position on screen of the left-top of the bitmap.
276 * @param textureLeft The left edge of the region inside b to use.
277 * @param textureTop The top edge of the region inside b to use.
278 * @param textureWidth The width of the region inside b to use.
279 * @param textureHeight The height of the region inside b to use.
280 * @param source An object representing where the drag originated
281 * @param dragInfo The data associated with the object that is being dragged
282 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
283 * {@link #DRAG_ACTION_COPY}
284 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
285 * Makes dragging feel more precise, e.g. you can clip out a transparent border
286 */
287 public void startDrag(Bitmap b, int screenX, int screenY,
288 int textureLeft, int textureTop, int textureWidth, int textureHeight,
289 DragSource source, Object dragInfo, int dragAction, Rect dragRegion) {
Joe Onorato00acb122009-08-04 16:04:30 -0400290 if (PROFILE_DRAWING_DURING_DRAG) {
291 android.os.Debug.startMethodTracing("Launcher");
292 }
293
294 // Hide soft keyboard, if visible
295 if (mInputMethodManager == null) {
296 mInputMethodManager = (InputMethodManager)
297 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
298 }
299 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
300
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700301 for (DragListener listener : mListeners) {
302 listener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400303 }
304
Joe Onorato00acb122009-08-04 16:04:30 -0400305 int registrationX = ((int)mMotionDownX) - screenX;
306 int registrationY = ((int)mMotionDownY) - screenY;
307
Michael Jurkaa63c4522010-08-19 13:52:27 -0700308 final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left;
309 final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top;
310 mTouchOffsetX = mMotionDownX - screenX - dragRegionLeft;
311 mTouchOffsetY = mMotionDownY - screenY - dragRegionTop;
Joe Onorato00acb122009-08-04 16:04:30 -0400312
313 mDragging = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400314 mDragSource = source;
315 mDragInfo = dragInfo;
316
317 mVibrator.vibrate(VIBRATE_DURATION);
318
Joe Onorato5162ea92009-09-03 09:39:42 -0700319 DragView dragView = mDragView = new DragView(mContext, b, registrationX, registrationY,
320 textureLeft, textureTop, textureWidth, textureHeight);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700321
322 if (dragRegion != null) {
323 dragView.setDragRegion(dragRegionLeft, dragRegion.top,
324 dragRegion.right - dragRegionLeft, dragRegion.bottom - dragRegionTop);
325 }
326
Joe Onorato00acb122009-08-04 16:04:30 -0400327 dragView.show(mWindowToken, (int)mMotionDownX, (int)mMotionDownY);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700328
329 handleMoveEvent((int) mMotionDownX, (int) mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400330 }
331
332 /**
333 * Draw the view into a bitmap.
334 */
335 private Bitmap getViewBitmap(View v) {
336 v.clearFocus();
337 v.setPressed(false);
338
339 boolean willNotCache = v.willNotCacheDrawing();
340 v.setWillNotCacheDrawing(false);
341
342 // Reset the drawing cache background color to fully transparent
343 // for the duration of this operation
344 int color = v.getDrawingCacheBackgroundColor();
345 v.setDrawingCacheBackgroundColor(0);
346
347 if (color != 0) {
348 v.destroyDrawingCache();
349 }
350 v.buildDrawingCache();
351 Bitmap cacheBitmap = v.getDrawingCache();
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400352 if (cacheBitmap == null) {
353 Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
354 return null;
355 }
Joe Onorato00acb122009-08-04 16:04:30 -0400356
357 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
358
359 // Restore the view
360 v.destroyDrawingCache();
361 v.setWillNotCacheDrawing(willNotCache);
362 v.setDrawingCacheBackgroundColor(color);
363
364 return bitmap;
365 }
366
367 /**
368 * Call this from a drag source view like this:
369 *
370 * <pre>
371 * @Override
372 * public boolean dispatchKeyEvent(KeyEvent event) {
373 * return mDragController.dispatchKeyEvent(this, event)
374 * || super.dispatchKeyEvent(event);
375 * </pre>
376 */
Romain Guyea3763c2010-01-11 18:02:04 -0800377 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato00acb122009-08-04 16:04:30 -0400378 public boolean dispatchKeyEvent(KeyEvent event) {
379 return mDragging;
380 }
381
Joe Onorato24b6fd82009-11-12 13:47:09 -0800382 /**
383 * Stop dragging without dropping.
384 */
385 public void cancelDrag() {
386 endDrag();
387 }
388
Joe Onorato00acb122009-08-04 16:04:30 -0400389 private void endDrag() {
390 if (mDragging) {
391 mDragging = false;
392 if (mOriginator != null) {
393 mOriginator.setVisibility(View.VISIBLE);
394 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700395 for (DragListener listener : mListeners) {
396 listener.onDragEnd();
Joe Onorato00acb122009-08-04 16:04:30 -0400397 }
398 if (mDragView != null) {
399 mDragView.remove();
400 mDragView = null;
401 }
Joe Onorato00acb122009-08-04 16:04:30 -0400402 }
403 }
404
405 /**
406 * Call this from a drag source view.
407 */
408 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400409 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800410 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400411 + mDragging);
412 }
Joe Onorato00acb122009-08-04 16:04:30 -0400413 final int action = ev.getAction();
414
Joe Onorato87467d32009-11-08 14:36:43 -0500415 if (action == MotionEvent.ACTION_DOWN) {
416 recordScreenSize();
417 }
418
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700419 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
420 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400421
422 switch (action) {
423 case MotionEvent.ACTION_MOVE:
424 break;
425
426 case MotionEvent.ACTION_DOWN:
427 // Remember location of down touch
428 mMotionDownX = screenX;
429 mMotionDownY = screenY;
430 mLastDropTarget = null;
431 break;
432
433 case MotionEvent.ACTION_CANCEL:
434 case MotionEvent.ACTION_UP:
435 if (mDragging) {
436 drop(screenX, screenY);
437 }
438 endDrag();
439 break;
440 }
441
442 return mDragging;
443 }
444
445 /**
Romain Guyea3763c2010-01-11 18:02:04 -0800446 * Sets the view that should handle move events.
447 */
448 void setMoveTarget(View view) {
449 mMoveTarget = view;
450 }
451
452 public boolean dispatchUnhandledMove(View focused, int direction) {
453 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
454 }
455
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700456 private void handleMoveEvent(int x, int y) {
457 mDragView.move(x, y);
458
459 // Drop on someone?
460 final int[] coordinates = mCoordinatesTemp;
461 DropTarget dropTarget = findDropTarget(x, y, coordinates);
462 if (dropTarget != null) {
463 DropTarget delegate = dropTarget.getDropTargetDelegate(
464 mDragSource, coordinates[0], coordinates[1],
465 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
466 if (delegate != null) {
467 dropTarget = delegate;
468 }
469
470 if (mLastDropTarget != dropTarget) {
471 if (mLastDropTarget != null) {
472 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
473 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
474 }
475 dropTarget.onDragEnter(mDragSource, coordinates[0], coordinates[1],
476 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
477 }
478 dropTarget.onDragOver(mDragSource, coordinates[0], coordinates[1],
479 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
480 } else {
481 if (mLastDropTarget != null) {
482 mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
483 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
484 }
485 }
486 mLastDropTarget = dropTarget;
487
488 // Scroll, maybe, but not if we're in the delete region.
489 boolean inDeleteRegion = false;
490 if (mDeleteRegion != null) {
491 inDeleteRegion = mDeleteRegion.contains(x, y);
492 }
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700493
494 // After a scroll, the touch point will still be in the scroll region.
495 // Rather than scrolling immediately, require a bit of twiddling to scroll again
496 final int slop = ViewConfiguration.get(mContext).getScaledWindowTouchSlop();
497 mDistanceSinceScroll +=
498 Math.sqrt(Math.pow(mLastTouch[0] - x, 2) + Math.pow(mLastTouch[1] - y, 2));
499 mLastTouch[0] = x;
500 mLastTouch[1] = y;
501
Joe Onorato658db742010-09-29 11:40:39 -0700502 if (!inDeleteRegion && x < mScrollZone) {
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700503 if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700504 mScrollState = SCROLL_WAITING_IN_ZONE;
505 mScrollRunnable.setDirection(SCROLL_LEFT);
506 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700507 mDragScroller.onEnterScrollArea(SCROLL_LEFT);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700508 }
Joe Onorato658db742010-09-29 11:40:39 -0700509 } else if (!inDeleteRegion && x > mScrollView.getWidth() - mScrollZone) {
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700510 if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700511 mScrollState = SCROLL_WAITING_IN_ZONE;
512 mScrollRunnable.setDirection(SCROLL_RIGHT);
513 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700514 mDragScroller.onEnterScrollArea(SCROLL_RIGHT);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700515 }
516 } else {
517 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
518 mScrollState = SCROLL_OUTSIDE_ZONE;
519 mScrollRunnable.setDirection(SCROLL_RIGHT);
520 mHandler.removeCallbacks(mScrollRunnable);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700521 mDragScroller.onExitScrollArea();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700522 }
523 }
524 }
525
Romain Guyea3763c2010-01-11 18:02:04 -0800526 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400527 * Call this from a drag source view.
528 */
529 public boolean onTouchEvent(MotionEvent ev) {
Joe Onorato00acb122009-08-04 16:04:30 -0400530 if (!mDragging) {
531 return false;
532 }
533
534 final int action = ev.getAction();
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700535 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
536 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400537
538 switch (action) {
539 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400540 // Remember where the motion event started
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700541 mMotionDownX = screenX;
542 mMotionDownY = screenY;
Joe Onorato00acb122009-08-04 16:04:30 -0400543
Joe Onorato658db742010-09-29 11:40:39 -0700544 if ((screenX < mScrollZone) || (screenX > mScrollView.getWidth() - mScrollZone)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400545 mScrollState = SCROLL_WAITING_IN_ZONE;
546 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
547 } else {
548 mScrollState = SCROLL_OUTSIDE_ZONE;
549 }
Joe Onorato00acb122009-08-04 16:04:30 -0400550 break;
551 case MotionEvent.ACTION_MOVE:
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700552 handleMoveEvent(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400553 break;
554 case MotionEvent.ACTION_UP:
555 mHandler.removeCallbacks(mScrollRunnable);
556 if (mDragging) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700557 drop(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400558 }
559 endDrag();
560
561 break;
562 case MotionEvent.ACTION_CANCEL:
Joe Onorato24b6fd82009-11-12 13:47:09 -0800563 cancelDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400564 }
565
566 return true;
567 }
568
569 private boolean drop(float x, float y) {
570 final int[] coordinates = mCoordinatesTemp;
571 DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
572
573 if (dropTarget != null) {
574 dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1],
575 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
576 if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1],
577 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) {
578 dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1],
579 (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo);
580 mDragSource.onDropCompleted((View) dropTarget, true);
581 return true;
582 } else {
583 mDragSource.onDropCompleted((View) dropTarget, false);
584 return true;
585 }
Winson Chunga34abf82010-11-12 12:10:35 -0800586 } else {
587 mDragSource.onDropCompleted(null, false);
Joe Onorato00acb122009-08-04 16:04:30 -0400588 }
589 return false;
590 }
591
592 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
593 final Rect r = mRectTemp;
594
595 final ArrayList<DropTarget> dropTargets = mDropTargets;
596 final int count = dropTargets.size();
597 for (int i=count-1; i>=0; i--) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700598 DropTarget target = dropTargets.get(i);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700599 if (!target.isDropEnabled())
600 continue;
601
Joe Onorato00acb122009-08-04 16:04:30 -0400602 target.getHitRect(r);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700603
604 // Convert the hit rect to screen coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400605 target.getLocationOnScreen(dropCoordinates);
606 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
Patrick Dubroy440c3602010-07-13 17:50:32 -0700607
Joe Onorato00acb122009-08-04 16:04:30 -0400608 if (r.contains(x, y)) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700609 DropTarget delegate = target.getDropTargetDelegate(mDragSource,
610 x, y, (int)mTouchOffsetX, (int)mTouchOffsetY, mDragView, mDragInfo);
611 if (delegate != null) {
612 target = delegate;
613 target.getLocationOnScreen(dropCoordinates);
614 }
615
616 // Make dropCoordinates relative to the DropTarget
Joe Onorato00acb122009-08-04 16:04:30 -0400617 dropCoordinates[0] = x - dropCoordinates[0];
618 dropCoordinates[1] = y - dropCoordinates[1];
Patrick Dubroy440c3602010-07-13 17:50:32 -0700619
Joe Onorato00acb122009-08-04 16:04:30 -0400620 return target;
621 }
622 }
623 return null;
624 }
625
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700626 /**
627 * Get the screen size so we can clamp events to the screen size so even if
628 * you drag off the edge of the screen, we find something.
629 */
630 private void recordScreenSize() {
631 ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
632 .getDefaultDisplay().getMetrics(mDisplayMetrics);
633 }
634
635 /**
636 * Clamp val to be &gt;= min and &lt; max.
637 */
638 private static int clamp(int val, int min, int max) {
639 if (val < min) {
640 return min;
641 } else if (val >= max) {
642 return max - 1;
643 } else {
644 return val;
645 }
646 }
647
Joe Onorato00acb122009-08-04 16:04:30 -0400648 public void setDragScoller(DragScroller scroller) {
649 mDragScroller = scroller;
650 }
651
652 public void setWindowToken(IBinder token) {
653 mWindowToken = token;
654 }
655
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 /**
657 * Sets the drag listner which will be notified when a drag starts or ends.
658 */
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700659 public void addDragListener(DragListener l) {
660 mListeners.add(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400661 }
662
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 /**
664 * Remove a previously installed drag listener.
665 */
Joe Onorato00acb122009-08-04 16:04:30 -0400666 public void removeDragListener(DragListener l) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700667 mListeners.remove(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400668 }
669
670 /**
671 * Add a DropTarget to the list of potential places to receive drop events.
672 */
673 public void addDropTarget(DropTarget target) {
674 mDropTargets.add(target);
675 }
676
677 /**
678 * Don't send drop events to <em>target</em> any more.
679 */
680 public void removeDropTarget(DropTarget target) {
681 mDropTargets.remove(target);
682 }
683
684 /**
685 * Set which view scrolls for touch events near the edge of the screen.
686 */
687 public void setScrollView(View v) {
688 mScrollView = v;
689 }
690
691 /**
692 * Specifies the delete region. We won't scroll on touch events over the delete region.
693 *
694 * @param region The rectangle in screen coordinates of the delete region.
695 */
696 void setDeleteRegion(RectF region) {
697 mDeleteRegion = region;
698 }
699
700 private class ScrollRunnable implements Runnable {
701 private int mDirection;
702
703 ScrollRunnable() {
704 }
705
706 public void run() {
707 if (mDragScroller != null) {
708 if (mDirection == SCROLL_LEFT) {
709 mDragScroller.scrollLeft();
710 } else {
711 mDragScroller.scrollRight();
712 }
713 mScrollState = SCROLL_OUTSIDE_ZONE;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700714 mDistanceSinceScroll = 0;
715 mDragScroller.onExitScrollArea();
Joe Onorato00acb122009-08-04 16:04:30 -0400716 }
717 }
718
719 void setDirection(int direction) {
720 mDirection = direction;
721 }
722 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723}