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