blob: 763ec3faa661513312e2618ff94073a80cb50a71 [file] [log] [blame]
Winson Chung4c98d922011-05-31 16:50:48 -07001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung4c98d922011-05-31 16:50:48 -070018
Winson Chung043f2af2012-03-01 16:09:54 -080019import android.animation.TimeInterpolator;
20import android.animation.ValueAnimator;
21import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka1e2f4652013-07-08 18:03:46 -070022import android.content.ComponentName;
Winson Chung4c98d922011-05-31 16:50:48 -070023import android.content.Context;
Michael Jurka1e2f4652013-07-08 18:03:46 -070024import android.content.pm.PackageManager;
25import android.content.pm.PackageManager.NameNotFoundException;
26import android.content.pm.ResolveInfo;
Winson Chunga62e9fd2011-07-11 15:20:48 -070027import android.content.res.ColorStateList;
Winson Chung201bc822011-06-20 15:41:53 -070028import android.content.res.Configuration;
Winson Chung4c98d922011-05-31 16:50:48 -070029import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080030import android.graphics.PointF;
Adam Cohend4d7aa52011-07-19 21:47:37 -070031import android.graphics.Rect;
Winson Chung967289b2011-06-30 18:09:30 -070032import android.graphics.drawable.TransitionDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070033import android.util.AttributeSet;
Michael Jurka1e2f4652013-07-08 18:03:46 -070034import android.util.Log;
Winson Chung4c98d922011-05-31 16:50:48 -070035import android.view.View;
Winson Chung043f2af2012-03-01 16:09:54 -080036import android.view.ViewConfiguration;
Winson Chunga6427b12011-07-27 10:53:39 -070037import android.view.ViewGroup;
Winson Chung043f2af2012-03-01 16:09:54 -080038import android.view.animation.AnimationUtils;
Adam Cohend4d7aa52011-07-19 21:47:37 -070039import android.view.animation.DecelerateInterpolator;
Winson Chung61967cb2012-02-28 18:11:33 -080040import android.view.animation.LinearInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070041
Michael Jurka1e2f4652013-07-08 18:03:46 -070042import java.util.List;
43
Winson Chung61fa4192011-06-12 15:15:29 -070044public class DeleteDropTarget extends ButtonDropTarget {
Winson Chung043f2af2012-03-01 16:09:54 -080045 private static int DELETE_ANIMATION_DURATION = 285;
Winson Chung6e1bdaf2012-05-29 17:03:45 -070046 private static int FLING_DELETE_ANIMATION_DURATION = 350;
47 private static float FLING_TO_DELETE_FRICTION = 0.035f;
Winson Chung043f2af2012-03-01 16:09:54 -080048 private static int MODE_FLING_DELETE_TO_TRASH = 0;
49 private static int MODE_FLING_DELETE_ALONG_VECTOR = 1;
Winson Chung4c98d922011-05-31 16:50:48 -070050
Winson Chung043f2af2012-03-01 16:09:54 -080051 private final int mFlingDeleteMode = MODE_FLING_DELETE_ALONG_VECTOR;
52
Winson Chunga62e9fd2011-07-11 15:20:48 -070053 private ColorStateList mOriginalTextColor;
Adam Cohenebea84d2011-11-09 17:20:41 -080054 private TransitionDrawable mUninstallDrawable;
55 private TransitionDrawable mRemoveDrawable;
56 private TransitionDrawable mCurrentDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070057
58 public DeleteDropTarget(Context context, AttributeSet attrs) {
59 this(context, attrs, 0);
60 }
61
62 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
63 super(context, attrs, defStyle);
64 }
65
66 @Override
67 protected void onFinishInflate() {
68 super.onFinishInflate();
69
70 // Get the drawable
Winson Chunga6427b12011-07-27 10:53:39 -070071 mOriginalTextColor = getTextColors();
Winson Chung4c98d922011-05-31 16:50:48 -070072
73 // Get the hover color
74 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070075 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080076 mUninstallDrawable = (TransitionDrawable)
77 r.getDrawable(R.drawable.uninstall_target_selector);
78 mRemoveDrawable = (TransitionDrawable) r.getDrawable(R.drawable.remove_target_selector);
79
80 mRemoveDrawable.setCrossFadeEnabled(true);
81 mUninstallDrawable.setCrossFadeEnabled(true);
82
83 // The current drawable is set to either the remove drawable or the uninstall drawable
84 // and is initially set to the remove drawable, as set in the layout xml.
Winson Chung947245b2012-05-15 16:34:19 -070085 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Winson Chung201bc822011-06-20 15:41:53 -070086
87 // Remove the text in the Phone UI in landscape
88 int orientation = getResources().getConfiguration().orientation;
89 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
Daniel Sandlere4f98912013-06-25 15:13:26 -040090 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070091 setText("");
Winson Chung201bc822011-06-20 15:41:53 -070092 }
93 }
Winson Chung4c98d922011-05-31 16:50:48 -070094 }
95
96 private boolean isAllAppsApplication(DragSource source, Object info) {
97 return (source instanceof AppsCustomizePagedView) && (info instanceof ApplicationInfo);
98 }
99 private boolean isAllAppsWidget(DragSource source, Object info) {
Winson Chung11a49372012-04-27 15:12:38 -0700100 if (source instanceof AppsCustomizePagedView) {
101 if (info instanceof PendingAddItemInfo) {
102 PendingAddItemInfo addInfo = (PendingAddItemInfo) info;
103 switch (addInfo.itemType) {
104 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
105 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
106 return true;
107 }
108 }
109 }
110 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700111 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700112 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
113 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -0700114 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700115 private boolean isWorkspaceOrFolderApplication(DragObject d) {
116 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
117 }
118 private boolean isWorkspaceOrFolderWidget(DragObject d) {
119 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700120 }
121 private boolean isWorkspaceFolder(DragObject d) {
122 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
123 }
124
Winson Chunga48487a2012-03-20 16:19:37 -0700125 private void setHoverColor() {
126 mCurrentDrawable.startTransition(mTransitionDuration);
127 setTextColor(mHoverColor);
128 }
129 private void resetHoverColor() {
130 mCurrentDrawable.resetTransition();
131 setTextColor(mOriginalTextColor);
132 }
133
Winson Chung4c98d922011-05-31 16:50:48 -0700134 @Override
135 public boolean acceptDrop(DragObject d) {
Adam Cohen7c4c5102013-06-14 17:42:35 -0700136 return willAcceptDrop(d.dragInfo);
137 }
138
139 public static boolean willAcceptDrop(Object info) {
140 if (info instanceof ItemInfo) {
141 ItemInfo item = (ItemInfo) info;
142 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET ||
143 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
144 return true;
145 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700146 if (AppsCustomizePagedView.DISABLE_ALL_APPS &&
147 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
148 item instanceof ShortcutInfo) {
149 ShortcutInfo shortcutInfo = (ShortcutInfo) info;
150 return (shortcutInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0;
151 }
Adam Cohen947dc542013-06-06 22:43:33 -0700152 }
Adam Cohen7c4c5102013-06-14 17:42:35 -0700153 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700154 }
155
156 @Override
157 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700158 boolean isVisible = true;
159 boolean isUninstall = false;
160
Winson Chungf0ea4d32011-06-06 14:27:16 -0700161 // If we are dragging a widget from AppsCustomize, hide the delete target
Winson Chung4c98d922011-05-31 16:50:48 -0700162 if (isAllAppsWidget(source, info)) {
163 isVisible = false;
164 }
165
166 // If we are dragging an application from AppsCustomize, only show the control if we can
167 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
Adam Cohen7c4c5102013-06-14 17:42:35 -0700168 if (willAcceptDrop(info)) {
Adam Cohen947dc542013-06-06 22:43:33 -0700169 isVisible = true;
170 } else {
171 isVisible = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700172 }
173
Adam Cohenebea84d2011-11-09 17:20:41 -0800174 if (isUninstall) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800175 setCompoundDrawablesRelativeWithIntrinsicBounds(mUninstallDrawable, null, null, null);
Adam Cohenebea84d2011-11-09 17:20:41 -0800176 } else {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800177 setCompoundDrawablesRelativeWithIntrinsicBounds(mRemoveDrawable, null, null, null);
Adam Cohenebea84d2011-11-09 17:20:41 -0800178 }
Winson Chung947245b2012-05-15 16:34:19 -0700179 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Adam Cohenebea84d2011-11-09 17:20:41 -0800180
Winson Chung4c98d922011-05-31 16:50:48 -0700181 mActive = isVisible;
Winson Chunga48487a2012-03-20 16:19:37 -0700182 resetHoverColor();
Winson Chunga6427b12011-07-27 10:53:39 -0700183 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
184 if (getText().length() > 0) {
185 setText(isUninstall ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700186 : R.string.delete_target_label);
187 }
188 }
189
190 @Override
191 public void onDragEnd() {
192 super.onDragEnd();
193 mActive = false;
194 }
195
196 public void onDragEnter(DragObject d) {
197 super.onDragEnter(d);
198
Winson Chunga48487a2012-03-20 16:19:37 -0700199 setHoverColor();
Winson Chung4c98d922011-05-31 16:50:48 -0700200 }
201
202 public void onDragExit(DragObject d) {
203 super.onDragExit(d);
204
Winson Chungaaa530a2011-07-11 21:06:30 -0700205 if (!d.dragComplete) {
Winson Chunga48487a2012-03-20 16:19:37 -0700206 resetHoverColor();
Winson Chung61967cb2012-02-28 18:11:33 -0800207 } else {
208 // Restore the hover color if we are deleting
209 d.dragView.setColor(mHoverColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700210 }
Winson Chung4c98d922011-05-31 16:50:48 -0700211 }
212
Adam Cohened66b2b2012-01-23 17:28:51 -0800213 private void animateToTrashAndCompleteDrop(final DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700214 final DragLayer dragLayer = mLauncher.getDragLayer();
215 final Rect from = new Rect();
Adam Cohened66b2b2012-01-23 17:28:51 -0800216 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Michael Jurka1e2f4652013-07-08 18:03:46 -0700217 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Winson Chung61967cb2012-02-28 18:11:33 -0800218 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
Michael Jurka1e2f4652013-07-08 18:03:46 -0700219 final float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800220
Adam Cohend4d7aa52011-07-19 21:47:37 -0700221 mSearchDropTargetBar.deferOnDragEnd();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700222 deferCompleteDropIfUninstalling(d);
223
Adam Cohend4d7aa52011-07-19 21:47:37 -0700224 Runnable onAnimationEndRunnable = new Runnable() {
225 @Override
226 public void run() {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700227 completeDrop(d);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700228 mSearchDropTargetBar.onDragEnd();
229 mLauncher.exitSpringLoadedDragMode();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700230 }
231 };
Winson Chung61967cb2012-02-28 18:11:33 -0800232 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700233 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800234 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800235 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700236 }
237
Michael Jurka1e2f4652013-07-08 18:03:46 -0700238 private void deferCompleteDropIfUninstalling(DragObject d) {
239 mWaitingForUninstall = false;
240 if (isUninstall(d)) {
241 if (d.dragSource instanceof Folder) {
242 ((Folder) d.dragSource).deferCompleteDropAfterUninstallActivity();
243 } else if (d.dragSource instanceof Workspace) {
244 ((Workspace) d.dragSource).deferCompleteDropAfterUninstallActivity();
245 }
246 mWaitingForUninstall = true;
247 }
248 }
Winson Chung4c98d922011-05-31 16:50:48 -0700249
Michael Jurka1e2f4652013-07-08 18:03:46 -0700250 private boolean isUninstall(DragObject d) {
251 return AppsCustomizePagedView.DISABLE_ALL_APPS && isWorkspaceOrFolderApplication(d);
252 }
253
254 private boolean mWaitingForUninstall = false;
255 private void completeDrop(final DragObject d) {
256 ItemInfo item = (ItemInfo) d.dragInfo;
257 boolean wasWaitingForUninstall = mWaitingForUninstall;
258 mWaitingForUninstall = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700259 if (isAllAppsApplication(d.dragSource, item)) {
260 // Uninstall the application if it is being dragged from AppsCustomize
Michael Jurka1e2f4652013-07-08 18:03:46 -0700261 ApplicationInfo appInfo = (ApplicationInfo) item;
262 mLauncher.startApplicationUninstallActivity(appInfo.componentName, appInfo.flags);
263 } else if (AppsCustomizePagedView.DISABLE_ALL_APPS && isWorkspaceOrFolderApplication(d)) {
264 ShortcutInfo shortcut = (ShortcutInfo) item;
265 if (shortcut.intent != null && shortcut.intent.getComponent() != null) {
266 ComponentName componentName = shortcut.intent.getComponent();
267 int flags = ApplicationInfo.initFlags(
268 ShortcutInfo.getPackageInfo(getContext(), componentName.getPackageName()));
269 mWaitingForUninstall =
270 mLauncher.startApplicationUninstallActivity(componentName, flags);
271 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700272 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700273 LauncherModel.deleteItemFromDatabase(mLauncher, item);
274 } else if (isWorkspaceFolder(d)) {
275 // Remove the folder from the workspace and delete the contents from launcher model
276 FolderInfo folderInfo = (FolderInfo) item;
277 mLauncher.removeFolder(folderInfo);
278 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700279 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700280 // Remove the widget from the workspace
281 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
282 LauncherModel.deleteItemFromDatabase(mLauncher, item);
283
284 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
285 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
286 if (appWidgetHost != null) {
287 // Deleting an app widget ID is a void call but writes to disk before returning
288 // to the caller...
289 new Thread("deleteAppWidgetId") {
290 public void run() {
291 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
292 }
293 }.start();
294 }
295 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700296 if (wasWaitingForUninstall && !mWaitingForUninstall) {
297 if (d.dragSource instanceof Folder) {
298 ((Folder) d.dragSource).onUninstallActivityReturned(false);
299 } else if (d.dragSource instanceof Workspace) {
300 ((Workspace) d.dragSource).onUninstallActivityReturned(false);
301 }
302 }
303 if (mWaitingForUninstall) {
304 final Runnable checkIfUninstallWasSuccess = new Runnable() {
305 @Override
306 public void run() {
307 mWaitingForUninstall = false;
308 ShortcutInfo shortcut = (ShortcutInfo) d.dragInfo;
309 if (shortcut.intent != null && shortcut.intent.getComponent() != null) {
310 String packageName = shortcut.intent.getComponent().getPackageName();
311 List<ResolveInfo> activities =
312 AllAppsList.findActivitiesForPackage(getContext(), packageName);
313 boolean uninstallSuccessful = activities.size() == 0;
314 mLauncher.removeOnResumeCallback(this);
315 if (d.dragSource instanceof Folder) {
316 ((Folder) d.dragSource).
317 onUninstallActivityReturned(uninstallSuccessful);
318 } else if (d.dragSource instanceof Workspace) {
319 ((Workspace) d.dragSource).
320 onUninstallActivityReturned(uninstallSuccessful);
321 }
322 }
323 }
324 };
325 mLauncher.addOnResumeCallback(checkIfUninstallWasSuccess);
326 }
Winson Chung4c98d922011-05-31 16:50:48 -0700327 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700328
329 public void onDrop(DragObject d) {
330 animateToTrashAndCompleteDrop(d);
331 }
Winson Chung043f2af2012-03-01 16:09:54 -0800332
333 /**
334 * Creates an animation from the current drag view to the delete trash icon.
335 */
336 private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer,
337 DragObject d, PointF vel, ViewConfiguration config) {
338 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
339 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
340 final Rect from = new Rect();
341 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
342
343 // Calculate how far along the velocity vector we should put the intermediate point on
344 // the bezier curve
345 float velocity = Math.abs(vel.length());
346 float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
347 int offsetY = (int) (-from.top * vp);
348 int offsetX = (int) (offsetY / (vel.y / vel.x));
349 final float y2 = from.top + offsetY; // intermediate t/l
350 final float x2 = from.left + offsetX;
351 final float x1 = from.left; // drag view t/l
352 final float y1 = from.top;
353 final float x3 = to.left; // delete target t/l
354 final float y3 = to.top;
355
356 final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {
357 @Override
358 public float getInterpolation(float t) {
359 return t * t * t * t * t * t * t * t;
360 }
361 };
362 return new AnimatorUpdateListener() {
363 @Override
364 public void onAnimationUpdate(ValueAnimator animation) {
365 final DragView dragView = (DragView) dragLayer.getAnimatedView();
366 float t = ((Float) animation.getAnimatedValue()).floatValue();
367 float tp = scaleAlphaInterpolator.getInterpolation(t);
368 float initialScale = dragView.getInitialScale();
369 float finalAlpha = 0.5f;
370 float scale = dragView.getScaleX();
371 float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
372 float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
373 float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) +
374 (t * t) * x3;
375 float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) +
376 (t * t) * y3;
377
378 dragView.setTranslationX(x);
379 dragView.setTranslationY(y);
380 dragView.setScaleX(initialScale * (1f - tp));
381 dragView.setScaleY(initialScale * (1f - tp));
382 dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
383 }
384 };
385 }
386
387 /**
388 * Creates an animation from the current drag view along its current velocity vector.
389 * For this animation, the alpha runs for a fixed duration and we update the position
390 * progressively.
391 */
392 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
Winson Chung043f2af2012-03-01 16:09:54 -0800393 private DragLayer mDragLayer;
394 private PointF mVelocity;
395 private Rect mFrom;
396 private long mPrevTime;
397 private boolean mHasOffsetForScale;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700398 private float mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800399
Winson Chung9658b1e2012-04-09 18:30:07 -0700400 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
Winson Chung043f2af2012-03-01 16:09:54 -0800401
402 public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from,
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700403 long startTime, float friction) {
Winson Chung043f2af2012-03-01 16:09:54 -0800404 mDragLayer = dragLayer;
405 mVelocity = vel;
406 mFrom = from;
407 mPrevTime = startTime;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700408 mFriction = 1f - (dragLayer.getResources().getDisplayMetrics().density * friction);
Winson Chung043f2af2012-03-01 16:09:54 -0800409 }
410
411 @Override
412 public void onAnimationUpdate(ValueAnimator animation) {
413 final DragView dragView = (DragView) mDragLayer.getAnimatedView();
414 float t = ((Float) animation.getAnimatedValue()).floatValue();
415 long curTime = AnimationUtils.currentAnimationTimeMillis();
416
417 if (!mHasOffsetForScale) {
418 mHasOffsetForScale = true;
419 float scale = dragView.getScaleX();
420 float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f;
421 float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f;
422
423 mFrom.left += xOffset;
424 mFrom.top += yOffset;
425 }
426
427 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
428 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
429
430 dragView.setTranslationX(mFrom.left);
431 dragView.setTranslationY(mFrom.top);
432 dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
433
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700434 mVelocity.x *= mFriction;
435 mVelocity.y *= mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800436 mPrevTime = curTime;
437 }
438 };
439 private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer,
440 DragObject d, PointF vel, final long startTime, final int duration,
441 ViewConfiguration config) {
442 final Rect from = new Rect();
443 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
444
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700445 return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime,
446 FLING_TO_DELETE_FRICTION);
Winson Chung043f2af2012-03-01 16:09:54 -0800447 }
448
449 public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
Winson Chunga48487a2012-03-20 16:19:37 -0700450 final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView;
451
Winson Chung043f2af2012-03-01 16:09:54 -0800452 // Don't highlight the icon as it's animating
453 d.dragView.setColor(0);
454 d.dragView.updateInitialScaleToCurrentScale();
Winson Chunga48487a2012-03-20 16:19:37 -0700455 // Don't highlight the target if we are flinging from AllApps
456 if (isAllApps) {
457 resetHoverColor();
458 }
Winson Chung043f2af2012-03-01 16:09:54 -0800459
460 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
461 // Defer animating out the drop target if we are animating to it
462 mSearchDropTargetBar.deferOnDragEnd();
463 mSearchDropTargetBar.finishAnimations();
464 }
465
466 final ViewConfiguration config = ViewConfiguration.get(mLauncher);
467 final DragLayer dragLayer = mLauncher.getDragLayer();
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700468 final int duration = FLING_DELETE_ANIMATION_DURATION;
Winson Chung043f2af2012-03-01 16:09:54 -0800469 final long startTime = AnimationUtils.currentAnimationTimeMillis();
470
471 // NOTE: Because it takes time for the first frame of animation to actually be
472 // called and we expect the animation to be a continuation of the fling, we have
473 // to account for the time that has elapsed since the fling finished. And since
474 // we don't have a startDelay, we will always get call to update when we call
475 // start() (which we want to ignore).
476 final TimeInterpolator tInterpolator = new TimeInterpolator() {
477 private int mCount = -1;
478 private float mOffset = 0f;
479
480 @Override
481 public float getInterpolation(float t) {
482 if (mCount < 0) {
483 mCount++;
484 } else if (mCount == 0) {
485 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
486 startTime) / duration);
487 mCount++;
488 }
489 return Math.min(1f, mOffset + t);
490 }
491 };
492 AnimatorUpdateListener updateCb = null;
493 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
494 updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
495 } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
496 updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime,
497 duration, config);
498 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700499 deferCompleteDropIfUninstalling(d);
500
Winson Chung043f2af2012-03-01 16:09:54 -0800501 Runnable onAnimationEndRunnable = new Runnable() {
502 @Override
503 public void run() {
504 mSearchDropTargetBar.onDragEnd();
Winson Chunga48487a2012-03-20 16:19:37 -0700505
506 // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up
507 // itself, otherwise, complete the drop to initiate the deletion process
508 if (!isAllApps) {
509 mLauncher.exitSpringLoadedDragMode();
510 completeDrop(d);
511 }
512 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800513 }
514 };
515 dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable,
516 DragLayer.ANIMATION_END_DISAPPEAR, null);
517 }
Winson Chung4c98d922011-05-31 16:50:48 -0700518}