blob: 01edb9eb675b4641dadb8a1c78962207448fc58c [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
Michael Jurka24715c72013-07-08 18:03:46 -070058 private boolean mWaitingForUninstall = false;
59
Winson Chung4c98d922011-05-31 16:50:48 -070060 public DeleteDropTarget(Context context, AttributeSet attrs) {
61 this(context, attrs, 0);
62 }
63
64 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
65 super(context, attrs, defStyle);
66 }
67
68 @Override
69 protected void onFinishInflate() {
70 super.onFinishInflate();
71
72 // Get the drawable
Winson Chunga6427b12011-07-27 10:53:39 -070073 mOriginalTextColor = getTextColors();
Winson Chung4c98d922011-05-31 16:50:48 -070074
75 // Get the hover color
76 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070077 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080078 mUninstallDrawable = (TransitionDrawable)
79 r.getDrawable(R.drawable.uninstall_target_selector);
80 mRemoveDrawable = (TransitionDrawable) r.getDrawable(R.drawable.remove_target_selector);
81
82 mRemoveDrawable.setCrossFadeEnabled(true);
83 mUninstallDrawable.setCrossFadeEnabled(true);
84
85 // The current drawable is set to either the remove drawable or the uninstall drawable
86 // and is initially set to the remove drawable, as set in the layout xml.
Winson Chung947245b2012-05-15 16:34:19 -070087 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Winson Chung201bc822011-06-20 15:41:53 -070088
89 // Remove the text in the Phone UI in landscape
90 int orientation = getResources().getConfiguration().orientation;
91 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
Daniel Sandlere4f98912013-06-25 15:13:26 -040092 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070093 setText("");
Winson Chung201bc822011-06-20 15:41:53 -070094 }
95 }
Winson Chung4c98d922011-05-31 16:50:48 -070096 }
97
98 private boolean isAllAppsApplication(DragSource source, Object info) {
99 return (source instanceof AppsCustomizePagedView) && (info instanceof ApplicationInfo);
100 }
101 private boolean isAllAppsWidget(DragSource source, Object info) {
Winson Chung11a49372012-04-27 15:12:38 -0700102 if (source instanceof AppsCustomizePagedView) {
103 if (info instanceof PendingAddItemInfo) {
104 PendingAddItemInfo addInfo = (PendingAddItemInfo) info;
105 switch (addInfo.itemType) {
106 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
107 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
108 return true;
109 }
110 }
111 }
112 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700113 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700114 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
115 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -0700116 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700117 private boolean isWorkspaceOrFolderApplication(DragObject d) {
118 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
119 }
120 private boolean isWorkspaceOrFolderWidget(DragObject d) {
121 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700122 }
123 private boolean isWorkspaceFolder(DragObject d) {
124 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
125 }
126
Winson Chunga48487a2012-03-20 16:19:37 -0700127 private void setHoverColor() {
128 mCurrentDrawable.startTransition(mTransitionDuration);
129 setTextColor(mHoverColor);
130 }
131 private void resetHoverColor() {
132 mCurrentDrawable.resetTransition();
133 setTextColor(mOriginalTextColor);
134 }
135
Winson Chung4c98d922011-05-31 16:50:48 -0700136 @Override
137 public boolean acceptDrop(DragObject d) {
Adam Cohen7c4c5102013-06-14 17:42:35 -0700138 return willAcceptDrop(d.dragInfo);
139 }
140
141 public static boolean willAcceptDrop(Object info) {
142 if (info instanceof ItemInfo) {
143 ItemInfo item = (ItemInfo) info;
144 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET ||
145 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
146 return true;
147 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700148 if (AppsCustomizePagedView.DISABLE_ALL_APPS &&
149 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
150 item instanceof ShortcutInfo) {
151 ShortcutInfo shortcutInfo = (ShortcutInfo) info;
152 return (shortcutInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0;
153 }
Adam Cohen947dc542013-06-06 22:43:33 -0700154 }
Adam Cohen7c4c5102013-06-14 17:42:35 -0700155 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700156 }
157
158 @Override
159 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700160 boolean isVisible = true;
161 boolean isUninstall = false;
162
Winson Chungf0ea4d32011-06-06 14:27:16 -0700163 // If we are dragging a widget from AppsCustomize, hide the delete target
Winson Chung4c98d922011-05-31 16:50:48 -0700164 if (isAllAppsWidget(source, info)) {
165 isVisible = false;
166 }
167
168 // If we are dragging an application from AppsCustomize, only show the control if we can
169 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
Adam Cohen7c4c5102013-06-14 17:42:35 -0700170 if (willAcceptDrop(info)) {
Adam Cohen947dc542013-06-06 22:43:33 -0700171 isVisible = true;
172 } else {
173 isVisible = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700174 }
175
Adam Cohenebea84d2011-11-09 17:20:41 -0800176 if (isUninstall) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800177 setCompoundDrawablesRelativeWithIntrinsicBounds(mUninstallDrawable, null, null, null);
Adam Cohenebea84d2011-11-09 17:20:41 -0800178 } else {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800179 setCompoundDrawablesRelativeWithIntrinsicBounds(mRemoveDrawable, null, null, null);
Adam Cohenebea84d2011-11-09 17:20:41 -0800180 }
Winson Chung947245b2012-05-15 16:34:19 -0700181 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Adam Cohenebea84d2011-11-09 17:20:41 -0800182
Winson Chung4c98d922011-05-31 16:50:48 -0700183 mActive = isVisible;
Winson Chunga48487a2012-03-20 16:19:37 -0700184 resetHoverColor();
Winson Chunga6427b12011-07-27 10:53:39 -0700185 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
186 if (getText().length() > 0) {
187 setText(isUninstall ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700188 : R.string.delete_target_label);
189 }
190 }
191
192 @Override
193 public void onDragEnd() {
194 super.onDragEnd();
195 mActive = false;
196 }
197
198 public void onDragEnter(DragObject d) {
199 super.onDragEnter(d);
200
Winson Chunga48487a2012-03-20 16:19:37 -0700201 setHoverColor();
Winson Chung4c98d922011-05-31 16:50:48 -0700202 }
203
204 public void onDragExit(DragObject d) {
205 super.onDragExit(d);
206
Winson Chungaaa530a2011-07-11 21:06:30 -0700207 if (!d.dragComplete) {
Winson Chunga48487a2012-03-20 16:19:37 -0700208 resetHoverColor();
Winson Chung61967cb2012-02-28 18:11:33 -0800209 } else {
210 // Restore the hover color if we are deleting
211 d.dragView.setColor(mHoverColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700212 }
Winson Chung4c98d922011-05-31 16:50:48 -0700213 }
214
Adam Cohened66b2b2012-01-23 17:28:51 -0800215 private void animateToTrashAndCompleteDrop(final DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700216 final DragLayer dragLayer = mLauncher.getDragLayer();
217 final Rect from = new Rect();
Adam Cohened66b2b2012-01-23 17:28:51 -0800218 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Michael Jurka1e2f4652013-07-08 18:03:46 -0700219 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Winson Chung61967cb2012-02-28 18:11:33 -0800220 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
Michael Jurka1e2f4652013-07-08 18:03:46 -0700221 final float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800222
Adam Cohend4d7aa52011-07-19 21:47:37 -0700223 mSearchDropTargetBar.deferOnDragEnd();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700224 deferCompleteDropIfUninstalling(d);
225
Adam Cohend4d7aa52011-07-19 21:47:37 -0700226 Runnable onAnimationEndRunnable = new Runnable() {
227 @Override
228 public void run() {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700229 completeDrop(d);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700230 mSearchDropTargetBar.onDragEnd();
231 mLauncher.exitSpringLoadedDragMode();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700232 }
233 };
Winson Chung61967cb2012-02-28 18:11:33 -0800234 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700235 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800236 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800237 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700238 }
239
Michael Jurka1e2f4652013-07-08 18:03:46 -0700240 private void deferCompleteDropIfUninstalling(DragObject d) {
241 mWaitingForUninstall = false;
242 if (isUninstall(d)) {
243 if (d.dragSource instanceof Folder) {
244 ((Folder) d.dragSource).deferCompleteDropAfterUninstallActivity();
245 } else if (d.dragSource instanceof Workspace) {
246 ((Workspace) d.dragSource).deferCompleteDropAfterUninstallActivity();
247 }
248 mWaitingForUninstall = true;
249 }
250 }
Winson Chung4c98d922011-05-31 16:50:48 -0700251
Michael Jurka1e2f4652013-07-08 18:03:46 -0700252 private boolean isUninstall(DragObject d) {
253 return AppsCustomizePagedView.DISABLE_ALL_APPS && isWorkspaceOrFolderApplication(d);
254 }
255
Michael Jurkaf3007582013-08-21 14:33:57 +0200256 private void completeDrop(DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700257 ItemInfo item = (ItemInfo) d.dragInfo;
258 boolean wasWaitingForUninstall = mWaitingForUninstall;
259 mWaitingForUninstall = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700260 if (isAllAppsApplication(d.dragSource, item)) {
261 // Uninstall the application if it is being dragged from AppsCustomize
Michael Jurka1e2f4652013-07-08 18:03:46 -0700262 ApplicationInfo appInfo = (ApplicationInfo) item;
263 mLauncher.startApplicationUninstallActivity(appInfo.componentName, appInfo.flags);
264 } else if (AppsCustomizePagedView.DISABLE_ALL_APPS && isWorkspaceOrFolderApplication(d)) {
265 ShortcutInfo shortcut = (ShortcutInfo) item;
266 if (shortcut.intent != null && shortcut.intent.getComponent() != null) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200267 final ComponentName componentName = shortcut.intent.getComponent();
268 final DragSource dragSource = d.dragSource;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700269 int flags = ApplicationInfo.initFlags(
270 ShortcutInfo.getPackageInfo(getContext(), componentName.getPackageName()));
271 mWaitingForUninstall =
272 mLauncher.startApplicationUninstallActivity(componentName, flags);
Michael Jurkaf3007582013-08-21 14:33:57 +0200273 if (mWaitingForUninstall) {
274 final Runnable checkIfUninstallWasSuccess = new Runnable() {
275 @Override
276 public void run() {
277 mWaitingForUninstall = false;
278 String packageName = componentName.getPackageName();
279 List<ResolveInfo> activities =
280 AllAppsList.findActivitiesForPackage(getContext(), packageName);
281 boolean uninstallSuccessful = activities.size() == 0;
282 if (dragSource instanceof Folder) {
283 ((Folder) dragSource).
284 onUninstallActivityReturned(uninstallSuccessful);
285 } else if (dragSource instanceof Workspace) {
286 ((Workspace) dragSource).
287 onUninstallActivityReturned(uninstallSuccessful);
288 }
289 }
290 };
291 mLauncher.addOnResumeCallback(checkIfUninstallWasSuccess);
292 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700293 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700294 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700295 LauncherModel.deleteItemFromDatabase(mLauncher, item);
296 } else if (isWorkspaceFolder(d)) {
297 // Remove the folder from the workspace and delete the contents from launcher model
298 FolderInfo folderInfo = (FolderInfo) item;
299 mLauncher.removeFolder(folderInfo);
300 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700301 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700302 // Remove the widget from the workspace
303 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
304 LauncherModel.deleteItemFromDatabase(mLauncher, item);
305
306 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
307 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
308 if (appWidgetHost != null) {
309 // Deleting an app widget ID is a void call but writes to disk before returning
310 // to the caller...
311 new Thread("deleteAppWidgetId") {
312 public void run() {
313 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
314 }
315 }.start();
316 }
317 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700318 if (wasWaitingForUninstall && !mWaitingForUninstall) {
319 if (d.dragSource instanceof Folder) {
320 ((Folder) d.dragSource).onUninstallActivityReturned(false);
321 } else if (d.dragSource instanceof Workspace) {
322 ((Workspace) d.dragSource).onUninstallActivityReturned(false);
323 }
324 }
Winson Chung4c98d922011-05-31 16:50:48 -0700325 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700326
327 public void onDrop(DragObject d) {
328 animateToTrashAndCompleteDrop(d);
329 }
Winson Chung043f2af2012-03-01 16:09:54 -0800330
331 /**
332 * Creates an animation from the current drag view to the delete trash icon.
333 */
334 private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer,
335 DragObject d, PointF vel, ViewConfiguration config) {
336 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
337 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
338 final Rect from = new Rect();
339 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
340
341 // Calculate how far along the velocity vector we should put the intermediate point on
342 // the bezier curve
343 float velocity = Math.abs(vel.length());
344 float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
345 int offsetY = (int) (-from.top * vp);
346 int offsetX = (int) (offsetY / (vel.y / vel.x));
347 final float y2 = from.top + offsetY; // intermediate t/l
348 final float x2 = from.left + offsetX;
349 final float x1 = from.left; // drag view t/l
350 final float y1 = from.top;
351 final float x3 = to.left; // delete target t/l
352 final float y3 = to.top;
353
354 final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {
355 @Override
356 public float getInterpolation(float t) {
357 return t * t * t * t * t * t * t * t;
358 }
359 };
360 return new AnimatorUpdateListener() {
361 @Override
362 public void onAnimationUpdate(ValueAnimator animation) {
363 final DragView dragView = (DragView) dragLayer.getAnimatedView();
364 float t = ((Float) animation.getAnimatedValue()).floatValue();
365 float tp = scaleAlphaInterpolator.getInterpolation(t);
366 float initialScale = dragView.getInitialScale();
367 float finalAlpha = 0.5f;
368 float scale = dragView.getScaleX();
369 float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
370 float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
371 float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) +
372 (t * t) * x3;
373 float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) +
374 (t * t) * y3;
375
376 dragView.setTranslationX(x);
377 dragView.setTranslationY(y);
378 dragView.setScaleX(initialScale * (1f - tp));
379 dragView.setScaleY(initialScale * (1f - tp));
380 dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
381 }
382 };
383 }
384
385 /**
386 * Creates an animation from the current drag view along its current velocity vector.
387 * For this animation, the alpha runs for a fixed duration and we update the position
388 * progressively.
389 */
390 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
Winson Chung043f2af2012-03-01 16:09:54 -0800391 private DragLayer mDragLayer;
392 private PointF mVelocity;
393 private Rect mFrom;
394 private long mPrevTime;
395 private boolean mHasOffsetForScale;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700396 private float mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800397
Winson Chung9658b1e2012-04-09 18:30:07 -0700398 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
Winson Chung043f2af2012-03-01 16:09:54 -0800399
400 public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from,
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700401 long startTime, float friction) {
Winson Chung043f2af2012-03-01 16:09:54 -0800402 mDragLayer = dragLayer;
403 mVelocity = vel;
404 mFrom = from;
405 mPrevTime = startTime;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700406 mFriction = 1f - (dragLayer.getResources().getDisplayMetrics().density * friction);
Winson Chung043f2af2012-03-01 16:09:54 -0800407 }
408
409 @Override
410 public void onAnimationUpdate(ValueAnimator animation) {
411 final DragView dragView = (DragView) mDragLayer.getAnimatedView();
412 float t = ((Float) animation.getAnimatedValue()).floatValue();
413 long curTime = AnimationUtils.currentAnimationTimeMillis();
414
415 if (!mHasOffsetForScale) {
416 mHasOffsetForScale = true;
417 float scale = dragView.getScaleX();
418 float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f;
419 float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f;
420
421 mFrom.left += xOffset;
422 mFrom.top += yOffset;
423 }
424
425 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
426 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
427
428 dragView.setTranslationX(mFrom.left);
429 dragView.setTranslationY(mFrom.top);
430 dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
431
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700432 mVelocity.x *= mFriction;
433 mVelocity.y *= mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800434 mPrevTime = curTime;
435 }
436 };
437 private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer,
438 DragObject d, PointF vel, final long startTime, final int duration,
439 ViewConfiguration config) {
440 final Rect from = new Rect();
441 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
442
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700443 return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime,
444 FLING_TO_DELETE_FRICTION);
Winson Chung043f2af2012-03-01 16:09:54 -0800445 }
446
447 public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
Winson Chunga48487a2012-03-20 16:19:37 -0700448 final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView;
449
Winson Chung043f2af2012-03-01 16:09:54 -0800450 // Don't highlight the icon as it's animating
451 d.dragView.setColor(0);
452 d.dragView.updateInitialScaleToCurrentScale();
Winson Chunga48487a2012-03-20 16:19:37 -0700453 // Don't highlight the target if we are flinging from AllApps
454 if (isAllApps) {
455 resetHoverColor();
456 }
Winson Chung043f2af2012-03-01 16:09:54 -0800457
458 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
459 // Defer animating out the drop target if we are animating to it
460 mSearchDropTargetBar.deferOnDragEnd();
461 mSearchDropTargetBar.finishAnimations();
462 }
463
464 final ViewConfiguration config = ViewConfiguration.get(mLauncher);
465 final DragLayer dragLayer = mLauncher.getDragLayer();
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700466 final int duration = FLING_DELETE_ANIMATION_DURATION;
Winson Chung043f2af2012-03-01 16:09:54 -0800467 final long startTime = AnimationUtils.currentAnimationTimeMillis();
468
469 // NOTE: Because it takes time for the first frame of animation to actually be
470 // called and we expect the animation to be a continuation of the fling, we have
471 // to account for the time that has elapsed since the fling finished. And since
472 // we don't have a startDelay, we will always get call to update when we call
473 // start() (which we want to ignore).
474 final TimeInterpolator tInterpolator = new TimeInterpolator() {
475 private int mCount = -1;
476 private float mOffset = 0f;
477
478 @Override
479 public float getInterpolation(float t) {
480 if (mCount < 0) {
481 mCount++;
482 } else if (mCount == 0) {
483 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
484 startTime) / duration);
485 mCount++;
486 }
487 return Math.min(1f, mOffset + t);
488 }
489 };
490 AnimatorUpdateListener updateCb = null;
491 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
492 updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
493 } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
494 updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime,
495 duration, config);
496 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700497 deferCompleteDropIfUninstalling(d);
498
Winson Chung043f2af2012-03-01 16:09:54 -0800499 Runnable onAnimationEndRunnable = new Runnable() {
500 @Override
501 public void run() {
502 mSearchDropTargetBar.onDragEnd();
Winson Chunga48487a2012-03-20 16:19:37 -0700503
504 // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up
505 // itself, otherwise, complete the drop to initiate the deletion process
506 if (!isAllApps) {
507 mLauncher.exitSpringLoadedDragMode();
508 completeDrop(d);
509 }
510 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800511 }
512 };
513 dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable,
514 DragLayer.ANIMATION_END_DISAPPEAR, null);
515 }
Winson Chung4c98d922011-05-31 16:50:48 -0700516}