Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 18 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 19 | import android.animation.TimeInterpolator; |
| 20 | import android.animation.ValueAnimator; |
| 21 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 23 | import android.graphics.PointF; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 24 | import android.graphics.Rect; |
Michael Jurka | 4346746 | 2013-11-14 12:03:58 +0100 | [diff] [blame] | 25 | import android.os.AsyncTask; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 26 | import android.util.AttributeSet; |
| 27 | import android.view.View; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 28 | import android.view.ViewConfiguration; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 29 | import android.view.animation.AnimationUtils; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 30 | import android.view.animation.DecelerateInterpolator; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 31 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 32 | import com.android.launcher3.R; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 33 | import com.android.launcher3.util.Thunk; |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 34 | import com.android.launcher3.widget.WidgetsContainerView; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 35 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 36 | public class DeleteDropTarget extends ButtonDropTarget { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 37 | |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 38 | private static int FLING_DELETE_ANIMATION_DURATION = 350; |
| 39 | private static float FLING_TO_DELETE_FRICTION = 0.035f; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 40 | private static int MODE_FLING_DELETE_TO_TRASH = 0; |
| 41 | private static int MODE_FLING_DELETE_ALONG_VECTOR = 1; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 42 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 43 | private final int mFlingDeleteMode = MODE_FLING_DELETE_ALONG_VECTOR; |
| 44 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 45 | public DeleteDropTarget(Context context, AttributeSet attrs) { |
| 46 | this(context, attrs, 0); |
| 47 | } |
| 48 | |
| 49 | public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 50 | super(context, attrs, defStyle); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected void onFinishInflate() { |
| 55 | super.onFinishInflate(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 56 | // Get the hover color |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 57 | mHoverColor = getResources().getColor(R.color.delete_target_hover_tint); |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 58 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 59 | setDrawable(R.drawable.remove_target_selector); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 62 | public static boolean willAcceptDrop(DragSource source, Object info) { |
| 63 | return (info instanceof ItemInfo) && source.supportsDeleteDropTarget(); |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 66 | @Override |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 67 | protected boolean supportsDrop(DragSource source, Object info) { |
| 68 | return willAcceptDrop(source, info); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 72 | @Thunk void completeDrop(DragObject d) { |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 73 | ItemInfo item = (ItemInfo) d.dragInfo; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 74 | if ((d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder)) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 75 | removeWorkspaceOrFolderItem(mLauncher, item, null); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 76 | } |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Removes the item from the workspace. If the view is not null, it also removes the view. |
| 81 | * @return true if the item was removed. |
| 82 | */ |
| 83 | public static boolean removeWorkspaceOrFolderItem(Launcher launcher, ItemInfo item, View view) { |
| 84 | if (item instanceof ShortcutInfo) { |
| 85 | LauncherModel.deleteItemFromDatabase(launcher, item); |
| 86 | } else if (item instanceof FolderInfo) { |
| 87 | FolderInfo folder = (FolderInfo) item; |
| 88 | launcher.removeFolder(folder); |
| 89 | LauncherModel.deleteFolderContentsFromDatabase(launcher, folder); |
| 90 | } else if (item instanceof LauncherAppWidgetInfo) { |
| 91 | final LauncherAppWidgetInfo widget = (LauncherAppWidgetInfo) item; |
| 92 | |
| 93 | // Remove the widget from the workspace |
| 94 | launcher.removeAppWidget(widget); |
| 95 | LauncherModel.deleteItemFromDatabase(launcher, widget); |
| 96 | |
| 97 | final LauncherAppWidgetHost appWidgetHost = launcher.getAppWidgetHost(); |
| 98 | |
| 99 | if (appWidgetHost != null && !widget.isCustomWidget() |
| 100 | && widget.isWidgetIdValid()) { |
| 101 | // Deleting an app widget ID is a void call but writes to disk before returning |
| 102 | // to the caller... |
| 103 | new AsyncTask<Void, Void, Void>() { |
| 104 | public Void doInBackground(Void ... args) { |
| 105 | appWidgetHost.deleteAppWidgetId(widget.appWidgetId); |
| 106 | return null; |
| 107 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 108 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 109 | } |
| 110 | } else { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | if (view != null) { |
| 115 | launcher.getWorkspace().removeWorkspaceItem(view); |
| 116 | launcher.getWorkspace().stripEmptyScreens(); |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 121 | /** |
| 122 | * Creates an animation from the current drag view to the delete trash icon. |
| 123 | */ |
| 124 | private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer, |
| 125 | DragObject d, PointF vel, ViewConfiguration config) { |
Adam Cohen | fe9da81 | 2014-08-04 17:08:01 -0700 | [diff] [blame] | 126 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 127 | int width = mDrawable.getIntrinsicWidth(); |
| 128 | int height = mDrawable.getIntrinsicHeight(); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 129 | final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(), |
Adam Cohen | fe9da81 | 2014-08-04 17:08:01 -0700 | [diff] [blame] | 130 | width, height); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 131 | final Rect from = new Rect(); |
| 132 | dragLayer.getViewRectRelativeToSelf(d.dragView, from); |
| 133 | |
| 134 | // Calculate how far along the velocity vector we should put the intermediate point on |
| 135 | // the bezier curve |
| 136 | float velocity = Math.abs(vel.length()); |
| 137 | float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f)); |
| 138 | int offsetY = (int) (-from.top * vp); |
| 139 | int offsetX = (int) (offsetY / (vel.y / vel.x)); |
| 140 | final float y2 = from.top + offsetY; // intermediate t/l |
| 141 | final float x2 = from.left + offsetX; |
| 142 | final float x1 = from.left; // drag view t/l |
| 143 | final float y1 = from.top; |
| 144 | final float x3 = to.left; // delete target t/l |
| 145 | final float y3 = to.top; |
| 146 | |
| 147 | final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() { |
| 148 | @Override |
| 149 | public float getInterpolation(float t) { |
| 150 | return t * t * t * t * t * t * t * t; |
| 151 | } |
| 152 | }; |
| 153 | return new AnimatorUpdateListener() { |
| 154 | @Override |
| 155 | public void onAnimationUpdate(ValueAnimator animation) { |
| 156 | final DragView dragView = (DragView) dragLayer.getAnimatedView(); |
| 157 | float t = ((Float) animation.getAnimatedValue()).floatValue(); |
| 158 | float tp = scaleAlphaInterpolator.getInterpolation(t); |
| 159 | float initialScale = dragView.getInitialScale(); |
| 160 | float finalAlpha = 0.5f; |
| 161 | float scale = dragView.getScaleX(); |
| 162 | float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f; |
| 163 | float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f; |
| 164 | float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) + |
| 165 | (t * t) * x3; |
| 166 | float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) + |
| 167 | (t * t) * y3; |
| 168 | |
| 169 | dragView.setTranslationX(x); |
| 170 | dragView.setTranslationY(y); |
| 171 | dragView.setScaleX(initialScale * (1f - tp)); |
| 172 | dragView.setScaleY(initialScale * (1f - tp)); |
| 173 | dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp)); |
| 174 | } |
| 175 | }; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Creates an animation from the current drag view along its current velocity vector. |
| 180 | * For this animation, the alpha runs for a fixed duration and we update the position |
| 181 | * progressively. |
| 182 | */ |
| 183 | private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 184 | private DragLayer mDragLayer; |
| 185 | private PointF mVelocity; |
| 186 | private Rect mFrom; |
| 187 | private long mPrevTime; |
| 188 | private boolean mHasOffsetForScale; |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 189 | private float mFriction; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 190 | |
Winson Chung | 9658b1e | 2012-04-09 18:30:07 -0700 | [diff] [blame] | 191 | private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 192 | |
| 193 | public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from, |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 194 | long startTime, float friction) { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 195 | mDragLayer = dragLayer; |
| 196 | mVelocity = vel; |
| 197 | mFrom = from; |
| 198 | mPrevTime = startTime; |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 199 | mFriction = 1f - (dragLayer.getResources().getDisplayMetrics().density * friction); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void onAnimationUpdate(ValueAnimator animation) { |
| 204 | final DragView dragView = (DragView) mDragLayer.getAnimatedView(); |
| 205 | float t = ((Float) animation.getAnimatedValue()).floatValue(); |
| 206 | long curTime = AnimationUtils.currentAnimationTimeMillis(); |
| 207 | |
| 208 | if (!mHasOffsetForScale) { |
| 209 | mHasOffsetForScale = true; |
| 210 | float scale = dragView.getScaleX(); |
| 211 | float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f; |
| 212 | float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f; |
| 213 | |
| 214 | mFrom.left += xOffset; |
| 215 | mFrom.top += yOffset; |
| 216 | } |
| 217 | |
| 218 | mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f); |
| 219 | mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f); |
| 220 | |
| 221 | dragView.setTranslationX(mFrom.left); |
| 222 | dragView.setTranslationY(mFrom.top); |
| 223 | dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t)); |
| 224 | |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 225 | mVelocity.x *= mFriction; |
| 226 | mVelocity.y *= mFriction; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 227 | mPrevTime = curTime; |
| 228 | } |
| 229 | }; |
| 230 | private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer, |
| 231 | DragObject d, PointF vel, final long startTime, final int duration, |
| 232 | ViewConfiguration config) { |
| 233 | final Rect from = new Rect(); |
| 234 | dragLayer.getViewRectRelativeToSelf(d.dragView, from); |
| 235 | |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 236 | return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime, |
| 237 | FLING_TO_DELETE_FRICTION); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) { |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 241 | final boolean isWidgets = d.dragSource instanceof WidgetsContainerView; |
| 242 | final boolean isAllapps = d.dragSource instanceof AppsContainerView; |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 243 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 244 | // Don't highlight the icon as it's animating |
| 245 | d.dragView.setColor(0); |
| 246 | d.dragView.updateInitialScaleToCurrentScale(); |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 247 | // Don't highlight the target if we are flinging from AllApps |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 248 | if (isWidgets || isAllapps) { |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 249 | resetHoverColor(); |
| 250 | } |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 251 | |
| 252 | if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) { |
| 253 | // Defer animating out the drop target if we are animating to it |
| 254 | mSearchDropTargetBar.deferOnDragEnd(); |
| 255 | mSearchDropTargetBar.finishAnimations(); |
| 256 | } |
| 257 | |
| 258 | final ViewConfiguration config = ViewConfiguration.get(mLauncher); |
| 259 | final DragLayer dragLayer = mLauncher.getDragLayer(); |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 260 | final int duration = FLING_DELETE_ANIMATION_DURATION; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 261 | final long startTime = AnimationUtils.currentAnimationTimeMillis(); |
| 262 | |
| 263 | // NOTE: Because it takes time for the first frame of animation to actually be |
| 264 | // called and we expect the animation to be a continuation of the fling, we have |
| 265 | // to account for the time that has elapsed since the fling finished. And since |
| 266 | // we don't have a startDelay, we will always get call to update when we call |
| 267 | // start() (which we want to ignore). |
| 268 | final TimeInterpolator tInterpolator = new TimeInterpolator() { |
| 269 | private int mCount = -1; |
| 270 | private float mOffset = 0f; |
| 271 | |
| 272 | @Override |
| 273 | public float getInterpolation(float t) { |
| 274 | if (mCount < 0) { |
| 275 | mCount++; |
| 276 | } else if (mCount == 0) { |
| 277 | mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() - |
| 278 | startTime) / duration); |
| 279 | mCount++; |
| 280 | } |
| 281 | return Math.min(1f, mOffset + t); |
| 282 | } |
| 283 | }; |
| 284 | AnimatorUpdateListener updateCb = null; |
| 285 | if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) { |
| 286 | updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config); |
| 287 | } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) { |
| 288 | updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime, |
| 289 | duration, config); |
| 290 | } |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 291 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 292 | Runnable onAnimationEndRunnable = new Runnable() { |
| 293 | @Override |
| 294 | public void run() { |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 295 | // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up |
| 296 | // itself, otherwise, complete the drop to initiate the deletion process |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 297 | if (!isWidgets || !isAllapps) { |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 298 | mLauncher.exitSpringLoadedDragMode(); |
| 299 | completeDrop(d); |
| 300 | } |
| 301 | mLauncher.getDragController().onDeferredEndFling(d); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 302 | } |
| 303 | }; |
| 304 | dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable, |
| 305 | DragLayer.ANIMATION_END_DISAPPEAR, null); |
| 306 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 307 | } |