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