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