blob: 9a6335bce549e79df6a6de1c6315ee2d8db57aa5 [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;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -070031import android.view.ViewConfiguration;
Joe Onoratoe048e8a2009-09-25 10:39:17 -070032import android.view.WindowManager;
Joe Onorato00acb122009-08-04 16:04:30 -040033import android.view.inputmethod.InputMethodManager;
Joe Onorato00acb122009-08-04 16:04:30 -040034
Adam Cohen120980b2010-12-08 11:05:37 -080035import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070036
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 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. */
Adam Cohene3e27a82011-04-15 12:07:39 -070076 private int mMotionDownX;
Joe Onorato00acb122009-08-04 16:04:30 -040077
78 /** Y coordinate of the down event. */
Adam Cohene3e27a82011-04-15 12:07:39 -070079 private int mMotionDownY;
Joe Onorato00acb122009-08-04 16:04:30 -040080
Joe Onoratoe048e8a2009-09-25 10:39:17 -070081 /** Info about the screen for clamping. */
82 private DisplayMetrics mDisplayMetrics = new DisplayMetrics();
83
Joe Onorato658db742010-09-29 11:40:39 -070084 /** the area at the edge of the screen that makes the workspace go left
85 * or right while you're dragging.
86 */
87 private int mScrollZone;
88
Adam Cohencb3382b2011-05-24 14:07:08 -070089 private DropTarget.DragObject mDragObject = new DropTarget.DragObject();
Joe Onorato00acb122009-08-04 16:04:30 -040090
91 /** Who can receive drop events */
92 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
93
Patrick Dubroy4ed62782010-08-17 15:11:18 -070094 private ArrayList<DragListener> mListeners = new ArrayList<DragListener>();
Joe Onorato00acb122009-08-04 16:04:30 -040095
96 /** The window token used as the parent for the DragView. */
97 private IBinder mWindowToken;
98
99 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
100 private View mScrollView;
101
Romain Guyea3763c2010-01-11 18:02:04 -0800102 private View mMoveTarget;
103
Joe Onorato00acb122009-08-04 16:04:30 -0400104 private DragScroller mDragScroller;
105 private int mScrollState = SCROLL_OUTSIDE_ZONE;
106 private ScrollRunnable mScrollRunnable = new ScrollRunnable();
107
108 private RectF mDeleteRegion;
109 private DropTarget mLastDropTarget;
110
111 private InputMethodManager mInputMethodManager;
112
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700113 private int mLastTouch[] = new int[2];
114 private int mDistanceSinceScroll = 0;
115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 /**
117 * Interface to receive notifications when a drag starts or stops
118 */
119 interface DragListener {
120
121 /**
122 * A drag has begun
123 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 * @param source An object representing where the drag originated
125 * @param info The data associated with the object that is being dragged
126 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
127 * or {@link DragController#DRAG_ACTION_COPY}
128 */
Joe Onorato5162ea92009-09-03 09:39:42 -0700129 void onDragStart(DragSource source, Object info, int dragAction);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
131 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700132 * The drag has ended
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 */
134 void onDragEnd();
135 }
136
137 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400138 * Used to create a new DragLayer from XML.
139 *
140 * @param context The application's context.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 */
Joe Onorato00acb122009-08-04 16:04:30 -0400142 public DragController(Context context) {
143 mContext = context;
144 mHandler = new Handler();
Joe Onorato658db742010-09-29 11:40:39 -0700145 mScrollZone = context.getResources().getDimensionPixelSize(R.dimen.scroll_zone);
Joe Onorato00acb122009-08-04 16:04:30 -0400146 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Patrick Dubroy1262e362010-10-06 15:49:50 -0700148 public boolean dragging() {
149 return mDragging;
150 }
151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 /**
Joe Onorato5162ea92009-09-03 09:39:42 -0700153 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700154 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 * @param v The view that is being dragged
156 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800157 * @param dragInfo The data associated with the object that is being dragged
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
159 * {@link #DRAG_ACTION_COPY}
160 */
Joe Onorato00acb122009-08-04 16:04:30 -0400161 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700162 startDrag(v, source, dragInfo, dragAction, null);
163 }
164
165 /**
166 * Starts a drag.
167 *
168 * @param v The view that is being dragged
169 * @param source An object representing where the drag originated
170 * @param dragInfo The data associated with the object that is being dragged
171 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
172 * {@link #DRAG_ACTION_COPY}
173 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
174 * Makes dragging feel more precise, e.g. you can clip out a transparent border
175 */
176 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction,
177 Rect dragRegion) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700178 Bitmap b = getViewBitmap(v);
179
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400180 if (b == null) {
181 // out of memory?
182 return;
183 }
184
Joe Onorato5162ea92009-09-03 09:39:42 -0700185 int[] loc = mCoordinatesTemp;
186 v.getLocationOnScreen(loc);
187 int screenX = loc[0];
188 int screenY = loc[1];
189
Patrick Dubroy5f445422011-02-18 14:35:21 -0800190 startDrag(b, screenX, screenY, source, dragInfo, dragAction, dragRegion);
Joe Onorato5162ea92009-09-03 09:39:42 -0700191 b.recycle();
192
193 if (dragAction == DRAG_ACTION_MOVE) {
194 v.setVisibility(View.GONE);
195 }
196 }
197
198 /**
199 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700200 *
Winson Chunge3193b92010-09-10 11:44:42 -0700201 * @param v The view that is being dragged
202 * @param bmp The bitmap that represents the view being dragged
203 * @param source An object representing where the drag originated
204 * @param dragInfo The data associated with the object that is being dragged
205 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
206 * {@link #DRAG_ACTION_COPY}
207 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
208 * Makes dragging feel more precise, e.g. you can clip out a transparent border
209 */
210 public void startDrag(View v, Bitmap bmp, DragSource source, Object dragInfo, int dragAction,
211 Rect dragRegion) {
Winson Chunge3193b92010-09-10 11:44:42 -0700212 int[] loc = mCoordinatesTemp;
213 v.getLocationOnScreen(loc);
214 int screenX = loc[0];
215 int screenY = loc[1];
216
Patrick Dubroy5f445422011-02-18 14:35:21 -0800217 startDrag(bmp, screenX, screenY, source, dragInfo, dragAction, dragRegion);
Winson Chunge3193b92010-09-10 11:44:42 -0700218
219 if (dragAction == DRAG_ACTION_MOVE) {
220 v.setVisibility(View.GONE);
221 }
222 }
223
224 /**
225 * Starts a drag.
226 *
Joe Onorato5162ea92009-09-03 09:39:42 -0700227 * @param b The bitmap to display as the drag image. It will be re-scaled to the
228 * enlarged size.
229 * @param screenX The x position on screen of the left-top of the bitmap.
230 * @param screenY The y position on screen of the left-top of the bitmap.
Joe Onorato5162ea92009-09-03 09:39:42 -0700231 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800232 * @param dragInfo The data associated with the object that is being dragged
Joe Onorato5162ea92009-09-03 09:39:42 -0700233 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
234 * {@link #DRAG_ACTION_COPY}
235 */
236 public void startDrag(Bitmap b, int screenX, int screenY,
Joe Onorato5162ea92009-09-03 09:39:42 -0700237 DragSource source, Object dragInfo, int dragAction) {
Patrick Dubroy5f445422011-02-18 14:35:21 -0800238 startDrag(b, screenX, screenY, source, dragInfo, dragAction, null);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700239 }
240
241 /**
242 * Starts a drag.
243 *
244 * @param b The bitmap to display as the drag image. It will be re-scaled to the
245 * enlarged size.
246 * @param screenX The x position on screen of the left-top of the bitmap.
247 * @param screenY The y position on screen of the left-top of the bitmap.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700248 * @param source An object representing where the drag originated
249 * @param dragInfo The data associated with the object that is being dragged
250 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
251 * {@link #DRAG_ACTION_COPY}
252 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
253 * Makes dragging feel more precise, e.g. you can clip out a transparent border
254 */
255 public void startDrag(Bitmap b, int screenX, int screenY,
Michael Jurkaa63c4522010-08-19 13:52:27 -0700256 DragSource source, Object dragInfo, int dragAction, Rect dragRegion) {
Joe Onorato00acb122009-08-04 16:04:30 -0400257 if (PROFILE_DRAWING_DURING_DRAG) {
258 android.os.Debug.startMethodTracing("Launcher");
259 }
260
261 // Hide soft keyboard, if visible
262 if (mInputMethodManager == null) {
263 mInputMethodManager = (InputMethodManager)
264 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
265 }
266 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
267
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700268 for (DragListener listener : mListeners) {
269 listener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400270 }
271
Adam Cohene3e27a82011-04-15 12:07:39 -0700272 final int registrationX = ((int)mMotionDownX) - screenX;
273 final int registrationY = ((int)mMotionDownY) - screenY;
Joe Onorato00acb122009-08-04 16:04:30 -0400274
Michael Jurkaa63c4522010-08-19 13:52:27 -0700275 final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left;
276 final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top;
Adam Cohene3e27a82011-04-15 12:07:39 -0700277
Joe Onorato00acb122009-08-04 16:04:30 -0400278 mDragging = true;
Adam Cohencb3382b2011-05-24 14:07:08 -0700279
Adam Cohenbfbfd262011-06-13 16:55:12 -0700280 mDragObject.dragComplete = false;
Adam Cohencb3382b2011-05-24 14:07:08 -0700281 mDragObject.xOffset = mMotionDownX - (screenX + dragRegionLeft);
282 mDragObject.yOffset = mMotionDownY - (screenY + dragRegionTop);
283 mDragObject.dragSource = source;
284 mDragObject.dragInfo = dragInfo;
Joe Onorato00acb122009-08-04 16:04:30 -0400285
286 mVibrator.vibrate(VIBRATE_DURATION);
287
Adam Cohencb3382b2011-05-24 14:07:08 -0700288 final DragView dragView = mDragObject.dragView = new DragView(mContext, b, registrationX,
289 registrationY, 0, 0, b.getWidth(), b.getHeight());
Michael Jurkaa63c4522010-08-19 13:52:27 -0700290
Patrick Dubroya669d792010-11-23 14:40:33 -0800291 final DragSource dragSource = source;
292 dragView.setOnDrawRunnable(new Runnable() {
293 public void run() {
294 dragSource.onDragViewVisible();
295 };
296 });
297
Michael Jurkaa63c4522010-08-19 13:52:27 -0700298 if (dragRegion != null) {
Adam Cohene3e27a82011-04-15 12:07:39 -0700299 dragView.setDragRegion(new Rect(dragRegion));
Michael Jurkaa63c4522010-08-19 13:52:27 -0700300 }
301
Joe Onorato00acb122009-08-04 16:04:30 -0400302 dragView.show(mWindowToken, (int)mMotionDownX, (int)mMotionDownY);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700303
304 handleMoveEvent((int) mMotionDownX, (int) mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400305 }
306
307 /**
308 * Draw the view into a bitmap.
309 */
Adam Cohen120980b2010-12-08 11:05:37 -0800310 Bitmap getViewBitmap(View v) {
Joe Onorato00acb122009-08-04 16:04:30 -0400311 v.clearFocus();
312 v.setPressed(false);
313
314 boolean willNotCache = v.willNotCacheDrawing();
315 v.setWillNotCacheDrawing(false);
316
317 // Reset the drawing cache background color to fully transparent
318 // for the duration of this operation
319 int color = v.getDrawingCacheBackgroundColor();
320 v.setDrawingCacheBackgroundColor(0);
Adam Cohen120980b2010-12-08 11:05:37 -0800321 float alpha = v.getAlpha();
322 v.setAlpha(1.0f);
Joe Onorato00acb122009-08-04 16:04:30 -0400323
324 if (color != 0) {
325 v.destroyDrawingCache();
326 }
327 v.buildDrawingCache();
328 Bitmap cacheBitmap = v.getDrawingCache();
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400329 if (cacheBitmap == null) {
330 Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
331 return null;
332 }
Joe Onorato00acb122009-08-04 16:04:30 -0400333
334 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
335
336 // Restore the view
337 v.destroyDrawingCache();
Adam Cohen120980b2010-12-08 11:05:37 -0800338 v.setAlpha(alpha);
Joe Onorato00acb122009-08-04 16:04:30 -0400339 v.setWillNotCacheDrawing(willNotCache);
340 v.setDrawingCacheBackgroundColor(color);
341
342 return bitmap;
343 }
344
345 /**
346 * Call this from a drag source view like this:
347 *
348 * <pre>
349 * @Override
350 * public boolean dispatchKeyEvent(KeyEvent event) {
351 * return mDragController.dispatchKeyEvent(this, event)
352 * || super.dispatchKeyEvent(event);
353 * </pre>
354 */
Romain Guyea3763c2010-01-11 18:02:04 -0800355 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato00acb122009-08-04 16:04:30 -0400356 public boolean dispatchKeyEvent(KeyEvent event) {
357 return mDragging;
358 }
359
Winson Chung304dcde2011-01-07 11:17:23 -0800360 public boolean isDragging() {
361 return mDragging;
362 }
363
Joe Onorato24b6fd82009-11-12 13:47:09 -0800364 /**
365 * Stop dragging without dropping.
366 */
367 public void cancelDrag() {
Winson Chung621e6402011-01-04 16:03:57 -0800368 if (mDragging) {
Winson Chungc07918d2011-07-01 15:35:26 -0700369 if (mLastDropTarget != null) {
370 mLastDropTarget.onDragExit(mDragObject);
371 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700372 mDragObject.dragComplete = true;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700373 mDragObject.dragSource.onDropCompleted(null, mDragObject, false);
Winson Chung621e6402011-01-04 16:03:57 -0800374 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800375 endDrag();
376 }
377
Joe Onorato00acb122009-08-04 16:04:30 -0400378 private void endDrag() {
379 if (mDragging) {
380 mDragging = false;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700381 for (DragListener listener : mListeners) {
382 listener.onDragEnd();
Joe Onorato00acb122009-08-04 16:04:30 -0400383 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700384 if (mDragObject.dragView != null) {
385 mDragObject.dragView.remove();
386 mDragObject.dragView = null;
Joe Onorato00acb122009-08-04 16:04:30 -0400387 }
Joe Onorato00acb122009-08-04 16:04:30 -0400388 }
389 }
390
391 /**
392 * Call this from a drag source view.
393 */
394 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400395 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800396 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400397 + mDragging);
398 }
Joe Onorato00acb122009-08-04 16:04:30 -0400399 final int action = ev.getAction();
400
Joe Onorato87467d32009-11-08 14:36:43 -0500401 if (action == MotionEvent.ACTION_DOWN) {
402 recordScreenSize();
403 }
404
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700405 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
406 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400407
408 switch (action) {
409 case MotionEvent.ACTION_MOVE:
410 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400411 case MotionEvent.ACTION_DOWN:
412 // Remember location of down touch
413 mMotionDownX = screenX;
414 mMotionDownY = screenY;
415 mLastDropTarget = null;
416 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400417 case MotionEvent.ACTION_UP:
418 if (mDragging) {
419 drop(screenX, screenY);
420 }
421 endDrag();
422 break;
Winson Chung621e6402011-01-04 16:03:57 -0800423 case MotionEvent.ACTION_CANCEL:
424 cancelDrag();
425 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400426 }
427
428 return mDragging;
429 }
430
431 /**
Romain Guyea3763c2010-01-11 18:02:04 -0800432 * Sets the view that should handle move events.
433 */
434 void setMoveTarget(View view) {
435 mMoveTarget = view;
436 }
437
438 public boolean dispatchUnhandledMove(View focused, int direction) {
439 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
440 }
441
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700442 private void handleMoveEvent(int x, int y) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700443 mDragObject.dragView.move(x, y);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700444
445 // Drop on someone?
446 final int[] coordinates = mCoordinatesTemp;
447 DropTarget dropTarget = findDropTarget(x, y, coordinates);
Adam Cohencb3382b2011-05-24 14:07:08 -0700448 mDragObject.x = coordinates[0];
449 mDragObject.y = coordinates[1];
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700450 if (dropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700451 DropTarget delegate = dropTarget.getDropTargetDelegate(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700452 if (delegate != null) {
453 dropTarget = delegate;
454 }
455
456 if (mLastDropTarget != dropTarget) {
457 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700458 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700459 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700460 dropTarget.onDragEnter(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700461 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700462 dropTarget.onDragOver(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700463 } else {
464 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700465 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700466 }
467 }
468 mLastDropTarget = dropTarget;
469
470 // Scroll, maybe, but not if we're in the delete region.
471 boolean inDeleteRegion = false;
472 if (mDeleteRegion != null) {
473 inDeleteRegion = mDeleteRegion.contains(x, y);
474 }
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700475
476 // After a scroll, the touch point will still be in the scroll region.
477 // Rather than scrolling immediately, require a bit of twiddling to scroll again
478 final int slop = ViewConfiguration.get(mContext).getScaledWindowTouchSlop();
479 mDistanceSinceScroll +=
480 Math.sqrt(Math.pow(mLastTouch[0] - x, 2) + Math.pow(mLastTouch[1] - y, 2));
481 mLastTouch[0] = x;
482 mLastTouch[1] = y;
483
Joe Onorato658db742010-09-29 11:40:39 -0700484 if (!inDeleteRegion && x < mScrollZone) {
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700485 if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700486 mScrollState = SCROLL_WAITING_IN_ZONE;
487 mScrollRunnable.setDirection(SCROLL_LEFT);
488 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700489 mDragScroller.onEnterScrollArea(SCROLL_LEFT);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700490 }
Joe Onorato658db742010-09-29 11:40:39 -0700491 } else if (!inDeleteRegion && x > mScrollView.getWidth() - mScrollZone) {
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700492 if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700493 mScrollState = SCROLL_WAITING_IN_ZONE;
494 mScrollRunnable.setDirection(SCROLL_RIGHT);
495 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700496 mDragScroller.onEnterScrollArea(SCROLL_RIGHT);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700497 }
498 } else {
499 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
500 mScrollState = SCROLL_OUTSIDE_ZONE;
501 mScrollRunnable.setDirection(SCROLL_RIGHT);
502 mHandler.removeCallbacks(mScrollRunnable);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700503 mDragScroller.onExitScrollArea();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700504 }
505 }
506 }
507
Romain Guyea3763c2010-01-11 18:02:04 -0800508 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400509 * Call this from a drag source view.
510 */
511 public boolean onTouchEvent(MotionEvent ev) {
Joe Onorato00acb122009-08-04 16:04:30 -0400512 if (!mDragging) {
513 return false;
514 }
515
516 final int action = ev.getAction();
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700517 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
518 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400519
520 switch (action) {
521 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400522 // Remember where the motion event started
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700523 mMotionDownX = screenX;
524 mMotionDownY = screenY;
Joe Onorato00acb122009-08-04 16:04:30 -0400525
Joe Onorato658db742010-09-29 11:40:39 -0700526 if ((screenX < mScrollZone) || (screenX > mScrollView.getWidth() - mScrollZone)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400527 mScrollState = SCROLL_WAITING_IN_ZONE;
528 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
529 } else {
530 mScrollState = SCROLL_OUTSIDE_ZONE;
531 }
Joe Onorato00acb122009-08-04 16:04:30 -0400532 break;
533 case MotionEvent.ACTION_MOVE:
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700534 handleMoveEvent(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400535 break;
536 case MotionEvent.ACTION_UP:
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800537 // Ensure that we've processed a move event at the current pointer location.
538 handleMoveEvent(screenX, screenY);
539
Joe Onorato00acb122009-08-04 16:04:30 -0400540 mHandler.removeCallbacks(mScrollRunnable);
541 if (mDragging) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700542 drop(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400543 }
544 endDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400545 break;
546 case MotionEvent.ACTION_CANCEL:
Joe Onorato24b6fd82009-11-12 13:47:09 -0800547 cancelDrag();
Winson Chung621e6402011-01-04 16:03:57 -0800548 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400549 }
550
551 return true;
552 }
553
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800554 private void drop(float x, float y) {
Joe Onorato00acb122009-08-04 16:04:30 -0400555 final int[] coordinates = mCoordinatesTemp;
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800556 final DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400557
Adam Cohencb3382b2011-05-24 14:07:08 -0700558 mDragObject.x = coordinates[0];
559 mDragObject.y = coordinates[1];
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800560 boolean accepted = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400561 if (dropTarget != null) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700562 mDragObject.dragComplete = true;
Adam Cohencb3382b2011-05-24 14:07:08 -0700563 dropTarget.onDragExit(mDragObject);
564 if (dropTarget.acceptDrop(mDragObject)) {
565 dropTarget.onDrop(mDragObject);
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800566 accepted = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400567 }
568 }
Adam Cohenc0dcf592011-06-01 15:30:43 -0700569 mDragObject.dragSource.onDropCompleted((View) dropTarget, mDragObject, accepted);
Joe Onorato00acb122009-08-04 16:04:30 -0400570 }
571
572 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
573 final Rect r = mRectTemp;
574
575 final ArrayList<DropTarget> dropTargets = mDropTargets;
576 final int count = dropTargets.size();
577 for (int i=count-1; i>=0; i--) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700578 DropTarget target = dropTargets.get(i);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700579 if (!target.isDropEnabled())
580 continue;
581
Joe Onorato00acb122009-08-04 16:04:30 -0400582 target.getHitRect(r);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700583
584 // Convert the hit rect to screen coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400585 target.getLocationOnScreen(dropCoordinates);
586 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
Patrick Dubroy440c3602010-07-13 17:50:32 -0700587
Adam Cohencb3382b2011-05-24 14:07:08 -0700588 mDragObject.x = x;
589 mDragObject.y = y;
Joe Onorato00acb122009-08-04 16:04:30 -0400590 if (r.contains(x, y)) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700591 DropTarget delegate = target.getDropTargetDelegate(mDragObject);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700592 if (delegate != null) {
593 target = delegate;
594 target.getLocationOnScreen(dropCoordinates);
595 }
596
597 // Make dropCoordinates relative to the DropTarget
Joe Onorato00acb122009-08-04 16:04:30 -0400598 dropCoordinates[0] = x - dropCoordinates[0];
599 dropCoordinates[1] = y - dropCoordinates[1];
Patrick Dubroy440c3602010-07-13 17:50:32 -0700600
Joe Onorato00acb122009-08-04 16:04:30 -0400601 return target;
602 }
603 }
604 return null;
605 }
606
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700607 /**
608 * Get the screen size so we can clamp events to the screen size so even if
609 * you drag off the edge of the screen, we find something.
610 */
611 private void recordScreenSize() {
612 ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
613 .getDefaultDisplay().getMetrics(mDisplayMetrics);
614 }
615
616 /**
617 * Clamp val to be &gt;= min and &lt; max.
618 */
619 private static int clamp(int val, int min, int max) {
620 if (val < min) {
621 return min;
622 } else if (val >= max) {
623 return max - 1;
624 } else {
625 return val;
626 }
627 }
628
Joe Onorato00acb122009-08-04 16:04:30 -0400629 public void setDragScoller(DragScroller scroller) {
630 mDragScroller = scroller;
631 }
632
633 public void setWindowToken(IBinder token) {
634 mWindowToken = token;
635 }
636
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 /**
638 * Sets the drag listner which will be notified when a drag starts or ends.
639 */
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700640 public void addDragListener(DragListener l) {
641 mListeners.add(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400642 }
643
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 /**
645 * Remove a previously installed drag listener.
646 */
Joe Onorato00acb122009-08-04 16:04:30 -0400647 public void removeDragListener(DragListener l) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700648 mListeners.remove(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400649 }
650
651 /**
652 * Add a DropTarget to the list of potential places to receive drop events.
653 */
654 public void addDropTarget(DropTarget target) {
655 mDropTargets.add(target);
656 }
657
658 /**
659 * Don't send drop events to <em>target</em> any more.
660 */
661 public void removeDropTarget(DropTarget target) {
662 mDropTargets.remove(target);
663 }
664
665 /**
666 * Set which view scrolls for touch events near the edge of the screen.
667 */
668 public void setScrollView(View v) {
669 mScrollView = v;
670 }
671
672 /**
673 * Specifies the delete region. We won't scroll on touch events over the delete region.
674 *
675 * @param region The rectangle in screen coordinates of the delete region.
676 */
677 void setDeleteRegion(RectF region) {
678 mDeleteRegion = region;
679 }
680
Patrick Dubroy5f445422011-02-18 14:35:21 -0800681 DragView getDragView() {
Adam Cohencb3382b2011-05-24 14:07:08 -0700682 return mDragObject.dragView;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800683 }
684
Joe Onorato00acb122009-08-04 16:04:30 -0400685 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;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700699 mDistanceSinceScroll = 0;
700 mDragScroller.onExitScrollArea();
Joe Onorato00acb122009-08-04 16:04:30 -0400701 }
702 }
703
704 void setDirection(int direction) {
705 mDirection = direction;
706 }
707 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708}