The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.graphics.Bitmap; |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 21 | import android.graphics.Point; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame^] | 22 | import android.graphics.PointF; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 23 | import android.graphics.Rect; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 24 | import android.os.Handler; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 25 | import android.os.IBinder; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 26 | import android.os.Vibrator; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 27 | import android.util.Log; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 28 | import android.view.KeyEvent; |
| 29 | import android.view.MotionEvent; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame^] | 30 | import android.view.VelocityTracker; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 31 | import android.view.View; |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 32 | import android.view.ViewConfiguration; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 33 | import android.view.inputmethod.InputMethodManager; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 34 | |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 35 | import com.android.launcher.R; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 36 | |
| 37 | import java.util.ArrayList; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 40 | * Class for initiating a drag within a view or across multiple views. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 41 | */ |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 42 | public class DragController { |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 43 | @SuppressWarnings({"UnusedDeclaration"}) |
Joe Onorato | 2e5c432 | 2009-10-06 12:34:42 -0700 | [diff] [blame] | 44 | private static final String TAG = "Launcher.DragController"; |
| 45 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 46 | /** 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 Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 52 | private static final int SCROLL_DELAY = 500; |
| 53 | private static final int RESCROLL_DELAY = 750; |
Winson Chung | 61b0c69 | 2012-02-23 16:31:13 -0800 | [diff] [blame] | 54 | private static final int VIBRATE_DURATION = 15; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 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 Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 61 | static final int SCROLL_NONE = -1; |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 62 | static final int SCROLL_LEFT = 0; |
| 63 | static final int SCROLL_RIGHT = 1; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 64 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame^] | 65 | private static final float MAX_FLING_DEGREES = 35f; |
| 66 | private static final int FLING_TO_DELETE_THRESHOLD_Y_VELOCITY = -1400; |
| 67 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 68 | private Launcher mLauncher; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 69 | private Handler mHandler; |
| 70 | private final Vibrator mVibrator = new Vibrator(); |
| 71 | |
| 72 | // temporaries to avoid gc thrash |
| 73 | private Rect mRectTemp = new Rect(); |
| 74 | private final int[] mCoordinatesTemp = new int[2]; |
| 75 | |
| 76 | /** Whether or not we're dragging. */ |
| 77 | private boolean mDragging; |
| 78 | |
| 79 | /** X coordinate of the down event. */ |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 80 | private int mMotionDownX; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 81 | |
| 82 | /** Y coordinate of the down event. */ |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 83 | private int mMotionDownY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 84 | |
Joe Onorato | 658db74 | 2010-09-29 11:40:39 -0700 | [diff] [blame] | 85 | /** 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 Cohen | 9932a9b | 2011-08-02 22:14:07 -0700 | [diff] [blame] | 90 | private DropTarget.DragObject mDragObject; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 91 | |
| 92 | /** Who can receive drop events */ |
| 93 | private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>(); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 94 | private ArrayList<DragListener> mListeners = new ArrayList<DragListener>(); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame^] | 95 | private DropTarget mFlingToDeleteDropTarget; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 96 | |
| 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 Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 103 | private View mMoveTarget; |
| 104 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 105 | private DragScroller mDragScroller; |
| 106 | private int mScrollState = SCROLL_OUTSIDE_ZONE; |
| 107 | private ScrollRunnable mScrollRunnable = new ScrollRunnable(); |
| 108 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 109 | private DropTarget mLastDropTarget; |
| 110 | |
| 111 | private InputMethodManager mInputMethodManager; |
| 112 | |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 113 | private int mLastTouch[] = new int[2]; |
| 114 | private int mDistanceSinceScroll = 0; |
| 115 | |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 116 | private int mTmpPoint[] = new int[2]; |
| 117 | private Rect mDragLayerRect = new Rect(); |
| 118 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame^] | 119 | protected int mFlingToDeleteThresholdVelocity; |
| 120 | private VelocityTracker mVelocityTracker; |
| 121 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 122 | /** |
| 123 | * Interface to receive notifications when a drag starts or stops |
| 124 | */ |
| 125 | interface DragListener { |
| 126 | |
| 127 | /** |
| 128 | * A drag has begun |
| 129 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 130 | * @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 Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 135 | void onDragStart(DragSource source, Object info, int dragAction); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 136 | |
| 137 | /** |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 138 | * The drag has ended |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 139 | */ |
| 140 | void onDragEnd(); |
| 141 | } |
| 142 | |
| 143 | /** |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 144 | * Used to create a new DragLayer from XML. |
| 145 | * |
| 146 | * @param context The application's context. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 147 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 148 | public DragController(Launcher launcher) { |
| 149 | mLauncher = launcher; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 150 | mHandler = new Handler(); |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 151 | mScrollZone = launcher.getResources().getDimensionPixelSize(R.dimen.scroll_zone); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame^] | 152 | mVelocityTracker = VelocityTracker.obtain(); |
| 153 | |
| 154 | float density = launcher.getResources().getDisplayMetrics().density; |
| 155 | mFlingToDeleteThresholdVelocity = (int) (FLING_TO_DELETE_THRESHOLD_Y_VELOCITY * density); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 156 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 157 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 158 | public boolean dragging() { |
| 159 | return mDragging; |
| 160 | } |
| 161 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 162 | /** |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 163 | * Starts a drag. |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 164 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 165 | * @param v The view that is being dragged |
| 166 | * @param source An object representing where the drag originated |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 167 | * @param dragInfo The data associated with the object that is being dragged |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 168 | * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or |
| 169 | * {@link #DRAG_ACTION_COPY} |
| 170 | */ |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 171 | public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) { |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 172 | startDrag(v, source, dragInfo, dragAction, null); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Starts a drag. |
| 177 | * |
| 178 | * @param v The view that is being dragged |
| 179 | * @param source An object representing where the drag originated |
| 180 | * @param dragInfo The data associated with the object that is being dragged |
| 181 | * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or |
| 182 | * {@link #DRAG_ACTION_COPY} |
| 183 | * @param dragRegion Coordinates within the bitmap b for the position of item being dragged. |
| 184 | * Makes dragging feel more precise, e.g. you can clip out a transparent border |
| 185 | */ |
| 186 | public void startDrag(View v, DragSource source, Object dragInfo, int dragAction, |
| 187 | Rect dragRegion) { |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 188 | Bitmap b = getViewBitmap(v); |
| 189 | |
Daniel Sandler | 3f8175a | 2010-05-25 11:48:32 -0400 | [diff] [blame] | 190 | if (b == null) { |
| 191 | // out of memory? |
| 192 | return; |
| 193 | } |
| 194 | |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 195 | int[] loc = mCoordinatesTemp; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 196 | mLauncher.getDragLayer().getLocationInDragLayer(v, loc); |
| 197 | int dragLayerX = loc[0]; |
| 198 | int dragLayerY = loc[1]; |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 199 | |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 200 | startDrag(b, dragLayerX, dragLayerY, source, dragInfo, dragAction, null, dragRegion, 1f); |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 201 | b.recycle(); |
| 202 | |
| 203 | if (dragAction == DRAG_ACTION_MOVE) { |
| 204 | v.setVisibility(View.GONE); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Starts a drag. |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 210 | * |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 211 | * @param v The view that is being dragged |
| 212 | * @param bmp The bitmap that represents the view being dragged |
| 213 | * @param source An object representing where the drag originated |
| 214 | * @param dragInfo The data associated with the object that is being dragged |
| 215 | * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or |
| 216 | * {@link #DRAG_ACTION_COPY} |
| 217 | * @param dragRegion Coordinates within the bitmap b for the position of item being dragged. |
| 218 | * Makes dragging feel more precise, e.g. you can clip out a transparent border |
| 219 | */ |
| 220 | public void startDrag(View v, Bitmap bmp, DragSource source, Object dragInfo, int dragAction, |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 221 | Rect dragRegion, float initialDragViewScale) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 222 | int[] loc = mCoordinatesTemp; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 223 | mLauncher.getDragLayer().getLocationInDragLayer(v, loc); |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 224 | int dragLayerX = loc[0] + v.getPaddingLeft() + |
| 225 | (int) ((initialDragViewScale * bmp.getWidth() - bmp.getWidth()) / 2); |
| 226 | int dragLayerY = loc[1] + v.getPaddingTop() + |
| 227 | (int) ((initialDragViewScale * bmp.getHeight() - bmp.getHeight()) / 2); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 228 | |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 229 | startDrag(bmp, dragLayerX, dragLayerY, source, dragInfo, dragAction, null, dragRegion, |
| 230 | initialDragViewScale); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 231 | |
| 232 | if (dragAction == DRAG_ACTION_MOVE) { |
| 233 | v.setVisibility(View.GONE); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Starts a drag. |
| 239 | * |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 240 | * @param b The bitmap to display as the drag image. It will be re-scaled to the |
| 241 | * enlarged size. |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 242 | * @param dragLayerX The x position in the DragLayer of the left-top of the bitmap. |
| 243 | * @param dragLayerY The y position in the DragLayer of the left-top of the bitmap. |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 244 | * @param source An object representing where the drag originated |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 245 | * @param dragInfo The data associated with the object that is being dragged |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 246 | * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or |
| 247 | * {@link #DRAG_ACTION_COPY} |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 248 | * @param dragRegion Coordinates within the bitmap b for the position of item being dragged. |
| 249 | * Makes dragging feel more precise, e.g. you can clip out a transparent border |
| 250 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 251 | public void startDrag(Bitmap b, int dragLayerX, int dragLayerY, |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 252 | DragSource source, Object dragInfo, int dragAction, Point dragOffset, Rect dragRegion, |
| 253 | float initialDragViewScale) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 254 | if (PROFILE_DRAWING_DURING_DRAG) { |
| 255 | android.os.Debug.startMethodTracing("Launcher"); |
| 256 | } |
| 257 | |
| 258 | // Hide soft keyboard, if visible |
| 259 | if (mInputMethodManager == null) { |
| 260 | mInputMethodManager = (InputMethodManager) |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 261 | mLauncher.getSystemService(Context.INPUT_METHOD_SERVICE); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 262 | } |
| 263 | mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0); |
| 264 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 265 | for (DragListener listener : mListeners) { |
| 266 | listener.onDragStart(source, dragInfo, dragAction); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 267 | } |
| 268 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 269 | final int registrationX = mMotionDownX - dragLayerX; |
| 270 | final int registrationY = mMotionDownY - dragLayerY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 271 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 272 | final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left; |
| 273 | final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 274 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 275 | mDragging = true; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 276 | |
Adam Cohen | 9932a9b | 2011-08-02 22:14:07 -0700 | [diff] [blame] | 277 | mDragObject = new DropTarget.DragObject(); |
| 278 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 279 | mDragObject.dragComplete = false; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 280 | mDragObject.xOffset = mMotionDownX - (dragLayerX + dragRegionLeft); |
| 281 | mDragObject.yOffset = mMotionDownY - (dragLayerY + dragRegionTop); |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 282 | mDragObject.dragSource = source; |
| 283 | mDragObject.dragInfo = dragInfo; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 284 | |
| 285 | mVibrator.vibrate(VIBRATE_DURATION); |
| 286 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 287 | final DragView dragView = mDragObject.dragView = new DragView(mLauncher, b, registrationX, |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 288 | registrationY, 0, 0, b.getWidth(), b.getHeight(), initialDragViewScale); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 289 | |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 290 | if (dragOffset != null) { |
| 291 | dragView.setDragVisualizeOffset(new Point(dragOffset)); |
| 292 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 293 | if (dragRegion != null) { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 294 | dragView.setDragRegion(new Rect(dragRegion)); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 297 | dragView.show(mMotionDownX, mMotionDownY); |
| 298 | handleMoveEvent(mMotionDownX, mMotionDownY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Draw the view into a bitmap. |
| 303 | */ |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 304 | Bitmap getViewBitmap(View v) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 305 | v.clearFocus(); |
| 306 | v.setPressed(false); |
| 307 | |
| 308 | boolean willNotCache = v.willNotCacheDrawing(); |
| 309 | v.setWillNotCacheDrawing(false); |
| 310 | |
| 311 | // Reset the drawing cache background color to fully transparent |
| 312 | // for the duration of this operation |
| 313 | int color = v.getDrawingCacheBackgroundColor(); |
| 314 | v.setDrawingCacheBackgroundColor(0); |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 315 | float alpha = v.getAlpha(); |
| 316 | v.setAlpha(1.0f); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 317 | |
| 318 | if (color != 0) { |
| 319 | v.destroyDrawingCache(); |
| 320 | } |
| 321 | v.buildDrawingCache(); |
| 322 | Bitmap cacheBitmap = v.getDrawingCache(); |
Daniel Sandler | 3f8175a | 2010-05-25 11:48:32 -0400 | [diff] [blame] | 323 | if (cacheBitmap == null) { |
| 324 | Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException()); |
| 325 | return null; |
| 326 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 327 | |
| 328 | Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); |
| 329 | |
| 330 | // Restore the view |
| 331 | v.destroyDrawingCache(); |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 332 | v.setAlpha(alpha); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 333 | v.setWillNotCacheDrawing(willNotCache); |
| 334 | v.setDrawingCacheBackgroundColor(color); |
| 335 | |
| 336 | return bitmap; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Call this from a drag source view like this: |
| 341 | * |
| 342 | * <pre> |
| 343 | * @Override |
| 344 | * public boolean dispatchKeyEvent(KeyEvent event) { |
| 345 | * return mDragController.dispatchKeyEvent(this, event) |
| 346 | * || super.dispatchKeyEvent(event); |
| 347 | * </pre> |
| 348 | */ |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 349 | @SuppressWarnings({"UnusedDeclaration"}) |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 350 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 351 | return mDragging; |
| 352 | } |
| 353 | |
Winson Chung | 304dcde | 2011-01-07 11:17:23 -0800 | [diff] [blame] | 354 | public boolean isDragging() { |
| 355 | return mDragging; |
| 356 | } |
| 357 | |
Joe Onorato | 24b6fd8 | 2009-11-12 13:47:09 -0800 | [diff] [blame] | 358 | /** |
| 359 | * Stop dragging without dropping. |
| 360 | */ |
| 361 | public void cancelDrag() { |
Winson Chung | 621e640 | 2011-01-04 16:03:57 -0800 | [diff] [blame] | 362 | if (mDragging) { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 363 | if (mLastDropTarget != null) { |
| 364 | mLastDropTarget.onDragExit(mDragObject); |
| 365 | } |
Winson Chung | 41bb19d | 2012-03-05 18:36:46 -0800 | [diff] [blame] | 366 | mDragObject.deferDragViewCleanupPostAnimation = false; |
Adam Cohen | 36cc09b | 2011-09-29 17:33:15 -0700 | [diff] [blame] | 367 | mDragObject.cancelled = true; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 368 | mDragObject.dragComplete = true; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 369 | mDragObject.dragSource.onDropCompleted(null, mDragObject, false); |
Winson Chung | 621e640 | 2011-01-04 16:03:57 -0800 | [diff] [blame] | 370 | } |
Joe Onorato | 24b6fd8 | 2009-11-12 13:47:09 -0800 | [diff] [blame] | 371 | endDrag(); |
| 372 | } |
Winson Chung | a182096 | 2011-10-03 16:31:06 -0700 | [diff] [blame] | 373 | public void onAppsRemoved(ArrayList<ApplicationInfo> apps, Context context) { |
| 374 | // Cancel the current drag if we are removing an app that we are dragging |
| 375 | if (mDragObject != null) { |
| 376 | Object rawDragInfo = mDragObject.dragInfo; |
| 377 | if (rawDragInfo instanceof ShortcutInfo) { |
| 378 | ShortcutInfo dragInfo = (ShortcutInfo) rawDragInfo; |
| 379 | for (ApplicationInfo info : apps) { |
| 380 | if (dragInfo.intent.getComponent().equals(info.intent.getComponent())) { |
| 381 | cancelDrag(); |
| 382 | return; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | } |
Joe Onorato | 24b6fd8 | 2009-11-12 13:47:09 -0800 | [diff] [blame] | 388 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 389 | private void endDrag() { |
| 390 | if (mDragging) { |
| 391 | mDragging = false; |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 392 | clearScrollRunnable(); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame^] | 393 | boolean isDeferred = false; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 394 | if (mDragObject.dragView != null) { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame^] | 395 | isDeferred = mDragObject.deferDragViewCleanupPostAnimation; |
| 396 | if (!isDeferred) { |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 397 | mDragObject.dragView.remove(); |
| 398 | } |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 399 | mDragObject.dragView = null; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 400 | } |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame^] | 401 | |
| 402 | // Only end the drag if we are not deferred |
| 403 | if (!isDeferred) { |
| 404 | for (DragListener listener : mListeners) { |
| 405 | listener.onDragEnd(); |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | releaseVelocityTracker(); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * This only gets called as a result of drag view cleanup being deferred in endDrag(); |
| 415 | */ |
| 416 | void onDeferredEndDrag(DragView dragView) { |
| 417 | dragView.remove(); |
| 418 | |
| 419 | // If we skipped calling onDragEnd() before, do it now |
| 420 | for (DragListener listener : mListeners) { |
| 421 | listener.onDragEnd(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
| 425 | /** |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 426 | * Clamps the position to the drag layer bounds. |
| 427 | */ |
| 428 | private int[] getClampedDragLayerPos(float x, float y) { |
| 429 | mLauncher.getDragLayer().getLocalVisibleRect(mDragLayerRect); |
| 430 | mTmpPoint[0] = (int) Math.max(mDragLayerRect.left, Math.min(x, mDragLayerRect.right - 1)); |
| 431 | mTmpPoint[1] = (int) Math.max(mDragLayerRect.top, Math.min(y, mDragLayerRect.bottom - 1)); |
| 432 | return mTmpPoint; |
| 433 | } |
| 434 | |
| 435 | /** |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 436 | * Call this from a drag source view. |
| 437 | */ |
| 438 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 439 | if (false) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 440 | Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging=" |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 441 | + mDragging); |
| 442 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 443 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame^] | 444 | // Update the velocity tracker |
| 445 | acquireVelocityTrackerAndAddMovement(ev); |
| 446 | |
| 447 | final int action = ev.getAction(); |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 448 | final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY()); |
| 449 | final int dragLayerX = dragLayerPos[0]; |
| 450 | final int dragLayerY = dragLayerPos[1]; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 451 | |
| 452 | switch (action) { |
| 453 | case MotionEvent.ACTION_MOVE: |
| 454 | break; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 455 | case MotionEvent.ACTION_DOWN: |
| 456 | // Remember location of down touch |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 457 | mMotionDownX = dragLayerX; |
| 458 | mMotionDownY = dragLayerY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 459 | mLastDropTarget = null; |
| 460 | break; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 461 | case MotionEvent.ACTION_UP: |
| 462 | if (mDragging) { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame^] | 463 | PointF vec = isFlingingToDelete(mDragObject.dragSource); |
| 464 | if (vec != null) { |
| 465 | dropOnFlingToDeleteTarget(dragLayerX, dragLayerY, vec); |
| 466 | } else { |
| 467 | drop(dragLayerX, dragLayerY); |
| 468 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 469 | } |
| 470 | endDrag(); |
| 471 | break; |
Winson Chung | 621e640 | 2011-01-04 16:03:57 -0800 | [diff] [blame] | 472 | case MotionEvent.ACTION_CANCEL: |
| 473 | cancelDrag(); |
| 474 | break; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | return mDragging; |
| 478 | } |
| 479 | |
| 480 | /** |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 481 | * Sets the view that should handle move events. |
| 482 | */ |
| 483 | void setMoveTarget(View view) { |
| 484 | mMoveTarget = view; |
| 485 | } |
| 486 | |
| 487 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 488 | return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction); |
| 489 | } |
| 490 | |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 491 | private void clearScrollRunnable() { |
| 492 | mHandler.removeCallbacks(mScrollRunnable); |
| 493 | if (mScrollState == SCROLL_WAITING_IN_ZONE) { |
| 494 | mScrollState = SCROLL_OUTSIDE_ZONE; |
| 495 | mScrollRunnable.setDirection(SCROLL_RIGHT); |
| 496 | mDragScroller.onExitScrollArea(); |
| 497 | } |
| 498 | } |
| 499 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 500 | private void handleMoveEvent(int x, int y) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 501 | mDragObject.dragView.move(x, y); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 502 | |
| 503 | // Drop on someone? |
| 504 | final int[] coordinates = mCoordinatesTemp; |
| 505 | DropTarget dropTarget = findDropTarget(x, y, coordinates); |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 506 | mDragObject.x = coordinates[0]; |
| 507 | mDragObject.y = coordinates[1]; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 508 | if (dropTarget != null) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 509 | DropTarget delegate = dropTarget.getDropTargetDelegate(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 510 | if (delegate != null) { |
| 511 | dropTarget = delegate; |
| 512 | } |
| 513 | |
| 514 | if (mLastDropTarget != dropTarget) { |
| 515 | if (mLastDropTarget != null) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 516 | mLastDropTarget.onDragExit(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 517 | } |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 518 | dropTarget.onDragEnter(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 519 | } |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 520 | dropTarget.onDragOver(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 521 | } else { |
| 522 | if (mLastDropTarget != null) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 523 | mLastDropTarget.onDragExit(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | mLastDropTarget = dropTarget; |
| 527 | |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 528 | // After a scroll, the touch point will still be in the scroll region. |
| 529 | // Rather than scrolling immediately, require a bit of twiddling to scroll again |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 530 | final int slop = ViewConfiguration.get(mLauncher).getScaledWindowTouchSlop(); |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 531 | mDistanceSinceScroll += |
| 532 | Math.sqrt(Math.pow(mLastTouch[0] - x, 2) + Math.pow(mLastTouch[1] - y, 2)); |
| 533 | mLastTouch[0] = x; |
| 534 | mLastTouch[1] = y; |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 535 | final int delay = mDistanceSinceScroll < slop ? RESCROLL_DELAY : SCROLL_DELAY; |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 536 | |
Winson Chung | 3f4e142 | 2011-11-17 14:58:51 -0800 | [diff] [blame] | 537 | if (x < mScrollZone) { |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 538 | if (mScrollState == SCROLL_OUTSIDE_ZONE) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 539 | mScrollState = SCROLL_WAITING_IN_ZONE; |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 540 | if (mDragScroller.onEnterScrollArea(x, y, SCROLL_LEFT)) { |
| 541 | mScrollRunnable.setDirection(SCROLL_LEFT); |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 542 | mHandler.postDelayed(mScrollRunnable, delay); |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 543 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 544 | } |
Winson Chung | 3f4e142 | 2011-11-17 14:58:51 -0800 | [diff] [blame] | 545 | } else if (x > mScrollView.getWidth() - mScrollZone) { |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 546 | if (mScrollState == SCROLL_OUTSIDE_ZONE) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 547 | mScrollState = SCROLL_WAITING_IN_ZONE; |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 548 | if (mDragScroller.onEnterScrollArea(x, y, SCROLL_RIGHT)) { |
| 549 | mScrollRunnable.setDirection(SCROLL_RIGHT); |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 550 | mHandler.postDelayed(mScrollRunnable, delay); |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 551 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 552 | } |
| 553 | } else { |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 554 | clearScrollRunnable(); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 558 | public void forceMoveEvent() { |
| 559 | if (mDragging) { |
| 560 | handleMoveEvent(mDragObject.x, mDragObject.y); |
| 561 | } |
| 562 | } |
| 563 | |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 564 | /** |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 565 | * Call this from a drag source view. |
| 566 | */ |
| 567 | public boolean onTouchEvent(MotionEvent ev) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 568 | if (!mDragging) { |
| 569 | return false; |
| 570 | } |
| 571 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame^] | 572 | // Update the velocity tracker |
| 573 | acquireVelocityTrackerAndAddMovement(ev); |
| 574 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 575 | final int action = ev.getAction(); |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 576 | final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY()); |
| 577 | final int dragLayerX = dragLayerPos[0]; |
| 578 | final int dragLayerY = dragLayerPos[1]; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 579 | |
| 580 | switch (action) { |
| 581 | case MotionEvent.ACTION_DOWN: |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 582 | // Remember where the motion event started |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 583 | mMotionDownX = dragLayerX; |
| 584 | mMotionDownY = dragLayerY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 585 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 586 | if ((dragLayerX < mScrollZone) || (dragLayerX > mScrollView.getWidth() - mScrollZone)) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 587 | mScrollState = SCROLL_WAITING_IN_ZONE; |
| 588 | mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY); |
| 589 | } else { |
| 590 | mScrollState = SCROLL_OUTSIDE_ZONE; |
| 591 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 592 | break; |
| 593 | case MotionEvent.ACTION_MOVE: |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 594 | handleMoveEvent(dragLayerX, dragLayerY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 595 | break; |
| 596 | case MotionEvent.ACTION_UP: |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 597 | // Ensure that we've processed a move event at the current pointer location. |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 598 | handleMoveEvent(dragLayerX, dragLayerY); |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 599 | mHandler.removeCallbacks(mScrollRunnable); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame^] | 600 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 601 | if (mDragging) { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame^] | 602 | PointF vec = isFlingingToDelete(mDragObject.dragSource); |
| 603 | if (vec != null) { |
| 604 | dropOnFlingToDeleteTarget(dragLayerX, dragLayerY, vec); |
| 605 | } else { |
| 606 | drop(dragLayerX, dragLayerY); |
| 607 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 608 | } |
| 609 | endDrag(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 610 | break; |
| 611 | case MotionEvent.ACTION_CANCEL: |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 612 | mHandler.removeCallbacks(mScrollRunnable); |
Joe Onorato | 24b6fd8 | 2009-11-12 13:47:09 -0800 | [diff] [blame] | 613 | cancelDrag(); |
Winson Chung | 621e640 | 2011-01-04 16:03:57 -0800 | [diff] [blame] | 614 | break; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | return true; |
| 618 | } |
| 619 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame^] | 620 | /** |
| 621 | * Determines whether the user flung the current item to delete it. |
| 622 | * |
| 623 | * @return the vector at which the item was flung, or null if no fling was detected. |
| 624 | */ |
| 625 | private PointF isFlingingToDelete(DragSource source) { |
| 626 | if (mFlingToDeleteDropTarget == null) return null; |
| 627 | if (!source.supportsFlingToDelete()) return null; |
| 628 | |
| 629 | ViewConfiguration config = ViewConfiguration.get(mLauncher); |
| 630 | mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity()); |
| 631 | |
| 632 | if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) { |
| 633 | // Do a quick dot product test to ensure that we are flinging upwards |
| 634 | PointF vel = new PointF(mVelocityTracker.getXVelocity(), |
| 635 | mVelocityTracker.getYVelocity()); |
| 636 | PointF upVec = new PointF(0f, -1f); |
| 637 | float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) / |
| 638 | (vel.length() * upVec.length())); |
| 639 | if (theta <= Math.toRadians(MAX_FLING_DEGREES)) { |
| 640 | return vel; |
| 641 | } |
| 642 | } |
| 643 | return null; |
| 644 | } |
| 645 | |
| 646 | private void dropOnFlingToDeleteTarget(float x, float y, PointF vel) { |
| 647 | final int[] coordinates = mCoordinatesTemp; |
| 648 | |
| 649 | mDragObject.x = coordinates[0]; |
| 650 | mDragObject.y = coordinates[1]; |
| 651 | mDragObject.dragComplete = true; |
| 652 | |
| 653 | // Clean up dragging on the target if it's not the current fling delete target otherwise, |
| 654 | // start dragging to it. |
| 655 | if (mLastDropTarget != null && mFlingToDeleteDropTarget != mLastDropTarget) { |
| 656 | mLastDropTarget.onDragExit(mDragObject); |
| 657 | } |
| 658 | |
| 659 | // Drop onto the fling-to-delete target |
| 660 | boolean accepted = false; |
| 661 | mFlingToDeleteDropTarget.onDragEnter(mDragObject); |
| 662 | mFlingToDeleteDropTarget.onDragExit(mDragObject); |
| 663 | if (mFlingToDeleteDropTarget.acceptDrop(mDragObject)) { |
| 664 | mFlingToDeleteDropTarget.onFlingToDelete(mDragObject, mDragObject.x, mDragObject.y, |
| 665 | vel); |
| 666 | accepted = true; |
| 667 | } |
| 668 | mDragObject.dragSource.onDropCompleted((View) mFlingToDeleteDropTarget, mDragObject, |
| 669 | accepted); |
| 670 | } |
| 671 | |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 672 | private void drop(float x, float y) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 673 | final int[] coordinates = mCoordinatesTemp; |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 674 | final DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 675 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 676 | mDragObject.x = coordinates[0]; |
| 677 | mDragObject.y = coordinates[1]; |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 678 | boolean accepted = false; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 679 | if (dropTarget != null) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 680 | mDragObject.dragComplete = true; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 681 | dropTarget.onDragExit(mDragObject); |
| 682 | if (dropTarget.acceptDrop(mDragObject)) { |
| 683 | dropTarget.onDrop(mDragObject); |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 684 | accepted = true; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 685 | } |
| 686 | } |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 687 | mDragObject.dragSource.onDropCompleted((View) dropTarget, mDragObject, accepted); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) { |
| 691 | final Rect r = mRectTemp; |
| 692 | |
| 693 | final ArrayList<DropTarget> dropTargets = mDropTargets; |
| 694 | final int count = dropTargets.size(); |
| 695 | for (int i=count-1; i>=0; i--) { |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 696 | DropTarget target = dropTargets.get(i); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 697 | if (!target.isDropEnabled()) |
| 698 | continue; |
| 699 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 700 | target.getHitRect(r); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 701 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 702 | // Convert the hit rect to DragLayer coordinates |
| 703 | target.getLocationInDragLayer(dropCoordinates); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 704 | r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop()); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 705 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 706 | mDragObject.x = x; |
| 707 | mDragObject.y = y; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 708 | if (r.contains(x, y)) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 709 | DropTarget delegate = target.getDropTargetDelegate(mDragObject); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 710 | if (delegate != null) { |
| 711 | target = delegate; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 712 | target.getLocationInDragLayer(dropCoordinates); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | // Make dropCoordinates relative to the DropTarget |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 716 | dropCoordinates[0] = x - dropCoordinates[0]; |
| 717 | dropCoordinates[1] = y - dropCoordinates[1]; |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 718 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 719 | return target; |
| 720 | } |
| 721 | } |
| 722 | return null; |
| 723 | } |
| 724 | |
| 725 | public void setDragScoller(DragScroller scroller) { |
| 726 | mDragScroller = scroller; |
| 727 | } |
| 728 | |
| 729 | public void setWindowToken(IBinder token) { |
| 730 | mWindowToken = token; |
| 731 | } |
| 732 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 733 | /** |
| 734 | * Sets the drag listner which will be notified when a drag starts or ends. |
| 735 | */ |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 736 | public void addDragListener(DragListener l) { |
| 737 | mListeners.add(l); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 738 | } |
| 739 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 740 | /** |
| 741 | * Remove a previously installed drag listener. |
| 742 | */ |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 743 | public void removeDragListener(DragListener l) { |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 744 | mListeners.remove(l); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | /** |
| 748 | * Add a DropTarget to the list of potential places to receive drop events. |
| 749 | */ |
| 750 | public void addDropTarget(DropTarget target) { |
| 751 | mDropTargets.add(target); |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * Don't send drop events to <em>target</em> any more. |
| 756 | */ |
| 757 | public void removeDropTarget(DropTarget target) { |
| 758 | mDropTargets.remove(target); |
| 759 | } |
| 760 | |
| 761 | /** |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame^] | 762 | * Sets the current fling-to-delete drop target. |
| 763 | */ |
| 764 | public void setFlingToDeleteDropTarget(DropTarget target) { |
| 765 | mFlingToDeleteDropTarget = target; |
| 766 | } |
| 767 | |
| 768 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 769 | if (mVelocityTracker == null) { |
| 770 | mVelocityTracker = VelocityTracker.obtain(); |
| 771 | } |
| 772 | mVelocityTracker.addMovement(ev); |
| 773 | } |
| 774 | |
| 775 | private void releaseVelocityTracker() { |
| 776 | if (mVelocityTracker != null) { |
| 777 | mVelocityTracker.recycle(); |
| 778 | mVelocityTracker = null; |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | /** |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 783 | * Set which view scrolls for touch events near the edge of the screen. |
| 784 | */ |
| 785 | public void setScrollView(View v) { |
| 786 | mScrollView = v; |
| 787 | } |
| 788 | |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 789 | DragView getDragView() { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 790 | return mDragObject.dragView; |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 791 | } |
| 792 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 793 | private class ScrollRunnable implements Runnable { |
| 794 | private int mDirection; |
| 795 | |
| 796 | ScrollRunnable() { |
| 797 | } |
| 798 | |
| 799 | public void run() { |
| 800 | if (mDragScroller != null) { |
| 801 | if (mDirection == SCROLL_LEFT) { |
| 802 | mDragScroller.scrollLeft(); |
| 803 | } else { |
| 804 | mDragScroller.scrollRight(); |
| 805 | } |
| 806 | mScrollState = SCROLL_OUTSIDE_ZONE; |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 807 | mDistanceSinceScroll = 0; |
| 808 | mDragScroller.onExitScrollArea(); |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 809 | |
| 810 | if (isDragging()) { |
| 811 | // Force an update so that we can requeue the scroller if necessary |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 812 | forceMoveEvent(); |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 813 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | |
| 817 | void setDirection(int direction) { |
| 818 | mDirection = direction; |
| 819 | } |
| 820 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 821 | } |