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; |
| 21 | import android.graphics.Canvas; |
| 22 | import android.graphics.Matrix; |
| 23 | import android.graphics.Rect; |
| 24 | import android.graphics.RectF; |
| 25 | import android.os.IBinder; |
| 26 | import android.os.Handler; |
| 27 | import android.os.Vibrator; |
| 28 | import android.os.SystemClock; |
| 29 | import android.util.AttributeSet; |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 30 | import android.util.DisplayMetrics; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 31 | import android.util.Log; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 32 | import android.view.View; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 33 | import android.view.ViewGroup; |
| 34 | import android.view.KeyEvent; |
| 35 | import android.view.MotionEvent; |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 36 | import android.view.WindowManager; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 37 | import android.view.inputmethod.InputMethodManager; |
| 38 | import android.widget.FrameLayout; |
| 39 | import android.widget.ImageView; |
| 40 | |
| 41 | import java.util.ArrayList; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 44 | * 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] | 45 | */ |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 46 | public class DragController { |
Joe Onorato | 2e5c432 | 2009-10-06 12:34:42 -0700 | [diff] [blame] | 47 | private static final String TAG = "Launcher.DragController"; |
| 48 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 49 | /** Indicates the drag is a move. */ |
| 50 | public static int DRAG_ACTION_MOVE = 0; |
| 51 | |
| 52 | /** Indicates the drag is a copy. */ |
| 53 | public static int DRAG_ACTION_COPY = 1; |
| 54 | |
| 55 | private static final int SCROLL_DELAY = 600; |
| 56 | private static final int SCROLL_ZONE = 20; |
| 57 | private static final int VIBRATE_DURATION = 35; |
| 58 | |
| 59 | private static final boolean PROFILE_DRAWING_DURING_DRAG = false; |
| 60 | |
| 61 | private static final int SCROLL_OUTSIDE_ZONE = 0; |
| 62 | private static final int SCROLL_WAITING_IN_ZONE = 1; |
| 63 | |
| 64 | private static final int SCROLL_LEFT = 0; |
| 65 | private static final int SCROLL_RIGHT = 1; |
| 66 | |
| 67 | private Context mContext; |
| 68 | private Handler mHandler; |
| 69 | private final Vibrator mVibrator = new Vibrator(); |
| 70 | |
| 71 | // temporaries to avoid gc thrash |
| 72 | private Rect mRectTemp = new Rect(); |
| 73 | private final int[] mCoordinatesTemp = new int[2]; |
| 74 | |
| 75 | /** Whether or not we're dragging. */ |
| 76 | private boolean mDragging; |
| 77 | |
| 78 | /** X coordinate of the down event. */ |
| 79 | private float mMotionDownX; |
| 80 | |
| 81 | /** Y coordinate of the down event. */ |
| 82 | private float mMotionDownY; |
| 83 | |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 84 | /** Info about the screen for clamping. */ |
| 85 | private DisplayMetrics mDisplayMetrics = new DisplayMetrics(); |
| 86 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 87 | /** Original view that is being dragged. */ |
| 88 | private View mOriginator; |
| 89 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 90 | /** X offset from the upper-left corner of the cell to where we touched. */ |
| 91 | private float mTouchOffsetX; |
| 92 | |
| 93 | /** Y offset from the upper-left corner of the cell to where we touched. */ |
| 94 | private float mTouchOffsetY; |
| 95 | |
| 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 | |
| 108 | private DragListener mListener; |
| 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 | |
| 116 | private DragScroller mDragScroller; |
| 117 | private int mScrollState = SCROLL_OUTSIDE_ZONE; |
| 118 | private ScrollRunnable mScrollRunnable = new ScrollRunnable(); |
| 119 | |
| 120 | private RectF mDeleteRegion; |
| 121 | private DropTarget mLastDropTarget; |
| 122 | |
| 123 | private InputMethodManager mInputMethodManager; |
| 124 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 125 | /** |
| 126 | * Interface to receive notifications when a drag starts or stops |
| 127 | */ |
| 128 | interface DragListener { |
| 129 | |
| 130 | /** |
| 131 | * A drag has begun |
| 132 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 133 | * @param source An object representing where the drag originated |
| 134 | * @param info The data associated with the object that is being dragged |
| 135 | * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE} |
| 136 | * or {@link DragController#DRAG_ACTION_COPY} |
| 137 | */ |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 138 | void onDragStart(DragSource source, Object info, int dragAction); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * The drag has eneded |
| 142 | */ |
| 143 | void onDragEnd(); |
| 144 | } |
| 145 | |
| 146 | /** |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 147 | * Used to create a new DragLayer from XML. |
| 148 | * |
| 149 | * @param context The application's context. |
| 150 | * @param attrs The attribtues set containing the Workspace's customization values. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 151 | */ |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 152 | public DragController(Context context) { |
| 153 | mContext = context; |
| 154 | mHandler = new Handler(); |
| 155 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 156 | |
| 157 | /** |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 158 | * Starts a drag. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 159 | * |
| 160 | * @param v The view that is being dragged |
| 161 | * @param source An object representing where the drag originated |
| 162 | * @param info The data associated with the object that is being dragged |
| 163 | * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or |
| 164 | * {@link #DRAG_ACTION_COPY} |
| 165 | */ |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 166 | public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) { |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 167 | mOriginator = v; |
| 168 | |
| 169 | Bitmap b = getViewBitmap(v); |
| 170 | |
| 171 | int[] loc = mCoordinatesTemp; |
| 172 | v.getLocationOnScreen(loc); |
| 173 | int screenX = loc[0]; |
| 174 | int screenY = loc[1]; |
| 175 | |
| 176 | startDrag(b, screenX, screenY, 0, 0, b.getWidth(), b.getHeight(), |
| 177 | source, dragInfo, dragAction); |
| 178 | |
| 179 | b.recycle(); |
| 180 | |
| 181 | if (dragAction == DRAG_ACTION_MOVE) { |
| 182 | v.setVisibility(View.GONE); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Starts a drag. |
| 188 | * |
| 189 | * @param b The bitmap to display as the drag image. It will be re-scaled to the |
| 190 | * enlarged size. |
| 191 | * @param screenX The x position on screen of the left-top of the bitmap. |
| 192 | * @param screenY The y position on screen of the left-top of the bitmap. |
| 193 | * @param textureLeft The left edge of the region inside b to use. |
| 194 | * @param textureTop The top edge of the region inside b to use. |
| 195 | * @param textureWidth The width of the region inside b to use. |
| 196 | * @param textureHeight The height of the region inside b to use. |
| 197 | * @param source An object representing where the drag originated |
| 198 | * @param info The data associated with the object that is being dragged |
| 199 | * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or |
| 200 | * {@link #DRAG_ACTION_COPY} |
| 201 | */ |
| 202 | public void startDrag(Bitmap b, int screenX, int screenY, |
| 203 | int textureLeft, int textureTop, int textureWidth, int textureHeight, |
| 204 | DragSource source, Object dragInfo, int dragAction) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 205 | if (PROFILE_DRAWING_DURING_DRAG) { |
| 206 | android.os.Debug.startMethodTracing("Launcher"); |
| 207 | } |
| 208 | |
| 209 | // Hide soft keyboard, if visible |
| 210 | if (mInputMethodManager == null) { |
| 211 | mInputMethodManager = (InputMethodManager) |
| 212 | mContext.getSystemService(Context.INPUT_METHOD_SERVICE); |
| 213 | } |
| 214 | mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0); |
| 215 | |
| 216 | if (mListener != null) { |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 217 | mListener.onDragStart(source, dragInfo, dragAction); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 220 | int registrationX = ((int)mMotionDownX) - screenX; |
| 221 | int registrationY = ((int)mMotionDownY) - screenY; |
| 222 | |
| 223 | mTouchOffsetX = mMotionDownX - screenX; |
| 224 | mTouchOffsetY = mMotionDownY - screenY; |
| 225 | |
| 226 | mDragging = true; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 227 | mDragSource = source; |
| 228 | mDragInfo = dragInfo; |
| 229 | |
| 230 | mVibrator.vibrate(VIBRATE_DURATION); |
| 231 | |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 232 | DragView dragView = mDragView = new DragView(mContext, b, registrationX, registrationY, |
| 233 | textureLeft, textureTop, textureWidth, textureHeight); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 234 | dragView.show(mWindowToken, (int)mMotionDownX, (int)mMotionDownY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Draw the view into a bitmap. |
| 239 | */ |
| 240 | private Bitmap getViewBitmap(View v) { |
| 241 | v.clearFocus(); |
| 242 | v.setPressed(false); |
| 243 | |
| 244 | boolean willNotCache = v.willNotCacheDrawing(); |
| 245 | v.setWillNotCacheDrawing(false); |
| 246 | |
| 247 | // Reset the drawing cache background color to fully transparent |
| 248 | // for the duration of this operation |
| 249 | int color = v.getDrawingCacheBackgroundColor(); |
| 250 | v.setDrawingCacheBackgroundColor(0); |
| 251 | |
| 252 | if (color != 0) { |
| 253 | v.destroyDrawingCache(); |
| 254 | } |
| 255 | v.buildDrawingCache(); |
| 256 | Bitmap cacheBitmap = v.getDrawingCache(); |
| 257 | |
| 258 | Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); |
| 259 | |
| 260 | // Restore the view |
| 261 | v.destroyDrawingCache(); |
| 262 | v.setWillNotCacheDrawing(willNotCache); |
| 263 | v.setDrawingCacheBackgroundColor(color); |
| 264 | |
| 265 | return bitmap; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Call this from a drag source view like this: |
| 270 | * |
| 271 | * <pre> |
| 272 | * @Override |
| 273 | * public boolean dispatchKeyEvent(KeyEvent event) { |
| 274 | * return mDragController.dispatchKeyEvent(this, event) |
| 275 | * || super.dispatchKeyEvent(event); |
| 276 | * </pre> |
| 277 | */ |
| 278 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 279 | return mDragging; |
| 280 | } |
| 281 | |
Joe Onorato | 24b6fd8 | 2009-11-12 13:47:09 -0800 | [diff] [blame^] | 282 | /** |
| 283 | * Stop dragging without dropping. |
| 284 | */ |
| 285 | public void cancelDrag() { |
| 286 | endDrag(); |
| 287 | } |
| 288 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 289 | private void endDrag() { |
| 290 | if (mDragging) { |
| 291 | mDragging = false; |
| 292 | if (mOriginator != null) { |
| 293 | mOriginator.setVisibility(View.VISIBLE); |
| 294 | } |
| 295 | if (mListener != null) { |
| 296 | mListener.onDragEnd(); |
| 297 | } |
| 298 | if (mDragView != null) { |
| 299 | mDragView.remove(); |
| 300 | mDragView = null; |
| 301 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Call this from a drag source view. |
| 307 | */ |
| 308 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 309 | if (false) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 310 | Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging=" |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 311 | + mDragging); |
| 312 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 313 | final int action = ev.getAction(); |
| 314 | |
Joe Onorato | 87467d3 | 2009-11-08 14:36:43 -0500 | [diff] [blame] | 315 | if (action == MotionEvent.ACTION_DOWN) { |
| 316 | recordScreenSize(); |
| 317 | } |
| 318 | |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 319 | final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels); |
| 320 | final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 321 | |
| 322 | switch (action) { |
| 323 | case MotionEvent.ACTION_MOVE: |
| 324 | break; |
| 325 | |
| 326 | case MotionEvent.ACTION_DOWN: |
| 327 | // Remember location of down touch |
| 328 | mMotionDownX = screenX; |
| 329 | mMotionDownY = screenY; |
| 330 | mLastDropTarget = null; |
| 331 | break; |
| 332 | |
| 333 | case MotionEvent.ACTION_CANCEL: |
| 334 | case MotionEvent.ACTION_UP: |
| 335 | if (mDragging) { |
| 336 | drop(screenX, screenY); |
| 337 | } |
| 338 | endDrag(); |
| 339 | break; |
| 340 | } |
| 341 | |
| 342 | return mDragging; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Call this from a drag source view. |
| 347 | */ |
| 348 | public boolean onTouchEvent(MotionEvent ev) { |
| 349 | View scrollView = mScrollView; |
| 350 | |
| 351 | if (!mDragging) { |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | final int action = ev.getAction(); |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 356 | final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels); |
| 357 | final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 358 | |
| 359 | switch (action) { |
| 360 | case MotionEvent.ACTION_DOWN: |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 361 | // Remember where the motion event started |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 362 | mMotionDownX = screenX; |
| 363 | mMotionDownY = screenY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 364 | |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 365 | if ((screenX < SCROLL_ZONE) || (screenX > scrollView.getWidth() - SCROLL_ZONE)) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 366 | mScrollState = SCROLL_WAITING_IN_ZONE; |
| 367 | mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY); |
| 368 | } else { |
| 369 | mScrollState = SCROLL_OUTSIDE_ZONE; |
| 370 | } |
| 371 | |
| 372 | break; |
| 373 | case MotionEvent.ACTION_MOVE: |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 374 | // Update the drag view. Don't use the clamped pos here so the dragging looks |
| 375 | // 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] | 376 | mDragView.move((int)ev.getRawX(), (int)ev.getRawY()); |
| 377 | |
| 378 | // Drop on someone? |
| 379 | final int[] coordinates = mCoordinatesTemp; |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 380 | DropTarget dropTarget = findDropTarget((int) screenX, (int) screenY, coordinates); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 381 | if (dropTarget != null) { |
| 382 | if (mLastDropTarget == dropTarget) { |
| 383 | dropTarget.onDragOver(mDragSource, coordinates[0], coordinates[1], |
| 384 | (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); |
| 385 | } else { |
| 386 | if (mLastDropTarget != null) { |
| 387 | mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1], |
| 388 | (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); |
| 389 | } |
| 390 | dropTarget.onDragEnter(mDragSource, coordinates[0], coordinates[1], |
| 391 | (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); |
| 392 | } |
| 393 | } else { |
| 394 | if (mLastDropTarget != null) { |
| 395 | mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1], |
| 396 | (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); |
| 397 | } |
| 398 | } |
| 399 | mLastDropTarget = dropTarget; |
| 400 | |
| 401 | // Scroll, maybe, but not if we're in the delete region. |
| 402 | boolean inDeleteRegion = false; |
| 403 | if (mDeleteRegion != null) { |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 404 | inDeleteRegion = mDeleteRegion.contains(screenX, screenY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 405 | } |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 406 | if (!inDeleteRegion && screenX < SCROLL_ZONE) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 407 | if (mScrollState == SCROLL_OUTSIDE_ZONE) { |
| 408 | mScrollState = SCROLL_WAITING_IN_ZONE; |
| 409 | mScrollRunnable.setDirection(SCROLL_LEFT); |
| 410 | mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY); |
| 411 | } |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 412 | } else if (!inDeleteRegion && screenX > scrollView.getWidth() - SCROLL_ZONE) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 413 | if (mScrollState == SCROLL_OUTSIDE_ZONE) { |
| 414 | mScrollState = SCROLL_WAITING_IN_ZONE; |
| 415 | mScrollRunnable.setDirection(SCROLL_RIGHT); |
| 416 | mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY); |
| 417 | } |
| 418 | } else { |
| 419 | if (mScrollState == SCROLL_WAITING_IN_ZONE) { |
| 420 | mScrollState = SCROLL_OUTSIDE_ZONE; |
| 421 | mScrollRunnable.setDirection(SCROLL_RIGHT); |
| 422 | mHandler.removeCallbacks(mScrollRunnable); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | break; |
| 427 | case MotionEvent.ACTION_UP: |
| 428 | mHandler.removeCallbacks(mScrollRunnable); |
| 429 | if (mDragging) { |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 430 | drop(screenX, screenY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 431 | } |
| 432 | endDrag(); |
| 433 | |
| 434 | break; |
| 435 | case MotionEvent.ACTION_CANCEL: |
Joe Onorato | 24b6fd8 | 2009-11-12 13:47:09 -0800 | [diff] [blame^] | 436 | cancelDrag(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | return true; |
| 440 | } |
| 441 | |
| 442 | private boolean drop(float x, float y) { |
| 443 | final int[] coordinates = mCoordinatesTemp; |
| 444 | DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates); |
| 445 | |
| 446 | if (dropTarget != null) { |
| 447 | dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1], |
| 448 | (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); |
| 449 | if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1], |
| 450 | (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) { |
| 451 | dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1], |
| 452 | (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); |
| 453 | mDragSource.onDropCompleted((View) dropTarget, true); |
| 454 | return true; |
| 455 | } else { |
| 456 | mDragSource.onDropCompleted((View) dropTarget, false); |
| 457 | return true; |
| 458 | } |
| 459 | } |
| 460 | return false; |
| 461 | } |
| 462 | |
| 463 | private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) { |
| 464 | final Rect r = mRectTemp; |
| 465 | |
| 466 | final ArrayList<DropTarget> dropTargets = mDropTargets; |
| 467 | final int count = dropTargets.size(); |
| 468 | for (int i=count-1; i>=0; i--) { |
| 469 | final DropTarget target = dropTargets.get(i); |
| 470 | target.getHitRect(r); |
| 471 | target.getLocationOnScreen(dropCoordinates); |
| 472 | r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop()); |
| 473 | if (r.contains(x, y)) { |
| 474 | dropCoordinates[0] = x - dropCoordinates[0]; |
| 475 | dropCoordinates[1] = y - dropCoordinates[1]; |
| 476 | return target; |
| 477 | } |
| 478 | } |
| 479 | return null; |
| 480 | } |
| 481 | |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 482 | /** |
| 483 | * Get the screen size so we can clamp events to the screen size so even if |
| 484 | * you drag off the edge of the screen, we find something. |
| 485 | */ |
| 486 | private void recordScreenSize() { |
| 487 | ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)) |
| 488 | .getDefaultDisplay().getMetrics(mDisplayMetrics); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Clamp val to be >= min and < max. |
| 493 | */ |
| 494 | private static int clamp(int val, int min, int max) { |
| 495 | if (val < min) { |
| 496 | return min; |
| 497 | } else if (val >= max) { |
| 498 | return max - 1; |
| 499 | } else { |
| 500 | return val; |
| 501 | } |
| 502 | } |
| 503 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 504 | public void setDragScoller(DragScroller scroller) { |
| 505 | mDragScroller = scroller; |
| 506 | } |
| 507 | |
| 508 | public void setWindowToken(IBinder token) { |
| 509 | mWindowToken = token; |
| 510 | } |
| 511 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 512 | /** |
| 513 | * Sets the drag listner which will be notified when a drag starts or ends. |
| 514 | */ |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 515 | public void setDragListener(DragListener l) { |
| 516 | mListener = l; |
| 517 | } |
| 518 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 519 | /** |
| 520 | * Remove a previously installed drag listener. |
| 521 | */ |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 522 | public void removeDragListener(DragListener l) { |
| 523 | mListener = null; |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Add a DropTarget to the list of potential places to receive drop events. |
| 528 | */ |
| 529 | public void addDropTarget(DropTarget target) { |
| 530 | mDropTargets.add(target); |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * Don't send drop events to <em>target</em> any more. |
| 535 | */ |
| 536 | public void removeDropTarget(DropTarget target) { |
| 537 | mDropTargets.remove(target); |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Set which view scrolls for touch events near the edge of the screen. |
| 542 | */ |
| 543 | public void setScrollView(View v) { |
| 544 | mScrollView = v; |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Specifies the delete region. We won't scroll on touch events over the delete region. |
| 549 | * |
| 550 | * @param region The rectangle in screen coordinates of the delete region. |
| 551 | */ |
| 552 | void setDeleteRegion(RectF region) { |
| 553 | mDeleteRegion = region; |
| 554 | } |
| 555 | |
| 556 | private class ScrollRunnable implements Runnable { |
| 557 | private int mDirection; |
| 558 | |
| 559 | ScrollRunnable() { |
| 560 | } |
| 561 | |
| 562 | public void run() { |
| 563 | if (mDragScroller != null) { |
| 564 | if (mDirection == SCROLL_LEFT) { |
| 565 | mDragScroller.scrollLeft(); |
| 566 | } else { |
| 567 | mDragScroller.scrollRight(); |
| 568 | } |
| 569 | mScrollState = SCROLL_OUTSIDE_ZONE; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | void setDirection(int direction) { |
| 574 | mDirection = direction; |
| 575 | } |
| 576 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 577 | } |