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