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 | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 23 | import android.content.res.ColorStateList; |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 24 | import android.content.res.Configuration; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 25 | import android.content.res.Resources; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 26 | import android.graphics.PointF; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 27 | import android.graphics.Rect; |
Winson Chung | 967289b | 2011-06-30 18:09:30 -0700 | [diff] [blame] | 28 | import android.graphics.drawable.TransitionDrawable; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 29 | import android.util.AttributeSet; |
| 30 | import android.view.View; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 31 | import android.view.ViewConfiguration; |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 32 | import android.view.ViewGroup; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 33 | import android.view.animation.AnimationUtils; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 34 | import android.view.animation.DecelerateInterpolator; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 35 | import android.view.animation.LinearInterpolator; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 36 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 37 | import com.android.launcher3.R; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 38 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 39 | public class DeleteDropTarget extends ButtonDropTarget { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 40 | private static int DELETE_ANIMATION_DURATION = 285; |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 41 | private static int FLING_DELETE_ANIMATION_DURATION = 350; |
| 42 | private static float FLING_TO_DELETE_FRICTION = 0.035f; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 43 | private static int MODE_FLING_DELETE_TO_TRASH = 0; |
| 44 | private static int MODE_FLING_DELETE_ALONG_VECTOR = 1; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 45 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 46 | private final int mFlingDeleteMode = MODE_FLING_DELETE_ALONG_VECTOR; |
| 47 | |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 48 | private ColorStateList mOriginalTextColor; |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 49 | private TransitionDrawable mUninstallDrawable; |
| 50 | private TransitionDrawable mRemoveDrawable; |
| 51 | private TransitionDrawable mCurrentDrawable; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 52 | |
| 53 | public DeleteDropTarget(Context context, AttributeSet attrs) { |
| 54 | this(context, attrs, 0); |
| 55 | } |
| 56 | |
| 57 | public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 58 | super(context, attrs, defStyle); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | protected void onFinishInflate() { |
| 63 | super.onFinishInflate(); |
| 64 | |
| 65 | // Get the drawable |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 66 | mOriginalTextColor = getTextColors(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 67 | |
| 68 | // Get the hover color |
| 69 | Resources r = getResources(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 70 | mHoverColor = r.getColor(R.color.delete_target_hover_tint); |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 71 | mUninstallDrawable = (TransitionDrawable) |
| 72 | r.getDrawable(R.drawable.uninstall_target_selector); |
| 73 | mRemoveDrawable = (TransitionDrawable) r.getDrawable(R.drawable.remove_target_selector); |
| 74 | |
| 75 | mRemoveDrawable.setCrossFadeEnabled(true); |
| 76 | mUninstallDrawable.setCrossFadeEnabled(true); |
| 77 | |
| 78 | // The current drawable is set to either the remove drawable or the uninstall drawable |
| 79 | // and is initially set to the remove drawable, as set in the layout xml. |
Winson Chung | 947245b | 2012-05-15 16:34:19 -0700 | [diff] [blame] | 80 | mCurrentDrawable = (TransitionDrawable) getCurrentDrawable(); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 81 | |
| 82 | // Remove the text in the Phone UI in landscape |
| 83 | int orientation = getResources().getConfiguration().orientation; |
| 84 | if (orientation == Configuration.ORIENTATION_LANDSCAPE) { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame^] | 85 | if (!LauncherAppState.isScreenLarge()) { |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 86 | setText(""); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | private boolean isAllAppsApplication(DragSource source, Object info) { |
| 92 | return (source instanceof AppsCustomizePagedView) && (info instanceof ApplicationInfo); |
| 93 | } |
| 94 | private boolean isAllAppsWidget(DragSource source, Object info) { |
Winson Chung | 11a4937 | 2012-04-27 15:12:38 -0700 | [diff] [blame] | 95 | if (source instanceof AppsCustomizePagedView) { |
| 96 | if (info instanceof PendingAddItemInfo) { |
| 97 | PendingAddItemInfo addInfo = (PendingAddItemInfo) info; |
| 98 | switch (addInfo.itemType) { |
| 99 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
| 100 | case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: |
| 101 | return true; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | return false; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 106 | } |
Michael Jurka | 0b4870d | 2011-07-10 13:39:08 -0700 | [diff] [blame] | 107 | private boolean isDragSourceWorkspaceOrFolder(DragObject d) { |
| 108 | return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 109 | } |
Michael Jurka | 0b4870d | 2011-07-10 13:39:08 -0700 | [diff] [blame] | 110 | private boolean isWorkspaceOrFolderApplication(DragObject d) { |
| 111 | return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo); |
| 112 | } |
| 113 | private boolean isWorkspaceOrFolderWidget(DragObject d) { |
| 114 | return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 115 | } |
| 116 | private boolean isWorkspaceFolder(DragObject d) { |
| 117 | return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo); |
| 118 | } |
| 119 | |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 120 | private void setHoverColor() { |
| 121 | mCurrentDrawable.startTransition(mTransitionDuration); |
| 122 | setTextColor(mHoverColor); |
| 123 | } |
| 124 | private void resetHoverColor() { |
| 125 | mCurrentDrawable.resetTransition(); |
| 126 | setTextColor(mOriginalTextColor); |
| 127 | } |
| 128 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 129 | @Override |
| 130 | public boolean acceptDrop(DragObject d) { |
| 131 | // We can remove everything including App shortcuts, folders, widgets, etc. |
Adam Cohen | 947dc54 | 2013-06-06 22:43:33 -0700 | [diff] [blame] | 132 | if ((d.dragInfo instanceof LauncherAppWidgetInfo) || |
| 133 | (d.dragInfo instanceof PendingAddWidgetInfo)) { |
| 134 | return true; |
| 135 | } else { |
| 136 | return false; |
| 137 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void onDragStart(DragSource source, Object info, int dragAction) { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 142 | boolean isVisible = true; |
| 143 | boolean isUninstall = false; |
| 144 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 145 | // If we are dragging a widget from AppsCustomize, hide the delete target |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 146 | if (isAllAppsWidget(source, info)) { |
| 147 | isVisible = false; |
| 148 | } |
| 149 | |
| 150 | // If we are dragging an application from AppsCustomize, only show the control if we can |
| 151 | // delete the app (it was downloaded), and rename the string to "uninstall" in such a case |
Adam Cohen | 947dc54 | 2013-06-06 22:43:33 -0700 | [diff] [blame] | 152 | |
| 153 | if ((info instanceof LauncherAppWidgetInfo) || |
| 154 | (info instanceof PendingAddWidgetInfo)) { |
| 155 | isVisible = true; |
| 156 | } else { |
| 157 | isVisible = false; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 160 | if (isUninstall) { |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 161 | setCompoundDrawablesRelativeWithIntrinsicBounds(mUninstallDrawable, null, null, null); |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 162 | } else { |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 163 | setCompoundDrawablesRelativeWithIntrinsicBounds(mRemoveDrawable, null, null, null); |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 164 | } |
Winson Chung | 947245b | 2012-05-15 16:34:19 -0700 | [diff] [blame] | 165 | mCurrentDrawable = (TransitionDrawable) getCurrentDrawable(); |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 166 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 167 | mActive = isVisible; |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 168 | resetHoverColor(); |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 169 | ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE); |
| 170 | if (getText().length() > 0) { |
| 171 | setText(isUninstall ? R.string.delete_target_uninstall_label |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 172 | : R.string.delete_target_label); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void onDragEnd() { |
| 178 | super.onDragEnd(); |
| 179 | mActive = false; |
| 180 | } |
| 181 | |
| 182 | public void onDragEnter(DragObject d) { |
| 183 | super.onDragEnter(d); |
| 184 | |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 185 | setHoverColor(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | public void onDragExit(DragObject d) { |
| 189 | super.onDragExit(d); |
| 190 | |
Winson Chung | aaa530a | 2011-07-11 21:06:30 -0700 | [diff] [blame] | 191 | if (!d.dragComplete) { |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 192 | resetHoverColor(); |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 193 | } else { |
| 194 | // Restore the hover color if we are deleting |
| 195 | d.dragView.setColor(mHoverColor); |
Winson Chung | aaa530a | 2011-07-11 21:06:30 -0700 | [diff] [blame] | 196 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 199 | private void animateToTrashAndCompleteDrop(final DragObject d) { |
| 200 | DragLayer dragLayer = mLauncher.getDragLayer(); |
| 201 | Rect from = new Rect(); |
| 202 | dragLayer.getViewRectRelativeToSelf(d.dragView, from); |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 203 | Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(), |
| 204 | mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight()); |
| 205 | float scale = (float) to.width() / from.width(); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 206 | |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 207 | mSearchDropTargetBar.deferOnDragEnd(); |
| 208 | Runnable onAnimationEndRunnable = new Runnable() { |
| 209 | @Override |
| 210 | public void run() { |
| 211 | mSearchDropTargetBar.onDragEnd(); |
| 212 | mLauncher.exitSpringLoadedDragMode(); |
| 213 | completeDrop(d); |
| 214 | } |
| 215 | }; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 216 | dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f, |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 217 | DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2), |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 218 | new LinearInterpolator(), onAnimationEndRunnable, |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 219 | DragLayer.ANIMATION_END_DISAPPEAR, null); |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | private void completeDrop(DragObject d) { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 223 | ItemInfo item = (ItemInfo) d.dragInfo; |
| 224 | |
| 225 | if (isAllAppsApplication(d.dragSource, item)) { |
| 226 | // Uninstall the application if it is being dragged from AppsCustomize |
| 227 | mLauncher.startApplicationUninstallActivity((ApplicationInfo) item); |
Michael Jurka | 0b4870d | 2011-07-10 13:39:08 -0700 | [diff] [blame] | 228 | } else if (isWorkspaceOrFolderApplication(d)) { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 229 | LauncherModel.deleteItemFromDatabase(mLauncher, item); |
| 230 | } else if (isWorkspaceFolder(d)) { |
| 231 | // Remove the folder from the workspace and delete the contents from launcher model |
| 232 | FolderInfo folderInfo = (FolderInfo) item; |
| 233 | mLauncher.removeFolder(folderInfo); |
| 234 | LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo); |
Michael Jurka | 0b4870d | 2011-07-10 13:39:08 -0700 | [diff] [blame] | 235 | } else if (isWorkspaceOrFolderWidget(d)) { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 236 | // Remove the widget from the workspace |
| 237 | mLauncher.removeAppWidget((LauncherAppWidgetInfo) item); |
| 238 | LauncherModel.deleteItemFromDatabase(mLauncher, item); |
| 239 | |
| 240 | final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item; |
| 241 | final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost(); |
| 242 | if (appWidgetHost != null) { |
| 243 | // Deleting an app widget ID is a void call but writes to disk before returning |
| 244 | // to the caller... |
| 245 | new Thread("deleteAppWidgetId") { |
| 246 | public void run() { |
| 247 | appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId); |
| 248 | } |
| 249 | }.start(); |
| 250 | } |
| 251 | } |
| 252 | } |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 253 | |
| 254 | public void onDrop(DragObject d) { |
| 255 | animateToTrashAndCompleteDrop(d); |
| 256 | } |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 257 | |
| 258 | /** |
| 259 | * Creates an animation from the current drag view to the delete trash icon. |
| 260 | */ |
| 261 | private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer, |
| 262 | DragObject d, PointF vel, ViewConfiguration config) { |
| 263 | final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(), |
| 264 | mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight()); |
| 265 | final Rect from = new Rect(); |
| 266 | dragLayer.getViewRectRelativeToSelf(d.dragView, from); |
| 267 | |
| 268 | // Calculate how far along the velocity vector we should put the intermediate point on |
| 269 | // the bezier curve |
| 270 | float velocity = Math.abs(vel.length()); |
| 271 | float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f)); |
| 272 | int offsetY = (int) (-from.top * vp); |
| 273 | int offsetX = (int) (offsetY / (vel.y / vel.x)); |
| 274 | final float y2 = from.top + offsetY; // intermediate t/l |
| 275 | final float x2 = from.left + offsetX; |
| 276 | final float x1 = from.left; // drag view t/l |
| 277 | final float y1 = from.top; |
| 278 | final float x3 = to.left; // delete target t/l |
| 279 | final float y3 = to.top; |
| 280 | |
| 281 | final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() { |
| 282 | @Override |
| 283 | public float getInterpolation(float t) { |
| 284 | return t * t * t * t * t * t * t * t; |
| 285 | } |
| 286 | }; |
| 287 | return new AnimatorUpdateListener() { |
| 288 | @Override |
| 289 | public void onAnimationUpdate(ValueAnimator animation) { |
| 290 | final DragView dragView = (DragView) dragLayer.getAnimatedView(); |
| 291 | float t = ((Float) animation.getAnimatedValue()).floatValue(); |
| 292 | float tp = scaleAlphaInterpolator.getInterpolation(t); |
| 293 | float initialScale = dragView.getInitialScale(); |
| 294 | float finalAlpha = 0.5f; |
| 295 | float scale = dragView.getScaleX(); |
| 296 | float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f; |
| 297 | float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f; |
| 298 | float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) + |
| 299 | (t * t) * x3; |
| 300 | float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) + |
| 301 | (t * t) * y3; |
| 302 | |
| 303 | dragView.setTranslationX(x); |
| 304 | dragView.setTranslationY(y); |
| 305 | dragView.setScaleX(initialScale * (1f - tp)); |
| 306 | dragView.setScaleY(initialScale * (1f - tp)); |
| 307 | dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp)); |
| 308 | } |
| 309 | }; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Creates an animation from the current drag view along its current velocity vector. |
| 314 | * For this animation, the alpha runs for a fixed duration and we update the position |
| 315 | * progressively. |
| 316 | */ |
| 317 | private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 318 | private DragLayer mDragLayer; |
| 319 | private PointF mVelocity; |
| 320 | private Rect mFrom; |
| 321 | private long mPrevTime; |
| 322 | private boolean mHasOffsetForScale; |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 323 | private float mFriction; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 324 | |
Winson Chung | 9658b1e | 2012-04-09 18:30:07 -0700 | [diff] [blame] | 325 | private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 326 | |
| 327 | public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from, |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 328 | long startTime, float friction) { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 329 | mDragLayer = dragLayer; |
| 330 | mVelocity = vel; |
| 331 | mFrom = from; |
| 332 | mPrevTime = startTime; |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 333 | mFriction = 1f - (dragLayer.getResources().getDisplayMetrics().density * friction); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public void onAnimationUpdate(ValueAnimator animation) { |
| 338 | final DragView dragView = (DragView) mDragLayer.getAnimatedView(); |
| 339 | float t = ((Float) animation.getAnimatedValue()).floatValue(); |
| 340 | long curTime = AnimationUtils.currentAnimationTimeMillis(); |
| 341 | |
| 342 | if (!mHasOffsetForScale) { |
| 343 | mHasOffsetForScale = true; |
| 344 | float scale = dragView.getScaleX(); |
| 345 | float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f; |
| 346 | float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f; |
| 347 | |
| 348 | mFrom.left += xOffset; |
| 349 | mFrom.top += yOffset; |
| 350 | } |
| 351 | |
| 352 | mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f); |
| 353 | mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f); |
| 354 | |
| 355 | dragView.setTranslationX(mFrom.left); |
| 356 | dragView.setTranslationY(mFrom.top); |
| 357 | dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t)); |
| 358 | |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 359 | mVelocity.x *= mFriction; |
| 360 | mVelocity.y *= mFriction; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 361 | mPrevTime = curTime; |
| 362 | } |
| 363 | }; |
| 364 | private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer, |
| 365 | DragObject d, PointF vel, final long startTime, final int duration, |
| 366 | ViewConfiguration config) { |
| 367 | final Rect from = new Rect(); |
| 368 | dragLayer.getViewRectRelativeToSelf(d.dragView, from); |
| 369 | |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 370 | return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime, |
| 371 | FLING_TO_DELETE_FRICTION); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) { |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 375 | final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView; |
| 376 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 377 | // Don't highlight the icon as it's animating |
| 378 | d.dragView.setColor(0); |
| 379 | d.dragView.updateInitialScaleToCurrentScale(); |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 380 | // Don't highlight the target if we are flinging from AllApps |
| 381 | if (isAllApps) { |
| 382 | resetHoverColor(); |
| 383 | } |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 384 | |
| 385 | if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) { |
| 386 | // Defer animating out the drop target if we are animating to it |
| 387 | mSearchDropTargetBar.deferOnDragEnd(); |
| 388 | mSearchDropTargetBar.finishAnimations(); |
| 389 | } |
| 390 | |
| 391 | final ViewConfiguration config = ViewConfiguration.get(mLauncher); |
| 392 | final DragLayer dragLayer = mLauncher.getDragLayer(); |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 393 | final int duration = FLING_DELETE_ANIMATION_DURATION; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 394 | final long startTime = AnimationUtils.currentAnimationTimeMillis(); |
| 395 | |
| 396 | // NOTE: Because it takes time for the first frame of animation to actually be |
| 397 | // called and we expect the animation to be a continuation of the fling, we have |
| 398 | // to account for the time that has elapsed since the fling finished. And since |
| 399 | // we don't have a startDelay, we will always get call to update when we call |
| 400 | // start() (which we want to ignore). |
| 401 | final TimeInterpolator tInterpolator = new TimeInterpolator() { |
| 402 | private int mCount = -1; |
| 403 | private float mOffset = 0f; |
| 404 | |
| 405 | @Override |
| 406 | public float getInterpolation(float t) { |
| 407 | if (mCount < 0) { |
| 408 | mCount++; |
| 409 | } else if (mCount == 0) { |
| 410 | mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() - |
| 411 | startTime) / duration); |
| 412 | mCount++; |
| 413 | } |
| 414 | return Math.min(1f, mOffset + t); |
| 415 | } |
| 416 | }; |
| 417 | AnimatorUpdateListener updateCb = null; |
| 418 | if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) { |
| 419 | updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config); |
| 420 | } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) { |
| 421 | updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime, |
| 422 | duration, config); |
| 423 | } |
| 424 | Runnable onAnimationEndRunnable = new Runnable() { |
| 425 | @Override |
| 426 | public void run() { |
| 427 | mSearchDropTargetBar.onDragEnd(); |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 428 | |
| 429 | // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up |
| 430 | // itself, otherwise, complete the drop to initiate the deletion process |
| 431 | if (!isAllApps) { |
| 432 | mLauncher.exitSpringLoadedDragMode(); |
| 433 | completeDrop(d); |
| 434 | } |
| 435 | mLauncher.getDragController().onDeferredEndFling(d); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 436 | } |
| 437 | }; |
| 438 | dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable, |
| 439 | DragLayer.ANIMATION_END_DISAPPEAR, null); |
| 440 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 441 | } |