blob: 09c59a057007de3086899250bde50996c4687027 [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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Winson Chungdf95eb12013-10-16 14:57:07 -070019import android.content.ComponentName;
Joe Onorato00acb122009-08-04 16:04:30 -040020import android.content.Context;
Winson Chung6e1bdaf2012-05-29 17:03:45 -070021import android.content.res.Resources;
Joe Onorato00acb122009-08-04 16:04:30 -040022import android.graphics.Bitmap;
Winson Chungb8c69f32011-10-19 21:36:08 -070023import android.graphics.Point;
Winson Chung043f2af2012-03-01 16:09:54 -080024import android.graphics.PointF;
Joe Onorato00acb122009-08-04 16:04:30 -040025import android.graphics.Rect;
Joe Onorato00acb122009-08-04 16:04:30 -040026import android.os.Handler;
Michael Jurka0280c3b2010-09-17 15:00:07 -070027import android.os.IBinder;
Joe Onorato00acb122009-08-04 16:04:30 -040028import android.util.Log;
Sunny Goyal4390ace2014-10-13 11:33:11 -070029import android.view.HapticFeedbackConstants;
30import android.view.KeyEvent;
31import android.view.MotionEvent;
32import android.view.VelocityTracker;
33import android.view.View;
34import android.view.ViewConfiguration;
Joe Onorato00acb122009-08-04 16:04:30 -040035import android.view.inputmethod.InputMethodManager;
Adam Cohenc0dcf592011-06-01 15:30:43 -070036
37import java.util.ArrayList;
Sunny Goyal4390ace2014-10-13 11:33:11 -070038import java.util.HashSet;
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 {
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
Winson Chungaa15ffe2012-01-18 15:45:28 -080052 private static final int SCROLL_DELAY = 500;
Adam Cohen2bf63d52013-09-29 17:46:49 -070053 private static final int RESCROLL_DELAY = PagedView.PAGE_SNAP_ANIMATION_DURATION + 150;
Joe Onorato00acb122009-08-04 16:04:30 -040054
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
Winson Chung043f2af2012-03-01 16:09:54 -080064 private static final float MAX_FLING_DEGREES = 35f;
Winson Chung043f2af2012-03-01 16:09:54 -080065
Adam Cohen8dfcba42011-07-07 16:38:18 -070066 private Launcher mLauncher;
Joe Onorato00acb122009-08-04 16:04:30 -040067 private Handler mHandler;
Joe Onorato00acb122009-08-04 16:04:30 -040068
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
Adam Cohenc9735cf2015-01-23 16:11:55 -080076 /** Whether or not this is an accessible drag operation */
77 private boolean mIsAccessibleDrag;
78
Joe Onorato00acb122009-08-04 16:04:30 -040079 /** X coordinate of the down event. */
Adam Cohene3e27a82011-04-15 12:07:39 -070080 private int mMotionDownX;
Joe Onorato00acb122009-08-04 16:04:30 -040081
82 /** Y coordinate of the down event. */
Adam Cohene3e27a82011-04-15 12:07:39 -070083 private int mMotionDownY;
Joe Onorato00acb122009-08-04 16:04:30 -040084
Joe Onorato658db742010-09-29 11:40:39 -070085 /** the area at the edge of the screen that makes the workspace go left
86 * or right while you're dragging.
87 */
88 private int mScrollZone;
89
Adam Cohen9932a9b2011-08-02 22:14:07 -070090 private DropTarget.DragObject mDragObject;
Joe Onorato00acb122009-08-04 16:04:30 -040091
92 /** Who can receive drop events */
93 private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>();
Patrick Dubroy4ed62782010-08-17 15:11:18 -070094 private ArrayList<DragListener> mListeners = new ArrayList<DragListener>();
Winson Chung043f2af2012-03-01 16:09:54 -080095 private DropTarget mFlingToDeleteDropTarget;
Joe Onorato00acb122009-08-04 16:04:30 -040096
97 /** The window token used as the parent for the DragView. */
98 private IBinder mWindowToken;
99
100 /** The view that will be scrolled when dragging to the left and right edges of the screen. */
101 private View mScrollView;
102
Romain Guyea3763c2010-01-11 18:02:04 -0800103 private View mMoveTarget;
104
Joe Onorato00acb122009-08-04 16:04:30 -0400105 private DragScroller mDragScroller;
106 private int mScrollState = SCROLL_OUTSIDE_ZONE;
107 private ScrollRunnable mScrollRunnable = new ScrollRunnable();
108
Joe Onorato00acb122009-08-04 16:04:30 -0400109 private DropTarget mLastDropTarget;
110
111 private InputMethodManager mInputMethodManager;
112
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700113 private int mLastTouch[] = new int[2];
Winson Chung318eee02012-04-12 10:59:27 -0700114 private long mLastTouchUpTime = -1;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700115 private int mDistanceSinceScroll = 0;
116
Winson Chung273c1022011-07-11 13:40:52 -0700117 private int mTmpPoint[] = new int[2];
118 private Rect mDragLayerRect = new Rect();
119
Winson Chung043f2af2012-03-01 16:09:54 -0800120 protected int mFlingToDeleteThresholdVelocity;
121 private VelocityTracker mVelocityTracker;
122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 /**
124 * Interface to receive notifications when a drag starts or stops
125 */
126 interface DragListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 /**
128 * A drag has begun
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700129 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 * @param source An object representing where the drag originated
131 * @param info The data associated with the object that is being dragged
132 * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE}
133 * or {@link DragController#DRAG_ACTION_COPY}
134 */
Joe Onorato5162ea92009-09-03 09:39:42 -0700135 void onDragStart(DragSource source, Object info, int dragAction);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700136
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700138 * The drag has ended
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 */
140 void onDragEnd();
141 }
142
143 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400144 * Used to create a new DragLayer from XML.
145 *
146 * @param context The application's context.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 */
Adam Cohen8dfcba42011-07-07 16:38:18 -0700148 public DragController(Launcher launcher) {
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700149 Resources r = launcher.getResources();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700150 mLauncher = launcher;
Joe Onorato00acb122009-08-04 16:04:30 -0400151 mHandler = new Handler();
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700152 mScrollZone = r.getDimensionPixelSize(R.dimen.scroll_zone);
Winson Chung043f2af2012-03-01 16:09:54 -0800153 mVelocityTracker = VelocityTracker.obtain();
154
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700155 float density = r.getDisplayMetrics().density;
156 mFlingToDeleteThresholdVelocity =
157 (int) (r.getInteger(R.integer.config_flingToDeleteMinVelocity) * density);
Joe Onorato00acb122009-08-04 16:04:30 -0400158 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Patrick Dubroy1262e362010-10-06 15:49:50 -0700160 public boolean dragging() {
161 return mDragging;
162 }
163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 /**
Joe Onorato5162ea92009-09-03 09:39:42 -0700165 * Starts a drag.
Michael Jurkaa63c4522010-08-19 13:52:27 -0700166 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 * @param v The view that is being dragged
Winson Chunge3193b92010-09-10 11:44:42 -0700168 * @param bmp The bitmap that represents the view 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, Bitmap bmp, DragSource source, Object dragInfo, int dragAction,
Michael Jurka05713af2013-01-23 12:39:24 +0100177 Point extraPadding, float initialDragViewScale) {
Winson Chunge3193b92010-09-10 11:44:42 -0700178 int[] loc = mCoordinatesTemp;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700179 mLauncher.getDragLayer().getLocationInDragLayer(v, loc);
Michael Jurka05713af2013-01-23 12:39:24 +0100180 int viewExtraPaddingLeft = extraPadding != null ? extraPadding.x : 0;
181 int viewExtraPaddingTop = extraPadding != null ? extraPadding.y : 0;
182 int dragLayerX = loc[0] + v.getPaddingLeft() + viewExtraPaddingLeft +
Winson Chung72d59842012-02-22 13:51:36 -0800183 (int) ((initialDragViewScale * bmp.getWidth() - bmp.getWidth()) / 2);
Michael Jurka05713af2013-01-23 12:39:24 +0100184 int dragLayerY = loc[1] + v.getPaddingTop() + viewExtraPaddingTop +
Winson Chung72d59842012-02-22 13:51:36 -0800185 (int) ((initialDragViewScale * bmp.getHeight() - bmp.getHeight()) / 2);
Winson Chunge3193b92010-09-10 11:44:42 -0700186
Michael Jurka05713af2013-01-23 12:39:24 +0100187 startDrag(bmp, dragLayerX, dragLayerY, source, dragInfo, dragAction, null,
Adam Cohenc9735cf2015-01-23 16:11:55 -0800188 null, initialDragViewScale, false);
Winson Chunge3193b92010-09-10 11:44:42 -0700189
190 if (dragAction == DRAG_ACTION_MOVE) {
191 v.setVisibility(View.GONE);
192 }
193 }
194
195 /**
196 * Starts a drag.
197 *
Joe Onorato5162ea92009-09-03 09:39:42 -0700198 * @param b The bitmap to display as the drag image. It will be re-scaled to the
199 * enlarged size.
Adam Cohen8dfcba42011-07-07 16:38:18 -0700200 * @param dragLayerX The x position in the DragLayer of the left-top of the bitmap.
201 * @param dragLayerY The y position in the DragLayer of the left-top of the bitmap.
Joe Onorato5162ea92009-09-03 09:39:42 -0700202 * @param source An object representing where the drag originated
Romain Guyea3763c2010-01-11 18:02:04 -0800203 * @param dragInfo The data associated with the object that is being dragged
Joe Onorato5162ea92009-09-03 09:39:42 -0700204 * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or
205 * {@link #DRAG_ACTION_COPY}
Michael Jurkaa63c4522010-08-19 13:52:27 -0700206 * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
207 * Makes dragging feel more precise, e.g. you can clip out a transparent border
Adam Cohenc9735cf2015-01-23 16:11:55 -0800208 * @param accessible whether this drag should occur in accessibility mode
Michael Jurkaa63c4522010-08-19 13:52:27 -0700209 */
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800210 public DragView startDrag(Bitmap b, int dragLayerX, int dragLayerY,
Winson Chung72d59842012-02-22 13:51:36 -0800211 DragSource source, Object dragInfo, int dragAction, Point dragOffset, Rect dragRegion,
Adam Cohenc9735cf2015-01-23 16:11:55 -0800212 float initialDragViewScale, boolean accessible) {
Joe Onorato00acb122009-08-04 16:04:30 -0400213 if (PROFILE_DRAWING_DURING_DRAG) {
214 android.os.Debug.startMethodTracing("Launcher");
215 }
216
217 // Hide soft keyboard, if visible
218 if (mInputMethodManager == null) {
219 mInputMethodManager = (InputMethodManager)
Adam Cohen8dfcba42011-07-07 16:38:18 -0700220 mLauncher.getSystemService(Context.INPUT_METHOD_SERVICE);
Joe Onorato00acb122009-08-04 16:04:30 -0400221 }
222 mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0);
223
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700224 for (DragListener listener : mListeners) {
225 listener.onDragStart(source, dragInfo, dragAction);
Joe Onorato00acb122009-08-04 16:04:30 -0400226 }
227
Adam Cohen8dfcba42011-07-07 16:38:18 -0700228 final int registrationX = mMotionDownX - dragLayerX;
229 final int registrationY = mMotionDownY - dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400230
Michael Jurkaa63c4522010-08-19 13:52:27 -0700231 final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left;
232 final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top;
Adam Cohene3e27a82011-04-15 12:07:39 -0700233
Joe Onorato00acb122009-08-04 16:04:30 -0400234 mDragging = true;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800235 mIsAccessibleDrag = accessible;
Adam Cohencb3382b2011-05-24 14:07:08 -0700236
Adam Cohen9932a9b2011-08-02 22:14:07 -0700237 mDragObject = new DropTarget.DragObject();
238
Adam Cohenbfbfd262011-06-13 16:55:12 -0700239 mDragObject.dragComplete = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800240 if (mIsAccessibleDrag) {
241 // For an accessible drag, we assume the view is being dragged from the center.
242 mDragObject.xOffset = b.getWidth() / 2;
243 mDragObject.yOffset = b.getHeight() / 2;
244 mDragObject.accessibleDrag = true;
245 } else {
246 mDragObject.xOffset = mMotionDownX - (dragLayerX + dragRegionLeft);
247 mDragObject.yOffset = mMotionDownY - (dragLayerY + dragRegionTop);
248 }
249
Adam Cohencb3382b2011-05-24 14:07:08 -0700250 mDragObject.dragSource = source;
251 mDragObject.dragInfo = dragInfo;
Joe Onorato00acb122009-08-04 16:04:30 -0400252
Adam Cohen8dfcba42011-07-07 16:38:18 -0700253 final DragView dragView = mDragObject.dragView = new DragView(mLauncher, b, registrationX,
Winson Chung72d59842012-02-22 13:51:36 -0800254 registrationY, 0, 0, b.getWidth(), b.getHeight(), initialDragViewScale);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700255
Winson Chungb8c69f32011-10-19 21:36:08 -0700256 if (dragOffset != null) {
257 dragView.setDragVisualizeOffset(new Point(dragOffset));
258 }
Michael Jurkaa63c4522010-08-19 13:52:27 -0700259 if (dragRegion != null) {
Adam Cohene3e27a82011-04-15 12:07:39 -0700260 dragView.setDragRegion(new Rect(dragRegion));
Michael Jurkaa63c4522010-08-19 13:52:27 -0700261 }
262
Winson Chung780fe592013-09-26 14:48:44 -0700263 mLauncher.getDragLayer().performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
Adam Cohen8dfcba42011-07-07 16:38:18 -0700264 dragView.show(mMotionDownX, mMotionDownY);
265 handleMoveEvent(mMotionDownX, mMotionDownY);
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800266 return dragView;
Joe Onorato00acb122009-08-04 16:04:30 -0400267 }
268
269 /**
270 * Draw the view into a bitmap.
271 */
Adam Cohen120980b2010-12-08 11:05:37 -0800272 Bitmap getViewBitmap(View v) {
Joe Onorato00acb122009-08-04 16:04:30 -0400273 v.clearFocus();
274 v.setPressed(false);
275
276 boolean willNotCache = v.willNotCacheDrawing();
277 v.setWillNotCacheDrawing(false);
278
279 // Reset the drawing cache background color to fully transparent
280 // for the duration of this operation
281 int color = v.getDrawingCacheBackgroundColor();
282 v.setDrawingCacheBackgroundColor(0);
Adam Cohen120980b2010-12-08 11:05:37 -0800283 float alpha = v.getAlpha();
284 v.setAlpha(1.0f);
Joe Onorato00acb122009-08-04 16:04:30 -0400285
286 if (color != 0) {
287 v.destroyDrawingCache();
288 }
289 v.buildDrawingCache();
290 Bitmap cacheBitmap = v.getDrawingCache();
Daniel Sandler3f8175a2010-05-25 11:48:32 -0400291 if (cacheBitmap == null) {
292 Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
293 return null;
294 }
Joe Onorato00acb122009-08-04 16:04:30 -0400295
296 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
297
298 // Restore the view
299 v.destroyDrawingCache();
Adam Cohen120980b2010-12-08 11:05:37 -0800300 v.setAlpha(alpha);
Joe Onorato00acb122009-08-04 16:04:30 -0400301 v.setWillNotCacheDrawing(willNotCache);
302 v.setDrawingCacheBackgroundColor(color);
303
304 return bitmap;
305 }
306
307 /**
308 * Call this from a drag source view like this:
309 *
310 * <pre>
311 * @Override
312 * public boolean dispatchKeyEvent(KeyEvent event) {
313 * return mDragController.dispatchKeyEvent(this, event)
314 * || super.dispatchKeyEvent(event);
315 * </pre>
316 */
317 public boolean dispatchKeyEvent(KeyEvent event) {
318 return mDragging;
319 }
320
Winson Chung304dcde2011-01-07 11:17:23 -0800321 public boolean isDragging() {
322 return mDragging;
323 }
324
Joe Onorato24b6fd82009-11-12 13:47:09 -0800325 /**
326 * Stop dragging without dropping.
327 */
328 public void cancelDrag() {
Winson Chung621e6402011-01-04 16:03:57 -0800329 if (mDragging) {
Winson Chungc07918d2011-07-01 15:35:26 -0700330 if (mLastDropTarget != null) {
331 mLastDropTarget.onDragExit(mDragObject);
332 }
Winson Chung41bb19d2012-03-05 18:36:46 -0800333 mDragObject.deferDragViewCleanupPostAnimation = false;
Adam Cohen36cc09b2011-09-29 17:33:15 -0700334 mDragObject.cancelled = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700335 mDragObject.dragComplete = true;
Winson Chunga48487a2012-03-20 16:19:37 -0700336 mDragObject.dragSource.onDropCompleted(null, mDragObject, false, false);
Winson Chung621e6402011-01-04 16:03:57 -0800337 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800338 endDrag();
339 }
Sunny Goyal4390ace2014-10-13 11:33:11 -0700340 public void onAppsRemoved(final ArrayList<String> packageNames, HashSet<ComponentName> cns) {
Winson Chunga1820962011-10-03 16:31:06 -0700341 // Cancel the current drag if we are removing an app that we are dragging
342 if (mDragObject != null) {
343 Object rawDragInfo = mDragObject.dragInfo;
344 if (rawDragInfo instanceof ShortcutInfo) {
345 ShortcutInfo dragInfo = (ShortcutInfo) rawDragInfo;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700346 for (ComponentName componentName : cns) {
Michael Jurka7bcadad2012-04-02 07:23:44 -0700347 // Added null checks to prevent NPE we've seen in the wild
Sunny Goyal4390ace2014-10-13 11:33:11 -0700348 if (dragInfo != null && dragInfo.intent != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -0700349 ComponentName cn = dragInfo.intent.getComponent();
Sunny Goyal4390ace2014-10-13 11:33:11 -0700350 boolean isSameComponent = cn != null && (cn.equals(componentName) ||
Adam Cohen60e1efe2014-06-13 19:11:57 -0700351 packageNames.contains(cn.getPackageName()));
Winson Chung83892cc2013-05-01 16:53:33 -0700352 if (isSameComponent) {
Winson Chung11a49372012-04-27 15:12:38 -0700353 cancelDrag();
354 return;
355 }
Winson Chunga1820962011-10-03 16:31:06 -0700356 }
357 }
358 }
359 }
360 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800361
Joe Onorato00acb122009-08-04 16:04:30 -0400362 private void endDrag() {
363 if (mDragging) {
364 mDragging = false;
Adam Cohenc9735cf2015-01-23 16:11:55 -0800365 mIsAccessibleDrag = false;
Winson Chungaa15ffe2012-01-18 15:45:28 -0800366 clearScrollRunnable();
Winson Chung043f2af2012-03-01 16:09:54 -0800367 boolean isDeferred = false;
Adam Cohencb3382b2011-05-24 14:07:08 -0700368 if (mDragObject.dragView != null) {
Winson Chung043f2af2012-03-01 16:09:54 -0800369 isDeferred = mDragObject.deferDragViewCleanupPostAnimation;
370 if (!isDeferred) {
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800371 mDragObject.dragView.remove();
372 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700373 mDragObject.dragView = null;
Joe Onorato00acb122009-08-04 16:04:30 -0400374 }
Winson Chung043f2af2012-03-01 16:09:54 -0800375
376 // Only end the drag if we are not deferred
377 if (!isDeferred) {
378 for (DragListener listener : mListeners) {
379 listener.onDragEnd();
380 }
381 }
382 }
383
384 releaseVelocityTracker();
385 }
386
387 /**
388 * This only gets called as a result of drag view cleanup being deferred in endDrag();
389 */
390 void onDeferredEndDrag(DragView dragView) {
391 dragView.remove();
392
Michael Jurka1e2f4652013-07-08 18:03:46 -0700393 if (mDragObject.deferDragViewCleanupPostAnimation) {
394 // If we skipped calling onDragEnd() before, do it now
395 for (DragListener listener : mListeners) {
396 listener.onDragEnd();
397 }
Joe Onorato00acb122009-08-04 16:04:30 -0400398 }
399 }
400
Winson Chunga48487a2012-03-20 16:19:37 -0700401 void onDeferredEndFling(DropTarget.DragObject d) {
402 d.dragSource.onFlingToDeleteCompleted();
403 }
404
Joe Onorato00acb122009-08-04 16:04:30 -0400405 /**
Winson Chung273c1022011-07-11 13:40:52 -0700406 * Clamps the position to the drag layer bounds.
407 */
408 private int[] getClampedDragLayerPos(float x, float y) {
409 mLauncher.getDragLayer().getLocalVisibleRect(mDragLayerRect);
410 mTmpPoint[0] = (int) Math.max(mDragLayerRect.left, Math.min(x, mDragLayerRect.right - 1));
411 mTmpPoint[1] = (int) Math.max(mDragLayerRect.top, Math.min(y, mDragLayerRect.bottom - 1));
412 return mTmpPoint;
413 }
414
Winson Chunga2413752012-04-03 14:22:34 -0700415 long getLastGestureUpTime() {
416 if (mDragging) {
417 return System.currentTimeMillis();
418 } else {
419 return mLastTouchUpTime;
420 }
421 }
422
423 void resetLastGestureUpTime() {
424 mLastTouchUpTime = -1;
425 }
426
Winson Chung273c1022011-07-11 13:40:52 -0700427 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400428 * Call this from a drag source view.
429 */
430 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700431 @SuppressWarnings("all") // suppress dead code warning
432 final boolean debug = false;
433 if (debug) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800434 Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging="
Joe Onorato9c1289c2009-08-17 11:03:03 -0400435 + mDragging);
436 }
Joe Onorato00acb122009-08-04 16:04:30 -0400437
Adam Cohenc9735cf2015-01-23 16:11:55 -0800438 if (mIsAccessibleDrag) {
439 return false;
440 }
441
Winson Chung043f2af2012-03-01 16:09:54 -0800442 // Update the velocity tracker
443 acquireVelocityTrackerAndAddMovement(ev);
444
445 final int action = ev.getAction();
Winson Chung273c1022011-07-11 13:40:52 -0700446 final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY());
447 final int dragLayerX = dragLayerPos[0];
448 final int dragLayerY = dragLayerPos[1];
Joe Onorato00acb122009-08-04 16:04:30 -0400449
450 switch (action) {
451 case MotionEvent.ACTION_MOVE:
452 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400453 case MotionEvent.ACTION_DOWN:
454 // Remember location of down touch
Adam Cohen8dfcba42011-07-07 16:38:18 -0700455 mMotionDownX = dragLayerX;
456 mMotionDownY = dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400457 mLastDropTarget = null;
458 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400459 case MotionEvent.ACTION_UP:
Winson Chunga2413752012-04-03 14:22:34 -0700460 mLastTouchUpTime = System.currentTimeMillis();
Joe Onorato00acb122009-08-04 16:04:30 -0400461 if (mDragging) {
Winson Chung043f2af2012-03-01 16:09:54 -0800462 PointF vec = isFlingingToDelete(mDragObject.dragSource);
Adam Cohen7c4c5102013-06-14 17:42:35 -0700463 if (!DeleteDropTarget.willAcceptDrop(mDragObject.dragInfo)) {
464 vec = null;
465 }
Winson Chung043f2af2012-03-01 16:09:54 -0800466 if (vec != null) {
467 dropOnFlingToDeleteTarget(dragLayerX, dragLayerY, vec);
468 } else {
469 drop(dragLayerX, dragLayerY);
470 }
Joe Onorato00acb122009-08-04 16:04:30 -0400471 }
472 endDrag();
473 break;
Winson Chung621e6402011-01-04 16:03:57 -0800474 case MotionEvent.ACTION_CANCEL:
475 cancelDrag();
476 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400477 }
478
479 return mDragging;
480 }
481
482 /**
Romain Guyea3763c2010-01-11 18:02:04 -0800483 * Sets the view that should handle move events.
484 */
485 void setMoveTarget(View view) {
486 mMoveTarget = view;
487 }
488
489 public boolean dispatchUnhandledMove(View focused, int direction) {
490 return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction);
491 }
492
Winson Chungaa15ffe2012-01-18 15:45:28 -0800493 private void clearScrollRunnable() {
494 mHandler.removeCallbacks(mScrollRunnable);
495 if (mScrollState == SCROLL_WAITING_IN_ZONE) {
496 mScrollState = SCROLL_OUTSIDE_ZONE;
497 mScrollRunnable.setDirection(SCROLL_RIGHT);
498 mDragScroller.onExitScrollArea();
Winson Chung360e63f2012-04-27 13:48:05 -0700499 mLauncher.getDragLayer().onExitScrollArea();
Winson Chungaa15ffe2012-01-18 15:45:28 -0800500 }
501 }
502
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700503 private void handleMoveEvent(int x, int y) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700504 mDragObject.dragView.move(x, y);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700505
506 // Drop on someone?
507 final int[] coordinates = mCoordinatesTemp;
508 DropTarget dropTarget = findDropTarget(x, y, coordinates);
Adam Cohencb3382b2011-05-24 14:07:08 -0700509 mDragObject.x = coordinates[0];
510 mDragObject.y = coordinates[1];
Winson Chung25460a12013-04-01 18:21:28 -0700511 checkTouchMove(dropTarget);
512
513 // Check if we are hovering over the scroll areas
514 mDistanceSinceScroll +=
515 Math.sqrt(Math.pow(mLastTouch[0] - x, 2) + Math.pow(mLastTouch[1] - y, 2));
516 mLastTouch[0] = x;
517 mLastTouch[1] = y;
518 checkScrollState(x, y);
519 }
520
521 public void forceTouchMove() {
522 int[] dummyCoordinates = mCoordinatesTemp;
523 DropTarget dropTarget = findDropTarget(mLastTouch[0], mLastTouch[1], dummyCoordinates);
Adam Cohen4ef75f52013-09-29 16:54:01 -0700524 mDragObject.x = dummyCoordinates[0];
525 mDragObject.y = dummyCoordinates[1];
Winson Chung25460a12013-04-01 18:21:28 -0700526 checkTouchMove(dropTarget);
527 }
528
529 private void checkTouchMove(DropTarget dropTarget) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700530 if (dropTarget != null) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700531 if (mLastDropTarget != dropTarget) {
532 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700533 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700534 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700535 dropTarget.onDragEnter(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700536 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700537 dropTarget.onDragOver(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700538 } else {
539 if (mLastDropTarget != null) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700540 mLastDropTarget.onDragExit(mDragObject);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700541 }
542 }
543 mLastDropTarget = dropTarget;
Winson Chung25460a12013-04-01 18:21:28 -0700544 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700545
Winson Chung25460a12013-04-01 18:21:28 -0700546 private void checkScrollState(int x, int y) {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700547 final int slop = ViewConfiguration.get(mLauncher).getScaledWindowTouchSlop();
Winson Chungaa15ffe2012-01-18 15:45:28 -0800548 final int delay = mDistanceSinceScroll < slop ? RESCROLL_DELAY : SCROLL_DELAY;
Winson Chungfe1fe262013-04-01 16:52:31 -0700549 final DragLayer dragLayer = mLauncher.getDragLayer();
550 final boolean isRtl = (dragLayer.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL);
551 final int forwardDirection = isRtl ? SCROLL_RIGHT : SCROLL_LEFT;
552 final int backwardsDirection = isRtl ? SCROLL_LEFT : SCROLL_RIGHT;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700553
Winson Chung3f4e1422011-11-17 14:58:51 -0800554 if (x < mScrollZone) {
Winson Chungaa15ffe2012-01-18 15:45:28 -0800555 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700556 mScrollState = SCROLL_WAITING_IN_ZONE;
Winson Chungfe1fe262013-04-01 16:52:31 -0700557 if (mDragScroller.onEnterScrollArea(x, y, forwardDirection)) {
558 dragLayer.onEnterScrollArea(forwardDirection);
559 mScrollRunnable.setDirection(forwardDirection);
Winson Chungaa15ffe2012-01-18 15:45:28 -0800560 mHandler.postDelayed(mScrollRunnable, delay);
Winson Chung3e0839e2011-10-03 15:15:18 -0700561 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700562 }
Winson Chung3f4e1422011-11-17 14:58:51 -0800563 } else if (x > mScrollView.getWidth() - mScrollZone) {
Winson Chungaa15ffe2012-01-18 15:45:28 -0800564 if (mScrollState == SCROLL_OUTSIDE_ZONE) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700565 mScrollState = SCROLL_WAITING_IN_ZONE;
Winson Chungfe1fe262013-04-01 16:52:31 -0700566 if (mDragScroller.onEnterScrollArea(x, y, backwardsDirection)) {
567 dragLayer.onEnterScrollArea(backwardsDirection);
568 mScrollRunnable.setDirection(backwardsDirection);
Winson Chungaa15ffe2012-01-18 15:45:28 -0800569 mHandler.postDelayed(mScrollRunnable, delay);
Winson Chung3e0839e2011-10-03 15:15:18 -0700570 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700571 }
572 } else {
Winson Chungaa15ffe2012-01-18 15:45:28 -0800573 clearScrollRunnable();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700574 }
575 }
576
Romain Guyea3763c2010-01-11 18:02:04 -0800577 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400578 * Call this from a drag source view.
579 */
580 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohenc9735cf2015-01-23 16:11:55 -0800581 if (!mDragging || mIsAccessibleDrag) {
Joe Onorato00acb122009-08-04 16:04:30 -0400582 return false;
583 }
584
Winson Chung043f2af2012-03-01 16:09:54 -0800585 // Update the velocity tracker
586 acquireVelocityTrackerAndAddMovement(ev);
587
Joe Onorato00acb122009-08-04 16:04:30 -0400588 final int action = ev.getAction();
Winson Chung273c1022011-07-11 13:40:52 -0700589 final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY());
590 final int dragLayerX = dragLayerPos[0];
591 final int dragLayerY = dragLayerPos[1];
Joe Onorato00acb122009-08-04 16:04:30 -0400592
593 switch (action) {
594 case MotionEvent.ACTION_DOWN:
Joe Onorato00acb122009-08-04 16:04:30 -0400595 // Remember where the motion event started
Adam Cohen8dfcba42011-07-07 16:38:18 -0700596 mMotionDownX = dragLayerX;
597 mMotionDownY = dragLayerY;
Joe Onorato00acb122009-08-04 16:04:30 -0400598
Adam Cohen8dfcba42011-07-07 16:38:18 -0700599 if ((dragLayerX < mScrollZone) || (dragLayerX > mScrollView.getWidth() - mScrollZone)) {
Joe Onorato00acb122009-08-04 16:04:30 -0400600 mScrollState = SCROLL_WAITING_IN_ZONE;
601 mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY);
602 } else {
603 mScrollState = SCROLL_OUTSIDE_ZONE;
604 }
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700605 handleMoveEvent(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400606 break;
607 case MotionEvent.ACTION_MOVE:
Adam Cohen8dfcba42011-07-07 16:38:18 -0700608 handleMoveEvent(dragLayerX, dragLayerY);
Joe Onorato00acb122009-08-04 16:04:30 -0400609 break;
610 case MotionEvent.ACTION_UP:
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800611 // Ensure that we've processed a move event at the current pointer location.
Adam Cohen8dfcba42011-07-07 16:38:18 -0700612 handleMoveEvent(dragLayerX, dragLayerY);
Winson Chung3bc21c32012-01-20 13:59:18 -0800613 mHandler.removeCallbacks(mScrollRunnable);
Winson Chung043f2af2012-03-01 16:09:54 -0800614
Joe Onorato00acb122009-08-04 16:04:30 -0400615 if (mDragging) {
Winson Chung043f2af2012-03-01 16:09:54 -0800616 PointF vec = isFlingingToDelete(mDragObject.dragSource);
Adam Cohen7c4c5102013-06-14 17:42:35 -0700617 if (!DeleteDropTarget.willAcceptDrop(mDragObject.dragInfo)) {
Adam Cohen947dc542013-06-06 22:43:33 -0700618 vec = null;
619 }
Winson Chung043f2af2012-03-01 16:09:54 -0800620 if (vec != null) {
621 dropOnFlingToDeleteTarget(dragLayerX, dragLayerY, vec);
622 } else {
623 drop(dragLayerX, dragLayerY);
624 }
Joe Onorato00acb122009-08-04 16:04:30 -0400625 }
626 endDrag();
Joe Onorato00acb122009-08-04 16:04:30 -0400627 break;
628 case MotionEvent.ACTION_CANCEL:
Winson Chung3bc21c32012-01-20 13:59:18 -0800629 mHandler.removeCallbacks(mScrollRunnable);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800630 cancelDrag();
Winson Chung621e6402011-01-04 16:03:57 -0800631 break;
Joe Onorato00acb122009-08-04 16:04:30 -0400632 }
633
634 return true;
635 }
636
Winson Chung043f2af2012-03-01 16:09:54 -0800637 /**
Adam Cohenc9735cf2015-01-23 16:11:55 -0800638 * Since accessible drag and drop won't cause the same sequence of touch events, we manually
639 * inject the appropriate state.
640 */
641 public void prepareAccessibleDrag(int x, int y) {
642 mMotionDownX = x;
643 mMotionDownY = y;
644 mLastDropTarget = null;
645 }
646
647 /**
648 * As above, since accessible drag and drop won't cause the same sequence of touch events,
649 * we manually ensure appropriate drag and drop events get emulated for accessible drag.
650 */
651 public void completeAccessibleDrag(int[] location) {
652 final int[] coordinates = mCoordinatesTemp;
653
654 // We make sure that we prime the target for drop.
655 DropTarget dropTarget = findDropTarget(location[0], location[1], coordinates);
656 mDragObject.x = coordinates[0];
657 mDragObject.y = coordinates[1];
658 checkTouchMove(dropTarget);
659
660 // Perform the drop
661 drop(location[0], location[1]);
662 endDrag();
663 }
664
665 /**
Winson Chung043f2af2012-03-01 16:09:54 -0800666 * Determines whether the user flung the current item to delete it.
667 *
668 * @return the vector at which the item was flung, or null if no fling was detected.
669 */
670 private PointF isFlingingToDelete(DragSource source) {
671 if (mFlingToDeleteDropTarget == null) return null;
672 if (!source.supportsFlingToDelete()) return null;
673
674 ViewConfiguration config = ViewConfiguration.get(mLauncher);
675 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
676
677 if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
678 // Do a quick dot product test to ensure that we are flinging upwards
679 PointF vel = new PointF(mVelocityTracker.getXVelocity(),
680 mVelocityTracker.getYVelocity());
681 PointF upVec = new PointF(0f, -1f);
682 float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) /
683 (vel.length() * upVec.length()));
684 if (theta <= Math.toRadians(MAX_FLING_DEGREES)) {
685 return vel;
686 }
687 }
688 return null;
689 }
690
691 private void dropOnFlingToDeleteTarget(float x, float y, PointF vel) {
692 final int[] coordinates = mCoordinatesTemp;
693
694 mDragObject.x = coordinates[0];
695 mDragObject.y = coordinates[1];
Winson Chung043f2af2012-03-01 16:09:54 -0800696
697 // Clean up dragging on the target if it's not the current fling delete target otherwise,
698 // start dragging to it.
699 if (mLastDropTarget != null && mFlingToDeleteDropTarget != mLastDropTarget) {
700 mLastDropTarget.onDragExit(mDragObject);
701 }
702
703 // Drop onto the fling-to-delete target
704 boolean accepted = false;
705 mFlingToDeleteDropTarget.onDragEnter(mDragObject);
Winson Chung232decb2012-03-28 15:09:05 -0700706 // We must set dragComplete to true _only_ after we "enter" the fling-to-delete target for
707 // "drop"
708 mDragObject.dragComplete = true;
Winson Chung043f2af2012-03-01 16:09:54 -0800709 mFlingToDeleteDropTarget.onDragExit(mDragObject);
710 if (mFlingToDeleteDropTarget.acceptDrop(mDragObject)) {
711 mFlingToDeleteDropTarget.onFlingToDelete(mDragObject, mDragObject.x, mDragObject.y,
712 vel);
713 accepted = true;
714 }
Winson Chunga48487a2012-03-20 16:19:37 -0700715 mDragObject.dragSource.onDropCompleted((View) mFlingToDeleteDropTarget, mDragObject, true,
Winson Chung043f2af2012-03-01 16:09:54 -0800716 accepted);
717 }
718
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800719 private void drop(float x, float y) {
Joe Onorato00acb122009-08-04 16:04:30 -0400720 final int[] coordinates = mCoordinatesTemp;
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800721 final DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates);
Joe Onorato00acb122009-08-04 16:04:30 -0400722
Adam Cohencb3382b2011-05-24 14:07:08 -0700723 mDragObject.x = coordinates[0];
724 mDragObject.y = coordinates[1];
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800725 boolean accepted = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400726 if (dropTarget != null) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700727 mDragObject.dragComplete = true;
Adam Cohencb3382b2011-05-24 14:07:08 -0700728 dropTarget.onDragExit(mDragObject);
729 if (dropTarget.acceptDrop(mDragObject)) {
730 dropTarget.onDrop(mDragObject);
Patrick Dubroyb0a6bbe2011-03-02 18:40:21 -0800731 accepted = true;
Joe Onorato00acb122009-08-04 16:04:30 -0400732 }
733 }
Winson Chunga48487a2012-03-20 16:19:37 -0700734 mDragObject.dragSource.onDropCompleted((View) dropTarget, mDragObject, false, accepted);
Joe Onorato00acb122009-08-04 16:04:30 -0400735 }
736
737 private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
738 final Rect r = mRectTemp;
739
740 final ArrayList<DropTarget> dropTargets = mDropTargets;
741 final int count = dropTargets.size();
742 for (int i=count-1; i>=0; i--) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700743 DropTarget target = dropTargets.get(i);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700744 if (!target.isDropEnabled())
745 continue;
746
Adam Cohen7d30a372013-07-01 17:03:59 -0700747 target.getHitRectRelativeToDragLayer(r);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700748
Adam Cohencb3382b2011-05-24 14:07:08 -0700749 mDragObject.x = x;
750 mDragObject.y = y;
Joe Onorato00acb122009-08-04 16:04:30 -0400751 if (r.contains(x, y)) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700752
Adam Cohen7d30a372013-07-01 17:03:59 -0700753 dropCoordinates[0] = x;
754 dropCoordinates[1] = y;
755 mLauncher.getDragLayer().mapCoordInSelfToDescendent((View) target, dropCoordinates);
Patrick Dubroy440c3602010-07-13 17:50:32 -0700756
Joe Onorato00acb122009-08-04 16:04:30 -0400757 return target;
758 }
759 }
760 return null;
761 }
762
763 public void setDragScoller(DragScroller scroller) {
764 mDragScroller = scroller;
765 }
766
767 public void setWindowToken(IBinder token) {
768 mWindowToken = token;
769 }
770
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 /**
772 * Sets the drag listner which will be notified when a drag starts or ends.
773 */
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700774 public void addDragListener(DragListener l) {
775 mListeners.add(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400776 }
777
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 /**
779 * Remove a previously installed drag listener.
780 */
Joe Onorato00acb122009-08-04 16:04:30 -0400781 public void removeDragListener(DragListener l) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700782 mListeners.remove(l);
Joe Onorato00acb122009-08-04 16:04:30 -0400783 }
784
785 /**
786 * Add a DropTarget to the list of potential places to receive drop events.
787 */
788 public void addDropTarget(DropTarget target) {
789 mDropTargets.add(target);
790 }
791
792 /**
793 * Don't send drop events to <em>target</em> any more.
794 */
795 public void removeDropTarget(DropTarget target) {
796 mDropTargets.remove(target);
797 }
798
799 /**
Winson Chung043f2af2012-03-01 16:09:54 -0800800 * Sets the current fling-to-delete drop target.
801 */
802 public void setFlingToDeleteDropTarget(DropTarget target) {
803 mFlingToDeleteDropTarget = target;
804 }
805
806 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
807 if (mVelocityTracker == null) {
808 mVelocityTracker = VelocityTracker.obtain();
809 }
810 mVelocityTracker.addMovement(ev);
811 }
812
813 private void releaseVelocityTracker() {
814 if (mVelocityTracker != null) {
815 mVelocityTracker.recycle();
816 mVelocityTracker = null;
817 }
818 }
819
820 /**
Joe Onorato00acb122009-08-04 16:04:30 -0400821 * Set which view scrolls for touch events near the edge of the screen.
822 */
823 public void setScrollView(View v) {
824 mScrollView = v;
825 }
826
Patrick Dubroy5f445422011-02-18 14:35:21 -0800827 DragView getDragView() {
Adam Cohencb3382b2011-05-24 14:07:08 -0700828 return mDragObject.dragView;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800829 }
830
Joe Onorato00acb122009-08-04 16:04:30 -0400831 private class ScrollRunnable implements Runnable {
832 private int mDirection;
833
834 ScrollRunnable() {
835 }
836
837 public void run() {
838 if (mDragScroller != null) {
839 if (mDirection == SCROLL_LEFT) {
840 mDragScroller.scrollLeft();
841 } else {
842 mDragScroller.scrollRight();
843 }
844 mScrollState = SCROLL_OUTSIDE_ZONE;
Patrick Dubroya16fd5a2010-10-07 16:47:28 -0700845 mDistanceSinceScroll = 0;
846 mDragScroller.onExitScrollArea();
Winson Chung360e63f2012-04-27 13:48:05 -0700847 mLauncher.getDragLayer().onExitScrollArea();
Winson Chungaa15ffe2012-01-18 15:45:28 -0800848
849 if (isDragging()) {
Winson Chung25460a12013-04-01 18:21:28 -0700850 // Check the scroll again so that we can requeue the scroller if necessary
851 checkScrollState(mLastTouch[0], mLastTouch[1]);
Winson Chungaa15ffe2012-01-18 15:45:28 -0800852 }
Joe Onorato00acb122009-08-04 16:04:30 -0400853 }
854 }
855
856 void setDirection(int direction) {
857 mDirection = direction;
858 }
859 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860}