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