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