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