blob: 20546b8d8d10e2bee303f1a73a50e6128af2e779 [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
Kenny Guyed131872014-04-30 03:02:21 +010041import com.android.launcher3.compat.LauncherAppsCompat;
42import com.android.launcher3.compat.UserHandleCompat;
43
Michael Jurka1e2f4652013-07-08 18:03:46 -070044import java.util.List;
Michael Jurka5e7af5d2013-09-14 18:35:38 +020045import java.util.Set;
Michael Jurka1e2f4652013-07-08 18:03:46 -070046
Winson Chung61fa4192011-06-12 15:15:29 -070047public class DeleteDropTarget extends ButtonDropTarget {
Winson Chung043f2af2012-03-01 16:09:54 -080048 private static int DELETE_ANIMATION_DURATION = 285;
Winson Chung6e1bdaf2012-05-29 17:03:45 -070049 private static int FLING_DELETE_ANIMATION_DURATION = 350;
50 private static float FLING_TO_DELETE_FRICTION = 0.035f;
Winson Chung043f2af2012-03-01 16:09:54 -080051 private static int MODE_FLING_DELETE_TO_TRASH = 0;
52 private static int MODE_FLING_DELETE_ALONG_VECTOR = 1;
Winson Chung4c98d922011-05-31 16:50:48 -070053
Winson Chung043f2af2012-03-01 16:09:54 -080054 private final int mFlingDeleteMode = MODE_FLING_DELETE_ALONG_VECTOR;
55
Winson Chunga62e9fd2011-07-11 15:20:48 -070056 private ColorStateList mOriginalTextColor;
Adam Cohenebea84d2011-11-09 17:20:41 -080057 private TransitionDrawable mUninstallDrawable;
58 private TransitionDrawable mRemoveDrawable;
59 private TransitionDrawable mCurrentDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070060
Michael Jurka24715c72013-07-08 18:03:46 -070061 private boolean mWaitingForUninstall = false;
62
Winson Chung4c98d922011-05-31 16:50:48 -070063 public DeleteDropTarget(Context context, AttributeSet attrs) {
64 this(context, attrs, 0);
65 }
66
67 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
68 super(context, attrs, defStyle);
69 }
70
71 @Override
72 protected void onFinishInflate() {
73 super.onFinishInflate();
74
75 // Get the drawable
Winson Chunga6427b12011-07-27 10:53:39 -070076 mOriginalTextColor = getTextColors();
Winson Chung4c98d922011-05-31 16:50:48 -070077
78 // Get the hover color
79 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070080 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080081 mUninstallDrawable = (TransitionDrawable)
82 r.getDrawable(R.drawable.uninstall_target_selector);
83 mRemoveDrawable = (TransitionDrawable) r.getDrawable(R.drawable.remove_target_selector);
84
85 mRemoveDrawable.setCrossFadeEnabled(true);
86 mUninstallDrawable.setCrossFadeEnabled(true);
87
88 // The current drawable is set to either the remove drawable or the uninstall drawable
89 // and is initially set to the remove drawable, as set in the layout xml.
Winson Chung947245b2012-05-15 16:34:19 -070090 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Winson Chung201bc822011-06-20 15:41:53 -070091
92 // Remove the text in the Phone UI in landscape
93 int orientation = getResources().getConfiguration().orientation;
94 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
Daniel Sandlere4f98912013-06-25 15:13:26 -040095 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070096 setText("");
Winson Chung201bc822011-06-20 15:41:53 -070097 }
98 }
Winson Chung4c98d922011-05-31 16:50:48 -070099 }
100
101 private boolean isAllAppsApplication(DragSource source, Object info) {
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000102 return source.supportsAppInfoDropTarget() && (info instanceof AppInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700103 }
104 private boolean isAllAppsWidget(DragSource source, Object info) {
Winson Chung11a49372012-04-27 15:12:38 -0700105 if (source instanceof AppsCustomizePagedView) {
106 if (info instanceof PendingAddItemInfo) {
107 PendingAddItemInfo addInfo = (PendingAddItemInfo) info;
108 switch (addInfo.itemType) {
109 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
110 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
111 return true;
112 }
113 }
114 }
115 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700116 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700117 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
118 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -0700119 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700120 private boolean isWorkspaceOrFolderApplication(DragObject d) {
121 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
122 }
123 private boolean isWorkspaceOrFolderWidget(DragObject d) {
124 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700125 }
126 private boolean isWorkspaceFolder(DragObject d) {
127 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
128 }
129
Winson Chunga48487a2012-03-20 16:19:37 -0700130 private void setHoverColor() {
131 mCurrentDrawable.startTransition(mTransitionDuration);
132 setTextColor(mHoverColor);
133 }
134 private void resetHoverColor() {
135 mCurrentDrawable.resetTransition();
136 setTextColor(mOriginalTextColor);
137 }
138
Winson Chung4c98d922011-05-31 16:50:48 -0700139 @Override
140 public boolean acceptDrop(DragObject d) {
Adam Cohen7c4c5102013-06-14 17:42:35 -0700141 return willAcceptDrop(d.dragInfo);
142 }
143
144 public static boolean willAcceptDrop(Object info) {
145 if (info instanceof ItemInfo) {
146 ItemInfo item = (ItemInfo) info;
147 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET ||
148 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
149 return true;
150 }
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200151
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800152 if (!LauncherAppState.isDisableAllApps() &&
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200153 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) {
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200154 return true;
155 }
156
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800157 if (!LauncherAppState.isDisableAllApps() &&
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200158 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
159 item instanceof AppInfo) {
160 AppInfo appInfo = (AppInfo) info;
161 return (appInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
162 }
163
Michael Jurka9bd4d282013-09-05 19:21:31 +0200164 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
Michael Jurka1e2f4652013-07-08 18:03:46 -0700165 item instanceof ShortcutInfo) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800166 if (LauncherAppState.isDisableAllApps()) {
Michael Jurka9bd4d282013-09-05 19:21:31 +0200167 ShortcutInfo shortcutInfo = (ShortcutInfo) info;
168 return (shortcutInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
169 } else {
170 return true;
171 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700172 }
Adam Cohen947dc542013-06-06 22:43:33 -0700173 }
Adam Cohen7c4c5102013-06-14 17:42:35 -0700174 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700175 }
176
177 @Override
178 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700179 boolean isVisible = true;
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800180 boolean useUninstallLabel = !LauncherAppState.isDisableAllApps() &&
Michael Jurkaaddcba62013-10-09 19:04:13 -0700181 isAllAppsApplication(source, info);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000182 boolean useDeleteLabel = !useUninstallLabel && source.supportsDeleteDropTarget();
Winson Chung4c98d922011-05-31 16:50:48 -0700183
Winson Chung4c98d922011-05-31 16:50:48 -0700184 // If we are dragging an application from AppsCustomize, only show the control if we can
Winson Chunge01af632013-09-26 10:43:20 -0700185 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case.
186 // Hide the delete target if it is a widget from AppsCustomize.
187 if (!willAcceptDrop(info) || isAllAppsWidget(source, info)) {
Adam Cohen947dc542013-06-06 22:43:33 -0700188 isVisible = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700189 }
Kenny Guyed131872014-04-30 03:02:21 +0100190 if (useUninstallLabel &&
191 !(((ItemInfo) info).user.equals(UserHandleCompat.myUserHandle()))) {
192 // Don't support uninstall for apps from other profiles.
193 isVisible = false;
194 }
Winson Chung4c98d922011-05-31 16:50:48 -0700195
Michael Jurkaaddcba62013-10-09 19:04:13 -0700196 if (useUninstallLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800197 setCompoundDrawablesRelativeWithIntrinsicBounds(mUninstallDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000198 } else if (useDeleteLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800199 setCompoundDrawablesRelativeWithIntrinsicBounds(mRemoveDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000200 } else {
201 isVisible = false;
Adam Cohenebea84d2011-11-09 17:20:41 -0800202 }
Winson Chung947245b2012-05-15 16:34:19 -0700203 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Adam Cohenebea84d2011-11-09 17:20:41 -0800204
Winson Chung4c98d922011-05-31 16:50:48 -0700205 mActive = isVisible;
Winson Chunga48487a2012-03-20 16:19:37 -0700206 resetHoverColor();
Winson Chunga6427b12011-07-27 10:53:39 -0700207 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000208 if (isVisible && getText().length() > 0) {
Michael Jurkaaddcba62013-10-09 19:04:13 -0700209 setText(useUninstallLabel ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700210 : R.string.delete_target_label);
211 }
212 }
213
214 @Override
215 public void onDragEnd() {
216 super.onDragEnd();
217 mActive = false;
218 }
219
220 public void onDragEnter(DragObject d) {
221 super.onDragEnter(d);
222
Winson Chunga48487a2012-03-20 16:19:37 -0700223 setHoverColor();
Winson Chung4c98d922011-05-31 16:50:48 -0700224 }
225
226 public void onDragExit(DragObject d) {
227 super.onDragExit(d);
228
Winson Chungaaa530a2011-07-11 21:06:30 -0700229 if (!d.dragComplete) {
Winson Chunga48487a2012-03-20 16:19:37 -0700230 resetHoverColor();
Winson Chung61967cb2012-02-28 18:11:33 -0800231 } else {
232 // Restore the hover color if we are deleting
233 d.dragView.setColor(mHoverColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700234 }
Winson Chung4c98d922011-05-31 16:50:48 -0700235 }
236
Adam Cohened66b2b2012-01-23 17:28:51 -0800237 private void animateToTrashAndCompleteDrop(final DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700238 final DragLayer dragLayer = mLauncher.getDragLayer();
239 final Rect from = new Rect();
Adam Cohened66b2b2012-01-23 17:28:51 -0800240 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Michael Jurka1e2f4652013-07-08 18:03:46 -0700241 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Winson Chung61967cb2012-02-28 18:11:33 -0800242 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
Michael Jurka1e2f4652013-07-08 18:03:46 -0700243 final float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800244
Adam Cohend4d7aa52011-07-19 21:47:37 -0700245 mSearchDropTargetBar.deferOnDragEnd();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700246 deferCompleteDropIfUninstalling(d);
247
Adam Cohend4d7aa52011-07-19 21:47:37 -0700248 Runnable onAnimationEndRunnable = new Runnable() {
249 @Override
250 public void run() {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700251 completeDrop(d);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700252 mSearchDropTargetBar.onDragEnd();
253 mLauncher.exitSpringLoadedDragMode();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700254 }
255 };
Winson Chung61967cb2012-02-28 18:11:33 -0800256 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700257 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800258 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800259 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700260 }
261
Michael Jurka1e2f4652013-07-08 18:03:46 -0700262 private void deferCompleteDropIfUninstalling(DragObject d) {
263 mWaitingForUninstall = false;
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200264 if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700265 if (d.dragSource instanceof Folder) {
266 ((Folder) d.dragSource).deferCompleteDropAfterUninstallActivity();
267 } else if (d.dragSource instanceof Workspace) {
268 ((Workspace) d.dragSource).deferCompleteDropAfterUninstallActivity();
269 }
270 mWaitingForUninstall = true;
271 }
272 }
Winson Chung4c98d922011-05-31 16:50:48 -0700273
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200274 private boolean isUninstallFromWorkspace(DragObject d) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800275 if (LauncherAppState.isDisableAllApps() && isWorkspaceOrFolderApplication(d)) {
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200276 ShortcutInfo shortcut = (ShortcutInfo) d.dragInfo;
Nilesh Agrawaldff0bfe2013-12-17 15:20:50 -0800277 // Only allow manifest shortcuts to initiate an un-install.
278 return !InstallShortcutReceiver.isValidShortcutLaunchIntent(shortcut.intent);
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200279 }
280 return false;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700281 }
282
Michael Jurkaf3007582013-08-21 14:33:57 +0200283 private void completeDrop(DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700284 ItemInfo item = (ItemInfo) d.dragInfo;
285 boolean wasWaitingForUninstall = mWaitingForUninstall;
286 mWaitingForUninstall = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700287 if (isAllAppsApplication(d.dragSource, item)) {
288 // Uninstall the application if it is being dragged from AppsCustomize
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200289 AppInfo appInfo = (AppInfo) item;
Kenny Guyed131872014-04-30 03:02:21 +0100290 // We don't support uninstalling apps from other profiles.
291 if (item.user.equals(UserHandleCompat.myUserHandle())) {
292 mLauncher.startApplicationUninstallActivity(appInfo.componentName, appInfo.flags);
293 }
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200294 } else if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700295 ShortcutInfo shortcut = (ShortcutInfo) item;
Kenny Guyed131872014-04-30 03:02:21 +0100296 // We don't support uninstalling apps from other profiles.
297 if (shortcut.intent != null && shortcut.intent.getComponent() != null &&
298 shortcut.user.equals(UserHandleCompat.myUserHandle())) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200299 final ComponentName componentName = shortcut.intent.getComponent();
300 final DragSource dragSource = d.dragSource;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700301 mWaitingForUninstall =
Kenny Guyed131872014-04-30 03:02:21 +0100302 mLauncher.startApplicationUninstallActivity(componentName, shortcut.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();
Kenny Guyed131872014-04-30 03:02:21 +0100309 boolean uninstallSuccessful = !AllAppsList.packageHasActivities(
310 getContext(), packageName, UserHandleCompat.myUserHandle());
Michael Jurkaf3007582013-08-21 14:33:57 +0200311 if (dragSource instanceof Folder) {
312 ((Folder) dragSource).
313 onUninstallActivityReturned(uninstallSuccessful);
314 } else if (dragSource instanceof Workspace) {
315 ((Workspace) dragSource).
316 onUninstallActivityReturned(uninstallSuccessful);
317 }
318 }
319 };
320 mLauncher.addOnResumeCallback(checkIfUninstallWasSuccess);
321 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700322 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700323 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700324 LauncherModel.deleteItemFromDatabase(mLauncher, item);
325 } else if (isWorkspaceFolder(d)) {
326 // Remove the folder from the workspace and delete the contents from launcher model
327 FolderInfo folderInfo = (FolderInfo) item;
328 mLauncher.removeFolder(folderInfo);
329 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700330 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700331 // Remove the widget from the workspace
332 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
333 LauncherModel.deleteItemFromDatabase(mLauncher, item);
334
335 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
336 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
337 if (appWidgetHost != null) {
338 // Deleting an app widget ID is a void call but writes to disk before returning
339 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +0100340 new AsyncTask<Void, Void, Void>() {
341 public Void doInBackground(Void ... args) {
Winson Chung4c98d922011-05-31 16:50:48 -0700342 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +0100343 return null;
Winson Chung4c98d922011-05-31 16:50:48 -0700344 }
Michael Jurka43467462013-11-14 12:03:58 +0100345 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chung4c98d922011-05-31 16:50:48 -0700346 }
347 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700348 if (wasWaitingForUninstall && !mWaitingForUninstall) {
349 if (d.dragSource instanceof Folder) {
350 ((Folder) d.dragSource).onUninstallActivityReturned(false);
351 } else if (d.dragSource instanceof Workspace) {
352 ((Workspace) d.dragSource).onUninstallActivityReturned(false);
353 }
354 }
Winson Chung4c98d922011-05-31 16:50:48 -0700355 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700356
357 public void onDrop(DragObject d) {
358 animateToTrashAndCompleteDrop(d);
359 }
Winson Chung043f2af2012-03-01 16:09:54 -0800360
361 /**
362 * Creates an animation from the current drag view to the delete trash icon.
363 */
364 private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer,
365 DragObject d, PointF vel, ViewConfiguration config) {
366 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
367 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
368 final Rect from = new Rect();
369 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
370
371 // Calculate how far along the velocity vector we should put the intermediate point on
372 // the bezier curve
373 float velocity = Math.abs(vel.length());
374 float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
375 int offsetY = (int) (-from.top * vp);
376 int offsetX = (int) (offsetY / (vel.y / vel.x));
377 final float y2 = from.top + offsetY; // intermediate t/l
378 final float x2 = from.left + offsetX;
379 final float x1 = from.left; // drag view t/l
380 final float y1 = from.top;
381 final float x3 = to.left; // delete target t/l
382 final float y3 = to.top;
383
384 final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {
385 @Override
386 public float getInterpolation(float t) {
387 return t * t * t * t * t * t * t * t;
388 }
389 };
390 return new AnimatorUpdateListener() {
391 @Override
392 public void onAnimationUpdate(ValueAnimator animation) {
393 final DragView dragView = (DragView) dragLayer.getAnimatedView();
394 float t = ((Float) animation.getAnimatedValue()).floatValue();
395 float tp = scaleAlphaInterpolator.getInterpolation(t);
396 float initialScale = dragView.getInitialScale();
397 float finalAlpha = 0.5f;
398 float scale = dragView.getScaleX();
399 float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
400 float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
401 float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) +
402 (t * t) * x3;
403 float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) +
404 (t * t) * y3;
405
406 dragView.setTranslationX(x);
407 dragView.setTranslationY(y);
408 dragView.setScaleX(initialScale * (1f - tp));
409 dragView.setScaleY(initialScale * (1f - tp));
410 dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
411 }
412 };
413 }
414
415 /**
416 * Creates an animation from the current drag view along its current velocity vector.
417 * For this animation, the alpha runs for a fixed duration and we update the position
418 * progressively.
419 */
420 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
Winson Chung043f2af2012-03-01 16:09:54 -0800421 private DragLayer mDragLayer;
422 private PointF mVelocity;
423 private Rect mFrom;
424 private long mPrevTime;
425 private boolean mHasOffsetForScale;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700426 private float mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800427
Winson Chung9658b1e2012-04-09 18:30:07 -0700428 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
Winson Chung043f2af2012-03-01 16:09:54 -0800429
430 public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from,
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700431 long startTime, float friction) {
Winson Chung043f2af2012-03-01 16:09:54 -0800432 mDragLayer = dragLayer;
433 mVelocity = vel;
434 mFrom = from;
435 mPrevTime = startTime;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700436 mFriction = 1f - (dragLayer.getResources().getDisplayMetrics().density * friction);
Winson Chung043f2af2012-03-01 16:09:54 -0800437 }
438
439 @Override
440 public void onAnimationUpdate(ValueAnimator animation) {
441 final DragView dragView = (DragView) mDragLayer.getAnimatedView();
442 float t = ((Float) animation.getAnimatedValue()).floatValue();
443 long curTime = AnimationUtils.currentAnimationTimeMillis();
444
445 if (!mHasOffsetForScale) {
446 mHasOffsetForScale = true;
447 float scale = dragView.getScaleX();
448 float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f;
449 float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f;
450
451 mFrom.left += xOffset;
452 mFrom.top += yOffset;
453 }
454
455 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
456 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
457
458 dragView.setTranslationX(mFrom.left);
459 dragView.setTranslationY(mFrom.top);
460 dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
461
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700462 mVelocity.x *= mFriction;
463 mVelocity.y *= mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800464 mPrevTime = curTime;
465 }
466 };
467 private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer,
468 DragObject d, PointF vel, final long startTime, final int duration,
469 ViewConfiguration config) {
470 final Rect from = new Rect();
471 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
472
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700473 return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime,
474 FLING_TO_DELETE_FRICTION);
Winson Chung043f2af2012-03-01 16:09:54 -0800475 }
476
477 public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
Winson Chunga48487a2012-03-20 16:19:37 -0700478 final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView;
479
Winson Chung043f2af2012-03-01 16:09:54 -0800480 // Don't highlight the icon as it's animating
481 d.dragView.setColor(0);
482 d.dragView.updateInitialScaleToCurrentScale();
Winson Chunga48487a2012-03-20 16:19:37 -0700483 // Don't highlight the target if we are flinging from AllApps
484 if (isAllApps) {
485 resetHoverColor();
486 }
Winson Chung043f2af2012-03-01 16:09:54 -0800487
488 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
489 // Defer animating out the drop target if we are animating to it
490 mSearchDropTargetBar.deferOnDragEnd();
491 mSearchDropTargetBar.finishAnimations();
492 }
493
494 final ViewConfiguration config = ViewConfiguration.get(mLauncher);
495 final DragLayer dragLayer = mLauncher.getDragLayer();
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700496 final int duration = FLING_DELETE_ANIMATION_DURATION;
Winson Chung043f2af2012-03-01 16:09:54 -0800497 final long startTime = AnimationUtils.currentAnimationTimeMillis();
498
499 // NOTE: Because it takes time for the first frame of animation to actually be
500 // called and we expect the animation to be a continuation of the fling, we have
501 // to account for the time that has elapsed since the fling finished. And since
502 // we don't have a startDelay, we will always get call to update when we call
503 // start() (which we want to ignore).
504 final TimeInterpolator tInterpolator = new TimeInterpolator() {
505 private int mCount = -1;
506 private float mOffset = 0f;
507
508 @Override
509 public float getInterpolation(float t) {
510 if (mCount < 0) {
511 mCount++;
512 } else if (mCount == 0) {
513 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
514 startTime) / duration);
515 mCount++;
516 }
517 return Math.min(1f, mOffset + t);
518 }
519 };
520 AnimatorUpdateListener updateCb = null;
521 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
522 updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
523 } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
524 updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime,
525 duration, config);
526 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700527 deferCompleteDropIfUninstalling(d);
528
Winson Chung043f2af2012-03-01 16:09:54 -0800529 Runnable onAnimationEndRunnable = new Runnable() {
530 @Override
531 public void run() {
Winson Chunga48487a2012-03-20 16:19:37 -0700532 // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up
533 // itself, otherwise, complete the drop to initiate the deletion process
534 if (!isAllApps) {
535 mLauncher.exitSpringLoadedDragMode();
536 completeDrop(d);
537 }
538 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800539 }
540 };
541 dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable,
542 DragLayer.ANIMATION_END_DISAPPEAR, null);
543 }
Winson Chung4c98d922011-05-31 16:50:48 -0700544}