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