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; |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 21 | import android.graphics.Point; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 22 | import android.graphics.PointF; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 23 | import android.graphics.Rect; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 24 | import android.os.Handler; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 25 | import android.os.IBinder; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 26 | import android.os.Vibrator; |
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; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 30 | import android.view.VelocityTracker; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 31 | import android.view.View; |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 32 | import android.view.ViewConfiguration; |
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 { |
Joe Onorato | 2e5c432 | 2009-10-06 12:34:42 -0700 | [diff] [blame] | 43 | private static final String TAG = "Launcher.DragController"; |
| 44 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 45 | /** Indicates the drag is a move. */ |
| 46 | public static int DRAG_ACTION_MOVE = 0; |
| 47 | |
| 48 | /** Indicates the drag is a copy. */ |
| 49 | public static int DRAG_ACTION_COPY = 1; |
| 50 | |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 51 | private static final int SCROLL_DELAY = 500; |
| 52 | private static final int RESCROLL_DELAY = 750; |
Winson Chung | 61b0c69 | 2012-02-23 16:31:13 -0800 | [diff] [blame] | 53 | private static final int VIBRATE_DURATION = 15; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 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 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 64 | private static final float MAX_FLING_DEGREES = 35f; |
Winson Chung | 9658b1e | 2012-04-09 18:30:07 -0700 | [diff] [blame] | 65 | private static final int FLING_TO_DELETE_THRESHOLD_Y_VELOCITY = -1500; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 66 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 67 | private Launcher mLauncher; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 68 | private Handler mHandler; |
Jeff Brown | 8ef85c7 | 2012-04-13 02:58:38 -0700 | [diff] [blame] | 69 | private final Vibrator mVibrator; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 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. */ |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 79 | private int mMotionDownX; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 80 | |
| 81 | /** Y coordinate of the down event. */ |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 82 | private int mMotionDownY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 83 | |
Joe Onorato | 658db74 | 2010-09-29 11:40:39 -0700 | [diff] [blame] | 84 | /** the area at the edge of the screen that makes the workspace go left |
| 85 | * or right while you're dragging. |
| 86 | */ |
| 87 | private int mScrollZone; |
| 88 | |
Adam Cohen | 9932a9b | 2011-08-02 22:14:07 -0700 | [diff] [blame] | 89 | private DropTarget.DragObject mDragObject; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 90 | |
| 91 | /** Who can receive drop events */ |
| 92 | private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>(); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 93 | private ArrayList<DragListener> mListeners = new ArrayList<DragListener>(); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 94 | private DropTarget mFlingToDeleteDropTarget; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 95 | |
| 96 | /** The window token used as the parent for the DragView. */ |
| 97 | private IBinder mWindowToken; |
| 98 | |
| 99 | /** The view that will be scrolled when dragging to the left and right edges of the screen. */ |
| 100 | private View mScrollView; |
| 101 | |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 102 | private View mMoveTarget; |
| 103 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 104 | private DragScroller mDragScroller; |
| 105 | private int mScrollState = SCROLL_OUTSIDE_ZONE; |
| 106 | private ScrollRunnable mScrollRunnable = new ScrollRunnable(); |
| 107 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 108 | private DropTarget mLastDropTarget; |
| 109 | |
| 110 | private InputMethodManager mInputMethodManager; |
| 111 | |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 112 | private int mLastTouch[] = new int[2]; |
Winson Chung | 318eee0 | 2012-04-12 10:59:27 -0700 | [diff] [blame] | 113 | private long mLastTouchUpTime = -1; |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 114 | private int mDistanceSinceScroll = 0; |
| 115 | |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 116 | private int mTmpPoint[] = new int[2]; |
| 117 | private Rect mDragLayerRect = new Rect(); |
| 118 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 119 | protected int mFlingToDeleteThresholdVelocity; |
| 120 | private VelocityTracker mVelocityTracker; |
| 121 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 122 | /** |
| 123 | * Interface to receive notifications when a drag starts or stops |
| 124 | */ |
| 125 | interface DragListener { |
| 126 | |
| 127 | /** |
| 128 | * A drag has begun |
| 129 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 130 | * @param source An object representing where the drag originated |
| 131 | * @param info The data associated with the object that is being dragged |
| 132 | * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE} |
| 133 | * or {@link DragController#DRAG_ACTION_COPY} |
| 134 | */ |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 135 | void onDragStart(DragSource source, Object info, int dragAction); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 136 | |
| 137 | /** |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 138 | * The drag has ended |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 139 | */ |
| 140 | void onDragEnd(); |
| 141 | } |
| 142 | |
| 143 | /** |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 144 | * Used to create a new DragLayer from XML. |
| 145 | * |
| 146 | * @param context The application's context. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 147 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 148 | public DragController(Launcher launcher) { |
| 149 | mLauncher = launcher; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 150 | mHandler = new Handler(); |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 151 | mScrollZone = launcher.getResources().getDimensionPixelSize(R.dimen.scroll_zone); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 152 | mVelocityTracker = VelocityTracker.obtain(); |
Jeff Brown | 8ef85c7 | 2012-04-13 02:58:38 -0700 | [diff] [blame] | 153 | mVibrator = (Vibrator)launcher.getSystemService(Context.VIBRATOR_SERVICE); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 154 | |
| 155 | float density = launcher.getResources().getDisplayMetrics().density; |
| 156 | mFlingToDeleteThresholdVelocity = (int) (FLING_TO_DELETE_THRESHOLD_Y_VELOCITY * density); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 157 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 158 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 159 | public boolean dragging() { |
| 160 | return mDragging; |
| 161 | } |
| 162 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 163 | /** |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 164 | * Starts a drag. |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 165 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 166 | * @param v The view that is being dragged |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 167 | * @param bmp The bitmap that represents the view being dragged |
| 168 | * @param source An object representing where the drag originated |
| 169 | * @param dragInfo The data associated with the object that is being dragged |
| 170 | * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or |
| 171 | * {@link #DRAG_ACTION_COPY} |
| 172 | * @param dragRegion Coordinates within the bitmap b for the position of item being dragged. |
| 173 | * Makes dragging feel more precise, e.g. you can clip out a transparent border |
| 174 | */ |
| 175 | public void startDrag(View v, Bitmap bmp, DragSource source, Object dragInfo, int dragAction, |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 176 | Rect dragRegion, float initialDragViewScale) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 177 | int[] loc = mCoordinatesTemp; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 178 | mLauncher.getDragLayer().getLocationInDragLayer(v, loc); |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 179 | int dragLayerX = loc[0] + v.getPaddingLeft() + |
| 180 | (int) ((initialDragViewScale * bmp.getWidth() - bmp.getWidth()) / 2); |
| 181 | int dragLayerY = loc[1] + v.getPaddingTop() + |
| 182 | (int) ((initialDragViewScale * bmp.getHeight() - bmp.getHeight()) / 2); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 183 | |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 184 | startDrag(bmp, dragLayerX, dragLayerY, source, dragInfo, dragAction, null, dragRegion, |
| 185 | initialDragViewScale); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 186 | |
| 187 | if (dragAction == DRAG_ACTION_MOVE) { |
| 188 | v.setVisibility(View.GONE); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Starts a drag. |
| 194 | * |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 195 | * @param b The bitmap to display as the drag image. It will be re-scaled to the |
| 196 | * enlarged size. |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 197 | * @param dragLayerX The x position in the DragLayer of the left-top of the bitmap. |
| 198 | * @param dragLayerY The y position in the DragLayer of the left-top of the bitmap. |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 199 | * @param source An object representing where the drag originated |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 200 | * @param dragInfo The data associated with the object that is being dragged |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 201 | * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or |
| 202 | * {@link #DRAG_ACTION_COPY} |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 203 | * @param dragRegion Coordinates within the bitmap b for the position of item being dragged. |
| 204 | * Makes dragging feel more precise, e.g. you can clip out a transparent border |
| 205 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 206 | public void startDrag(Bitmap b, int dragLayerX, int dragLayerY, |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 207 | DragSource source, Object dragInfo, int dragAction, Point dragOffset, Rect dragRegion, |
| 208 | float initialDragViewScale) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 209 | if (PROFILE_DRAWING_DURING_DRAG) { |
| 210 | android.os.Debug.startMethodTracing("Launcher"); |
| 211 | } |
| 212 | |
| 213 | // Hide soft keyboard, if visible |
| 214 | if (mInputMethodManager == null) { |
| 215 | mInputMethodManager = (InputMethodManager) |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 216 | mLauncher.getSystemService(Context.INPUT_METHOD_SERVICE); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 217 | } |
| 218 | mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0); |
| 219 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 220 | for (DragListener listener : mListeners) { |
| 221 | listener.onDragStart(source, dragInfo, dragAction); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 222 | } |
| 223 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 224 | final int registrationX = mMotionDownX - dragLayerX; |
| 225 | final int registrationY = mMotionDownY - dragLayerY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 226 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 227 | final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left; |
| 228 | final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 229 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 230 | mDragging = true; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 231 | |
Adam Cohen | 9932a9b | 2011-08-02 22:14:07 -0700 | [diff] [blame] | 232 | mDragObject = new DropTarget.DragObject(); |
| 233 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 234 | mDragObject.dragComplete = false; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 235 | mDragObject.xOffset = mMotionDownX - (dragLayerX + dragRegionLeft); |
| 236 | mDragObject.yOffset = mMotionDownY - (dragLayerY + dragRegionTop); |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 237 | mDragObject.dragSource = source; |
| 238 | mDragObject.dragInfo = dragInfo; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 239 | |
| 240 | mVibrator.vibrate(VIBRATE_DURATION); |
| 241 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 242 | final DragView dragView = mDragObject.dragView = new DragView(mLauncher, b, registrationX, |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 243 | registrationY, 0, 0, b.getWidth(), b.getHeight(), initialDragViewScale); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 244 | |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 245 | if (dragOffset != null) { |
| 246 | dragView.setDragVisualizeOffset(new Point(dragOffset)); |
| 247 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 248 | if (dragRegion != null) { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 249 | dragView.setDragRegion(new Rect(dragRegion)); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 252 | dragView.show(mMotionDownX, mMotionDownY); |
| 253 | handleMoveEvent(mMotionDownX, mMotionDownY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Draw the view into a bitmap. |
| 258 | */ |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 259 | Bitmap getViewBitmap(View v) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 260 | v.clearFocus(); |
| 261 | v.setPressed(false); |
| 262 | |
| 263 | boolean willNotCache = v.willNotCacheDrawing(); |
| 264 | v.setWillNotCacheDrawing(false); |
| 265 | |
| 266 | // Reset the drawing cache background color to fully transparent |
| 267 | // for the duration of this operation |
| 268 | int color = v.getDrawingCacheBackgroundColor(); |
| 269 | v.setDrawingCacheBackgroundColor(0); |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 270 | float alpha = v.getAlpha(); |
| 271 | v.setAlpha(1.0f); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 272 | |
| 273 | if (color != 0) { |
| 274 | v.destroyDrawingCache(); |
| 275 | } |
| 276 | v.buildDrawingCache(); |
| 277 | Bitmap cacheBitmap = v.getDrawingCache(); |
Daniel Sandler | 3f8175a | 2010-05-25 11:48:32 -0400 | [diff] [blame] | 278 | if (cacheBitmap == null) { |
| 279 | Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException()); |
| 280 | return null; |
| 281 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 282 | |
| 283 | Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); |
| 284 | |
| 285 | // Restore the view |
| 286 | v.destroyDrawingCache(); |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 287 | v.setAlpha(alpha); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 288 | v.setWillNotCacheDrawing(willNotCache); |
| 289 | v.setDrawingCacheBackgroundColor(color); |
| 290 | |
| 291 | return bitmap; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Call this from a drag source view like this: |
| 296 | * |
| 297 | * <pre> |
| 298 | * @Override |
| 299 | * public boolean dispatchKeyEvent(KeyEvent event) { |
| 300 | * return mDragController.dispatchKeyEvent(this, event) |
| 301 | * || super.dispatchKeyEvent(event); |
| 302 | * </pre> |
| 303 | */ |
| 304 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 305 | return mDragging; |
| 306 | } |
| 307 | |
Winson Chung | 304dcde | 2011-01-07 11:17:23 -0800 | [diff] [blame] | 308 | public boolean isDragging() { |
| 309 | return mDragging; |
| 310 | } |
| 311 | |
Joe Onorato | 24b6fd8 | 2009-11-12 13:47:09 -0800 | [diff] [blame] | 312 | /** |
| 313 | * Stop dragging without dropping. |
| 314 | */ |
| 315 | public void cancelDrag() { |
Winson Chung | 621e640 | 2011-01-04 16:03:57 -0800 | [diff] [blame] | 316 | if (mDragging) { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 317 | if (mLastDropTarget != null) { |
| 318 | mLastDropTarget.onDragExit(mDragObject); |
| 319 | } |
Winson Chung | 41bb19d | 2012-03-05 18:36:46 -0800 | [diff] [blame] | 320 | mDragObject.deferDragViewCleanupPostAnimation = false; |
Adam Cohen | 36cc09b | 2011-09-29 17:33:15 -0700 | [diff] [blame] | 321 | mDragObject.cancelled = true; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 322 | mDragObject.dragComplete = true; |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 323 | mDragObject.dragSource.onDropCompleted(null, mDragObject, false, false); |
Winson Chung | 621e640 | 2011-01-04 16:03:57 -0800 | [diff] [blame] | 324 | } |
Joe Onorato | 24b6fd8 | 2009-11-12 13:47:09 -0800 | [diff] [blame] | 325 | endDrag(); |
| 326 | } |
Winson Chung | a182096 | 2011-10-03 16:31:06 -0700 | [diff] [blame] | 327 | public void onAppsRemoved(ArrayList<ApplicationInfo> apps, Context context) { |
| 328 | // Cancel the current drag if we are removing an app that we are dragging |
| 329 | if (mDragObject != null) { |
| 330 | Object rawDragInfo = mDragObject.dragInfo; |
| 331 | if (rawDragInfo instanceof ShortcutInfo) { |
| 332 | ShortcutInfo dragInfo = (ShortcutInfo) rawDragInfo; |
| 333 | for (ApplicationInfo info : apps) { |
Michael Jurka | 7bcadad | 2012-04-02 07:23:44 -0700 | [diff] [blame] | 334 | // Added null checks to prevent NPE we've seen in the wild |
| 335 | if (dragInfo != null && |
| 336 | dragInfo.intent != null && |
Winson Chung | 11a4937 | 2012-04-27 15:12:38 -0700 | [diff] [blame] | 337 | info.intent != null) { |
Winson Chung | be36516 | 2012-05-07 10:34:12 -0700 | [diff] [blame] | 338 | boolean isSamePackage = dragInfo.getPackageName().equals( |
| 339 | info.getPackageName()); |
Winson Chung | 11a4937 | 2012-04-27 15:12:38 -0700 | [diff] [blame] | 340 | if (isSamePackage) { |
| 341 | cancelDrag(); |
| 342 | return; |
| 343 | } |
Winson Chung | a182096 | 2011-10-03 16:31:06 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |
Joe Onorato | 24b6fd8 | 2009-11-12 13:47:09 -0800 | [diff] [blame] | 349 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 350 | private void endDrag() { |
| 351 | if (mDragging) { |
| 352 | mDragging = false; |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 353 | clearScrollRunnable(); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 354 | boolean isDeferred = false; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 355 | if (mDragObject.dragView != null) { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 356 | isDeferred = mDragObject.deferDragViewCleanupPostAnimation; |
| 357 | if (!isDeferred) { |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 358 | mDragObject.dragView.remove(); |
| 359 | } |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 360 | mDragObject.dragView = null; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 361 | } |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 362 | |
| 363 | // Only end the drag if we are not deferred |
| 364 | if (!isDeferred) { |
| 365 | for (DragListener listener : mListeners) { |
| 366 | listener.onDragEnd(); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | releaseVelocityTracker(); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * This only gets called as a result of drag view cleanup being deferred in endDrag(); |
| 376 | */ |
| 377 | void onDeferredEndDrag(DragView dragView) { |
| 378 | dragView.remove(); |
| 379 | |
| 380 | // If we skipped calling onDragEnd() before, do it now |
| 381 | for (DragListener listener : mListeners) { |
| 382 | listener.onDragEnd(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 386 | void onDeferredEndFling(DropTarget.DragObject d) { |
| 387 | d.dragSource.onFlingToDeleteCompleted(); |
| 388 | } |
| 389 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 390 | /** |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 391 | * Clamps the position to the drag layer bounds. |
| 392 | */ |
| 393 | private int[] getClampedDragLayerPos(float x, float y) { |
| 394 | mLauncher.getDragLayer().getLocalVisibleRect(mDragLayerRect); |
| 395 | mTmpPoint[0] = (int) Math.max(mDragLayerRect.left, Math.min(x, mDragLayerRect.right - 1)); |
| 396 | mTmpPoint[1] = (int) Math.max(mDragLayerRect.top, Math.min(y, mDragLayerRect.bottom - 1)); |
| 397 | return mTmpPoint; |
| 398 | } |
| 399 | |
Winson Chung | a241375 | 2012-04-03 14:22:34 -0700 | [diff] [blame] | 400 | long getLastGestureUpTime() { |
| 401 | if (mDragging) { |
| 402 | return System.currentTimeMillis(); |
| 403 | } else { |
| 404 | return mLastTouchUpTime; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | void resetLastGestureUpTime() { |
| 409 | mLastTouchUpTime = -1; |
| 410 | } |
| 411 | |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 412 | /** |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 413 | * Call this from a drag source view. |
| 414 | */ |
| 415 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 416 | @SuppressWarnings("all") // suppress dead code warning |
| 417 | final boolean debug = false; |
| 418 | if (debug) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 419 | Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging=" |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 420 | + mDragging); |
| 421 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 422 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 423 | // Update the velocity tracker |
| 424 | acquireVelocityTrackerAndAddMovement(ev); |
| 425 | |
| 426 | final int action = ev.getAction(); |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 427 | final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY()); |
| 428 | final int dragLayerX = dragLayerPos[0]; |
| 429 | final int dragLayerY = dragLayerPos[1]; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 430 | |
| 431 | switch (action) { |
| 432 | case MotionEvent.ACTION_MOVE: |
| 433 | break; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 434 | case MotionEvent.ACTION_DOWN: |
| 435 | // Remember location of down touch |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 436 | mMotionDownX = dragLayerX; |
| 437 | mMotionDownY = dragLayerY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 438 | mLastDropTarget = null; |
| 439 | break; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 440 | case MotionEvent.ACTION_UP: |
Winson Chung | a241375 | 2012-04-03 14:22:34 -0700 | [diff] [blame] | 441 | mLastTouchUpTime = System.currentTimeMillis(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 442 | if (mDragging) { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 443 | PointF vec = isFlingingToDelete(mDragObject.dragSource); |
| 444 | if (vec != null) { |
| 445 | dropOnFlingToDeleteTarget(dragLayerX, dragLayerY, vec); |
| 446 | } else { |
| 447 | drop(dragLayerX, dragLayerY); |
| 448 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 449 | } |
| 450 | endDrag(); |
| 451 | break; |
Winson Chung | 621e640 | 2011-01-04 16:03:57 -0800 | [diff] [blame] | 452 | case MotionEvent.ACTION_CANCEL: |
| 453 | cancelDrag(); |
| 454 | break; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | return mDragging; |
| 458 | } |
| 459 | |
| 460 | /** |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 461 | * Sets the view that should handle move events. |
| 462 | */ |
| 463 | void setMoveTarget(View view) { |
| 464 | mMoveTarget = view; |
| 465 | } |
| 466 | |
| 467 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 468 | return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction); |
| 469 | } |
| 470 | |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 471 | private void clearScrollRunnable() { |
| 472 | mHandler.removeCallbacks(mScrollRunnable); |
| 473 | if (mScrollState == SCROLL_WAITING_IN_ZONE) { |
| 474 | mScrollState = SCROLL_OUTSIDE_ZONE; |
| 475 | mScrollRunnable.setDirection(SCROLL_RIGHT); |
| 476 | mDragScroller.onExitScrollArea(); |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 477 | mLauncher.getDragLayer().onExitScrollArea(); |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 481 | private void handleMoveEvent(int x, int y) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 482 | mDragObject.dragView.move(x, y); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 483 | |
| 484 | // Drop on someone? |
| 485 | final int[] coordinates = mCoordinatesTemp; |
| 486 | DropTarget dropTarget = findDropTarget(x, y, coordinates); |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 487 | mDragObject.x = coordinates[0]; |
| 488 | mDragObject.y = coordinates[1]; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 489 | if (dropTarget != null) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 490 | DropTarget delegate = dropTarget.getDropTargetDelegate(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 491 | if (delegate != null) { |
| 492 | dropTarget = delegate; |
| 493 | } |
| 494 | |
| 495 | if (mLastDropTarget != dropTarget) { |
| 496 | if (mLastDropTarget != null) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 497 | mLastDropTarget.onDragExit(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 498 | } |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 499 | dropTarget.onDragEnter(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 500 | } |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 501 | dropTarget.onDragOver(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 502 | } else { |
| 503 | if (mLastDropTarget != null) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 504 | mLastDropTarget.onDragExit(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | mLastDropTarget = dropTarget; |
| 508 | |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 509 | // After a scroll, the touch point will still be in the scroll region. |
| 510 | // Rather than scrolling immediately, require a bit of twiddling to scroll again |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 511 | final int slop = ViewConfiguration.get(mLauncher).getScaledWindowTouchSlop(); |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 512 | mDistanceSinceScroll += |
| 513 | Math.sqrt(Math.pow(mLastTouch[0] - x, 2) + Math.pow(mLastTouch[1] - y, 2)); |
| 514 | mLastTouch[0] = x; |
| 515 | mLastTouch[1] = y; |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 516 | final int delay = mDistanceSinceScroll < slop ? RESCROLL_DELAY : SCROLL_DELAY; |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 517 | |
Winson Chung | 3f4e142 | 2011-11-17 14:58:51 -0800 | [diff] [blame] | 518 | if (x < mScrollZone) { |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 519 | if (mScrollState == SCROLL_OUTSIDE_ZONE) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 520 | mScrollState = SCROLL_WAITING_IN_ZONE; |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 521 | if (mDragScroller.onEnterScrollArea(x, y, SCROLL_LEFT)) { |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 522 | mLauncher.getDragLayer().onEnterScrollArea(SCROLL_LEFT); |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 523 | mScrollRunnable.setDirection(SCROLL_LEFT); |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 524 | mHandler.postDelayed(mScrollRunnable, delay); |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 525 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 526 | } |
Winson Chung | 3f4e142 | 2011-11-17 14:58:51 -0800 | [diff] [blame] | 527 | } else if (x > mScrollView.getWidth() - mScrollZone) { |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 528 | if (mScrollState == SCROLL_OUTSIDE_ZONE) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 529 | mScrollState = SCROLL_WAITING_IN_ZONE; |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 530 | if (mDragScroller.onEnterScrollArea(x, y, SCROLL_RIGHT)) { |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 531 | mLauncher.getDragLayer().onEnterScrollArea(SCROLL_RIGHT); |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 532 | mScrollRunnable.setDirection(SCROLL_RIGHT); |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 533 | mHandler.postDelayed(mScrollRunnable, delay); |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 534 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 535 | } |
| 536 | } else { |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 537 | clearScrollRunnable(); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 541 | public void forceMoveEvent() { |
| 542 | if (mDragging) { |
| 543 | handleMoveEvent(mDragObject.x, mDragObject.y); |
| 544 | } |
| 545 | } |
| 546 | |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 547 | /** |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 548 | * Call this from a drag source view. |
| 549 | */ |
| 550 | public boolean onTouchEvent(MotionEvent ev) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 551 | if (!mDragging) { |
| 552 | return false; |
| 553 | } |
| 554 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 555 | // Update the velocity tracker |
| 556 | acquireVelocityTrackerAndAddMovement(ev); |
| 557 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 558 | final int action = ev.getAction(); |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 559 | final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY()); |
| 560 | final int dragLayerX = dragLayerPos[0]; |
| 561 | final int dragLayerY = dragLayerPos[1]; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 562 | |
| 563 | switch (action) { |
| 564 | case MotionEvent.ACTION_DOWN: |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 565 | // Remember where the motion event started |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 566 | mMotionDownX = dragLayerX; |
| 567 | mMotionDownY = dragLayerY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 568 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 569 | if ((dragLayerX < mScrollZone) || (dragLayerX > mScrollView.getWidth() - mScrollZone)) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 570 | mScrollState = SCROLL_WAITING_IN_ZONE; |
| 571 | mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY); |
| 572 | } else { |
| 573 | mScrollState = SCROLL_OUTSIDE_ZONE; |
| 574 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 575 | break; |
| 576 | case MotionEvent.ACTION_MOVE: |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 577 | handleMoveEvent(dragLayerX, dragLayerY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 578 | break; |
| 579 | case MotionEvent.ACTION_UP: |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 580 | // Ensure that we've processed a move event at the current pointer location. |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 581 | handleMoveEvent(dragLayerX, dragLayerY); |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 582 | mHandler.removeCallbacks(mScrollRunnable); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 583 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 584 | if (mDragging) { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 585 | PointF vec = isFlingingToDelete(mDragObject.dragSource); |
| 586 | if (vec != null) { |
| 587 | dropOnFlingToDeleteTarget(dragLayerX, dragLayerY, vec); |
| 588 | } else { |
| 589 | drop(dragLayerX, dragLayerY); |
| 590 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 591 | } |
| 592 | endDrag(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 593 | break; |
| 594 | case MotionEvent.ACTION_CANCEL: |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 595 | mHandler.removeCallbacks(mScrollRunnable); |
Joe Onorato | 24b6fd8 | 2009-11-12 13:47:09 -0800 | [diff] [blame] | 596 | cancelDrag(); |
Winson Chung | 621e640 | 2011-01-04 16:03:57 -0800 | [diff] [blame] | 597 | break; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | return true; |
| 601 | } |
| 602 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 603 | /** |
| 604 | * Determines whether the user flung the current item to delete it. |
| 605 | * |
| 606 | * @return the vector at which the item was flung, or null if no fling was detected. |
| 607 | */ |
| 608 | private PointF isFlingingToDelete(DragSource source) { |
| 609 | if (mFlingToDeleteDropTarget == null) return null; |
| 610 | if (!source.supportsFlingToDelete()) return null; |
| 611 | |
| 612 | ViewConfiguration config = ViewConfiguration.get(mLauncher); |
| 613 | mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity()); |
| 614 | |
| 615 | if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) { |
| 616 | // Do a quick dot product test to ensure that we are flinging upwards |
| 617 | PointF vel = new PointF(mVelocityTracker.getXVelocity(), |
| 618 | mVelocityTracker.getYVelocity()); |
| 619 | PointF upVec = new PointF(0f, -1f); |
| 620 | float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) / |
| 621 | (vel.length() * upVec.length())); |
| 622 | if (theta <= Math.toRadians(MAX_FLING_DEGREES)) { |
| 623 | return vel; |
| 624 | } |
| 625 | } |
| 626 | return null; |
| 627 | } |
| 628 | |
| 629 | private void dropOnFlingToDeleteTarget(float x, float y, PointF vel) { |
| 630 | final int[] coordinates = mCoordinatesTemp; |
| 631 | |
| 632 | mDragObject.x = coordinates[0]; |
| 633 | mDragObject.y = coordinates[1]; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 634 | |
| 635 | // Clean up dragging on the target if it's not the current fling delete target otherwise, |
| 636 | // start dragging to it. |
| 637 | if (mLastDropTarget != null && mFlingToDeleteDropTarget != mLastDropTarget) { |
| 638 | mLastDropTarget.onDragExit(mDragObject); |
| 639 | } |
| 640 | |
| 641 | // Drop onto the fling-to-delete target |
| 642 | boolean accepted = false; |
| 643 | mFlingToDeleteDropTarget.onDragEnter(mDragObject); |
Winson Chung | 232decb | 2012-03-28 15:09:05 -0700 | [diff] [blame] | 644 | // We must set dragComplete to true _only_ after we "enter" the fling-to-delete target for |
| 645 | // "drop" |
| 646 | mDragObject.dragComplete = true; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 647 | mFlingToDeleteDropTarget.onDragExit(mDragObject); |
| 648 | if (mFlingToDeleteDropTarget.acceptDrop(mDragObject)) { |
| 649 | mFlingToDeleteDropTarget.onFlingToDelete(mDragObject, mDragObject.x, mDragObject.y, |
| 650 | vel); |
| 651 | accepted = true; |
| 652 | } |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 653 | mDragObject.dragSource.onDropCompleted((View) mFlingToDeleteDropTarget, mDragObject, true, |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 654 | accepted); |
| 655 | } |
| 656 | |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 657 | private void drop(float x, float y) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 658 | final int[] coordinates = mCoordinatesTemp; |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 659 | final DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 660 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 661 | mDragObject.x = coordinates[0]; |
| 662 | mDragObject.y = coordinates[1]; |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 663 | boolean accepted = false; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 664 | if (dropTarget != null) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 665 | mDragObject.dragComplete = true; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 666 | dropTarget.onDragExit(mDragObject); |
| 667 | if (dropTarget.acceptDrop(mDragObject)) { |
| 668 | dropTarget.onDrop(mDragObject); |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 669 | accepted = true; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 670 | } |
| 671 | } |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 672 | mDragObject.dragSource.onDropCompleted((View) dropTarget, mDragObject, false, accepted); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) { |
| 676 | final Rect r = mRectTemp; |
| 677 | |
| 678 | final ArrayList<DropTarget> dropTargets = mDropTargets; |
| 679 | final int count = dropTargets.size(); |
| 680 | for (int i=count-1; i>=0; i--) { |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 681 | DropTarget target = dropTargets.get(i); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 682 | if (!target.isDropEnabled()) |
| 683 | continue; |
| 684 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 685 | target.getHitRect(r); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 686 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 687 | // Convert the hit rect to DragLayer coordinates |
| 688 | target.getLocationInDragLayer(dropCoordinates); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 689 | r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop()); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 690 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 691 | mDragObject.x = x; |
| 692 | mDragObject.y = y; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 693 | if (r.contains(x, y)) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 694 | DropTarget delegate = target.getDropTargetDelegate(mDragObject); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 695 | if (delegate != null) { |
| 696 | target = delegate; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 697 | target.getLocationInDragLayer(dropCoordinates); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | // Make dropCoordinates relative to the DropTarget |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 701 | dropCoordinates[0] = x - dropCoordinates[0]; |
| 702 | dropCoordinates[1] = y - dropCoordinates[1]; |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 703 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 704 | return target; |
| 705 | } |
| 706 | } |
| 707 | return null; |
| 708 | } |
| 709 | |
| 710 | public void setDragScoller(DragScroller scroller) { |
| 711 | mDragScroller = scroller; |
| 712 | } |
| 713 | |
| 714 | public void setWindowToken(IBinder token) { |
| 715 | mWindowToken = token; |
| 716 | } |
| 717 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 718 | /** |
| 719 | * Sets the drag listner which will be notified when a drag starts or ends. |
| 720 | */ |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 721 | public void addDragListener(DragListener l) { |
| 722 | mListeners.add(l); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 723 | } |
| 724 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 725 | /** |
| 726 | * Remove a previously installed drag listener. |
| 727 | */ |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 728 | public void removeDragListener(DragListener l) { |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 729 | mListeners.remove(l); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | /** |
| 733 | * Add a DropTarget to the list of potential places to receive drop events. |
| 734 | */ |
| 735 | public void addDropTarget(DropTarget target) { |
| 736 | mDropTargets.add(target); |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * Don't send drop events to <em>target</em> any more. |
| 741 | */ |
| 742 | public void removeDropTarget(DropTarget target) { |
| 743 | mDropTargets.remove(target); |
| 744 | } |
| 745 | |
| 746 | /** |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 747 | * Sets the current fling-to-delete drop target. |
| 748 | */ |
| 749 | public void setFlingToDeleteDropTarget(DropTarget target) { |
| 750 | mFlingToDeleteDropTarget = target; |
| 751 | } |
| 752 | |
| 753 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 754 | if (mVelocityTracker == null) { |
| 755 | mVelocityTracker = VelocityTracker.obtain(); |
| 756 | } |
| 757 | mVelocityTracker.addMovement(ev); |
| 758 | } |
| 759 | |
| 760 | private void releaseVelocityTracker() { |
| 761 | if (mVelocityTracker != null) { |
| 762 | mVelocityTracker.recycle(); |
| 763 | mVelocityTracker = null; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | /** |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 768 | * Set which view scrolls for touch events near the edge of the screen. |
| 769 | */ |
| 770 | public void setScrollView(View v) { |
| 771 | mScrollView = v; |
| 772 | } |
| 773 | |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 774 | DragView getDragView() { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 775 | return mDragObject.dragView; |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 776 | } |
| 777 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 778 | private class ScrollRunnable implements Runnable { |
| 779 | private int mDirection; |
| 780 | |
| 781 | ScrollRunnable() { |
| 782 | } |
| 783 | |
| 784 | public void run() { |
| 785 | if (mDragScroller != null) { |
| 786 | if (mDirection == SCROLL_LEFT) { |
| 787 | mDragScroller.scrollLeft(); |
| 788 | } else { |
| 789 | mDragScroller.scrollRight(); |
| 790 | } |
| 791 | mScrollState = SCROLL_OUTSIDE_ZONE; |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 792 | mDistanceSinceScroll = 0; |
| 793 | mDragScroller.onExitScrollArea(); |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 794 | mLauncher.getDragLayer().onExitScrollArea(); |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 795 | |
| 796 | if (isDragging()) { |
| 797 | // Force an update so that we can requeue the scroller if necessary |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 798 | forceMoveEvent(); |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 799 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
| 803 | void setDirection(int direction) { |
| 804 | mDirection = direction; |
| 805 | } |
| 806 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 807 | } |