blob: a8ac0746c8cba56df494e1fd86595aab33de51e8 [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 Jurka5e7af5d2013-09-14 18:35:38 +020024import android.content.Intent;
Michael Jurka1e2f4652013-07-08 18:03:46 -070025import android.content.pm.ResolveInfo;
Winson Chunga62e9fd2011-07-11 15:20:48 -070026import android.content.res.ColorStateList;
Winson Chung201bc822011-06-20 15:41:53 -070027import android.content.res.Configuration;
Winson Chung4c98d922011-05-31 16:50:48 -070028import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080029import android.graphics.PointF;
Adam Cohend4d7aa52011-07-19 21:47:37 -070030import android.graphics.Rect;
Winson Chung967289b2011-06-30 18:09:30 -070031import android.graphics.drawable.TransitionDrawable;
Michael Jurka43467462013-11-14 12:03:58 +010032import android.os.AsyncTask;
Winson Chung4c98d922011-05-31 16:50:48 -070033import android.util.AttributeSet;
34import android.view.View;
Winson Chung043f2af2012-03-01 16:09:54 -080035import android.view.ViewConfiguration;
Winson Chunga6427b12011-07-27 10:53:39 -070036import android.view.ViewGroup;
Winson Chung043f2af2012-03-01 16:09:54 -080037import android.view.animation.AnimationUtils;
Adam Cohend4d7aa52011-07-19 21:47:37 -070038import android.view.animation.DecelerateInterpolator;
Winson Chung61967cb2012-02-28 18:11:33 -080039import android.view.animation.LinearInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070040
Michael Jurka1e2f4652013-07-08 18:03:46 -070041import java.util.List;
Michael Jurka5e7af5d2013-09-14 18:35:38 +020042import java.util.Set;
Michael Jurka1e2f4652013-07-08 18:03:46 -070043
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) {
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +000099 return source.supportsAppInfoDropTarget() && (info instanceof AppInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700100 }
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 Jurkaed3d4df2013-09-12 18:09:24 +0200148
149 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200150 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) {
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200151 return true;
152 }
153
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200154 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
155 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
156 item instanceof AppInfo) {
157 AppInfo appInfo = (AppInfo) info;
158 return (appInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
159 }
160
Michael Jurka9bd4d282013-09-05 19:21:31 +0200161 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
Michael Jurka1e2f4652013-07-08 18:03:46 -0700162 item instanceof ShortcutInfo) {
Michael Jurka9bd4d282013-09-05 19:21:31 +0200163 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
164 ShortcutInfo shortcutInfo = (ShortcutInfo) info;
165 return (shortcutInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
166 } else {
167 return true;
168 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700169 }
Adam Cohen947dc542013-06-06 22:43:33 -0700170 }
Adam Cohen7c4c5102013-06-14 17:42:35 -0700171 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700172 }
173
174 @Override
175 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700176 boolean isVisible = true;
Michael Jurkaaddcba62013-10-09 19:04:13 -0700177 boolean useUninstallLabel = !AppsCustomizePagedView.DISABLE_ALL_APPS &&
178 isAllAppsApplication(source, info);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000179 boolean useDeleteLabel = !useUninstallLabel && source.supportsDeleteDropTarget();
Winson Chung4c98d922011-05-31 16:50:48 -0700180
Winson Chung4c98d922011-05-31 16:50:48 -0700181 // If we are dragging an application from AppsCustomize, only show the control if we can
Winson Chunge01af632013-09-26 10:43:20 -0700182 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case.
183 // Hide the delete target if it is a widget from AppsCustomize.
184 if (!willAcceptDrop(info) || isAllAppsWidget(source, info)) {
Adam Cohen947dc542013-06-06 22:43:33 -0700185 isVisible = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700186 }
187
Michael Jurkaaddcba62013-10-09 19:04:13 -0700188 if (useUninstallLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800189 setCompoundDrawablesRelativeWithIntrinsicBounds(mUninstallDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000190 } else if (useDeleteLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800191 setCompoundDrawablesRelativeWithIntrinsicBounds(mRemoveDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000192 } else {
193 isVisible = false;
Adam Cohenebea84d2011-11-09 17:20:41 -0800194 }
Winson Chung947245b2012-05-15 16:34:19 -0700195 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Adam Cohenebea84d2011-11-09 17:20:41 -0800196
Winson Chung4c98d922011-05-31 16:50:48 -0700197 mActive = isVisible;
Winson Chunga48487a2012-03-20 16:19:37 -0700198 resetHoverColor();
Winson Chunga6427b12011-07-27 10:53:39 -0700199 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000200 if (isVisible && getText().length() > 0) {
Michael Jurkaaddcba62013-10-09 19:04:13 -0700201 setText(useUninstallLabel ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700202 : R.string.delete_target_label);
203 }
204 }
205
206 @Override
207 public void onDragEnd() {
208 super.onDragEnd();
209 mActive = false;
210 }
211
212 public void onDragEnter(DragObject d) {
213 super.onDragEnter(d);
214
Winson Chunga48487a2012-03-20 16:19:37 -0700215 setHoverColor();
Winson Chung4c98d922011-05-31 16:50:48 -0700216 }
217
218 public void onDragExit(DragObject d) {
219 super.onDragExit(d);
220
Winson Chungaaa530a2011-07-11 21:06:30 -0700221 if (!d.dragComplete) {
Winson Chunga48487a2012-03-20 16:19:37 -0700222 resetHoverColor();
Winson Chung61967cb2012-02-28 18:11:33 -0800223 } else {
224 // Restore the hover color if we are deleting
225 d.dragView.setColor(mHoverColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700226 }
Winson Chung4c98d922011-05-31 16:50:48 -0700227 }
228
Adam Cohened66b2b2012-01-23 17:28:51 -0800229 private void animateToTrashAndCompleteDrop(final DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700230 final DragLayer dragLayer = mLauncher.getDragLayer();
231 final Rect from = new Rect();
Adam Cohened66b2b2012-01-23 17:28:51 -0800232 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Michael Jurka1e2f4652013-07-08 18:03:46 -0700233 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Winson Chung61967cb2012-02-28 18:11:33 -0800234 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
Michael Jurka1e2f4652013-07-08 18:03:46 -0700235 final float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800236
Adam Cohend4d7aa52011-07-19 21:47:37 -0700237 mSearchDropTargetBar.deferOnDragEnd();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700238 deferCompleteDropIfUninstalling(d);
239
Adam Cohend4d7aa52011-07-19 21:47:37 -0700240 Runnable onAnimationEndRunnable = new Runnable() {
241 @Override
242 public void run() {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700243 completeDrop(d);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700244 mSearchDropTargetBar.onDragEnd();
245 mLauncher.exitSpringLoadedDragMode();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700246 }
247 };
Winson Chung61967cb2012-02-28 18:11:33 -0800248 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700249 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800250 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800251 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700252 }
253
Michael Jurka1e2f4652013-07-08 18:03:46 -0700254 private void deferCompleteDropIfUninstalling(DragObject d) {
255 mWaitingForUninstall = false;
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200256 if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700257 if (d.dragSource instanceof Folder) {
258 ((Folder) d.dragSource).deferCompleteDropAfterUninstallActivity();
259 } else if (d.dragSource instanceof Workspace) {
260 ((Workspace) d.dragSource).deferCompleteDropAfterUninstallActivity();
261 }
262 mWaitingForUninstall = true;
263 }
264 }
Winson Chung4c98d922011-05-31 16:50:48 -0700265
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200266 private boolean isUninstallFromWorkspace(DragObject d) {
267 if (AppsCustomizePagedView.DISABLE_ALL_APPS && isWorkspaceOrFolderApplication(d)) {
268 ShortcutInfo shortcut = (ShortcutInfo) d.dragInfo;
269 if (shortcut.intent != null && shortcut.intent.getComponent() != null) {
270 Set<String> categories = shortcut.intent.getCategories();
271 boolean includesLauncherCategory = false;
272 if (categories != null) {
273 for (String category : categories) {
274 if (category.equals(Intent.CATEGORY_LAUNCHER)) {
275 includesLauncherCategory = true;
276 break;
277 }
278 }
279 }
280 return includesLauncherCategory;
281 }
282 }
283 return false;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700284 }
285
Michael Jurkaf3007582013-08-21 14:33:57 +0200286 private void completeDrop(DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700287 ItemInfo item = (ItemInfo) d.dragInfo;
288 boolean wasWaitingForUninstall = mWaitingForUninstall;
289 mWaitingForUninstall = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700290 if (isAllAppsApplication(d.dragSource, item)) {
291 // Uninstall the application if it is being dragged from AppsCustomize
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200292 AppInfo appInfo = (AppInfo) item;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700293 mLauncher.startApplicationUninstallActivity(appInfo.componentName, appInfo.flags);
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200294 } else if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700295 ShortcutInfo shortcut = (ShortcutInfo) item;
296 if (shortcut.intent != null && shortcut.intent.getComponent() != null) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200297 final ComponentName componentName = shortcut.intent.getComponent();
298 final DragSource dragSource = d.dragSource;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200299 int flags = AppInfo.initFlags(
Michael Jurka1e2f4652013-07-08 18:03:46 -0700300 ShortcutInfo.getPackageInfo(getContext(), componentName.getPackageName()));
301 mWaitingForUninstall =
302 mLauncher.startApplicationUninstallActivity(componentName, flags);
Michael Jurkaf3007582013-08-21 14:33:57 +0200303 if (mWaitingForUninstall) {
304 final Runnable checkIfUninstallWasSuccess = new Runnable() {
305 @Override
306 public void run() {
307 mWaitingForUninstall = false;
308 String packageName = componentName.getPackageName();
309 List<ResolveInfo> activities =
310 AllAppsList.findActivitiesForPackage(getContext(), packageName);
311 boolean uninstallSuccessful = activities.size() == 0;
312 if (dragSource instanceof Folder) {
313 ((Folder) dragSource).
314 onUninstallActivityReturned(uninstallSuccessful);
315 } else if (dragSource instanceof Workspace) {
316 ((Workspace) dragSource).
317 onUninstallActivityReturned(uninstallSuccessful);
318 }
319 }
320 };
321 mLauncher.addOnResumeCallback(checkIfUninstallWasSuccess);
322 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700323 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700324 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700325 LauncherModel.deleteItemFromDatabase(mLauncher, item);
326 } else if (isWorkspaceFolder(d)) {
327 // Remove the folder from the workspace and delete the contents from launcher model
328 FolderInfo folderInfo = (FolderInfo) item;
329 mLauncher.removeFolder(folderInfo);
330 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700331 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700332 // Remove the widget from the workspace
333 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
334 LauncherModel.deleteItemFromDatabase(mLauncher, item);
335
336 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
337 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
338 if (appWidgetHost != null) {
339 // Deleting an app widget ID is a void call but writes to disk before returning
340 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +0100341 new AsyncTask<Void, Void, Void>() {
342 public Void doInBackground(Void ... args) {
Winson Chung4c98d922011-05-31 16:50:48 -0700343 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +0100344 return null;
Winson Chung4c98d922011-05-31 16:50:48 -0700345 }
Michael Jurka43467462013-11-14 12:03:58 +0100346 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chung4c98d922011-05-31 16:50:48 -0700347 }
348 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700349 if (wasWaitingForUninstall && !mWaitingForUninstall) {
350 if (d.dragSource instanceof Folder) {
351 ((Folder) d.dragSource).onUninstallActivityReturned(false);
352 } else if (d.dragSource instanceof Workspace) {
353 ((Workspace) d.dragSource).onUninstallActivityReturned(false);
354 }
355 }
Winson Chung4c98d922011-05-31 16:50:48 -0700356 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700357
358 public void onDrop(DragObject d) {
359 animateToTrashAndCompleteDrop(d);
360 }
Winson Chung043f2af2012-03-01 16:09:54 -0800361
362 /**
363 * Creates an animation from the current drag view to the delete trash icon.
364 */
365 private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer,
366 DragObject d, PointF vel, ViewConfiguration config) {
367 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
368 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
369 final Rect from = new Rect();
370 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
371
372 // Calculate how far along the velocity vector we should put the intermediate point on
373 // the bezier curve
374 float velocity = Math.abs(vel.length());
375 float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
376 int offsetY = (int) (-from.top * vp);
377 int offsetX = (int) (offsetY / (vel.y / vel.x));
378 final float y2 = from.top + offsetY; // intermediate t/l
379 final float x2 = from.left + offsetX;
380 final float x1 = from.left; // drag view t/l
381 final float y1 = from.top;
382 final float x3 = to.left; // delete target t/l
383 final float y3 = to.top;
384
385 final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {
386 @Override
387 public float getInterpolation(float t) {
388 return t * t * t * t * t * t * t * t;
389 }
390 };
391 return new AnimatorUpdateListener() {
392 @Override
393 public void onAnimationUpdate(ValueAnimator animation) {
394 final DragView dragView = (DragView) dragLayer.getAnimatedView();
395 float t = ((Float) animation.getAnimatedValue()).floatValue();
396 float tp = scaleAlphaInterpolator.getInterpolation(t);
397 float initialScale = dragView.getInitialScale();
398 float finalAlpha = 0.5f;
399 float scale = dragView.getScaleX();
400 float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
401 float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
402 float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) +
403 (t * t) * x3;
404 float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) +
405 (t * t) * y3;
406
407 dragView.setTranslationX(x);
408 dragView.setTranslationY(y);
409 dragView.setScaleX(initialScale * (1f - tp));
410 dragView.setScaleY(initialScale * (1f - tp));
411 dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
412 }
413 };
414 }
415
416 /**
417 * Creates an animation from the current drag view along its current velocity vector.
418 * For this animation, the alpha runs for a fixed duration and we update the position
419 * progressively.
420 */
421 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
Winson Chung043f2af2012-03-01 16:09:54 -0800422 private DragLayer mDragLayer;
423 private PointF mVelocity;
424 private Rect mFrom;
425 private long mPrevTime;
426 private boolean mHasOffsetForScale;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700427 private float mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800428
Winson Chung9658b1e2012-04-09 18:30:07 -0700429 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
Winson Chung043f2af2012-03-01 16:09:54 -0800430
431 public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from,
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700432 long startTime, float friction) {
Winson Chung043f2af2012-03-01 16:09:54 -0800433 mDragLayer = dragLayer;
434 mVelocity = vel;
435 mFrom = from;
436 mPrevTime = startTime;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700437 mFriction = 1f - (dragLayer.getResources().getDisplayMetrics().density * friction);
Winson Chung043f2af2012-03-01 16:09:54 -0800438 }
439
440 @Override
441 public void onAnimationUpdate(ValueAnimator animation) {
442 final DragView dragView = (DragView) mDragLayer.getAnimatedView();
443 float t = ((Float) animation.getAnimatedValue()).floatValue();
444 long curTime = AnimationUtils.currentAnimationTimeMillis();
445
446 if (!mHasOffsetForScale) {
447 mHasOffsetForScale = true;
448 float scale = dragView.getScaleX();
449 float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f;
450 float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f;
451
452 mFrom.left += xOffset;
453 mFrom.top += yOffset;
454 }
455
456 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
457 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
458
459 dragView.setTranslationX(mFrom.left);
460 dragView.setTranslationY(mFrom.top);
461 dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
462
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700463 mVelocity.x *= mFriction;
464 mVelocity.y *= mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800465 mPrevTime = curTime;
466 }
467 };
468 private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer,
469 DragObject d, PointF vel, final long startTime, final int duration,
470 ViewConfiguration config) {
471 final Rect from = new Rect();
472 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
473
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700474 return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime,
475 FLING_TO_DELETE_FRICTION);
Winson Chung043f2af2012-03-01 16:09:54 -0800476 }
477
478 public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
Winson Chunga48487a2012-03-20 16:19:37 -0700479 final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView;
480
Winson Chung043f2af2012-03-01 16:09:54 -0800481 // Don't highlight the icon as it's animating
482 d.dragView.setColor(0);
483 d.dragView.updateInitialScaleToCurrentScale();
Winson Chunga48487a2012-03-20 16:19:37 -0700484 // Don't highlight the target if we are flinging from AllApps
485 if (isAllApps) {
486 resetHoverColor();
487 }
Winson Chung043f2af2012-03-01 16:09:54 -0800488
489 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
490 // Defer animating out the drop target if we are animating to it
491 mSearchDropTargetBar.deferOnDragEnd();
492 mSearchDropTargetBar.finishAnimations();
493 }
494
495 final ViewConfiguration config = ViewConfiguration.get(mLauncher);
496 final DragLayer dragLayer = mLauncher.getDragLayer();
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700497 final int duration = FLING_DELETE_ANIMATION_DURATION;
Winson Chung043f2af2012-03-01 16:09:54 -0800498 final long startTime = AnimationUtils.currentAnimationTimeMillis();
499
500 // NOTE: Because it takes time for the first frame of animation to actually be
501 // called and we expect the animation to be a continuation of the fling, we have
502 // to account for the time that has elapsed since the fling finished. And since
503 // we don't have a startDelay, we will always get call to update when we call
504 // start() (which we want to ignore).
505 final TimeInterpolator tInterpolator = new TimeInterpolator() {
506 private int mCount = -1;
507 private float mOffset = 0f;
508
509 @Override
510 public float getInterpolation(float t) {
511 if (mCount < 0) {
512 mCount++;
513 } else if (mCount == 0) {
514 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
515 startTime) / duration);
516 mCount++;
517 }
518 return Math.min(1f, mOffset + t);
519 }
520 };
521 AnimatorUpdateListener updateCb = null;
522 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
523 updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
524 } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
525 updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime,
526 duration, config);
527 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700528 deferCompleteDropIfUninstalling(d);
529
Winson Chung043f2af2012-03-01 16:09:54 -0800530 Runnable onAnimationEndRunnable = new Runnable() {
531 @Override
532 public void run() {
Winson Chunga48487a2012-03-20 16:19:37 -0700533 // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up
534 // itself, otherwise, complete the drop to initiate the deletion process
535 if (!isAllApps) {
536 mLauncher.exitSpringLoadedDragMode();
537 completeDrop(d);
538 }
539 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800540 }
541 };
542 dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable,
543 DragLayer.ANIMATION_END_DISAPPEAR, null);
544 }
Winson Chung4c98d922011-05-31 16:50:48 -0700545}