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