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 | |
Winson Chung | df95eb1 | 2013-10-16 14:57:07 -0700 | [diff] [blame] | 19 | import android.content.ComponentName; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 20 | import android.content.Context; |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 22 | import android.graphics.Bitmap; |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 23 | import android.graphics.Point; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 24 | import android.graphics.PointF; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 25 | import android.graphics.Rect; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 26 | import android.os.Handler; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 27 | import android.os.IBinder; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 28 | import android.util.Log; |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 29 | import android.view.HapticFeedbackConstants; |
| 30 | import android.view.KeyEvent; |
| 31 | import android.view.MotionEvent; |
| 32 | import android.view.VelocityTracker; |
| 33 | import android.view.View; |
| 34 | import android.view.ViewConfiguration; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 35 | import android.view.inputmethod.InputMethodManager; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 36 | |
| 37 | import java.util.ArrayList; |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 38 | import java.util.HashSet; |
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; |
Adam Cohen | 2bf63d5 | 2013-09-29 17:46:49 -0700 | [diff] [blame] | 53 | private static final int RESCROLL_DELAY = PagedView.PAGE_SNAP_ANIMATION_DURATION + 150; |
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 | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 65 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 66 | private Launcher mLauncher; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 67 | private Handler mHandler; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 68 | |
| 69 | // temporaries to avoid gc thrash |
| 70 | private Rect mRectTemp = new Rect(); |
| 71 | private final int[] mCoordinatesTemp = new int[2]; |
| 72 | |
| 73 | /** Whether or not we're dragging. */ |
| 74 | private boolean mDragging; |
| 75 | |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 76 | /** Whether or not this is an accessible drag operation */ |
| 77 | private boolean mIsAccessibleDrag; |
| 78 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 79 | /** X coordinate of the down event. */ |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 80 | private int mMotionDownX; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 81 | |
| 82 | /** Y coordinate of the down event. */ |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 83 | private int mMotionDownY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 84 | |
Joe Onorato | 658db74 | 2010-09-29 11:40:39 -0700 | [diff] [blame] | 85 | /** the area at the edge of the screen that makes the workspace go left |
| 86 | * or right while you're dragging. |
| 87 | */ |
| 88 | private int mScrollZone; |
| 89 | |
Adam Cohen | 9932a9b | 2011-08-02 22:14:07 -0700 | [diff] [blame] | 90 | private DropTarget.DragObject mDragObject; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 91 | |
| 92 | /** Who can receive drop events */ |
| 93 | private ArrayList<DropTarget> mDropTargets = new ArrayList<DropTarget>(); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 94 | private ArrayList<DragListener> mListeners = new ArrayList<DragListener>(); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 95 | private DropTarget mFlingToDeleteDropTarget; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 96 | |
| 97 | /** The window token used as the parent for the DragView. */ |
| 98 | private IBinder mWindowToken; |
| 99 | |
| 100 | /** The view that will be scrolled when dragging to the left and right edges of the screen. */ |
| 101 | private View mScrollView; |
| 102 | |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 103 | private View mMoveTarget; |
| 104 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 105 | private DragScroller mDragScroller; |
| 106 | private int mScrollState = SCROLL_OUTSIDE_ZONE; |
| 107 | private ScrollRunnable mScrollRunnable = new ScrollRunnable(); |
| 108 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 109 | private DropTarget mLastDropTarget; |
| 110 | |
| 111 | private InputMethodManager mInputMethodManager; |
| 112 | |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 113 | private int mLastTouch[] = new int[2]; |
Winson Chung | 318eee0 | 2012-04-12 10:59:27 -0700 | [diff] [blame] | 114 | private long mLastTouchUpTime = -1; |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 115 | private int mDistanceSinceScroll = 0; |
| 116 | |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 117 | private int mTmpPoint[] = new int[2]; |
| 118 | private Rect mDragLayerRect = new Rect(); |
| 119 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 120 | protected int mFlingToDeleteThresholdVelocity; |
| 121 | private VelocityTracker mVelocityTracker; |
| 122 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 123 | /** |
| 124 | * Interface to receive notifications when a drag starts or stops |
| 125 | */ |
| 126 | interface DragListener { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 127 | /** |
| 128 | * A drag has begun |
Mindy DelliCarpini | 53b8d07 | 2013-07-03 08:23:13 -0700 | [diff] [blame] | 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); |
Mindy DelliCarpini | 53b8d07 | 2013-07-03 08:23:13 -0700 | [diff] [blame] | 136 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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) { |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 149 | Resources r = launcher.getResources(); |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 150 | mLauncher = launcher; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 151 | mHandler = new Handler(); |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 152 | mScrollZone = r.getDimensionPixelSize(R.dimen.scroll_zone); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 153 | mVelocityTracker = VelocityTracker.obtain(); |
| 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, |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 188 | null, initialDragViewScale, false); |
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 |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 208 | * @param accessible whether this drag should occur in accessibility mode |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 209 | */ |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 210 | public DragView startDrag(Bitmap b, int dragLayerX, int dragLayerY, |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 211 | DragSource source, Object dragInfo, int dragAction, Point dragOffset, Rect dragRegion, |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 212 | float initialDragViewScale, boolean accessible) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 213 | if (PROFILE_DRAWING_DURING_DRAG) { |
| 214 | android.os.Debug.startMethodTracing("Launcher"); |
| 215 | } |
| 216 | |
| 217 | // Hide soft keyboard, if visible |
| 218 | if (mInputMethodManager == null) { |
| 219 | mInputMethodManager = (InputMethodManager) |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 220 | mLauncher.getSystemService(Context.INPUT_METHOD_SERVICE); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 221 | } |
| 222 | mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0); |
| 223 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 224 | for (DragListener listener : mListeners) { |
| 225 | listener.onDragStart(source, dragInfo, dragAction); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 226 | } |
| 227 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 228 | final int registrationX = mMotionDownX - dragLayerX; |
| 229 | final int registrationY = mMotionDownY - dragLayerY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 230 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 231 | final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left; |
| 232 | final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 233 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 234 | mDragging = true; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 235 | mIsAccessibleDrag = accessible; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 236 | |
Adam Cohen | 9932a9b | 2011-08-02 22:14:07 -0700 | [diff] [blame] | 237 | mDragObject = new DropTarget.DragObject(); |
| 238 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 239 | mDragObject.dragComplete = false; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 240 | if (mIsAccessibleDrag) { |
| 241 | // For an accessible drag, we assume the view is being dragged from the center. |
| 242 | mDragObject.xOffset = b.getWidth() / 2; |
| 243 | mDragObject.yOffset = b.getHeight() / 2; |
| 244 | mDragObject.accessibleDrag = true; |
| 245 | } else { |
| 246 | mDragObject.xOffset = mMotionDownX - (dragLayerX + dragRegionLeft); |
| 247 | mDragObject.yOffset = mMotionDownY - (dragLayerY + dragRegionTop); |
| 248 | } |
| 249 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 250 | mDragObject.dragSource = source; |
| 251 | mDragObject.dragInfo = dragInfo; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 252 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 253 | final DragView dragView = mDragObject.dragView = new DragView(mLauncher, b, registrationX, |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 254 | registrationY, 0, 0, b.getWidth(), b.getHeight(), initialDragViewScale); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 255 | |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 256 | if (dragOffset != null) { |
| 257 | dragView.setDragVisualizeOffset(new Point(dragOffset)); |
| 258 | } |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 259 | if (dragRegion != null) { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 260 | dragView.setDragRegion(new Rect(dragRegion)); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Winson Chung | 780fe59 | 2013-09-26 14:48:44 -0700 | [diff] [blame] | 263 | mLauncher.getDragLayer().performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 264 | dragView.show(mMotionDownX, mMotionDownY); |
| 265 | handleMoveEvent(mMotionDownX, mMotionDownY); |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 266 | return dragView; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Draw the view into a bitmap. |
| 271 | */ |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 272 | Bitmap getViewBitmap(View v) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 273 | v.clearFocus(); |
| 274 | v.setPressed(false); |
| 275 | |
| 276 | boolean willNotCache = v.willNotCacheDrawing(); |
| 277 | v.setWillNotCacheDrawing(false); |
| 278 | |
| 279 | // Reset the drawing cache background color to fully transparent |
| 280 | // for the duration of this operation |
| 281 | int color = v.getDrawingCacheBackgroundColor(); |
| 282 | v.setDrawingCacheBackgroundColor(0); |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 283 | float alpha = v.getAlpha(); |
| 284 | v.setAlpha(1.0f); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 285 | |
| 286 | if (color != 0) { |
| 287 | v.destroyDrawingCache(); |
| 288 | } |
| 289 | v.buildDrawingCache(); |
| 290 | Bitmap cacheBitmap = v.getDrawingCache(); |
Daniel Sandler | 3f8175a | 2010-05-25 11:48:32 -0400 | [diff] [blame] | 291 | if (cacheBitmap == null) { |
| 292 | Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException()); |
| 293 | return null; |
| 294 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 295 | |
| 296 | Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); |
| 297 | |
| 298 | // Restore the view |
| 299 | v.destroyDrawingCache(); |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 300 | v.setAlpha(alpha); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 301 | v.setWillNotCacheDrawing(willNotCache); |
| 302 | v.setDrawingCacheBackgroundColor(color); |
| 303 | |
| 304 | return bitmap; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Call this from a drag source view like this: |
| 309 | * |
| 310 | * <pre> |
| 311 | * @Override |
| 312 | * public boolean dispatchKeyEvent(KeyEvent event) { |
| 313 | * return mDragController.dispatchKeyEvent(this, event) |
| 314 | * || super.dispatchKeyEvent(event); |
| 315 | * </pre> |
| 316 | */ |
| 317 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 318 | return mDragging; |
| 319 | } |
| 320 | |
Winson Chung | 304dcde | 2011-01-07 11:17:23 -0800 | [diff] [blame] | 321 | public boolean isDragging() { |
| 322 | return mDragging; |
| 323 | } |
| 324 | |
Joe Onorato | 24b6fd8 | 2009-11-12 13:47:09 -0800 | [diff] [blame] | 325 | /** |
| 326 | * Stop dragging without dropping. |
| 327 | */ |
| 328 | public void cancelDrag() { |
Winson Chung | 621e640 | 2011-01-04 16:03:57 -0800 | [diff] [blame] | 329 | if (mDragging) { |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 330 | if (mLastDropTarget != null) { |
| 331 | mLastDropTarget.onDragExit(mDragObject); |
| 332 | } |
Winson Chung | 41bb19d | 2012-03-05 18:36:46 -0800 | [diff] [blame] | 333 | mDragObject.deferDragViewCleanupPostAnimation = false; |
Adam Cohen | 36cc09b | 2011-09-29 17:33:15 -0700 | [diff] [blame] | 334 | mDragObject.cancelled = true; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 335 | mDragObject.dragComplete = true; |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 336 | mDragObject.dragSource.onDropCompleted(null, mDragObject, false, false); |
Winson Chung | 621e640 | 2011-01-04 16:03:57 -0800 | [diff] [blame] | 337 | } |
Joe Onorato | 24b6fd8 | 2009-11-12 13:47:09 -0800 | [diff] [blame] | 338 | endDrag(); |
| 339 | } |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 340 | public void onAppsRemoved(final ArrayList<String> packageNames, HashSet<ComponentName> cns) { |
Winson Chung | a182096 | 2011-10-03 16:31:06 -0700 | [diff] [blame] | 341 | // Cancel the current drag if we are removing an app that we are dragging |
| 342 | if (mDragObject != null) { |
| 343 | Object rawDragInfo = mDragObject.dragInfo; |
| 344 | if (rawDragInfo instanceof ShortcutInfo) { |
| 345 | ShortcutInfo dragInfo = (ShortcutInfo) rawDragInfo; |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 346 | for (ComponentName componentName : cns) { |
Michael Jurka | 7bcadad | 2012-04-02 07:23:44 -0700 | [diff] [blame] | 347 | // Added null checks to prevent NPE we've seen in the wild |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 348 | if (dragInfo != null && dragInfo.intent != null) { |
Winson Chung | df95eb1 | 2013-10-16 14:57:07 -0700 | [diff] [blame] | 349 | ComponentName cn = dragInfo.intent.getComponent(); |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 350 | boolean isSameComponent = cn != null && (cn.equals(componentName) || |
Adam Cohen | 60e1efe | 2014-06-13 19:11:57 -0700 | [diff] [blame] | 351 | packageNames.contains(cn.getPackageName())); |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 352 | if (isSameComponent) { |
Winson Chung | 11a4937 | 2012-04-27 15:12:38 -0700 | [diff] [blame] | 353 | cancelDrag(); |
| 354 | return; |
| 355 | } |
Winson Chung | a182096 | 2011-10-03 16:31:06 -0700 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | } |
Joe Onorato | 24b6fd8 | 2009-11-12 13:47:09 -0800 | [diff] [blame] | 361 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 362 | private void endDrag() { |
| 363 | if (mDragging) { |
| 364 | mDragging = false; |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 365 | mIsAccessibleDrag = false; |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 366 | clearScrollRunnable(); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 367 | boolean isDeferred = false; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 368 | if (mDragObject.dragView != null) { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 369 | isDeferred = mDragObject.deferDragViewCleanupPostAnimation; |
| 370 | if (!isDeferred) { |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 371 | mDragObject.dragView.remove(); |
| 372 | } |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 373 | mDragObject.dragView = null; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 374 | } |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 375 | |
| 376 | // Only end the drag if we are not deferred |
| 377 | if (!isDeferred) { |
| 378 | for (DragListener listener : mListeners) { |
| 379 | listener.onDragEnd(); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | releaseVelocityTracker(); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * This only gets called as a result of drag view cleanup being deferred in endDrag(); |
| 389 | */ |
| 390 | void onDeferredEndDrag(DragView dragView) { |
| 391 | dragView.remove(); |
| 392 | |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 393 | if (mDragObject.deferDragViewCleanupPostAnimation) { |
| 394 | // If we skipped calling onDragEnd() before, do it now |
| 395 | for (DragListener listener : mListeners) { |
| 396 | listener.onDragEnd(); |
| 397 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 401 | void onDeferredEndFling(DropTarget.DragObject d) { |
| 402 | d.dragSource.onFlingToDeleteCompleted(); |
| 403 | } |
| 404 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 405 | /** |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 406 | * Clamps the position to the drag layer bounds. |
| 407 | */ |
| 408 | private int[] getClampedDragLayerPos(float x, float y) { |
| 409 | mLauncher.getDragLayer().getLocalVisibleRect(mDragLayerRect); |
| 410 | mTmpPoint[0] = (int) Math.max(mDragLayerRect.left, Math.min(x, mDragLayerRect.right - 1)); |
| 411 | mTmpPoint[1] = (int) Math.max(mDragLayerRect.top, Math.min(y, mDragLayerRect.bottom - 1)); |
| 412 | return mTmpPoint; |
| 413 | } |
| 414 | |
Winson Chung | a241375 | 2012-04-03 14:22:34 -0700 | [diff] [blame] | 415 | long getLastGestureUpTime() { |
| 416 | if (mDragging) { |
| 417 | return System.currentTimeMillis(); |
| 418 | } else { |
| 419 | return mLastTouchUpTime; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | void resetLastGestureUpTime() { |
| 424 | mLastTouchUpTime = -1; |
| 425 | } |
| 426 | |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 427 | /** |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 428 | * Call this from a drag source view. |
| 429 | */ |
| 430 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 431 | @SuppressWarnings("all") // suppress dead code warning |
| 432 | final boolean debug = false; |
| 433 | if (debug) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 434 | Log.d(Launcher.TAG, "DragController.onInterceptTouchEvent " + ev + " mDragging=" |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 435 | + mDragging); |
| 436 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 437 | |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 438 | if (mIsAccessibleDrag) { |
| 439 | return false; |
| 440 | } |
| 441 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 442 | // Update the velocity tracker |
| 443 | acquireVelocityTrackerAndAddMovement(ev); |
| 444 | |
| 445 | final int action = ev.getAction(); |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 446 | final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY()); |
| 447 | final int dragLayerX = dragLayerPos[0]; |
| 448 | final int dragLayerY = dragLayerPos[1]; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 449 | |
| 450 | switch (action) { |
| 451 | case MotionEvent.ACTION_MOVE: |
| 452 | break; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 453 | case MotionEvent.ACTION_DOWN: |
| 454 | // Remember location of down touch |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 455 | mMotionDownX = dragLayerX; |
| 456 | mMotionDownY = dragLayerY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 457 | mLastDropTarget = null; |
| 458 | break; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 459 | case MotionEvent.ACTION_UP: |
Winson Chung | a241375 | 2012-04-03 14:22:34 -0700 | [diff] [blame] | 460 | mLastTouchUpTime = System.currentTimeMillis(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 461 | if (mDragging) { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 462 | PointF vec = isFlingingToDelete(mDragObject.dragSource); |
Adam Cohen | 7c4c510 | 2013-06-14 17:42:35 -0700 | [diff] [blame] | 463 | if (!DeleteDropTarget.willAcceptDrop(mDragObject.dragInfo)) { |
| 464 | vec = null; |
| 465 | } |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 466 | if (vec != null) { |
| 467 | dropOnFlingToDeleteTarget(dragLayerX, dragLayerY, vec); |
| 468 | } else { |
| 469 | drop(dragLayerX, dragLayerY); |
| 470 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 471 | } |
| 472 | endDrag(); |
| 473 | break; |
Winson Chung | 621e640 | 2011-01-04 16:03:57 -0800 | [diff] [blame] | 474 | case MotionEvent.ACTION_CANCEL: |
| 475 | cancelDrag(); |
| 476 | break; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | return mDragging; |
| 480 | } |
| 481 | |
| 482 | /** |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 483 | * Sets the view that should handle move events. |
| 484 | */ |
| 485 | void setMoveTarget(View view) { |
| 486 | mMoveTarget = view; |
| 487 | } |
| 488 | |
| 489 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 490 | return mMoveTarget != null && mMoveTarget.dispatchUnhandledMove(focused, direction); |
| 491 | } |
| 492 | |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 493 | private void clearScrollRunnable() { |
| 494 | mHandler.removeCallbacks(mScrollRunnable); |
| 495 | if (mScrollState == SCROLL_WAITING_IN_ZONE) { |
| 496 | mScrollState = SCROLL_OUTSIDE_ZONE; |
| 497 | mScrollRunnable.setDirection(SCROLL_RIGHT); |
| 498 | mDragScroller.onExitScrollArea(); |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 499 | mLauncher.getDragLayer().onExitScrollArea(); |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 503 | private void handleMoveEvent(int x, int y) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 504 | mDragObject.dragView.move(x, y); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 505 | |
| 506 | // Drop on someone? |
| 507 | final int[] coordinates = mCoordinatesTemp; |
| 508 | DropTarget dropTarget = findDropTarget(x, y, coordinates); |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 509 | mDragObject.x = coordinates[0]; |
| 510 | mDragObject.y = coordinates[1]; |
Winson Chung | 25460a1 | 2013-04-01 18:21:28 -0700 | [diff] [blame] | 511 | checkTouchMove(dropTarget); |
| 512 | |
| 513 | // Check if we are hovering over the scroll areas |
| 514 | mDistanceSinceScroll += |
| 515 | Math.sqrt(Math.pow(mLastTouch[0] - x, 2) + Math.pow(mLastTouch[1] - y, 2)); |
| 516 | mLastTouch[0] = x; |
| 517 | mLastTouch[1] = y; |
| 518 | checkScrollState(x, y); |
| 519 | } |
| 520 | |
| 521 | public void forceTouchMove() { |
| 522 | int[] dummyCoordinates = mCoordinatesTemp; |
| 523 | DropTarget dropTarget = findDropTarget(mLastTouch[0], mLastTouch[1], dummyCoordinates); |
Adam Cohen | 4ef75f5 | 2013-09-29 16:54:01 -0700 | [diff] [blame] | 524 | mDragObject.x = dummyCoordinates[0]; |
| 525 | mDragObject.y = dummyCoordinates[1]; |
Winson Chung | 25460a1 | 2013-04-01 18:21:28 -0700 | [diff] [blame] | 526 | checkTouchMove(dropTarget); |
| 527 | } |
| 528 | |
| 529 | private void checkTouchMove(DropTarget dropTarget) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 530 | if (dropTarget != null) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 531 | if (mLastDropTarget != dropTarget) { |
| 532 | if (mLastDropTarget != null) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 533 | mLastDropTarget.onDragExit(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 534 | } |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 535 | dropTarget.onDragEnter(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 536 | } |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 537 | dropTarget.onDragOver(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 538 | } else { |
| 539 | if (mLastDropTarget != null) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 540 | mLastDropTarget.onDragExit(mDragObject); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | mLastDropTarget = dropTarget; |
Winson Chung | 25460a1 | 2013-04-01 18:21:28 -0700 | [diff] [blame] | 544 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 545 | |
Winson Chung | 25460a1 | 2013-04-01 18:21:28 -0700 | [diff] [blame] | 546 | private void checkScrollState(int x, int y) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 547 | final int slop = ViewConfiguration.get(mLauncher).getScaledWindowTouchSlop(); |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 548 | final int delay = mDistanceSinceScroll < slop ? RESCROLL_DELAY : SCROLL_DELAY; |
Winson Chung | fe1fe26 | 2013-04-01 16:52:31 -0700 | [diff] [blame] | 549 | final DragLayer dragLayer = mLauncher.getDragLayer(); |
| 550 | final boolean isRtl = (dragLayer.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL); |
| 551 | final int forwardDirection = isRtl ? SCROLL_RIGHT : SCROLL_LEFT; |
| 552 | final int backwardsDirection = isRtl ? SCROLL_LEFT : SCROLL_RIGHT; |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 553 | |
Winson Chung | 3f4e142 | 2011-11-17 14:58:51 -0800 | [diff] [blame] | 554 | if (x < mScrollZone) { |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 555 | if (mScrollState == SCROLL_OUTSIDE_ZONE) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 556 | mScrollState = SCROLL_WAITING_IN_ZONE; |
Winson Chung | fe1fe26 | 2013-04-01 16:52:31 -0700 | [diff] [blame] | 557 | if (mDragScroller.onEnterScrollArea(x, y, forwardDirection)) { |
| 558 | dragLayer.onEnterScrollArea(forwardDirection); |
| 559 | mScrollRunnable.setDirection(forwardDirection); |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 560 | mHandler.postDelayed(mScrollRunnable, delay); |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 561 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 562 | } |
Winson Chung | 3f4e142 | 2011-11-17 14:58:51 -0800 | [diff] [blame] | 563 | } else if (x > mScrollView.getWidth() - mScrollZone) { |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 564 | if (mScrollState == SCROLL_OUTSIDE_ZONE) { |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 565 | mScrollState = SCROLL_WAITING_IN_ZONE; |
Winson Chung | fe1fe26 | 2013-04-01 16:52:31 -0700 | [diff] [blame] | 566 | if (mDragScroller.onEnterScrollArea(x, y, backwardsDirection)) { |
| 567 | dragLayer.onEnterScrollArea(backwardsDirection); |
| 568 | mScrollRunnable.setDirection(backwardsDirection); |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 569 | mHandler.postDelayed(mScrollRunnable, delay); |
Winson Chung | 3e0839e | 2011-10-03 15:15:18 -0700 | [diff] [blame] | 570 | } |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 571 | } |
| 572 | } else { |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 573 | clearScrollRunnable(); |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 577 | /** |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 578 | * Call this from a drag source view. |
| 579 | */ |
| 580 | public boolean onTouchEvent(MotionEvent ev) { |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 581 | if (!mDragging || mIsAccessibleDrag) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 582 | return false; |
| 583 | } |
| 584 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 585 | // Update the velocity tracker |
| 586 | acquireVelocityTrackerAndAddMovement(ev); |
| 587 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 588 | final int action = ev.getAction(); |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 589 | final int[] dragLayerPos = getClampedDragLayerPos(ev.getX(), ev.getY()); |
| 590 | final int dragLayerX = dragLayerPos[0]; |
| 591 | final int dragLayerY = dragLayerPos[1]; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 592 | |
| 593 | switch (action) { |
| 594 | case MotionEvent.ACTION_DOWN: |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 595 | // Remember where the motion event started |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 596 | mMotionDownX = dragLayerX; |
| 597 | mMotionDownY = dragLayerY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 598 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 599 | if ((dragLayerX < mScrollZone) || (dragLayerX > mScrollView.getWidth() - mScrollZone)) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 600 | mScrollState = SCROLL_WAITING_IN_ZONE; |
| 601 | mHandler.postDelayed(mScrollRunnable, SCROLL_DELAY); |
| 602 | } else { |
| 603 | mScrollState = SCROLL_OUTSIDE_ZONE; |
| 604 | } |
Mindy DelliCarpini | 53b8d07 | 2013-07-03 08:23:13 -0700 | [diff] [blame] | 605 | handleMoveEvent(dragLayerX, dragLayerY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 606 | break; |
| 607 | case MotionEvent.ACTION_MOVE: |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 608 | handleMoveEvent(dragLayerX, dragLayerY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 609 | break; |
| 610 | case MotionEvent.ACTION_UP: |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 611 | // 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] | 612 | handleMoveEvent(dragLayerX, dragLayerY); |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 613 | mHandler.removeCallbacks(mScrollRunnable); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 614 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 615 | if (mDragging) { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 616 | PointF vec = isFlingingToDelete(mDragObject.dragSource); |
Adam Cohen | 7c4c510 | 2013-06-14 17:42:35 -0700 | [diff] [blame] | 617 | if (!DeleteDropTarget.willAcceptDrop(mDragObject.dragInfo)) { |
Adam Cohen | 947dc54 | 2013-06-06 22:43:33 -0700 | [diff] [blame] | 618 | vec = null; |
| 619 | } |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 620 | if (vec != null) { |
| 621 | dropOnFlingToDeleteTarget(dragLayerX, dragLayerY, vec); |
| 622 | } else { |
| 623 | drop(dragLayerX, dragLayerY); |
| 624 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 625 | } |
| 626 | endDrag(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 627 | break; |
| 628 | case MotionEvent.ACTION_CANCEL: |
Winson Chung | 3bc21c3 | 2012-01-20 13:59:18 -0800 | [diff] [blame] | 629 | mHandler.removeCallbacks(mScrollRunnable); |
Joe Onorato | 24b6fd8 | 2009-11-12 13:47:09 -0800 | [diff] [blame] | 630 | cancelDrag(); |
Winson Chung | 621e640 | 2011-01-04 16:03:57 -0800 | [diff] [blame] | 631 | break; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | return true; |
| 635 | } |
| 636 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 637 | /** |
Adam Cohen | c9735cf | 2015-01-23 16:11:55 -0800 | [diff] [blame^] | 638 | * Since accessible drag and drop won't cause the same sequence of touch events, we manually |
| 639 | * inject the appropriate state. |
| 640 | */ |
| 641 | public void prepareAccessibleDrag(int x, int y) { |
| 642 | mMotionDownX = x; |
| 643 | mMotionDownY = y; |
| 644 | mLastDropTarget = null; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * As above, since accessible drag and drop won't cause the same sequence of touch events, |
| 649 | * we manually ensure appropriate drag and drop events get emulated for accessible drag. |
| 650 | */ |
| 651 | public void completeAccessibleDrag(int[] location) { |
| 652 | final int[] coordinates = mCoordinatesTemp; |
| 653 | |
| 654 | // We make sure that we prime the target for drop. |
| 655 | DropTarget dropTarget = findDropTarget(location[0], location[1], coordinates); |
| 656 | mDragObject.x = coordinates[0]; |
| 657 | mDragObject.y = coordinates[1]; |
| 658 | checkTouchMove(dropTarget); |
| 659 | |
| 660 | // Perform the drop |
| 661 | drop(location[0], location[1]); |
| 662 | endDrag(); |
| 663 | } |
| 664 | |
| 665 | /** |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 666 | * Determines whether the user flung the current item to delete it. |
| 667 | * |
| 668 | * @return the vector at which the item was flung, or null if no fling was detected. |
| 669 | */ |
| 670 | private PointF isFlingingToDelete(DragSource source) { |
| 671 | if (mFlingToDeleteDropTarget == null) return null; |
| 672 | if (!source.supportsFlingToDelete()) return null; |
| 673 | |
| 674 | ViewConfiguration config = ViewConfiguration.get(mLauncher); |
| 675 | mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity()); |
| 676 | |
| 677 | if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) { |
| 678 | // Do a quick dot product test to ensure that we are flinging upwards |
| 679 | PointF vel = new PointF(mVelocityTracker.getXVelocity(), |
| 680 | mVelocityTracker.getYVelocity()); |
| 681 | PointF upVec = new PointF(0f, -1f); |
| 682 | float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) / |
| 683 | (vel.length() * upVec.length())); |
| 684 | if (theta <= Math.toRadians(MAX_FLING_DEGREES)) { |
| 685 | return vel; |
| 686 | } |
| 687 | } |
| 688 | return null; |
| 689 | } |
| 690 | |
| 691 | private void dropOnFlingToDeleteTarget(float x, float y, PointF vel) { |
| 692 | final int[] coordinates = mCoordinatesTemp; |
| 693 | |
| 694 | mDragObject.x = coordinates[0]; |
| 695 | mDragObject.y = coordinates[1]; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 696 | |
| 697 | // Clean up dragging on the target if it's not the current fling delete target otherwise, |
| 698 | // start dragging to it. |
| 699 | if (mLastDropTarget != null && mFlingToDeleteDropTarget != mLastDropTarget) { |
| 700 | mLastDropTarget.onDragExit(mDragObject); |
| 701 | } |
| 702 | |
| 703 | // Drop onto the fling-to-delete target |
| 704 | boolean accepted = false; |
| 705 | mFlingToDeleteDropTarget.onDragEnter(mDragObject); |
Winson Chung | 232decb | 2012-03-28 15:09:05 -0700 | [diff] [blame] | 706 | // We must set dragComplete to true _only_ after we "enter" the fling-to-delete target for |
| 707 | // "drop" |
| 708 | mDragObject.dragComplete = true; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 709 | mFlingToDeleteDropTarget.onDragExit(mDragObject); |
| 710 | if (mFlingToDeleteDropTarget.acceptDrop(mDragObject)) { |
| 711 | mFlingToDeleteDropTarget.onFlingToDelete(mDragObject, mDragObject.x, mDragObject.y, |
| 712 | vel); |
| 713 | accepted = true; |
| 714 | } |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 715 | mDragObject.dragSource.onDropCompleted((View) mFlingToDeleteDropTarget, mDragObject, true, |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 716 | accepted); |
| 717 | } |
| 718 | |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 719 | private void drop(float x, float y) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 720 | final int[] coordinates = mCoordinatesTemp; |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 721 | final DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 722 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 723 | mDragObject.x = coordinates[0]; |
| 724 | mDragObject.y = coordinates[1]; |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 725 | boolean accepted = false; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 726 | if (dropTarget != null) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 727 | mDragObject.dragComplete = true; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 728 | dropTarget.onDragExit(mDragObject); |
| 729 | if (dropTarget.acceptDrop(mDragObject)) { |
| 730 | dropTarget.onDrop(mDragObject); |
Patrick Dubroy | b0a6bbe | 2011-03-02 18:40:21 -0800 | [diff] [blame] | 731 | accepted = true; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 732 | } |
| 733 | } |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 734 | mDragObject.dragSource.onDropCompleted((View) dropTarget, mDragObject, false, accepted); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) { |
| 738 | final Rect r = mRectTemp; |
| 739 | |
| 740 | final ArrayList<DropTarget> dropTargets = mDropTargets; |
| 741 | final int count = dropTargets.size(); |
| 742 | for (int i=count-1; i>=0; i--) { |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 743 | DropTarget target = dropTargets.get(i); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 744 | if (!target.isDropEnabled()) |
| 745 | continue; |
| 746 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 747 | target.getHitRectRelativeToDragLayer(r); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 748 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 749 | mDragObject.x = x; |
| 750 | mDragObject.y = y; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 751 | if (r.contains(x, y)) { |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 752 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 753 | dropCoordinates[0] = x; |
| 754 | dropCoordinates[1] = y; |
| 755 | mLauncher.getDragLayer().mapCoordInSelfToDescendent((View) target, dropCoordinates); |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 756 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 757 | return target; |
| 758 | } |
| 759 | } |
| 760 | return null; |
| 761 | } |
| 762 | |
| 763 | public void setDragScoller(DragScroller scroller) { |
| 764 | mDragScroller = scroller; |
| 765 | } |
| 766 | |
| 767 | public void setWindowToken(IBinder token) { |
| 768 | mWindowToken = token; |
| 769 | } |
| 770 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 771 | /** |
| 772 | * Sets the drag listner which will be notified when a drag starts or ends. |
| 773 | */ |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 774 | public void addDragListener(DragListener l) { |
| 775 | mListeners.add(l); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 776 | } |
| 777 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 778 | /** |
| 779 | * Remove a previously installed drag listener. |
| 780 | */ |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 781 | public void removeDragListener(DragListener l) { |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 782 | mListeners.remove(l); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | /** |
| 786 | * Add a DropTarget to the list of potential places to receive drop events. |
| 787 | */ |
| 788 | public void addDropTarget(DropTarget target) { |
| 789 | mDropTargets.add(target); |
| 790 | } |
| 791 | |
| 792 | /** |
| 793 | * Don't send drop events to <em>target</em> any more. |
| 794 | */ |
| 795 | public void removeDropTarget(DropTarget target) { |
| 796 | mDropTargets.remove(target); |
| 797 | } |
| 798 | |
| 799 | /** |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 800 | * Sets the current fling-to-delete drop target. |
| 801 | */ |
| 802 | public void setFlingToDeleteDropTarget(DropTarget target) { |
| 803 | mFlingToDeleteDropTarget = target; |
| 804 | } |
| 805 | |
| 806 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 807 | if (mVelocityTracker == null) { |
| 808 | mVelocityTracker = VelocityTracker.obtain(); |
| 809 | } |
| 810 | mVelocityTracker.addMovement(ev); |
| 811 | } |
| 812 | |
| 813 | private void releaseVelocityTracker() { |
| 814 | if (mVelocityTracker != null) { |
| 815 | mVelocityTracker.recycle(); |
| 816 | mVelocityTracker = null; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | /** |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 821 | * Set which view scrolls for touch events near the edge of the screen. |
| 822 | */ |
| 823 | public void setScrollView(View v) { |
| 824 | mScrollView = v; |
| 825 | } |
| 826 | |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 827 | DragView getDragView() { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 828 | return mDragObject.dragView; |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 829 | } |
| 830 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 831 | private class ScrollRunnable implements Runnable { |
| 832 | private int mDirection; |
| 833 | |
| 834 | ScrollRunnable() { |
| 835 | } |
| 836 | |
| 837 | public void run() { |
| 838 | if (mDragScroller != null) { |
| 839 | if (mDirection == SCROLL_LEFT) { |
| 840 | mDragScroller.scrollLeft(); |
| 841 | } else { |
| 842 | mDragScroller.scrollRight(); |
| 843 | } |
| 844 | mScrollState = SCROLL_OUTSIDE_ZONE; |
Patrick Dubroy | a16fd5a | 2010-10-07 16:47:28 -0700 | [diff] [blame] | 845 | mDistanceSinceScroll = 0; |
| 846 | mDragScroller.onExitScrollArea(); |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 847 | mLauncher.getDragLayer().onExitScrollArea(); |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 848 | |
| 849 | if (isDragging()) { |
Winson Chung | 25460a1 | 2013-04-01 18:21:28 -0700 | [diff] [blame] | 850 | // Check the scroll again so that we can requeue the scroller if necessary |
| 851 | checkScrollState(mLastTouch[0], mLastTouch[1]); |
Winson Chung | aa15ffe | 2012-01-18 15:45:28 -0800 | [diff] [blame] | 852 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
| 856 | void setDirection(int direction) { |
| 857 | mDirection = direction; |
| 858 | } |
| 859 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 860 | } |