blob: 1690cac71d0fc33e3a538841263aba828d3ed7bf [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;
Adam Cohencb3382b2011-05-24 14:07:08 -070038import com.android.launcher2.DropTarget.DragObject;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039
40/**
Joe Onorato00acb122009-08-04 16:04:30 -040041 * Class for initiating a drag within a view or across multiple views.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042 */
Joe Onorato00acb122009-08-04 16:04:30 -040043public class DragController {
Romain Guyea3763c2010-01-11 18:02:04 -080044 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato2e5c4322009-10-06 12:34:42 -070045 private static final String TAG = "Launcher.DragController";
46
Joe Onorato00acb122009-08-04 16:04:30 -040047 /** Indicates the drag is a move. */
48 public static int DRAG_ACTION_MOVE = 0;
49
50 /** Indicates the drag is a copy. */
51 public static int DRAG_ACTION_COPY = 1;
52
53 private static final int SCROLL_DELAY = 600;
Joe Onorato00acb122009-08-04 16:04:30 -040054 private static final int VIBRATE_DURATION = 35;
55
56 private static final boolean PROFILE_DRAWING_DURING_DRAG = false;
57
58 private static final int SCROLL_OUTSIDE_ZONE = 0;
59 private static final int SCROLL_WAITING_IN_ZONE = 1;
60
Patrick Dubroy54fa3b92010-11-17 12:18:45 -080061 static final int SCROLL_NONE = -1;
Patrick Dubroy1262e362010-10-06 15:49:50 -070062 static final int SCROLL_LEFT = 0;
63 static final int SCROLL_RIGHT = 1;
Joe Onorato00acb122009-08-04 16:04:30 -040064
65 private Context mContext;
66 private Handler mHandler;
67 private final Vibrator mVibrator = new Vibrator();
68
69 // temporaries to avoid gc thrash
70 private Rect mRectTemp = new Rect();
71 private final int[] mCoordinatesTemp = new int[2];
72
73 /** Whether or not we're dragging. */
74 private boolean mDragging;
75
76 /** X coordinate of the down event. */
Adam Cohene3e27a82011-04-15 12:07:39 -070077 private int mMotionDownX;
Joe Onorato00acb122009-08-04 16:04:30 -040078
79 /** Y coordinate of the down event. */
Adam Cohene3e27a82011-04-15 12:07:39 -070080 private int mMotionDownY;
Joe Onorato00acb122009-08-04 16:04:30 -040081
Joe Onoratoe048e8a2009-09-25 10:39:17 -070082 /** Info about the screen for clamping. */
83 private DisplayMetrics mDisplayMetrics = new DisplayMetrics();
84
Joe Onorato00acb122009-08-04 16:04:30 -040085 /** Original view that is being dragged. */
86 private View mOriginator;
87
Joe Onorato658db742010-09-29 11:40:39 -070088 /** the area at the edge of the screen that makes the workspace go left
89 * or right while you're dragging.
90 */
91 private int mScrollZone;
92
Adam Cohencb3382b2011-05-24 14:07:08 -070093 private DropTarget.DragObject mDragObject = new DropTarget.DragObject();
Joe Onorato00acb122009-08-04 16:04:30 -040094
95 /** Who can receive drop events */
96 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
97
Patrick Dubroy4ed62782010-08-17 15:11:18 -070098 private ArrayList<DragListener> mListeners = new ArrayList<DragListener>();
Joe Onorato00acb122009-08-04 16:04:30 -040099
100 /** The window token used as the parent for the DragView. */
101 private IBinder mWindowToken;
102
103 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
104 private View mScrollView;
105
Romain Guyea3763c2010-01-11 18:02:04 -0800106 private View mMoveTarget;
107
Joe Onorato00acb122009-08-04 16:04:30 -0400108 private DragScroller mDragScroller;
109 private int mScrollState = SCROLL_OUTSIDE_ZONE;
110 private ScrollRunnable mScrollRunnable = new ScrollRunnable();
111
112 private RectF mDeleteRegion;
113 private DropTarget mLastDropTarget;
114
115 private InputMethodManager mInputMethodManager;
116
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700117 private int mLastTouch[] = new int[2];
118 private int mDistanceSinceScroll = 0;
119
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 /**
121 * Interface to receive notifications when a drag starts or stops
122 */
123 interface DragListener {
124
125 /**
126 * A drag has begun
127 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 * @param source An object representing where the drag originated
129 * @param info The data associated with the object that is being dragged
130 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
131 * or {@link DragController#DRAG_ACTION_COPY}
132 */
Joe Onorato5162ea92009-09-03 09:39:42 -0700133 void onDragStart(DragSource source, Object info, int dragAction);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
135 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700136 * The drag has ended
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 */
138 void onDragEnd();
139 }
140
141 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400142 * Used to create a new DragLayer from XML.
143 *
144 * @param context The application's context.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 */
Joe Onorato00acb122009-08-04 16:04:30 -0400146 public DragController(Context context) {
147 mContext = context;
148 mHandler = new Handler();
Joe Onorato658db742010-09-29 11:40:39 -0700149 mScrollZone = context.getResources().getDimensionPixelSize(R.dimen.scroll_zone);
Joe Onorato00acb122009-08-04 16:04:30 -0400150 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Patrick Dubroy1262e362010-10-06 15:49:50 -0700152 public boolean dragging() {
153 return mDragging;
154 }
155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 /**
Joe Onorato5162ea92009-09-03 09:39:42 -0700157 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700158 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 * @param v The view that is being dragged
160 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800161 * @param dragInfo The data associated with the object that is being dragged
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
163 * {@link #DRAG_ACTION_COPY}
164 */
Joe Onorato00acb122009-08-04 16:04:30 -0400165 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700166 startDrag(v, source, dragInfo, dragAction, null);
167 }
168
169 /**
170 * Starts a drag.
171 *
172 * @param v The view that is being dragged
173 * @param source An object representing where the drag originated
174 * @param dragInfo The data associated with the object that is being dragged
175 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
176 * {@link #DRAG_ACTION_COPY}
177 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
178 * Makes dragging feel more precise, e.g. you can clip out a transparent border
179 */
180 public void startDrag(View v, DragSource source, Object dragInfo, int dragAction,
181 Rect dragRegion) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700182 mOriginator = v;
183
184 Bitmap b = getViewBitmap(v);
185
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400186 if (b == null) {
187 // out of memory?
188 return;
189 }
190
Joe Onorato5162ea92009-09-03 09:39:42 -0700191 int[] loc = mCoordinatesTemp;
192 v.getLocationOnScreen(loc);
193 int screenX = loc[0];
194 int screenY = loc[1];
195
Patrick Dubroy5f445422011-02-18 14:35:21 -0800196 startDrag(b, screenX, screenY, source, dragInfo, dragAction, dragRegion);
Joe Onorato5162ea92009-09-03 09:39:42 -0700197 b.recycle();
198
199 if (dragAction == DRAG_ACTION_MOVE) {
200 v.setVisibility(View.GONE);
201 }
202 }
203
204 /**
205 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700206 *
Winson Chunge3193b92010-09-10 11:44:42 -0700207 * @param v The view that is being dragged
208 * @param bmp The bitmap that represents the view being dragged
209 * @param source An object representing where the drag originated
210 * @param dragInfo The data associated with the object that is being dragged
211 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
212 * {@link #DRAG_ACTION_COPY}
213 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
214 * Makes dragging feel more precise, e.g. you can clip out a transparent border
215 */
216 public void startDrag(View v, Bitmap bmp, DragSource source, Object dragInfo, int dragAction,
217 Rect dragRegion) {
218 mOriginator = v;
219
220 int[] loc = mCoordinatesTemp;
221 v.getLocationOnScreen(loc);
222 int screenX = loc[0];
223 int screenY = loc[1];
224
Patrick Dubroy5f445422011-02-18 14:35:21 -0800225 startDrag(bmp, screenX, screenY, source, dragInfo, dragAction, dragRegion);
Winson Chunge3193b92010-09-10 11:44:42 -0700226
227 if (dragAction == DRAG_ACTION_MOVE) {
228 v.setVisibility(View.GONE);
229 }
230 }
231
232 /**
233 * Starts a drag.
234 *
Joe Onorato5162ea92009-09-03 09:39:42 -0700235 * @param b The bitmap to display as the drag image. It will be re-scaled to the
236 * enlarged size.
237 * @param screenX The x position on screen of the left-top of the bitmap.
238 * @param screenY The y position on screen of the left-top of the bitmap.
Joe Onorato5162ea92009-09-03 09:39:42 -0700239 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800240 * @param dragInfo The data associated with the object that is being dragged
Joe Onorato5162ea92009-09-03 09:39:42 -0700241 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
242 * {@link #DRAG_ACTION_COPY}
243 */
244 public void startDrag(Bitmap b, int screenX, int screenY,
Joe Onorato5162ea92009-09-03 09:39:42 -0700245 DragSource source, Object dragInfo, int dragAction) {
Patrick Dubroy5f445422011-02-18 14:35:21 -0800246 startDrag(b, screenX, screenY, source, dragInfo, dragAction, null);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700247 }
248
249 /**
250 * Starts a drag.
251 *
252 * @param b The bitmap to display as the drag image. It will be re-scaled to the
253 * enlarged size.
254 * @param screenX The x position on screen of the left-top of the bitmap.
255 * @param screenY The y position on screen of the left-top of the bitmap.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700256 * @param source An object representing where the drag originated
257 * @param dragInfo The data associated with the object that is being dragged
258 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
259 * {@link #DRAG_ACTION_COPY}
260 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
261 * Makes dragging feel more precise, e.g. you can clip out a transparent border
262 */
263 public void startDrag(Bitmap b, int screenX, int screenY,
Michael Jurkaa63c4522010-08-19 13:52:27 -0700264 DragSource source, Object dragInfo, int dragAction, Rect dragRegion) {
Joe Onorato00acb122009-08-04 16:04:30 -0400265 if (PROFILE_DRAWING_DURING_DRAG) {
266 android.os.Debug.startMethodTracing("Launcher");
267 }
268
269 // Hide soft keyboard, if visible
270 if (mInputMethodManager == null) {
271 mInputMethodManager = (InputMethodManager)
272 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
273 }
274 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
275
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700276 for (DragListener listener : mListeners) {
277 listener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400278 }
279
Adam Cohene3e27a82011-04-15 12:07:39 -0700280 final int registrationX = ((int)mMotionDownX) - screenX;
281 final int registrationY = ((int)mMotionDownY) - screenY;
Joe Onorato00acb122009-08-04 16:04:30 -0400282
Michael Jurkaa63c4522010-08-19 13:52:27 -0700283 final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left;
284 final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top;
Adam Cohene3e27a82011-04-15 12:07:39 -0700285
Joe Onorato00acb122009-08-04 16:04:30 -0400286 mDragging = true;
Adam Cohencb3382b2011-05-24 14:07:08 -0700287
288 mDragObject.xOffset = mMotionDownX - (screenX + dragRegionLeft);
289 mDragObject.yOffset = mMotionDownY - (screenY + dragRegionTop);
290 mDragObject.dragSource = source;
291 mDragObject.dragInfo = dragInfo;
Joe Onorato00acb122009-08-04 16:04:30 -0400292
293 mVibrator.vibrate(VIBRATE_DURATION);
294
Adam Cohencb3382b2011-05-24 14:07:08 -0700295 final DragView dragView = mDragObject.dragView = new DragView(mContext, b, registrationX,
296 registrationY, 0, 0, b.getWidth(), b.getHeight());
Michael Jurkaa63c4522010-08-19 13:52:27 -0700297
Patrick Dubroya669d792010-11-23 14:40:33 -0800298 final DragSource dragSource = source;
299 dragView.setOnDrawRunnable(new Runnable() {
300 public void run() {
301 dragSource.onDragViewVisible();
302 };
303 });
304
Michael Jurkaa63c4522010-08-19 13:52:27 -0700305 if (dragRegion != null) {
Adam Cohene3e27a82011-04-15 12:07:39 -0700306 dragView.setDragRegion(new Rect(dragRegion));
Michael Jurkaa63c4522010-08-19 13:52:27 -0700307 }
308
Joe Onorato00acb122009-08-04 16:04:30 -0400309 dragView.show(mWindowToken, (int)mMotionDownX, (int)mMotionDownY);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700310
311 handleMoveEvent((int) mMotionDownX, (int) mMotionDownY);
Joe Onorato00acb122009-08-04 16:04:30 -0400312 }
313
314 /**
315 * Draw the view into a bitmap.
316 */
Adam Cohen120980b2010-12-08 11:05:37 -0800317 Bitmap getViewBitmap(View v) {
Joe Onorato00acb122009-08-04 16:04:30 -0400318 v.clearFocus();
319 v.setPressed(false);
320
321 boolean willNotCache = v.willNotCacheDrawing();
322 v.setWillNotCacheDrawing(false);
323
324 // Reset the drawing cache background color to fully transparent
325 // for the duration of this operation
326 int color = v.getDrawingCacheBackgroundColor();
327 v.setDrawingCacheBackgroundColor(0);
Adam Cohen120980b2010-12-08 11:05:37 -0800328 float alpha = v.getAlpha();
329 v.setAlpha(1.0f);
Joe Onorato00acb122009-08-04 16:04:30 -0400330
331 if (color != 0) {
332 v.destroyDrawingCache();
333 }
334 v.buildDrawingCache();
335 Bitmap cacheBitmap = v.getDrawingCache();
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400336 if (cacheBitmap == null) {
337 Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
338 return null;
339 }
Joe Onorato00acb122009-08-04 16:04:30 -0400340
341 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
342
343 // Restore the view
344 v.destroyDrawingCache();
Adam Cohen120980b2010-12-08 11:05:37 -0800345 v.setAlpha(alpha);
Joe Onorato00acb122009-08-04 16:04:30 -0400346 v.setWillNotCacheDrawing(willNotCache);
347 v.setDrawingCacheBackgroundColor(color);
348
349 return bitmap;
350 }
351
352 /**
353 * Call this from a drag source view like this:
354 *
355 * <pre>
356 * @Override
357 * public boolean dispatchKeyEvent(KeyEvent event) {
358 * return mDragController.dispatchKeyEvent(this, event)
359 * || super.dispatchKeyEvent(event);
360 * </pre>
361 */
Romain Guyea3763c2010-01-11 18:02:04 -0800362 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato00acb122009-08-04 16:04:30 -0400363 public boolean dispatchKeyEvent(KeyEvent event) {
364 return mDragging;
365 }
366
Winson Chung304dcde2011-01-07 11:17:23 -0800367 public boolean isDragging() {
368 return mDragging;
369 }
370
Joe Onorato24b6fd82009-11-12 13:47:09 -0800371 /**
372 * Stop dragging without dropping.
373 */
374 public void cancelDrag() {
Winson Chung621e6402011-01-04 16:03:57 -0800375 if (mDragging) {
Patrick Dubroye3887cc2011-01-20 10:43:40 -0800376 // Should we also be calling onDragExit() here?
Adam Cohencb3382b2011-05-24 14:07:08 -0700377 mDragObject.dragSource.onDropCompleted(null, mDragObject.dragInfo, false);
Winson Chung621e6402011-01-04 16:03:57 -0800378 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800379 endDrag();
380 }
381
Joe Onorato00acb122009-08-04 16:04:30 -0400382 private void endDrag() {
383 if (mDragging) {
384 mDragging = false;
385 if (mOriginator != null) {
386 mOriginator.setVisibility(View.VISIBLE);
387 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700388 for (DragListener listener : mListeners) {
389 listener.onDragEnd();
Joe Onorato00acb122009-08-04 16:04:30 -0400390 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700391 if (mDragObject.dragView != null) {
392 mDragObject.dragView.remove();
393 mDragObject.dragView = null;
Joe Onorato00acb122009-08-04 16:04:30 -0400394 }
Joe Onorato00acb122009-08-04 16:04:30 -0400395 }
396 }
397
398 /**
399 * Call this from a drag source view.
400 */
401 public boolean onInterceptTouchEvent(MotionEvent ev) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400402 if (false) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800403 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400404 + mDragging);
405 }
Joe Onorato00acb122009-08-04 16:04:30 -0400406 final int action = ev.getAction();
407
Joe Onorato87467d32009-11-08 14:36:43 -0500408 if (action == MotionEvent.ACTION_DOWN) {
409 recordScreenSize();
410 }
411
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700412 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
413 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400414
415 switch (action) {
416 case MotionEvent.ACTION_MOVE:
417 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400418 case MotionEvent.ACTION_DOWN:
419 // Remember location of down touch
420 mMotionDownX = screenX;
421 mMotionDownY = screenY;
422 mLastDropTarget = null;
423 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400424 case MotionEvent.ACTION_UP:
425 if (mDragging) {
426 drop(screenX, screenY);
427 }
428 endDrag();
429 break;
Winson Chung621e6402011-01-04 16:03:57 -0800430 case MotionEvent.ACTION_CANCEL:
431 cancelDrag();
432 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400433 }
434
435 return mDragging;
436 }
437
438 /**
Romain Guyea3763c2010-01-11 18:02:04 -0800439 * Sets the view that should handle move events.
440 */
441 void setMoveTarget(View view) {
442 mMoveTarget = view;
443 }
444
445 public boolean dispatchUnhandledMove(View focused, int direction) {
446 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
447 }
448
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700449 private void handleMoveEvent(int x, int y) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700450 mDragObject.dragView.move(x, y);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700451
452 // Drop on someone?
453 final int[] coordinates = mCoordinatesTemp;
454 DropTarget dropTarget = findDropTarget(x, y, coordinates);
Adam Cohencb3382b2011-05-24 14:07:08 -0700455 mDragObject.x = coordinates[0];
456 mDragObject.y = coordinates[1];
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700457 if (dropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700458 DropTarget delegate = dropTarget.getDropTargetDelegate(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700459 if (delegate != null) {
460 dropTarget = delegate;
461 }
462
463 if (mLastDropTarget != dropTarget) {
464 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700465 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700466 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700467 dropTarget.onDragEnter(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700468 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700469 dropTarget.onDragOver(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700470 } else {
471 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700472 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700473 }
474 }
475 mLastDropTarget = dropTarget;
476
477 // Scroll, maybe, but not if we're in the delete region.
478 boolean inDeleteRegion = false;
479 if (mDeleteRegion != null) {
480 inDeleteRegion = mDeleteRegion.contains(x, y);
481 }
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700482
483 // After a scroll, the touch point will still be in the scroll region.
484 // Rather than scrolling immediately, require a bit of twiddling to scroll again
485 final int slop = ViewConfiguration.get(mContext).getScaledWindowTouchSlop();
486 mDistanceSinceScroll +=
487 Math.sqrt(Math.pow(mLastTouch[0] - x, 2) + Math.pow(mLastTouch[1] - y, 2));
488 mLastTouch[0] = x;
489 mLastTouch[1] = y;
490
Joe Onorato658db742010-09-29 11:40:39 -0700491 if (!inDeleteRegion && x < 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_LEFT);
495 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700496 mDragScroller.onEnterScrollArea(SCROLL_LEFT);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700497 }
Joe Onorato658db742010-09-29 11:40:39 -0700498 } else if (!inDeleteRegion && x > mScrollView.getWidth() - mScrollZone) {
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700499 if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700500 mScrollState = SCROLL_WAITING_IN_ZONE;
501 mScrollRunnable.setDirection(SCROLL_RIGHT);
502 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700503 mDragScroller.onEnterScrollArea(SCROLL_RIGHT);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700504 }
505 } else {
506 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
507 mScrollState = SCROLL_OUTSIDE_ZONE;
508 mScrollRunnable.setDirection(SCROLL_RIGHT);
509 mHandler.removeCallbacks(mScrollRunnable);
Patrick Dubroy1262e362010-10-06 15:49:50 -0700510 mDragScroller.onExitScrollArea();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700511 }
512 }
513 }
514
Romain Guyea3763c2010-01-11 18:02:04 -0800515 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400516 * Call this from a drag source view.
517 */
518 public boolean onTouchEvent(MotionEvent ev) {
Joe Onorato00acb122009-08-04 16:04:30 -0400519 if (!mDragging) {
520 return false;
521 }
522
523 final int action = ev.getAction();
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700524 final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
525 final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
Joe Onorato00acb122009-08-04 16:04:30 -0400526
527 switch (action) {
528 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400529 // Remember where the motion event started
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700530 mMotionDownX = screenX;
531 mMotionDownY = screenY;
Joe Onorato00acb122009-08-04 16:04:30 -0400532
Joe Onorato658db742010-09-29 11:40:39 -0700533 if ((screenX < mScrollZone) || (screenX > mScrollView.getWidth() - mScrollZone)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400534 mScrollState = SCROLL_WAITING_IN_ZONE;
535 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
536 } else {
537 mScrollState = SCROLL_OUTSIDE_ZONE;
538 }
Joe Onorato00acb122009-08-04 16:04:30 -0400539 break;
540 case MotionEvent.ACTION_MOVE:
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700541 handleMoveEvent(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400542 break;
543 case MotionEvent.ACTION_UP:
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800544 // Ensure that we've processed a move event at the current pointer location.
545 handleMoveEvent(screenX, screenY);
546
Joe Onorato00acb122009-08-04 16:04:30 -0400547 mHandler.removeCallbacks(mScrollRunnable);
548 if (mDragging) {
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700549 drop(screenX, screenY);
Joe Onorato00acb122009-08-04 16:04:30 -0400550 }
551 endDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400552 break;
553 case MotionEvent.ACTION_CANCEL:
Joe Onorato24b6fd82009-11-12 13:47:09 -0800554 cancelDrag();
Winson Chung621e6402011-01-04 16:03:57 -0800555 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400556 }
557
558 return true;
559 }
560
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800561 private void drop(float x, float y) {
Joe Onorato00acb122009-08-04 16:04:30 -0400562 final int[] coordinates = mCoordinatesTemp;
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800563 final DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400564
Adam Cohencb3382b2011-05-24 14:07:08 -0700565 mDragObject.x = coordinates[0];
566 mDragObject.y = coordinates[1];
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800567 boolean accepted = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400568 if (dropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700569 dropTarget.onDragExit(mDragObject);
570 if (dropTarget.acceptDrop(mDragObject)) {
571 dropTarget.onDrop(mDragObject);
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800572 accepted = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400573 }
574 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700575 mDragObject.dragSource.onDropCompleted((View) dropTarget, mDragObject.dragInfo, accepted);
576
Joe Onorato00acb122009-08-04 16:04:30 -0400577 }
578
579 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
580 final Rect r = mRectTemp;
581
582 final ArrayList<DropTarget> dropTargets = mDropTargets;
583 final int count = dropTargets.size();
584 for (int i=count-1; i>=0; i--) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700585 DropTarget target = dropTargets.get(i);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700586 if (!target.isDropEnabled())
587 continue;
588
Joe Onorato00acb122009-08-04 16:04:30 -0400589 target.getHitRect(r);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700590
591 // Convert the hit rect to screen coordinates
Joe Onorato00acb122009-08-04 16:04:30 -0400592 target.getLocationOnScreen(dropCoordinates);
593 r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
Patrick Dubroy440c3602010-07-13 17:50:32 -0700594
Adam Cohencb3382b2011-05-24 14:07:08 -0700595 mDragObject.x = x;
596 mDragObject.y = y;
Joe Onorato00acb122009-08-04 16:04:30 -0400597 if (r.contains(x, y)) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700598 DropTarget delegate = target.getDropTargetDelegate(mDragObject);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700599 if (delegate != null) {
600 target = delegate;
601 target.getLocationOnScreen(dropCoordinates);
602 }
603
604 // Make dropCoordinates relative to the DropTarget
Joe Onorato00acb122009-08-04 16:04:30 -0400605 dropCoordinates[0] = x - dropCoordinates[0];
606 dropCoordinates[1] = y - dropCoordinates[1];
Patrick Dubroy440c3602010-07-13 17:50:32 -0700607
Joe Onorato00acb122009-08-04 16:04:30 -0400608 return target;
609 }
610 }
611 return null;
612 }
613
Joe Onoratoe048e8a2009-09-25 10:39:17 -0700614 /**
615 * Get the screen size so we can clamp events to the screen size so even if
616 * you drag off the edge of the screen, we find something.
617 */
618 private void recordScreenSize() {
619 ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
620 .getDefaultDisplay().getMetrics(mDisplayMetrics);
621 }
622
623 /**
624 * Clamp val to be &gt;= min and &lt; max.
625 */
626 private static int clamp(int val, int min, int max) {
627 if (val < min) {
628 return min;
629 } else if (val >= max) {
630 return max - 1;
631 } else {
632 return val;
633 }
634 }
635
Joe Onorato00acb122009-08-04 16:04:30 -0400636 public void setDragScoller(DragScroller scroller) {
637 mDragScroller = scroller;
638 }
639
640 public void setWindowToken(IBinder token) {
641 mWindowToken = token;
642 }
643
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 /**
645 * Sets the drag listner which will be notified when a drag starts or ends.
646 */
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700647 public void addDragListener(DragListener l) {
648 mListeners.add(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400649 }
650
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 /**
652 * Remove a previously installed drag listener.
653 */
Joe Onorato00acb122009-08-04 16:04:30 -0400654 public void removeDragListener(DragListener l) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700655 mListeners.remove(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400656 }
657
658 /**
659 * Add a DropTarget to the list of potential places to receive drop events.
660 */
661 public void addDropTarget(DropTarget target) {
662 mDropTargets.add(target);
663 }
664
665 /**
666 * Don't send drop events to <em>target</em> any more.
667 */
668 public void removeDropTarget(DropTarget target) {
669 mDropTargets.remove(target);
670 }
671
672 /**
673 * Set which view scrolls for touch events near the edge of the screen.
674 */
675 public void setScrollView(View v) {
676 mScrollView = v;
677 }
678
679 /**
680 * Specifies the delete region. We won't scroll on touch events over the delete region.
681 *
682 * @param region The rectangle in screen coordinates of the delete region.
683 */
684 void setDeleteRegion(RectF region) {
685 mDeleteRegion = region;
686 }
687
Patrick Dubroy5f445422011-02-18 14:35:21 -0800688 DragView getDragView() {
Adam Cohencb3382b2011-05-24 14:07:08 -0700689 return mDragObject.dragView;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800690 }
691
Joe Onorato00acb122009-08-04 16:04:30 -0400692 private class ScrollRunnable implements Runnable {
693 private int mDirection;
694
695 ScrollRunnable() {
696 }
697
698 public void run() {
699 if (mDragScroller != null) {
700 if (mDirection == SCROLL_LEFT) {
701 mDragScroller.scrollLeft();
702 } else {
703 mDragScroller.scrollRight();
704 }
705 mScrollState = SCROLL_OUTSIDE_ZONE;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700706 mDistanceSinceScroll = 0;
707 mDragScroller.onExitScrollArea();
Joe Onorato00acb122009-08-04 16:04:30 -0400708 }
709 }
710
711 void setDirection(int direction) {
712 mDirection = direction;
713 }
714 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715}