blob: 05e8906cb5cfb0445916f3fba87102f3e425f0f6 [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 Monk6a2c7782014-09-02 13:13:24 -040033import android.os.Build;
34import android.os.Bundle;
Jason Monk950a4672014-07-15 09:22:36 -040035import android.os.UserManager;
Winson Chung4c98d922011-05-31 16:50:48 -070036import android.util.AttributeSet;
37import android.view.View;
Winson Chung043f2af2012-03-01 16:09:54 -080038import android.view.ViewConfiguration;
Winson Chunga6427b12011-07-27 10:53:39 -070039import android.view.ViewGroup;
Winson Chung043f2af2012-03-01 16:09:54 -080040import android.view.animation.AnimationUtils;
Adam Cohend4d7aa52011-07-19 21:47:37 -070041import android.view.animation.DecelerateInterpolator;
Winson Chung61967cb2012-02-28 18:11:33 -080042import android.view.animation.LinearInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070043
Kenny Guyed131872014-04-30 03:02:21 +010044import com.android.launcher3.compat.LauncherAppsCompat;
45import com.android.launcher3.compat.UserHandleCompat;
46
Michael Jurka1e2f4652013-07-08 18:03:46 -070047import java.util.List;
Michael Jurka5e7af5d2013-09-14 18:35:38 +020048import java.util.Set;
Michael Jurka1e2f4652013-07-08 18:03:46 -070049
Winson Chung61fa4192011-06-12 15:15:29 -070050public class DeleteDropTarget extends ButtonDropTarget {
Winson Chung043f2af2012-03-01 16:09:54 -080051 private static int DELETE_ANIMATION_DURATION = 285;
Winson Chung6e1bdaf2012-05-29 17:03:45 -070052 private static int FLING_DELETE_ANIMATION_DURATION = 350;
53 private static float FLING_TO_DELETE_FRICTION = 0.035f;
Winson Chung043f2af2012-03-01 16:09:54 -080054 private static int MODE_FLING_DELETE_TO_TRASH = 0;
55 private static int MODE_FLING_DELETE_ALONG_VECTOR = 1;
Winson Chung4c98d922011-05-31 16:50:48 -070056
Winson Chung043f2af2012-03-01 16:09:54 -080057 private final int mFlingDeleteMode = MODE_FLING_DELETE_ALONG_VECTOR;
58
Winson Chunga62e9fd2011-07-11 15:20:48 -070059 private ColorStateList mOriginalTextColor;
Adam Cohenebea84d2011-11-09 17:20:41 -080060 private TransitionDrawable mUninstallDrawable;
61 private TransitionDrawable mRemoveDrawable;
62 private TransitionDrawable mCurrentDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070063
Michael Jurka24715c72013-07-08 18:03:46 -070064 private boolean mWaitingForUninstall = false;
65
Winson Chung4c98d922011-05-31 16:50:48 -070066 public DeleteDropTarget(Context context, AttributeSet attrs) {
67 this(context, attrs, 0);
68 }
69
70 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
71 super(context, attrs, defStyle);
72 }
73
74 @Override
75 protected void onFinishInflate() {
76 super.onFinishInflate();
77
78 // Get the drawable
Winson Chunga6427b12011-07-27 10:53:39 -070079 mOriginalTextColor = getTextColors();
Winson Chung4c98d922011-05-31 16:50:48 -070080
81 // Get the hover color
82 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070083 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080084 mUninstallDrawable = (TransitionDrawable)
85 r.getDrawable(R.drawable.uninstall_target_selector);
86 mRemoveDrawable = (TransitionDrawable) r.getDrawable(R.drawable.remove_target_selector);
87
88 mRemoveDrawable.setCrossFadeEnabled(true);
89 mUninstallDrawable.setCrossFadeEnabled(true);
90
91 // The current drawable is set to either the remove drawable or the uninstall drawable
92 // and is initially set to the remove drawable, as set in the layout xml.
Winson Chung947245b2012-05-15 16:34:19 -070093 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Winson Chung201bc822011-06-20 15:41:53 -070094
95 // Remove the text in the Phone UI in landscape
96 int orientation = getResources().getConfiguration().orientation;
97 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
Daniel Sandlere4f98912013-06-25 15:13:26 -040098 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070099 setText("");
Winson Chung201bc822011-06-20 15:41:53 -0700100 }
101 }
Winson Chung4c98d922011-05-31 16:50:48 -0700102 }
103
104 private boolean isAllAppsApplication(DragSource source, Object info) {
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000105 return source.supportsAppInfoDropTarget() && (info instanceof AppInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700106 }
107 private boolean isAllAppsWidget(DragSource source, Object info) {
Winson Chung11a49372012-04-27 15:12:38 -0700108 if (source instanceof AppsCustomizePagedView) {
109 if (info instanceof PendingAddItemInfo) {
110 PendingAddItemInfo addInfo = (PendingAddItemInfo) info;
111 switch (addInfo.itemType) {
112 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
113 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
114 return true;
115 }
116 }
117 }
118 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700119 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700120 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
121 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -0700122 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700123 private boolean isWorkspaceOrFolderApplication(DragObject d) {
124 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
125 }
126 private boolean isWorkspaceOrFolderWidget(DragObject d) {
127 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700128 }
129 private boolean isWorkspaceFolder(DragObject d) {
130 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
131 }
132
Winson Chunga48487a2012-03-20 16:19:37 -0700133 private void setHoverColor() {
Adam Cohenfe9da812014-08-04 17:08:01 -0700134 if (mCurrentDrawable != null) {
135 mCurrentDrawable.startTransition(mTransitionDuration);
136 }
Winson Chunga48487a2012-03-20 16:19:37 -0700137 setTextColor(mHoverColor);
138 }
139 private void resetHoverColor() {
Adam Cohenfe9da812014-08-04 17:08:01 -0700140 if (mCurrentDrawable != null) {
141 mCurrentDrawable.resetTransition();
142 }
Winson Chunga48487a2012-03-20 16:19:37 -0700143 setTextColor(mOriginalTextColor);
144 }
145
Winson Chung4c98d922011-05-31 16:50:48 -0700146 @Override
147 public boolean acceptDrop(DragObject d) {
Adam Cohen7c4c5102013-06-14 17:42:35 -0700148 return willAcceptDrop(d.dragInfo);
149 }
150
151 public static boolean willAcceptDrop(Object info) {
152 if (info instanceof ItemInfo) {
153 ItemInfo item = (ItemInfo) info;
154 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET ||
155 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
156 return true;
157 }
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200158
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800159 if (!LauncherAppState.isDisableAllApps() &&
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200160 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) {
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200161 return true;
162 }
163
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800164 if (!LauncherAppState.isDisableAllApps() &&
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200165 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
166 item instanceof AppInfo) {
167 AppInfo appInfo = (AppInfo) info;
168 return (appInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
169 }
170
Michael Jurka9bd4d282013-09-05 19:21:31 +0200171 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
Michael Jurka1e2f4652013-07-08 18:03:46 -0700172 item instanceof ShortcutInfo) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800173 if (LauncherAppState.isDisableAllApps()) {
Michael Jurka9bd4d282013-09-05 19:21:31 +0200174 ShortcutInfo shortcutInfo = (ShortcutInfo) info;
175 return (shortcutInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
176 } else {
177 return true;
178 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700179 }
Adam Cohen947dc542013-06-06 22:43:33 -0700180 }
Adam Cohen7c4c5102013-06-14 17:42:35 -0700181 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700182 }
183
184 @Override
185 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700186 boolean isVisible = true;
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800187 boolean useUninstallLabel = !LauncherAppState.isDisableAllApps() &&
Michael Jurkaaddcba62013-10-09 19:04:13 -0700188 isAllAppsApplication(source, info);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000189 boolean useDeleteLabel = !useUninstallLabel && source.supportsDeleteDropTarget();
Winson Chung4c98d922011-05-31 16:50:48 -0700190
Winson Chung4c98d922011-05-31 16:50:48 -0700191 // If we are dragging an application from AppsCustomize, only show the control if we can
Winson Chunge01af632013-09-26 10:43:20 -0700192 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case.
193 // Hide the delete target if it is a widget from AppsCustomize.
194 if (!willAcceptDrop(info) || isAllAppsWidget(source, info)) {
Adam Cohen947dc542013-06-06 22:43:33 -0700195 isVisible = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700196 }
Jason Monk950a4672014-07-15 09:22:36 -0400197 if (useUninstallLabel) {
Jason Monk6a2c7782014-09-02 13:13:24 -0400198 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
199 UserManager userManager = (UserManager)
200 getContext().getSystemService(Context.USER_SERVICE);
201 Bundle restrictions = userManager.getUserRestrictions();
202 if (restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
203 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false)) {
204 isVisible = false;
205 }
Jason Monk950a4672014-07-15 09:22:36 -0400206 }
207 }
Winson Chung4c98d922011-05-31 16:50:48 -0700208
Michael Jurkaaddcba62013-10-09 19:04:13 -0700209 if (useUninstallLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800210 setCompoundDrawablesRelativeWithIntrinsicBounds(mUninstallDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000211 } else if (useDeleteLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800212 setCompoundDrawablesRelativeWithIntrinsicBounds(mRemoveDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000213 } else {
214 isVisible = false;
Adam Cohenebea84d2011-11-09 17:20:41 -0800215 }
Winson Chung947245b2012-05-15 16:34:19 -0700216 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Adam Cohenebea84d2011-11-09 17:20:41 -0800217
Winson Chung4c98d922011-05-31 16:50:48 -0700218 mActive = isVisible;
Winson Chunga48487a2012-03-20 16:19:37 -0700219 resetHoverColor();
Winson Chunga6427b12011-07-27 10:53:39 -0700220 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000221 if (isVisible && getText().length() > 0) {
Michael Jurkaaddcba62013-10-09 19:04:13 -0700222 setText(useUninstallLabel ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700223 : R.string.delete_target_label);
224 }
225 }
226
227 @Override
228 public void onDragEnd() {
229 super.onDragEnd();
230 mActive = false;
231 }
232
233 public void onDragEnter(DragObject d) {
234 super.onDragEnter(d);
235
Winson Chunga48487a2012-03-20 16:19:37 -0700236 setHoverColor();
Winson Chung4c98d922011-05-31 16:50:48 -0700237 }
238
239 public void onDragExit(DragObject d) {
240 super.onDragExit(d);
241
Winson Chungaaa530a2011-07-11 21:06:30 -0700242 if (!d.dragComplete) {
Winson Chunga48487a2012-03-20 16:19:37 -0700243 resetHoverColor();
Winson Chung61967cb2012-02-28 18:11:33 -0800244 } else {
245 // Restore the hover color if we are deleting
246 d.dragView.setColor(mHoverColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700247 }
Winson Chung4c98d922011-05-31 16:50:48 -0700248 }
249
Adam Cohened66b2b2012-01-23 17:28:51 -0800250 private void animateToTrashAndCompleteDrop(final DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700251 final DragLayer dragLayer = mLauncher.getDragLayer();
252 final Rect from = new Rect();
Adam Cohened66b2b2012-01-23 17:28:51 -0800253 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Adam Cohenfe9da812014-08-04 17:08:01 -0700254
255 int width = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicWidth();
256 int height = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicHeight();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700257 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Adam Cohenfe9da812014-08-04 17:08:01 -0700258 width, height);
Michael Jurka1e2f4652013-07-08 18:03:46 -0700259 final float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800260
Adam Cohend4d7aa52011-07-19 21:47:37 -0700261 mSearchDropTargetBar.deferOnDragEnd();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700262 deferCompleteDropIfUninstalling(d);
263
Adam Cohend4d7aa52011-07-19 21:47:37 -0700264 Runnable onAnimationEndRunnable = new Runnable() {
265 @Override
266 public void run() {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700267 completeDrop(d);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700268 mSearchDropTargetBar.onDragEnd();
269 mLauncher.exitSpringLoadedDragMode();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700270 }
271 };
Winson Chung61967cb2012-02-28 18:11:33 -0800272 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700273 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800274 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800275 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700276 }
277
Michael Jurka1e2f4652013-07-08 18:03:46 -0700278 private void deferCompleteDropIfUninstalling(DragObject d) {
279 mWaitingForUninstall = false;
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200280 if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700281 if (d.dragSource instanceof Folder) {
282 ((Folder) d.dragSource).deferCompleteDropAfterUninstallActivity();
283 } else if (d.dragSource instanceof Workspace) {
284 ((Workspace) d.dragSource).deferCompleteDropAfterUninstallActivity();
285 }
286 mWaitingForUninstall = true;
287 }
288 }
Winson Chung4c98d922011-05-31 16:50:48 -0700289
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200290 private boolean isUninstallFromWorkspace(DragObject d) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800291 if (LauncherAppState.isDisableAllApps() && isWorkspaceOrFolderApplication(d)) {
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200292 ShortcutInfo shortcut = (ShortcutInfo) d.dragInfo;
Nilesh Agrawaldff0bfe2013-12-17 15:20:50 -0800293 // Only allow manifest shortcuts to initiate an un-install.
294 return !InstallShortcutReceiver.isValidShortcutLaunchIntent(shortcut.intent);
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200295 }
296 return false;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700297 }
298
Michael Jurkaf3007582013-08-21 14:33:57 +0200299 private void completeDrop(DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700300 ItemInfo item = (ItemInfo) d.dragInfo;
301 boolean wasWaitingForUninstall = mWaitingForUninstall;
302 mWaitingForUninstall = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700303 if (isAllAppsApplication(d.dragSource, item)) {
304 // Uninstall the application if it is being dragged from AppsCustomize
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200305 AppInfo appInfo = (AppInfo) item;
Kenny Guyd31df542014-06-30 15:12:11 +0100306 mLauncher.startApplicationUninstallActivity(appInfo.componentName, appInfo.flags,
307 appInfo.user);
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200308 } else if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700309 ShortcutInfo shortcut = (ShortcutInfo) item;
Kenny Guyd31df542014-06-30 15:12:11 +0100310 if (shortcut.intent != null && shortcut.intent.getComponent() != null) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200311 final ComponentName componentName = shortcut.intent.getComponent();
312 final DragSource dragSource = d.dragSource;
Kenny Guyd31df542014-06-30 15:12:11 +0100313 final UserHandleCompat user = shortcut.user;
314 mWaitingForUninstall = mLauncher.startApplicationUninstallActivity(
315 componentName, shortcut.flags, user);
Michael Jurkaf3007582013-08-21 14:33:57 +0200316 if (mWaitingForUninstall) {
317 final Runnable checkIfUninstallWasSuccess = new Runnable() {
318 @Override
319 public void run() {
320 mWaitingForUninstall = false;
321 String packageName = componentName.getPackageName();
Kenny Guyed131872014-04-30 03:02:21 +0100322 boolean uninstallSuccessful = !AllAppsList.packageHasActivities(
Kenny Guyd31df542014-06-30 15:12:11 +0100323 getContext(), packageName, user);
Michael Jurkaf3007582013-08-21 14:33:57 +0200324 if (dragSource instanceof Folder) {
325 ((Folder) dragSource).
326 onUninstallActivityReturned(uninstallSuccessful);
327 } else if (dragSource instanceof Workspace) {
328 ((Workspace) dragSource).
329 onUninstallActivityReturned(uninstallSuccessful);
330 }
331 }
332 };
333 mLauncher.addOnResumeCallback(checkIfUninstallWasSuccess);
334 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700335 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700336 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700337 LauncherModel.deleteItemFromDatabase(mLauncher, item);
338 } else if (isWorkspaceFolder(d)) {
339 // Remove the folder from the workspace and delete the contents from launcher model
340 FolderInfo folderInfo = (FolderInfo) item;
341 mLauncher.removeFolder(folderInfo);
342 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700343 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700344 // Remove the widget from the workspace
345 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
346 LauncherModel.deleteItemFromDatabase(mLauncher, item);
347
348 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
349 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
Sunny Goyal651077b2014-06-30 14:15:31 -0700350 if ((appWidgetHost != null) && launcherAppWidgetInfo.isWidgetIdValid()) {
Winson Chung4c98d922011-05-31 16:50:48 -0700351 // Deleting an app widget ID is a void call but writes to disk before returning
352 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +0100353 new AsyncTask<Void, Void, Void>() {
354 public Void doInBackground(Void ... args) {
Winson Chung4c98d922011-05-31 16:50:48 -0700355 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +0100356 return null;
Winson Chung4c98d922011-05-31 16:50:48 -0700357 }
Michael Jurka43467462013-11-14 12:03:58 +0100358 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chung4c98d922011-05-31 16:50:48 -0700359 }
360 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700361 if (wasWaitingForUninstall && !mWaitingForUninstall) {
362 if (d.dragSource instanceof Folder) {
363 ((Folder) d.dragSource).onUninstallActivityReturned(false);
364 } else if (d.dragSource instanceof Workspace) {
365 ((Workspace) d.dragSource).onUninstallActivityReturned(false);
366 }
367 }
Winson Chung4c98d922011-05-31 16:50:48 -0700368 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700369
370 public void onDrop(DragObject d) {
371 animateToTrashAndCompleteDrop(d);
372 }
Winson Chung043f2af2012-03-01 16:09:54 -0800373
374 /**
375 * Creates an animation from the current drag view to the delete trash icon.
376 */
377 private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer,
378 DragObject d, PointF vel, ViewConfiguration config) {
Adam Cohenfe9da812014-08-04 17:08:01 -0700379
380 int width = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicWidth();
381 int height = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicHeight();
Winson Chung043f2af2012-03-01 16:09:54 -0800382 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Adam Cohenfe9da812014-08-04 17:08:01 -0700383 width, height);
Winson Chung043f2af2012-03-01 16:09:54 -0800384 final Rect from = new Rect();
385 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
386
387 // Calculate how far along the velocity vector we should put the intermediate point on
388 // the bezier curve
389 float velocity = Math.abs(vel.length());
390 float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
391 int offsetY = (int) (-from.top * vp);
392 int offsetX = (int) (offsetY / (vel.y / vel.x));
393 final float y2 = from.top + offsetY; // intermediate t/l
394 final float x2 = from.left + offsetX;
395 final float x1 = from.left; // drag view t/l
396 final float y1 = from.top;
397 final float x3 = to.left; // delete target t/l
398 final float y3 = to.top;
399
400 final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {
401 @Override
402 public float getInterpolation(float t) {
403 return t * t * t * t * t * t * t * t;
404 }
405 };
406 return new AnimatorUpdateListener() {
407 @Override
408 public void onAnimationUpdate(ValueAnimator animation) {
409 final DragView dragView = (DragView) dragLayer.getAnimatedView();
410 float t = ((Float) animation.getAnimatedValue()).floatValue();
411 float tp = scaleAlphaInterpolator.getInterpolation(t);
412 float initialScale = dragView.getInitialScale();
413 float finalAlpha = 0.5f;
414 float scale = dragView.getScaleX();
415 float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
416 float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
417 float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) +
418 (t * t) * x3;
419 float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) +
420 (t * t) * y3;
421
422 dragView.setTranslationX(x);
423 dragView.setTranslationY(y);
424 dragView.setScaleX(initialScale * (1f - tp));
425 dragView.setScaleY(initialScale * (1f - tp));
426 dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
427 }
428 };
429 }
430
431 /**
432 * Creates an animation from the current drag view along its current velocity vector.
433 * For this animation, the alpha runs for a fixed duration and we update the position
434 * progressively.
435 */
436 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
Winson Chung043f2af2012-03-01 16:09:54 -0800437 private DragLayer mDragLayer;
438 private PointF mVelocity;
439 private Rect mFrom;
440 private long mPrevTime;
441 private boolean mHasOffsetForScale;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700442 private float mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800443
Winson Chung9658b1e2012-04-09 18:30:07 -0700444 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
Winson Chung043f2af2012-03-01 16:09:54 -0800445
446 public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from,
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700447 long startTime, float friction) {
Winson Chung043f2af2012-03-01 16:09:54 -0800448 mDragLayer = dragLayer;
449 mVelocity = vel;
450 mFrom = from;
451 mPrevTime = startTime;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700452 mFriction = 1f - (dragLayer.getResources().getDisplayMetrics().density * friction);
Winson Chung043f2af2012-03-01 16:09:54 -0800453 }
454
455 @Override
456 public void onAnimationUpdate(ValueAnimator animation) {
457 final DragView dragView = (DragView) mDragLayer.getAnimatedView();
458 float t = ((Float) animation.getAnimatedValue()).floatValue();
459 long curTime = AnimationUtils.currentAnimationTimeMillis();
460
461 if (!mHasOffsetForScale) {
462 mHasOffsetForScale = true;
463 float scale = dragView.getScaleX();
464 float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f;
465 float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f;
466
467 mFrom.left += xOffset;
468 mFrom.top += yOffset;
469 }
470
471 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
472 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
473
474 dragView.setTranslationX(mFrom.left);
475 dragView.setTranslationY(mFrom.top);
476 dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
477
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700478 mVelocity.x *= mFriction;
479 mVelocity.y *= mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800480 mPrevTime = curTime;
481 }
482 };
483 private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer,
484 DragObject d, PointF vel, final long startTime, final int duration,
485 ViewConfiguration config) {
486 final Rect from = new Rect();
487 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
488
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700489 return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime,
490 FLING_TO_DELETE_FRICTION);
Winson Chung043f2af2012-03-01 16:09:54 -0800491 }
492
493 public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
Winson Chunga48487a2012-03-20 16:19:37 -0700494 final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView;
495
Winson Chung043f2af2012-03-01 16:09:54 -0800496 // Don't highlight the icon as it's animating
497 d.dragView.setColor(0);
498 d.dragView.updateInitialScaleToCurrentScale();
Winson Chunga48487a2012-03-20 16:19:37 -0700499 // Don't highlight the target if we are flinging from AllApps
500 if (isAllApps) {
501 resetHoverColor();
502 }
Winson Chung043f2af2012-03-01 16:09:54 -0800503
504 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
505 // Defer animating out the drop target if we are animating to it
506 mSearchDropTargetBar.deferOnDragEnd();
507 mSearchDropTargetBar.finishAnimations();
508 }
509
510 final ViewConfiguration config = ViewConfiguration.get(mLauncher);
511 final DragLayer dragLayer = mLauncher.getDragLayer();
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700512 final int duration = FLING_DELETE_ANIMATION_DURATION;
Winson Chung043f2af2012-03-01 16:09:54 -0800513 final long startTime = AnimationUtils.currentAnimationTimeMillis();
514
515 // NOTE: Because it takes time for the first frame of animation to actually be
516 // called and we expect the animation to be a continuation of the fling, we have
517 // to account for the time that has elapsed since the fling finished. And since
518 // we don't have a startDelay, we will always get call to update when we call
519 // start() (which we want to ignore).
520 final TimeInterpolator tInterpolator = new TimeInterpolator() {
521 private int mCount = -1;
522 private float mOffset = 0f;
523
524 @Override
525 public float getInterpolation(float t) {
526 if (mCount < 0) {
527 mCount++;
528 } else if (mCount == 0) {
529 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
530 startTime) / duration);
531 mCount++;
532 }
533 return Math.min(1f, mOffset + t);
534 }
535 };
536 AnimatorUpdateListener updateCb = null;
537 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
538 updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
539 } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
540 updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime,
541 duration, config);
542 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700543 deferCompleteDropIfUninstalling(d);
544
Winson Chung043f2af2012-03-01 16:09:54 -0800545 Runnable onAnimationEndRunnable = new Runnable() {
546 @Override
547 public void run() {
Winson Chunga48487a2012-03-20 16:19:37 -0700548 // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up
549 // itself, otherwise, complete the drop to initiate the deletion process
550 if (!isAllApps) {
551 mLauncher.exitSpringLoadedDragMode();
552 completeDrop(d);
553 }
554 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800555 }
556 };
557 dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable,
558 DragLayer.ANIMATION_END_DISAPPEAR, null);
559 }
Winson Chung4c98d922011-05-31 16:50:48 -0700560}