blob: fbdd7eb8c7247e8f64201b3056a0224459717561 [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;
Jason Monk950a4672014-07-15 09:22:36 -040033import android.os.UserManager;
Winson Chung4c98d922011-05-31 16:50:48 -070034import android.util.AttributeSet;
35import 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
Kenny Guyed131872014-04-30 03:02:21 +010042import com.android.launcher3.compat.LauncherAppsCompat;
43import com.android.launcher3.compat.UserHandleCompat;
44
Michael Jurka1e2f4652013-07-08 18:03:46 -070045import java.util.List;
Michael Jurka5e7af5d2013-09-14 18:35:38 +020046import java.util.Set;
Michael Jurka1e2f4652013-07-08 18:03:46 -070047
Winson Chung61fa4192011-06-12 15:15:29 -070048public class DeleteDropTarget extends ButtonDropTarget {
Winson Chung043f2af2012-03-01 16:09:54 -080049 private static int DELETE_ANIMATION_DURATION = 285;
Winson Chung6e1bdaf2012-05-29 17:03:45 -070050 private static int FLING_DELETE_ANIMATION_DURATION = 350;
51 private static float FLING_TO_DELETE_FRICTION = 0.035f;
Winson Chung043f2af2012-03-01 16:09:54 -080052 private static int MODE_FLING_DELETE_TO_TRASH = 0;
53 private static int MODE_FLING_DELETE_ALONG_VECTOR = 1;
Winson Chung4c98d922011-05-31 16:50:48 -070054
Winson Chung043f2af2012-03-01 16:09:54 -080055 private final int mFlingDeleteMode = MODE_FLING_DELETE_ALONG_VECTOR;
56
Winson Chunga62e9fd2011-07-11 15:20:48 -070057 private ColorStateList mOriginalTextColor;
Adam Cohenebea84d2011-11-09 17:20:41 -080058 private TransitionDrawable mUninstallDrawable;
59 private TransitionDrawable mRemoveDrawable;
60 private TransitionDrawable mCurrentDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070061
Michael Jurka24715c72013-07-08 18:03:46 -070062 private boolean mWaitingForUninstall = false;
63
Winson Chung4c98d922011-05-31 16:50:48 -070064 public DeleteDropTarget(Context context, AttributeSet attrs) {
65 this(context, attrs, 0);
66 }
67
68 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
69 super(context, attrs, defStyle);
70 }
71
72 @Override
73 protected void onFinishInflate() {
74 super.onFinishInflate();
75
76 // Get the drawable
Winson Chunga6427b12011-07-27 10:53:39 -070077 mOriginalTextColor = getTextColors();
Winson Chung4c98d922011-05-31 16:50:48 -070078
79 // Get the hover color
80 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070081 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080082 mUninstallDrawable = (TransitionDrawable)
83 r.getDrawable(R.drawable.uninstall_target_selector);
84 mRemoveDrawable = (TransitionDrawable) r.getDrawable(R.drawable.remove_target_selector);
85
86 mRemoveDrawable.setCrossFadeEnabled(true);
87 mUninstallDrawable.setCrossFadeEnabled(true);
88
89 // The current drawable is set to either the remove drawable or the uninstall drawable
90 // and is initially set to the remove drawable, as set in the layout xml.
Winson Chung947245b2012-05-15 16:34:19 -070091 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Winson Chung201bc822011-06-20 15:41:53 -070092
93 // Remove the text in the Phone UI in landscape
94 int orientation = getResources().getConfiguration().orientation;
95 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
Daniel Sandlere4f98912013-06-25 15:13:26 -040096 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070097 setText("");
Winson Chung201bc822011-06-20 15:41:53 -070098 }
99 }
Winson Chung4c98d922011-05-31 16:50:48 -0700100 }
101
102 private boolean isAllAppsApplication(DragSource source, Object info) {
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000103 return source.supportsAppInfoDropTarget() && (info instanceof AppInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700104 }
105 private boolean isAllAppsWidget(DragSource source, Object info) {
Winson Chung11a49372012-04-27 15:12:38 -0700106 if (source instanceof AppsCustomizePagedView) {
107 if (info instanceof PendingAddItemInfo) {
108 PendingAddItemInfo addInfo = (PendingAddItemInfo) info;
109 switch (addInfo.itemType) {
110 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
111 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
112 return true;
113 }
114 }
115 }
116 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700117 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700118 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
119 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -0700120 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700121 private boolean isWorkspaceOrFolderApplication(DragObject d) {
122 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
123 }
124 private boolean isWorkspaceOrFolderWidget(DragObject d) {
125 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700126 }
127 private boolean isWorkspaceFolder(DragObject d) {
128 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
129 }
130
Winson Chunga48487a2012-03-20 16:19:37 -0700131 private void setHoverColor() {
Adam Cohenfe9da812014-08-04 17:08:01 -0700132 if (mCurrentDrawable != null) {
133 mCurrentDrawable.startTransition(mTransitionDuration);
134 }
Winson Chunga48487a2012-03-20 16:19:37 -0700135 setTextColor(mHoverColor);
136 }
137 private void resetHoverColor() {
Adam Cohenfe9da812014-08-04 17:08:01 -0700138 if (mCurrentDrawable != null) {
139 mCurrentDrawable.resetTransition();
140 }
Winson Chunga48487a2012-03-20 16:19:37 -0700141 setTextColor(mOriginalTextColor);
142 }
143
Winson Chung4c98d922011-05-31 16:50:48 -0700144 @Override
145 public boolean acceptDrop(DragObject d) {
Adam Cohen7c4c5102013-06-14 17:42:35 -0700146 return willAcceptDrop(d.dragInfo);
147 }
148
149 public static boolean willAcceptDrop(Object info) {
150 if (info instanceof ItemInfo) {
151 ItemInfo item = (ItemInfo) info;
152 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET ||
153 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
154 return true;
155 }
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200156
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_FOLDER) {
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200159 return true;
160 }
161
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800162 if (!LauncherAppState.isDisableAllApps() &&
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200163 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
164 item instanceof AppInfo) {
165 AppInfo appInfo = (AppInfo) info;
166 return (appInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
167 }
168
Michael Jurka9bd4d282013-09-05 19:21:31 +0200169 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
Michael Jurka1e2f4652013-07-08 18:03:46 -0700170 item instanceof ShortcutInfo) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800171 if (LauncherAppState.isDisableAllApps()) {
Michael Jurka9bd4d282013-09-05 19:21:31 +0200172 ShortcutInfo shortcutInfo = (ShortcutInfo) info;
173 return (shortcutInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
174 } else {
175 return true;
176 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700177 }
Adam Cohen947dc542013-06-06 22:43:33 -0700178 }
Adam Cohen7c4c5102013-06-14 17:42:35 -0700179 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700180 }
181
182 @Override
183 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700184 boolean isVisible = true;
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800185 boolean useUninstallLabel = !LauncherAppState.isDisableAllApps() &&
Michael Jurkaaddcba62013-10-09 19:04:13 -0700186 isAllAppsApplication(source, info);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000187 boolean useDeleteLabel = !useUninstallLabel && source.supportsDeleteDropTarget();
Winson Chung4c98d922011-05-31 16:50:48 -0700188
Winson Chung4c98d922011-05-31 16:50:48 -0700189 // If we are dragging an application from AppsCustomize, only show the control if we can
Winson Chunge01af632013-09-26 10:43:20 -0700190 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case.
191 // Hide the delete target if it is a widget from AppsCustomize.
192 if (!willAcceptDrop(info) || isAllAppsWidget(source, info)) {
Adam Cohen947dc542013-06-06 22:43:33 -0700193 isVisible = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700194 }
Jason Monk950a4672014-07-15 09:22:36 -0400195 if (useUninstallLabel) {
196 UserManager userManager = (UserManager)
197 getContext().getSystemService(Context.USER_SERVICE);
198 if (userManager.hasUserRestriction(UserManager.DISALLOW_APPS_CONTROL)
199 || userManager.hasUserRestriction(UserManager.DISALLOW_UNINSTALL_APPS)) {
200 isVisible = false;
201 }
202 }
Winson Chung4c98d922011-05-31 16:50:48 -0700203
Michael Jurkaaddcba62013-10-09 19:04:13 -0700204 if (useUninstallLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800205 setCompoundDrawablesRelativeWithIntrinsicBounds(mUninstallDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000206 } else if (useDeleteLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800207 setCompoundDrawablesRelativeWithIntrinsicBounds(mRemoveDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000208 } else {
209 isVisible = false;
Adam Cohenebea84d2011-11-09 17:20:41 -0800210 }
Winson Chung947245b2012-05-15 16:34:19 -0700211 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Adam Cohenebea84d2011-11-09 17:20:41 -0800212
Winson Chung4c98d922011-05-31 16:50:48 -0700213 mActive = isVisible;
Winson Chunga48487a2012-03-20 16:19:37 -0700214 resetHoverColor();
Winson Chunga6427b12011-07-27 10:53:39 -0700215 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000216 if (isVisible && getText().length() > 0) {
Michael Jurkaaddcba62013-10-09 19:04:13 -0700217 setText(useUninstallLabel ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700218 : R.string.delete_target_label);
219 }
220 }
221
222 @Override
223 public void onDragEnd() {
224 super.onDragEnd();
225 mActive = false;
226 }
227
228 public void onDragEnter(DragObject d) {
229 super.onDragEnter(d);
230
Winson Chunga48487a2012-03-20 16:19:37 -0700231 setHoverColor();
Winson Chung4c98d922011-05-31 16:50:48 -0700232 }
233
234 public void onDragExit(DragObject d) {
235 super.onDragExit(d);
236
Winson Chungaaa530a2011-07-11 21:06:30 -0700237 if (!d.dragComplete) {
Winson Chunga48487a2012-03-20 16:19:37 -0700238 resetHoverColor();
Winson Chung61967cb2012-02-28 18:11:33 -0800239 } else {
240 // Restore the hover color if we are deleting
241 d.dragView.setColor(mHoverColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700242 }
Winson Chung4c98d922011-05-31 16:50:48 -0700243 }
244
Adam Cohened66b2b2012-01-23 17:28:51 -0800245 private void animateToTrashAndCompleteDrop(final DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700246 final DragLayer dragLayer = mLauncher.getDragLayer();
247 final Rect from = new Rect();
Adam Cohened66b2b2012-01-23 17:28:51 -0800248 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Adam Cohenfe9da812014-08-04 17:08:01 -0700249
250 int width = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicWidth();
251 int height = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicHeight();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700252 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Adam Cohenfe9da812014-08-04 17:08:01 -0700253 width, height);
Michael Jurka1e2f4652013-07-08 18:03:46 -0700254 final float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800255
Adam Cohend4d7aa52011-07-19 21:47:37 -0700256 mSearchDropTargetBar.deferOnDragEnd();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700257 deferCompleteDropIfUninstalling(d);
258
Adam Cohend4d7aa52011-07-19 21:47:37 -0700259 Runnable onAnimationEndRunnable = new Runnable() {
260 @Override
261 public void run() {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700262 completeDrop(d);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700263 mSearchDropTargetBar.onDragEnd();
264 mLauncher.exitSpringLoadedDragMode();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700265 }
266 };
Winson Chung61967cb2012-02-28 18:11:33 -0800267 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700268 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800269 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800270 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700271 }
272
Michael Jurka1e2f4652013-07-08 18:03:46 -0700273 private void deferCompleteDropIfUninstalling(DragObject d) {
274 mWaitingForUninstall = false;
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200275 if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700276 if (d.dragSource instanceof Folder) {
277 ((Folder) d.dragSource).deferCompleteDropAfterUninstallActivity();
278 } else if (d.dragSource instanceof Workspace) {
279 ((Workspace) d.dragSource).deferCompleteDropAfterUninstallActivity();
280 }
281 mWaitingForUninstall = true;
282 }
283 }
Winson Chung4c98d922011-05-31 16:50:48 -0700284
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200285 private boolean isUninstallFromWorkspace(DragObject d) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800286 if (LauncherAppState.isDisableAllApps() && isWorkspaceOrFolderApplication(d)) {
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200287 ShortcutInfo shortcut = (ShortcutInfo) d.dragInfo;
Nilesh Agrawaldff0bfe2013-12-17 15:20:50 -0800288 // Only allow manifest shortcuts to initiate an un-install.
289 return !InstallShortcutReceiver.isValidShortcutLaunchIntent(shortcut.intent);
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200290 }
291 return false;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700292 }
293
Michael Jurkaf3007582013-08-21 14:33:57 +0200294 private void completeDrop(DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700295 ItemInfo item = (ItemInfo) d.dragInfo;
296 boolean wasWaitingForUninstall = mWaitingForUninstall;
297 mWaitingForUninstall = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700298 if (isAllAppsApplication(d.dragSource, item)) {
299 // Uninstall the application if it is being dragged from AppsCustomize
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200300 AppInfo appInfo = (AppInfo) item;
Kenny Guyd31df542014-06-30 15:12:11 +0100301 mLauncher.startApplicationUninstallActivity(appInfo.componentName, appInfo.flags,
302 appInfo.user);
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200303 } else if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700304 ShortcutInfo shortcut = (ShortcutInfo) item;
Kenny Guyd31df542014-06-30 15:12:11 +0100305 if (shortcut.intent != null && shortcut.intent.getComponent() != null) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200306 final ComponentName componentName = shortcut.intent.getComponent();
307 final DragSource dragSource = d.dragSource;
Kenny Guyd31df542014-06-30 15:12:11 +0100308 final UserHandleCompat user = shortcut.user;
309 mWaitingForUninstall = mLauncher.startApplicationUninstallActivity(
310 componentName, shortcut.flags, user);
Michael Jurkaf3007582013-08-21 14:33:57 +0200311 if (mWaitingForUninstall) {
312 final Runnable checkIfUninstallWasSuccess = new Runnable() {
313 @Override
314 public void run() {
315 mWaitingForUninstall = false;
316 String packageName = componentName.getPackageName();
Kenny Guyed131872014-04-30 03:02:21 +0100317 boolean uninstallSuccessful = !AllAppsList.packageHasActivities(
Kenny Guyd31df542014-06-30 15:12:11 +0100318 getContext(), packageName, user);
Michael Jurkaf3007582013-08-21 14:33:57 +0200319 if (dragSource instanceof Folder) {
320 ((Folder) dragSource).
321 onUninstallActivityReturned(uninstallSuccessful);
322 } else if (dragSource instanceof Workspace) {
323 ((Workspace) dragSource).
324 onUninstallActivityReturned(uninstallSuccessful);
325 }
326 }
327 };
328 mLauncher.addOnResumeCallback(checkIfUninstallWasSuccess);
329 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700330 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700331 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700332 LauncherModel.deleteItemFromDatabase(mLauncher, item);
333 } else if (isWorkspaceFolder(d)) {
334 // Remove the folder from the workspace and delete the contents from launcher model
335 FolderInfo folderInfo = (FolderInfo) item;
336 mLauncher.removeFolder(folderInfo);
337 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700338 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700339 // Remove the widget from the workspace
340 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
341 LauncherModel.deleteItemFromDatabase(mLauncher, item);
342
343 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
344 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
Sunny Goyal651077b2014-06-30 14:15:31 -0700345 if ((appWidgetHost != null) && launcherAppWidgetInfo.isWidgetIdValid()) {
Winson Chung4c98d922011-05-31 16:50:48 -0700346 // Deleting an app widget ID is a void call but writes to disk before returning
347 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +0100348 new AsyncTask<Void, Void, Void>() {
349 public Void doInBackground(Void ... args) {
Winson Chung4c98d922011-05-31 16:50:48 -0700350 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +0100351 return null;
Winson Chung4c98d922011-05-31 16:50:48 -0700352 }
Michael Jurka43467462013-11-14 12:03:58 +0100353 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chung4c98d922011-05-31 16:50:48 -0700354 }
355 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700356 if (wasWaitingForUninstall && !mWaitingForUninstall) {
357 if (d.dragSource instanceof Folder) {
358 ((Folder) d.dragSource).onUninstallActivityReturned(false);
359 } else if (d.dragSource instanceof Workspace) {
360 ((Workspace) d.dragSource).onUninstallActivityReturned(false);
361 }
362 }
Winson Chung4c98d922011-05-31 16:50:48 -0700363 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700364
365 public void onDrop(DragObject d) {
366 animateToTrashAndCompleteDrop(d);
367 }
Winson Chung043f2af2012-03-01 16:09:54 -0800368
369 /**
370 * Creates an animation from the current drag view to the delete trash icon.
371 */
372 private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer,
373 DragObject d, PointF vel, ViewConfiguration config) {
Adam Cohenfe9da812014-08-04 17:08:01 -0700374
375 int width = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicWidth();
376 int height = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicHeight();
Winson Chung043f2af2012-03-01 16:09:54 -0800377 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Adam Cohenfe9da812014-08-04 17:08:01 -0700378 width, height);
Winson Chung043f2af2012-03-01 16:09:54 -0800379 final Rect from = new Rect();
380 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
381
382 // Calculate how far along the velocity vector we should put the intermediate point on
383 // the bezier curve
384 float velocity = Math.abs(vel.length());
385 float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
386 int offsetY = (int) (-from.top * vp);
387 int offsetX = (int) (offsetY / (vel.y / vel.x));
388 final float y2 = from.top + offsetY; // intermediate t/l
389 final float x2 = from.left + offsetX;
390 final float x1 = from.left; // drag view t/l
391 final float y1 = from.top;
392 final float x3 = to.left; // delete target t/l
393 final float y3 = to.top;
394
395 final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {
396 @Override
397 public float getInterpolation(float t) {
398 return t * t * t * t * t * t * t * t;
399 }
400 };
401 return new AnimatorUpdateListener() {
402 @Override
403 public void onAnimationUpdate(ValueAnimator animation) {
404 final DragView dragView = (DragView) dragLayer.getAnimatedView();
405 float t = ((Float) animation.getAnimatedValue()).floatValue();
406 float tp = scaleAlphaInterpolator.getInterpolation(t);
407 float initialScale = dragView.getInitialScale();
408 float finalAlpha = 0.5f;
409 float scale = dragView.getScaleX();
410 float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
411 float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
412 float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) +
413 (t * t) * x3;
414 float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) +
415 (t * t) * y3;
416
417 dragView.setTranslationX(x);
418 dragView.setTranslationY(y);
419 dragView.setScaleX(initialScale * (1f - tp));
420 dragView.setScaleY(initialScale * (1f - tp));
421 dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
422 }
423 };
424 }
425
426 /**
427 * Creates an animation from the current drag view along its current velocity vector.
428 * For this animation, the alpha runs for a fixed duration and we update the position
429 * progressively.
430 */
431 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
Winson Chung043f2af2012-03-01 16:09:54 -0800432 private DragLayer mDragLayer;
433 private PointF mVelocity;
434 private Rect mFrom;
435 private long mPrevTime;
436 private boolean mHasOffsetForScale;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700437 private float mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800438
Winson Chung9658b1e2012-04-09 18:30:07 -0700439 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
Winson Chung043f2af2012-03-01 16:09:54 -0800440
441 public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from,
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700442 long startTime, float friction) {
Winson Chung043f2af2012-03-01 16:09:54 -0800443 mDragLayer = dragLayer;
444 mVelocity = vel;
445 mFrom = from;
446 mPrevTime = startTime;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700447 mFriction = 1f - (dragLayer.getResources().getDisplayMetrics().density * friction);
Winson Chung043f2af2012-03-01 16:09:54 -0800448 }
449
450 @Override
451 public void onAnimationUpdate(ValueAnimator animation) {
452 final DragView dragView = (DragView) mDragLayer.getAnimatedView();
453 float t = ((Float) animation.getAnimatedValue()).floatValue();
454 long curTime = AnimationUtils.currentAnimationTimeMillis();
455
456 if (!mHasOffsetForScale) {
457 mHasOffsetForScale = true;
458 float scale = dragView.getScaleX();
459 float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f;
460 float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f;
461
462 mFrom.left += xOffset;
463 mFrom.top += yOffset;
464 }
465
466 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
467 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
468
469 dragView.setTranslationX(mFrom.left);
470 dragView.setTranslationY(mFrom.top);
471 dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
472
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700473 mVelocity.x *= mFriction;
474 mVelocity.y *= mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800475 mPrevTime = curTime;
476 }
477 };
478 private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer,
479 DragObject d, PointF vel, final long startTime, final int duration,
480 ViewConfiguration config) {
481 final Rect from = new Rect();
482 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
483
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700484 return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime,
485 FLING_TO_DELETE_FRICTION);
Winson Chung043f2af2012-03-01 16:09:54 -0800486 }
487
488 public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
Winson Chunga48487a2012-03-20 16:19:37 -0700489 final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView;
490
Winson Chung043f2af2012-03-01 16:09:54 -0800491 // Don't highlight the icon as it's animating
492 d.dragView.setColor(0);
493 d.dragView.updateInitialScaleToCurrentScale();
Winson Chunga48487a2012-03-20 16:19:37 -0700494 // Don't highlight the target if we are flinging from AllApps
495 if (isAllApps) {
496 resetHoverColor();
497 }
Winson Chung043f2af2012-03-01 16:09:54 -0800498
499 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
500 // Defer animating out the drop target if we are animating to it
501 mSearchDropTargetBar.deferOnDragEnd();
502 mSearchDropTargetBar.finishAnimations();
503 }
504
505 final ViewConfiguration config = ViewConfiguration.get(mLauncher);
506 final DragLayer dragLayer = mLauncher.getDragLayer();
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700507 final int duration = FLING_DELETE_ANIMATION_DURATION;
Winson Chung043f2af2012-03-01 16:09:54 -0800508 final long startTime = AnimationUtils.currentAnimationTimeMillis();
509
510 // NOTE: Because it takes time for the first frame of animation to actually be
511 // called and we expect the animation to be a continuation of the fling, we have
512 // to account for the time that has elapsed since the fling finished. And since
513 // we don't have a startDelay, we will always get call to update when we call
514 // start() (which we want to ignore).
515 final TimeInterpolator tInterpolator = new TimeInterpolator() {
516 private int mCount = -1;
517 private float mOffset = 0f;
518
519 @Override
520 public float getInterpolation(float t) {
521 if (mCount < 0) {
522 mCount++;
523 } else if (mCount == 0) {
524 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
525 startTime) / duration);
526 mCount++;
527 }
528 return Math.min(1f, mOffset + t);
529 }
530 };
531 AnimatorUpdateListener updateCb = null;
532 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
533 updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
534 } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
535 updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime,
536 duration, config);
537 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700538 deferCompleteDropIfUninstalling(d);
539
Winson Chung043f2af2012-03-01 16:09:54 -0800540 Runnable onAnimationEndRunnable = new Runnable() {
541 @Override
542 public void run() {
Winson Chunga48487a2012-03-20 16:19:37 -0700543 // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up
544 // itself, otherwise, complete the drop to initiate the deletion process
545 if (!isAllApps) {
546 mLauncher.exitSpringLoadedDragMode();
547 completeDrop(d);
548 }
549 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800550 }
551 };
552 dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable,
553 DragLayer.ANIMATION_END_DISAPPEAR, null);
554 }
Winson Chung4c98d922011-05-31 16:50:48 -0700555}