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; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 21 | import android.graphics.Rect; |
| 22 | import android.graphics.RectF; |
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 | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 26 | import android.util.DisplayMetrics; |
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; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 30 | import android.view.View; |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 31 | import android.view.ViewConfiguration; |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 32 | import android.view.WindowManager; |
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 | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 86 | private DropTarget.DragObject mDragObject = new DropTarget.DragObject(); |
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 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 113 | /** |
| 114 | * Interface to receive notifications when a drag starts or stops |
| 115 | */ |
| 116 | interface DragListener { |
| 117 | |
| 118 | /** |
| 119 | * A drag has begun |
| 120 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 121 | * @param source An object representing where the drag originated |
| 122 | * @param info The data associated with the object that is being dragged |
| 123 | * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE} |
| 124 | * or {@link DragController#DRAG_ACTION_COPY} |
| 125 | */ |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 126 | void onDragStart(DragSource source, Object info, int dragAction); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 127 | |
| 128 | /** |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 129 | * The drag has ended |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 130 | */ |
| 131 | void onDragEnd(); |
| 132 | } |
| 133 | |
| 134 | /** |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 135 | * Used to create a new DragLayer from XML. |
| 136 | * |
| 137 | * @param context The application's context. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 138 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 139 | public DragController(Launcher launcher) { |
| 140 | mLauncher = launcher; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 141 | mHandler = new Handler(); |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 142 | mScrollZone = launcher.getResources().getDimensionPixelSize(R.dimen.scroll_zone); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 143 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 144 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 145 | public boolean dragging() { |
| 146 | return mDragging; |
| 147 | } |
| 148 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 149 | /** |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 150 | * Starts a drag. |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 151 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 152 | * @param v The view that is being dragged |
| 153 | * @param source An object representing where the drag originated |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 154 | * @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] | 155 | * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or |
| 156 | * {@link #DRAG_ACTION_COPY} |
| 157 | */ |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 158 | public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) { |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 159 | startDrag(v, source, dragInfo, dragAction, null); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Starts a drag. |
| 164 | * |
| 165 | * @param v The view that is being dragged |
| 166 | * @param source An object representing where the drag originated |
| 167 | * @param dragInfo The data associated with the object that is being dragged |
| 168 | * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or |
| 169 | * {@link #DRAG_ACTION_COPY} |
| 170 | * @param dragRegion Coordinates within the bitmap b for the position of item being dragged. |
| 171 | * Makes dragging feel more precise, e.g. you can clip out a transparent border |
| 172 | */ |
| 173 | public void startDrag(View v, DragSource source, Object dragInfo, int dragAction, |
| 174 | Rect dragRegion) { |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 175 | Bitmap b = getViewBitmap(v); |
| 176 | |
Daniel Sandler | 3f8175a | 2010-05-25 11:48:32 -0400 | [diff] [blame] | 177 | if (b == null) { |
| 178 | // out of memory? |
| 179 | return; |
| 180 | } |
| 181 | |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 182 | int[] loc = mCoordinatesTemp; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 183 | mLauncher.getDragLayer().getLocationInDragLayer(v, loc); |
| 184 | int dragLayerX = loc[0]; |
| 185 | int dragLayerY = loc[1]; |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 186 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 187 | startDrag(b, dragLayerX, dragLayerY, source, dragInfo, dragAction, dragRegion); |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 188 | b.recycle(); |
| 189 | |
| 190 | if (dragAction == DRAG_ACTION_MOVE) { |
| 191 | v.setVisibility(View.GONE); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Starts a drag. |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 197 | * |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 198 | * @param v The view that is being dragged |
| 199 | * @param bmp The bitmap that represents the view being dragged |
| 200 | * @param source An object representing where the drag originated |
| 201 | * @param dragInfo The data associated with the object that is being dragged |
| 202 | * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or |
| 203 | * {@link #DRAG_ACTION_COPY} |
| 204 | * @param dragRegion Coordinates within the bitmap b for the position of item being dragged. |
| 205 | * Makes dragging feel more precise, e.g. you can clip out a transparent border |
| 206 | */ |
| 207 | public void startDrag(View v, Bitmap bmp, DragSource source, Object dragInfo, int dragAction, |
| 208 | Rect dragRegion) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 209 | int[] loc = mCoordinatesTemp; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 210 | mLauncher.getDragLayer().getLocationInDragLayer(v, loc); |
| 211 | int dragLayerX = loc[0]; |
| 212 | int dragLayerY = loc[1]; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 213 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 214 | startDrag(bmp, dragLayerX, dragLayerY, source, dragInfo, dragAction, dragRegion); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 215 | |
| 216 | if (dragAction == DRAG_ACTION_MOVE) { |
| 217 | v.setVisibility(View.GONE); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Starts a drag. |
| 223 | * |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 224 | * @param b The bitmap to display as the drag image. It will be re-scaled to the |
| 225 | * enlarged size. |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 226 | * @param dragLayerX The x position in the DragLayer of the left-top of the bitmap. |
| 227 | * @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] | 228 | * @param source An object representing where the drag originated |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 229 | * @param dragInfo The data associated with the object that is being dragged |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 230 | * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or |
| 231 | * {@link #DRAG_ACTION_COPY} |
| 232 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 233 | public void startDrag(Bitmap b, int dragLayerX, int dragLayerY, |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 234 | DragSource source, Object dragInfo, int dragAction) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 235 | startDrag(b, dragLayerX, dragLayerY, source, dragInfo, dragAction, null); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Starts a drag. |
| 240 | * |
| 241 | * @param b The bitmap to display as the drag image. It will be re-scaled to the |
| 242 | * enlarged size. |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 243 | * @param dragLayerX The x position in the DragLayer of the left-top of the bitmap. |
| 244 | * @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] | 245 | * @param source An object representing where the drag originated |
| 246 | * @param dragInfo The data associated with the object that is being dragged |
| 247 | * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or |
| 248 | * {@link #DRAG_ACTION_COPY} |
| 249 | * @param dragRegion Coordinates within the bitmap b for the position of item being dragged. |
| 250 | * Makes dragging feel more precise, e.g. you can clip out a transparent border |
| 251 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 252 | public void startDrag(Bitmap b, int dragLayerX, int dragLayerY, |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 253 | DragSource source, Object dragInfo, int dragAction, Rect dragRegion) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 254 | if (PROFILE_DRAWING_DURING_DRAG) { |
| 255 | android.os.Debug.startMethodTracing("Launcher"); |
| 256 | } |
| 257 | |
| 258 | // Hide soft keyboard, if visible |
| 259 | if (mInputMethodManager == null) { |
| 260 | mInputMethodManager = (InputMethodManager) |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 261 | mLauncher.getSystemService(Context.INPUT_METHOD_SERVICE); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 262 | } |
| 263 | mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0); |
| 264 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 265 | for (DragListener listener : mListeners) { |
| 266 | listener.onDragStart(source, dragInfo, dragAction); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 267 | } |
| 268 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 269 | final int registrationX = mMotionDownX - dragLayerX; |
| 270 | final int registrationY = mMotionDownY - dragLayerY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 271 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 272 | final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left; |
| 273 | final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 274 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 275 | mDragging = true; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 276 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 277 | mDragObject.dragComplete = false; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 278 | mDragObject.xOffset = mMotionDownX - (dragLayerX + dragRegionLeft); |
| 279 | mDragObject.yOffset = mMotionDownY - (dragLayerY + dragRegionTop); |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 280 | mDragObject.dragSource = source; |
| 281 | mDragObject.dragInfo = dragInfo; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 282 | |
| 283 | mVibrator.vibrate(VIBRATE_DURATION); |
| 284 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 285 | final DragView dragView = mDragObject.dragView = new DragView(mLauncher, b, registrationX, |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 286 | registrationY, 0, 0, b.getWidth(), b.getHeight()); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 287 | |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 288 | final DragSource dragSource = source; |
| 289 | dragView.setOnDrawRunnable(new Runnable() { |
| 290 | public void run() { |
| 291 | dragSource.onDragViewVisible(); |
| 292 | }; |
| 293 | }); |
| 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 | } |
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 | } |
| 373 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 374 | private void endDrag() { |
| 375 | if (mDragging) { |
| 376 | mDragging = false; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 377 | for (DragListener listener : mListeners) { |
| 378 | listener.onDragEnd(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 379 | } |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 380 | if (mDragObject.dragView != null) { |
| 381 | mDragObject.dragView.remove(); |
| 382 | mDragObject.dragView = null; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 383 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Call this from a drag source view. |
| 389 | */ |
| 390 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 391 | if (false) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 392 | Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging=" |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 393 | + mDragging); |
| 394 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 395 | final int action = ev.getAction(); |
| 396 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 397 | final int dragLayerX = (int) ev.getX(); |
| 398 | final int dragLayerY = (int) ev.getY(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 399 | |
| 400 | switch (action) { |
| 401 | case MotionEvent.ACTION_MOVE: |
| 402 | break; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 403 | case MotionEvent.ACTION_DOWN: |
| 404 | // Remember location of down touch |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 405 | mMotionDownX = dragLayerX; |
| 406 | mMotionDownY = dragLayerY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 407 | mLastDropTarget = null; |
| 408 | break; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 409 | case MotionEvent.ACTION_UP: |
| 410 | if (mDragging) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 411 | drop(dragLayerX, dragLayerY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 412 | } |
| 413 | endDrag(); |
| 414 | break; |
Winson Chung | 621e640 | 2011-01-04 16:03:57 -0800 | [diff] [blame] | 415 | case MotionEvent.ACTION_CANCEL: |
| 416 | cancelDrag(); |
| 417 | break; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | return mDragging; |
| 421 | } |
| 422 | |
| 423 | /** |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 424 | * Sets the view that should handle move events. |
| 425 | */ |
| 426 | void setMoveTarget(View view) { |
| 427 | mMoveTarget = view; |
| 428 | } |
| 429 | |
| 430 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 431 | return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction); |
| 432 | } |
| 433 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 434 | private void handleMoveEvent(int x, int y) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 435 | mDragObject.dragView.move(x, y); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 436 | |
| 437 | // Drop on someone? |
| 438 | final int[] coordinates = mCoordinatesTemp; |
| 439 | DropTarget dropTarget = findDropTarget(x, y, coordinates); |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 440 | mDragObject.x = coordinates[0]; |
| 441 | mDragObject.y = coordinates[1]; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 442 | if (dropTarget != null) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 443 | DropTarget delegate = dropTarget.getDropTargetDelegate(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 444 | if (delegate != null) { |
| 445 | dropTarget = delegate; |
| 446 | } |
| 447 | |
| 448 | if (mLastDropTarget != dropTarget) { |
| 449 | if (mLastDropTarget != null) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 450 | mLastDropTarget.onDragExit(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 451 | } |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 452 | dropTarget.onDragEnter(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 453 | } |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 454 | dropTarget.onDragOver(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 455 | } else { |
| 456 | if (mLastDropTarget != null) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 457 | mLastDropTarget.onDragExit(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | mLastDropTarget = dropTarget; |
| 461 | |
| 462 | // Scroll, maybe, but not if we're in the delete region. |
| 463 | boolean inDeleteRegion = false; |
| 464 | if (mDeleteRegion != null) { |
| 465 | inDeleteRegion = mDeleteRegion.contains(x, y); |
| 466 | } |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 467 | |
| 468 | // After a scroll, the touch point will still be in the scroll region. |
| 469 | // Rather than scrolling immediately, require a bit of twiddling to scroll again |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 470 | final int slop = ViewConfiguration.get(mLauncher).getScaledWindowTouchSlop(); |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 471 | mDistanceSinceScroll += |
| 472 | Math.sqrt(Math.pow(mLastTouch[0] - x, 2) + Math.pow(mLastTouch[1] - y, 2)); |
| 473 | mLastTouch[0] = x; |
| 474 | mLastTouch[1] = y; |
| 475 | |
Joe Onorato | 658db74 | 2010-09-29 11:40:39 -0700 | [diff] [blame] | 476 | if (!inDeleteRegion && x < mScrollZone) { |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 477 | if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 478 | mScrollState = SCROLL_WAITING_IN_ZONE; |
| 479 | mScrollRunnable.setDirection(SCROLL_LEFT); |
| 480 | mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY); |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 481 | mDragScroller.onEnterScrollArea(SCROLL_LEFT); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 482 | } |
Joe Onorato | 658db74 | 2010-09-29 11:40:39 -0700 | [diff] [blame] | 483 | } else if (!inDeleteRegion && x > mScrollView.getWidth() - mScrollZone) { |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 484 | if (mScrollState == SCROLL_OUTSIDE_ZONE && mDistanceSinceScroll > slop) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 485 | mScrollState = SCROLL_WAITING_IN_ZONE; |
| 486 | mScrollRunnable.setDirection(SCROLL_RIGHT); |
| 487 | mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY); |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 488 | mDragScroller.onEnterScrollArea(SCROLL_RIGHT); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 489 | } |
| 490 | } else { |
| 491 | if (mScrollState == SCROLL_WAITING_IN_ZONE) { |
| 492 | mScrollState = SCROLL_OUTSIDE_ZONE; |
| 493 | mScrollRunnable.setDirection(SCROLL_RIGHT); |
| 494 | mHandler.removeCallbacks(mScrollRunnable); |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 495 | mDragScroller.onExitScrollArea(); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 500 | /** |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 501 | * Call this from a drag source view. |
| 502 | */ |
| 503 | public boolean onTouchEvent(MotionEvent ev) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 504 | if (!mDragging) { |
| 505 | return false; |
| 506 | } |
| 507 | |
| 508 | final int action = ev.getAction(); |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 509 | final int dragLayerX = (int) ev.getX(); |
| 510 | final int dragLayerY = (int) ev.getY(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 511 | |
| 512 | switch (action) { |
| 513 | case MotionEvent.ACTION_DOWN: |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 514 | // Remember where the motion event started |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 515 | mMotionDownX = dragLayerX; |
| 516 | mMotionDownY = dragLayerY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 517 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 518 | if ((dragLayerX < mScrollZone) || (dragLayerX > mScrollView.getWidth() - mScrollZone)) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 519 | mScrollState = SCROLL_WAITING_IN_ZONE; |
| 520 | mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY); |
| 521 | } else { |
| 522 | mScrollState = SCROLL_OUTSIDE_ZONE; |
| 523 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 524 | break; |
| 525 | case MotionEvent.ACTION_MOVE: |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 526 | handleMoveEvent(dragLayerX, dragLayerY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 527 | break; |
| 528 | case MotionEvent.ACTION_UP: |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 529 | // 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] | 530 | handleMoveEvent(dragLayerX, dragLayerY); |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 531 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 532 | mHandler.removeCallbacks(mScrollRunnable); |
| 533 | if (mDragging) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 534 | drop(dragLayerX, dragLayerY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 535 | } |
| 536 | endDrag(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 537 | break; |
| 538 | case MotionEvent.ACTION_CANCEL: |
Joe Onorato | 24b6fd8 | 2009-11-12 13:47:09 -0800 | [diff] [blame] | 539 | cancelDrag(); |
Winson Chung | 621e640 | 2011-01-04 16:03:57 -0800 | [diff] [blame] | 540 | break; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | return true; |
| 544 | } |
| 545 | |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 546 | private void drop(float x, float y) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 547 | final int[] coordinates = mCoordinatesTemp; |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 548 | final DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 549 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 550 | mDragObject.x = coordinates[0]; |
| 551 | mDragObject.y = coordinates[1]; |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 552 | boolean accepted = false; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 553 | if (dropTarget != null) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 554 | mDragObject.dragComplete = true; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 555 | dropTarget.onDragExit(mDragObject); |
| 556 | if (dropTarget.acceptDrop(mDragObject)) { |
| 557 | dropTarget.onDrop(mDragObject); |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 558 | accepted = true; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 559 | } |
| 560 | } |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 561 | mDragObject.dragSource.onDropCompleted((View) dropTarget, mDragObject, accepted); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) { |
| 565 | final Rect r = mRectTemp; |
| 566 | |
| 567 | final ArrayList<DropTarget> dropTargets = mDropTargets; |
| 568 | final int count = dropTargets.size(); |
| 569 | for (int i=count-1; i>=0; i--) { |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 570 | DropTarget target = dropTargets.get(i); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 571 | if (!target.isDropEnabled()) |
| 572 | continue; |
| 573 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 574 | target.getHitRect(r); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 575 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 576 | // Convert the hit rect to DragLayer coordinates |
| 577 | target.getLocationInDragLayer(dropCoordinates); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 578 | r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop()); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 579 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 580 | mDragObject.x = x; |
| 581 | mDragObject.y = y; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 582 | if (r.contains(x, y)) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 583 | DropTarget delegate = target.getDropTargetDelegate(mDragObject); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 584 | if (delegate != null) { |
| 585 | target = delegate; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 586 | target.getLocationInDragLayer(dropCoordinates); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | // Make dropCoordinates relative to the DropTarget |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 590 | dropCoordinates[0] = x - dropCoordinates[0]; |
| 591 | dropCoordinates[1] = y - dropCoordinates[1]; |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 592 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 593 | return target; |
| 594 | } |
| 595 | } |
| 596 | return null; |
| 597 | } |
| 598 | |
| 599 | public void setDragScoller(DragScroller scroller) { |
| 600 | mDragScroller = scroller; |
| 601 | } |
| 602 | |
| 603 | public void setWindowToken(IBinder token) { |
| 604 | mWindowToken = token; |
| 605 | } |
| 606 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 607 | /** |
| 608 | * Sets the drag listner which will be notified when a drag starts or ends. |
| 609 | */ |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 610 | public void addDragListener(DragListener l) { |
| 611 | mListeners.add(l); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 612 | } |
| 613 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 614 | /** |
| 615 | * Remove a previously installed drag listener. |
| 616 | */ |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 617 | public void removeDragListener(DragListener l) { |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 618 | mListeners.remove(l); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /** |
| 622 | * Add a DropTarget to the list of potential places to receive drop events. |
| 623 | */ |
| 624 | public void addDropTarget(DropTarget target) { |
| 625 | mDropTargets.add(target); |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Don't send drop events to <em>target</em> any more. |
| 630 | */ |
| 631 | public void removeDropTarget(DropTarget target) { |
| 632 | mDropTargets.remove(target); |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * Set which view scrolls for touch events near the edge of the screen. |
| 637 | */ |
| 638 | public void setScrollView(View v) { |
| 639 | mScrollView = v; |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Specifies the delete region. We won't scroll on touch events over the delete region. |
| 644 | * |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 645 | * @param region The rectangle in DragLayer coordinates of the delete region. |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 646 | */ |
| 647 | void setDeleteRegion(RectF region) { |
| 648 | mDeleteRegion = region; |
| 649 | } |
| 650 | |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 651 | DragView getDragView() { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 652 | return mDragObject.dragView; |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 653 | } |
| 654 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 655 | private class ScrollRunnable implements Runnable { |
| 656 | private int mDirection; |
| 657 | |
| 658 | ScrollRunnable() { |
| 659 | } |
| 660 | |
| 661 | public void run() { |
| 662 | if (mDragScroller != null) { |
| 663 | if (mDirection == SCROLL_LEFT) { |
| 664 | mDragScroller.scrollLeft(); |
| 665 | } else { |
| 666 | mDragScroller.scrollRight(); |
| 667 | } |
| 668 | mScrollState = SCROLL_OUTSIDE_ZONE; |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 669 | mDistanceSinceScroll = 0; |
| 670 | mDragScroller.onExitScrollArea(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
| 674 | void setDirection(int direction) { |
| 675 | mDirection = direction; |
| 676 | } |
| 677 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 678 | } |