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