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