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 | |
| 282 | private void endDrag() { |
| 283 | if (mDragging) { |
| 284 | mDragging = false; |
| 285 | if (mOriginator != null) { |
| 286 | mOriginator.setVisibility(View.VISIBLE); |
| 287 | } |
| 288 | if (mListener != null) { |
| 289 | mListener.onDragEnd(); |
| 290 | } |
| 291 | if (mDragView != null) { |
| 292 | mDragView.remove(); |
| 293 | mDragView = null; |
| 294 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Call this from a drag source view. |
| 300 | */ |
| 301 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 302 | if (false) { |
| 303 | Log.d(Launcher.LOG_TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging=" |
| 304 | + mDragging); |
| 305 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 306 | final int action = ev.getAction(); |
| 307 | |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 308 | final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels); |
| 309 | final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 310 | |
| 311 | switch (action) { |
| 312 | case MotionEvent.ACTION_MOVE: |
| 313 | break; |
| 314 | |
| 315 | case MotionEvent.ACTION_DOWN: |
| 316 | // Remember location of down touch |
| 317 | mMotionDownX = screenX; |
| 318 | mMotionDownY = screenY; |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 319 | recordScreenSize(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 320 | mLastDropTarget = null; |
| 321 | break; |
| 322 | |
| 323 | case MotionEvent.ACTION_CANCEL: |
| 324 | case MotionEvent.ACTION_UP: |
| 325 | if (mDragging) { |
| 326 | drop(screenX, screenY); |
| 327 | } |
| 328 | endDrag(); |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | return mDragging; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Call this from a drag source view. |
| 337 | */ |
| 338 | public boolean onTouchEvent(MotionEvent ev) { |
| 339 | View scrollView = mScrollView; |
| 340 | |
| 341 | if (!mDragging) { |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | final int action = ev.getAction(); |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 346 | final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels); |
| 347 | final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 348 | |
| 349 | switch (action) { |
| 350 | case MotionEvent.ACTION_DOWN: |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 351 | // Remember where the motion event started |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 352 | mMotionDownX = screenX; |
| 353 | mMotionDownY = screenY; |
| 354 | recordScreenSize(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 355 | |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 356 | if ((screenX < SCROLL_ZONE) || (screenX > scrollView.getWidth() - SCROLL_ZONE)) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 357 | mScrollState = SCROLL_WAITING_IN_ZONE; |
| 358 | mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY); |
| 359 | } else { |
| 360 | mScrollState = SCROLL_OUTSIDE_ZONE; |
| 361 | } |
| 362 | |
| 363 | break; |
| 364 | case MotionEvent.ACTION_MOVE: |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 365 | // Update the drag view. Don't use the clamped pos here so the dragging looks |
| 366 | // 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] | 367 | mDragView.move((int)ev.getRawX(), (int)ev.getRawY()); |
| 368 | |
| 369 | // Drop on someone? |
| 370 | final int[] coordinates = mCoordinatesTemp; |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 371 | DropTarget dropTarget = findDropTarget((int) screenX, (int) screenY, coordinates); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 372 | if (dropTarget != null) { |
| 373 | if (mLastDropTarget == dropTarget) { |
| 374 | dropTarget.onDragOver(mDragSource, coordinates[0], coordinates[1], |
| 375 | (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); |
| 376 | } else { |
| 377 | if (mLastDropTarget != null) { |
| 378 | mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1], |
| 379 | (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); |
| 380 | } |
| 381 | dropTarget.onDragEnter(mDragSource, coordinates[0], coordinates[1], |
| 382 | (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); |
| 383 | } |
| 384 | } else { |
| 385 | if (mLastDropTarget != null) { |
| 386 | mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1], |
| 387 | (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); |
| 388 | } |
| 389 | } |
| 390 | mLastDropTarget = dropTarget; |
| 391 | |
| 392 | // Scroll, maybe, but not if we're in the delete region. |
| 393 | boolean inDeleteRegion = false; |
| 394 | if (mDeleteRegion != null) { |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 395 | inDeleteRegion = mDeleteRegion.contains(screenX, screenY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 396 | } |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 397 | if (!inDeleteRegion && screenX < SCROLL_ZONE) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 398 | if (mScrollState == SCROLL_OUTSIDE_ZONE) { |
| 399 | mScrollState = SCROLL_WAITING_IN_ZONE; |
| 400 | mScrollRunnable.setDirection(SCROLL_LEFT); |
| 401 | mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY); |
| 402 | } |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 403 | } else if (!inDeleteRegion && screenX > scrollView.getWidth() - SCROLL_ZONE) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 404 | if (mScrollState == SCROLL_OUTSIDE_ZONE) { |
| 405 | mScrollState = SCROLL_WAITING_IN_ZONE; |
| 406 | mScrollRunnable.setDirection(SCROLL_RIGHT); |
| 407 | mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY); |
| 408 | } |
| 409 | } else { |
| 410 | if (mScrollState == SCROLL_WAITING_IN_ZONE) { |
| 411 | mScrollState = SCROLL_OUTSIDE_ZONE; |
| 412 | mScrollRunnable.setDirection(SCROLL_RIGHT); |
| 413 | mHandler.removeCallbacks(mScrollRunnable); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | break; |
| 418 | case MotionEvent.ACTION_UP: |
| 419 | mHandler.removeCallbacks(mScrollRunnable); |
| 420 | if (mDragging) { |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 421 | drop(screenX, screenY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 422 | } |
| 423 | endDrag(); |
| 424 | |
| 425 | break; |
| 426 | case MotionEvent.ACTION_CANCEL: |
| 427 | endDrag(); |
| 428 | } |
| 429 | |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | private boolean drop(float x, float y) { |
| 434 | final int[] coordinates = mCoordinatesTemp; |
| 435 | DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates); |
| 436 | |
| 437 | if (dropTarget != null) { |
| 438 | dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1], |
| 439 | (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); |
| 440 | if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1], |
| 441 | (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) { |
| 442 | dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1], |
| 443 | (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); |
| 444 | mDragSource.onDropCompleted((View) dropTarget, true); |
| 445 | return true; |
| 446 | } else { |
| 447 | mDragSource.onDropCompleted((View) dropTarget, false); |
| 448 | return true; |
| 449 | } |
| 450 | } |
| 451 | return false; |
| 452 | } |
| 453 | |
| 454 | private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) { |
| 455 | final Rect r = mRectTemp; |
| 456 | |
| 457 | final ArrayList<DropTarget> dropTargets = mDropTargets; |
| 458 | final int count = dropTargets.size(); |
| 459 | for (int i=count-1; i>=0; i--) { |
| 460 | final DropTarget target = dropTargets.get(i); |
| 461 | target.getHitRect(r); |
| 462 | target.getLocationOnScreen(dropCoordinates); |
| 463 | r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop()); |
| 464 | if (r.contains(x, y)) { |
| 465 | dropCoordinates[0] = x - dropCoordinates[0]; |
| 466 | dropCoordinates[1] = y - dropCoordinates[1]; |
| 467 | return target; |
| 468 | } |
| 469 | } |
| 470 | return null; |
| 471 | } |
| 472 | |
Joe Onorato | e048e8a | 2009-09-25 10:39:17 -0700 | [diff] [blame] | 473 | /** |
| 474 | * Get the screen size so we can clamp events to the screen size so even if |
| 475 | * you drag off the edge of the screen, we find something. |
| 476 | */ |
| 477 | private void recordScreenSize() { |
| 478 | ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)) |
| 479 | .getDefaultDisplay().getMetrics(mDisplayMetrics); |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Clamp val to be >= min and < max. |
| 484 | */ |
| 485 | private static int clamp(int val, int min, int max) { |
| 486 | if (val < min) { |
| 487 | return min; |
| 488 | } else if (val >= max) { |
| 489 | return max - 1; |
| 490 | } else { |
| 491 | return val; |
| 492 | } |
| 493 | } |
| 494 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 495 | public void setDragScoller(DragScroller scroller) { |
| 496 | mDragScroller = scroller; |
| 497 | } |
| 498 | |
| 499 | public void setWindowToken(IBinder token) { |
| 500 | mWindowToken = token; |
| 501 | } |
| 502 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 503 | /** |
| 504 | * Sets the drag listner which will be notified when a drag starts or ends. |
| 505 | */ |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 506 | public void setDragListener(DragListener l) { |
| 507 | mListener = l; |
| 508 | } |
| 509 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 510 | /** |
| 511 | * Remove a previously installed drag listener. |
| 512 | */ |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 513 | public void removeDragListener(DragListener l) { |
| 514 | mListener = null; |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Add a DropTarget to the list of potential places to receive drop events. |
| 519 | */ |
| 520 | public void addDropTarget(DropTarget target) { |
| 521 | mDropTargets.add(target); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Don't send drop events to <em>target</em> any more. |
| 526 | */ |
| 527 | public void removeDropTarget(DropTarget target) { |
| 528 | mDropTargets.remove(target); |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * Set which view scrolls for touch events near the edge of the screen. |
| 533 | */ |
| 534 | public void setScrollView(View v) { |
| 535 | mScrollView = v; |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Specifies the delete region. We won't scroll on touch events over the delete region. |
| 540 | * |
| 541 | * @param region The rectangle in screen coordinates of the delete region. |
| 542 | */ |
| 543 | void setDeleteRegion(RectF region) { |
| 544 | mDeleteRegion = region; |
| 545 | } |
| 546 | |
| 547 | private class ScrollRunnable implements Runnable { |
| 548 | private int mDirection; |
| 549 | |
| 550 | ScrollRunnable() { |
| 551 | } |
| 552 | |
| 553 | public void run() { |
| 554 | if (mDragScroller != null) { |
| 555 | if (mDirection == SCROLL_LEFT) { |
| 556 | mDragScroller.scrollLeft(); |
| 557 | } else { |
| 558 | mDragScroller.scrollRight(); |
| 559 | } |
| 560 | mScrollState = SCROLL_OUTSIDE_ZONE; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | void setDirection(int direction) { |
| 565 | mDirection = direction; |
| 566 | } |
| 567 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 568 | } |