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