blob: 5a5c0027ed0f0a300ded2629f5d12006022f7e2e [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;
Winson Chunga62e9fd2011-07-11 15:20:48 -070024import android.content.res.ColorStateList;
Winson Chung201bc822011-06-20 15:41:53 -070025import android.content.res.Configuration;
Winson Chung4c98d922011-05-31 16:50:48 -070026import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080027import android.graphics.PointF;
Adam Cohend4d7aa52011-07-19 21:47:37 -070028import android.graphics.Rect;
Winson Chung967289b2011-06-30 18:09:30 -070029import android.graphics.drawable.TransitionDrawable;
Michael Jurka43467462013-11-14 12:03:58 +010030import android.os.AsyncTask;
Jason Monk6a2c7782014-09-02 13:13:24 -040031import android.os.Build;
32import android.os.Bundle;
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.UserHandleCompat;
43
Winson Chung61fa4192011-06-12 15:15:29 -070044public class DeleteDropTarget extends ButtonDropTarget {
Winson Chung043f2af2012-03-01 16:09:54 -080045 private static int DELETE_ANIMATION_DURATION = 285;
Winson Chung6e1bdaf2012-05-29 17:03:45 -070046 private static int FLING_DELETE_ANIMATION_DURATION = 350;
47 private static float FLING_TO_DELETE_FRICTION = 0.035f;
Winson Chung043f2af2012-03-01 16:09:54 -080048 private static int MODE_FLING_DELETE_TO_TRASH = 0;
49 private static int MODE_FLING_DELETE_ALONG_VECTOR = 1;
Winson Chung4c98d922011-05-31 16:50:48 -070050
Winson Chung043f2af2012-03-01 16:09:54 -080051 private final int mFlingDeleteMode = MODE_FLING_DELETE_ALONG_VECTOR;
52
Winson Chunga62e9fd2011-07-11 15:20:48 -070053 private ColorStateList mOriginalTextColor;
Adam Cohenebea84d2011-11-09 17:20:41 -080054 private TransitionDrawable mUninstallDrawable;
55 private TransitionDrawable mRemoveDrawable;
56 private TransitionDrawable mCurrentDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070057
Michael Jurka24715c72013-07-08 18:03:46 -070058 private boolean mWaitingForUninstall = false;
59
Winson Chung4c98d922011-05-31 16:50:48 -070060 public DeleteDropTarget(Context context, AttributeSet attrs) {
61 this(context, attrs, 0);
62 }
63
64 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
65 super(context, attrs, defStyle);
66 }
67
68 @Override
69 protected void onFinishInflate() {
70 super.onFinishInflate();
71
72 // Get the drawable
Winson Chunga6427b12011-07-27 10:53:39 -070073 mOriginalTextColor = getTextColors();
Winson Chung4c98d922011-05-31 16:50:48 -070074
75 // Get the hover color
76 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070077 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080078 mUninstallDrawable = (TransitionDrawable)
79 r.getDrawable(R.drawable.uninstall_target_selector);
80 mRemoveDrawable = (TransitionDrawable) r.getDrawable(R.drawable.remove_target_selector);
81
82 mRemoveDrawable.setCrossFadeEnabled(true);
83 mUninstallDrawable.setCrossFadeEnabled(true);
84
85 // The current drawable is set to either the remove drawable or the uninstall drawable
86 // and is initially set to the remove drawable, as set in the layout xml.
Winson Chung947245b2012-05-15 16:34:19 -070087 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Winson Chung201bc822011-06-20 15:41:53 -070088
89 // Remove the text in the Phone UI in landscape
90 int orientation = getResources().getConfiguration().orientation;
91 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
Daniel Sandlere4f98912013-06-25 15:13:26 -040092 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070093 setText("");
Winson Chung201bc822011-06-20 15:41:53 -070094 }
95 }
Winson Chung4c98d922011-05-31 16:50:48 -070096 }
97
98 private boolean isAllAppsApplication(DragSource source, Object info) {
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:
Adam Cohen59400422014-03-05 18:07:04 -0800108 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chung11a49372012-04-27 15:12:38 -0700109 return true;
110 }
111 }
112 }
113 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700114 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700115 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
116 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -0700117 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700118 private boolean isWorkspaceOrFolderApplication(DragObject d) {
119 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
120 }
121 private boolean isWorkspaceOrFolderWidget(DragObject d) {
122 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700123 }
124 private boolean isWorkspaceFolder(DragObject d) {
125 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
126 }
127
Winson Chunga48487a2012-03-20 16:19:37 -0700128 private void setHoverColor() {
Adam Cohenfe9da812014-08-04 17:08:01 -0700129 if (mCurrentDrawable != null) {
130 mCurrentDrawable.startTransition(mTransitionDuration);
131 }
Winson Chunga48487a2012-03-20 16:19:37 -0700132 setTextColor(mHoverColor);
133 }
134 private void resetHoverColor() {
Adam Cohenfe9da812014-08-04 17:08:01 -0700135 if (mCurrentDrawable != null) {
136 mCurrentDrawable.resetTransition();
137 }
Winson Chunga48487a2012-03-20 16:19:37 -0700138 setTextColor(mOriginalTextColor);
139 }
140
Winson Chung4c98d922011-05-31 16:50:48 -0700141 @Override
142 public boolean acceptDrop(DragObject d) {
Adam Cohen7c4c5102013-06-14 17:42:35 -0700143 return willAcceptDrop(d.dragInfo);
144 }
145
146 public static boolean willAcceptDrop(Object info) {
147 if (info instanceof ItemInfo) {
148 ItemInfo item = (ItemInfo) info;
149 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET ||
Adam Cohen59400422014-03-05 18:07:04 -0800150 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET ||
Adam Cohen7c4c5102013-06-14 17:42:35 -0700151 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
152 return true;
153 }
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200154
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800155 if (!LauncherAppState.isDisableAllApps() &&
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200156 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) {
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200157 return true;
158 }
159
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800160 if (!LauncherAppState.isDisableAllApps() &&
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200161 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
162 item instanceof AppInfo) {
163 AppInfo appInfo = (AppInfo) info;
164 return (appInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
165 }
166
Michael Jurka9bd4d282013-09-05 19:21:31 +0200167 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
Michael Jurka1e2f4652013-07-08 18:03:46 -0700168 item instanceof ShortcutInfo) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800169 if (LauncherAppState.isDisableAllApps()) {
Michael Jurka9bd4d282013-09-05 19:21:31 +0200170 ShortcutInfo shortcutInfo = (ShortcutInfo) info;
171 return (shortcutInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
172 } else {
173 return true;
174 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700175 }
Adam Cohen947dc542013-06-06 22:43:33 -0700176 }
Adam Cohen7c4c5102013-06-14 17:42:35 -0700177 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700178 }
179
180 @Override
181 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700182 boolean isVisible = true;
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800183 boolean useUninstallLabel = !LauncherAppState.isDisableAllApps() &&
Michael Jurkaaddcba62013-10-09 19:04:13 -0700184 isAllAppsApplication(source, info);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000185 boolean useDeleteLabel = !useUninstallLabel && source.supportsDeleteDropTarget();
Winson Chung4c98d922011-05-31 16:50:48 -0700186
Winson Chung4c98d922011-05-31 16:50:48 -0700187 // If we are dragging an application from AppsCustomize, only show the control if we can
Winson Chunge01af632013-09-26 10:43:20 -0700188 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case.
189 // Hide the delete target if it is a widget from AppsCustomize.
190 if (!willAcceptDrop(info) || isAllAppsWidget(source, info)) {
Adam Cohen947dc542013-06-06 22:43:33 -0700191 isVisible = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700192 }
Jason Monk950a4672014-07-15 09:22:36 -0400193 if (useUninstallLabel) {
Jason Monk6a2c7782014-09-02 13:13:24 -0400194 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
195 UserManager userManager = (UserManager)
196 getContext().getSystemService(Context.USER_SERVICE);
197 Bundle restrictions = userManager.getUserRestrictions();
198 if (restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
199 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false)) {
200 isVisible = false;
201 }
Jason Monk950a4672014-07-15 09:22:36 -0400202 }
203 }
Winson Chung4c98d922011-05-31 16:50:48 -0700204
Michael Jurkaaddcba62013-10-09 19:04:13 -0700205 if (useUninstallLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800206 setCompoundDrawablesRelativeWithIntrinsicBounds(mUninstallDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000207 } else if (useDeleteLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800208 setCompoundDrawablesRelativeWithIntrinsicBounds(mRemoveDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000209 } else {
210 isVisible = false;
Adam Cohenebea84d2011-11-09 17:20:41 -0800211 }
Winson Chung947245b2012-05-15 16:34:19 -0700212 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Adam Cohenebea84d2011-11-09 17:20:41 -0800213
Winson Chung4c98d922011-05-31 16:50:48 -0700214 mActive = isVisible;
Winson Chunga48487a2012-03-20 16:19:37 -0700215 resetHoverColor();
Winson Chunga6427b12011-07-27 10:53:39 -0700216 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000217 if (isVisible && getText().length() > 0) {
Michael Jurkaaddcba62013-10-09 19:04:13 -0700218 setText(useUninstallLabel ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700219 : R.string.delete_target_label);
220 }
221 }
222
223 @Override
224 public void onDragEnd() {
225 super.onDragEnd();
226 mActive = false;
227 }
228
229 public void onDragEnter(DragObject d) {
230 super.onDragEnter(d);
231
Winson Chunga48487a2012-03-20 16:19:37 -0700232 setHoverColor();
Winson Chung4c98d922011-05-31 16:50:48 -0700233 }
234
235 public void onDragExit(DragObject d) {
236 super.onDragExit(d);
237
Winson Chungaaa530a2011-07-11 21:06:30 -0700238 if (!d.dragComplete) {
Winson Chunga48487a2012-03-20 16:19:37 -0700239 resetHoverColor();
Winson Chung61967cb2012-02-28 18:11:33 -0800240 } else {
241 // Restore the hover color if we are deleting
242 d.dragView.setColor(mHoverColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700243 }
Winson Chung4c98d922011-05-31 16:50:48 -0700244 }
245
Adam Cohened66b2b2012-01-23 17:28:51 -0800246 private void animateToTrashAndCompleteDrop(final DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700247 final DragLayer dragLayer = mLauncher.getDragLayer();
248 final Rect from = new Rect();
Adam Cohened66b2b2012-01-23 17:28:51 -0800249 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Adam Cohenfe9da812014-08-04 17:08:01 -0700250
251 int width = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicWidth();
252 int height = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicHeight();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700253 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Adam Cohenfe9da812014-08-04 17:08:01 -0700254 width, height);
Michael Jurka1e2f4652013-07-08 18:03:46 -0700255 final float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800256
Adam Cohend4d7aa52011-07-19 21:47:37 -0700257 mSearchDropTargetBar.deferOnDragEnd();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700258 deferCompleteDropIfUninstalling(d);
259
Adam Cohend4d7aa52011-07-19 21:47:37 -0700260 Runnable onAnimationEndRunnable = new Runnable() {
261 @Override
262 public void run() {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700263 completeDrop(d);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700264 mSearchDropTargetBar.onDragEnd();
Sunny Goyal8498eb42014-10-16 12:08:41 -0700265 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700266 }
267 };
Winson Chung61967cb2012-02-28 18:11:33 -0800268 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700269 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800270 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800271 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700272 }
273
Michael Jurka1e2f4652013-07-08 18:03:46 -0700274 private void deferCompleteDropIfUninstalling(DragObject d) {
275 mWaitingForUninstall = false;
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200276 if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700277 if (d.dragSource instanceof Folder) {
278 ((Folder) d.dragSource).deferCompleteDropAfterUninstallActivity();
279 } else if (d.dragSource instanceof Workspace) {
280 ((Workspace) d.dragSource).deferCompleteDropAfterUninstallActivity();
281 }
282 mWaitingForUninstall = true;
283 }
284 }
Winson Chung4c98d922011-05-31 16:50:48 -0700285
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200286 private boolean isUninstallFromWorkspace(DragObject d) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800287 if (LauncherAppState.isDisableAllApps() && isWorkspaceOrFolderApplication(d)) {
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200288 ShortcutInfo shortcut = (ShortcutInfo) d.dragInfo;
Nilesh Agrawaldff0bfe2013-12-17 15:20:50 -0800289 // Only allow manifest shortcuts to initiate an un-install.
290 return !InstallShortcutReceiver.isValidShortcutLaunchIntent(shortcut.intent);
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200291 }
292 return false;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700293 }
294
Michael Jurkaf3007582013-08-21 14:33:57 +0200295 private void completeDrop(DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700296 ItemInfo item = (ItemInfo) d.dragInfo;
297 boolean wasWaitingForUninstall = mWaitingForUninstall;
298 mWaitingForUninstall = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700299 if (isAllAppsApplication(d.dragSource, item)) {
300 // Uninstall the application if it is being dragged from AppsCustomize
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200301 AppInfo appInfo = (AppInfo) item;
Kenny Guyd31df542014-06-30 15:12:11 +0100302 mLauncher.startApplicationUninstallActivity(appInfo.componentName, appInfo.flags,
303 appInfo.user);
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200304 } else if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700305 ShortcutInfo shortcut = (ShortcutInfo) item;
Kenny Guyd31df542014-06-30 15:12:11 +0100306 if (shortcut.intent != null && shortcut.intent.getComponent() != null) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200307 final ComponentName componentName = shortcut.intent.getComponent();
308 final DragSource dragSource = d.dragSource;
Kenny Guyd31df542014-06-30 15:12:11 +0100309 final UserHandleCompat user = shortcut.user;
310 mWaitingForUninstall = mLauncher.startApplicationUninstallActivity(
311 componentName, shortcut.flags, user);
Michael Jurkaf3007582013-08-21 14:33:57 +0200312 if (mWaitingForUninstall) {
313 final Runnable checkIfUninstallWasSuccess = new Runnable() {
314 @Override
315 public void run() {
316 mWaitingForUninstall = false;
317 String packageName = componentName.getPackageName();
Kenny Guyed131872014-04-30 03:02:21 +0100318 boolean uninstallSuccessful = !AllAppsList.packageHasActivities(
Kenny Guyd31df542014-06-30 15:12:11 +0100319 getContext(), packageName, user);
Michael Jurkaf3007582013-08-21 14:33:57 +0200320 if (dragSource instanceof Folder) {
321 ((Folder) dragSource).
322 onUninstallActivityReturned(uninstallSuccessful);
323 } else if (dragSource instanceof Workspace) {
324 ((Workspace) dragSource).
325 onUninstallActivityReturned(uninstallSuccessful);
326 }
327 }
328 };
329 mLauncher.addOnResumeCallback(checkIfUninstallWasSuccess);
330 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700331 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700332 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700333 LauncherModel.deleteItemFromDatabase(mLauncher, item);
334 } else if (isWorkspaceFolder(d)) {
335 // Remove the folder from the workspace and delete the contents from launcher model
336 FolderInfo folderInfo = (FolderInfo) item;
337 mLauncher.removeFolder(folderInfo);
338 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700339 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700340 // Remove the widget from the workspace
341 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
342 LauncherModel.deleteItemFromDatabase(mLauncher, item);
343
344 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
345 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
Adam Cohen59400422014-03-05 18:07:04 -0800346
347 if (appWidgetHost != null && !launcherAppWidgetInfo.isCustomWidget()
348 && launcherAppWidgetInfo.isWidgetIdValid()) {
Winson Chung4c98d922011-05-31 16:50:48 -0700349 // Deleting an app widget ID is a void call but writes to disk before returning
350 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +0100351 new AsyncTask<Void, Void, Void>() {
352 public Void doInBackground(Void ... args) {
Winson Chung4c98d922011-05-31 16:50:48 -0700353 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +0100354 return null;
Winson Chung4c98d922011-05-31 16:50:48 -0700355 }
Michael Jurka43467462013-11-14 12:03:58 +0100356 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chung4c98d922011-05-31 16:50:48 -0700357 }
358 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700359 if (wasWaitingForUninstall && !mWaitingForUninstall) {
360 if (d.dragSource instanceof Folder) {
361 ((Folder) d.dragSource).onUninstallActivityReturned(false);
362 } else if (d.dragSource instanceof Workspace) {
363 ((Workspace) d.dragSource).onUninstallActivityReturned(false);
364 }
365 }
Winson Chung4c98d922011-05-31 16:50:48 -0700366 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700367
368 public void onDrop(DragObject d) {
369 animateToTrashAndCompleteDrop(d);
370 }
Winson Chung043f2af2012-03-01 16:09:54 -0800371
372 /**
373 * Creates an animation from the current drag view to the delete trash icon.
374 */
375 private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer,
376 DragObject d, PointF vel, ViewConfiguration config) {
Adam Cohenfe9da812014-08-04 17:08:01 -0700377
378 int width = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicWidth();
379 int height = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicHeight();
Winson Chung043f2af2012-03-01 16:09:54 -0800380 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Adam Cohenfe9da812014-08-04 17:08:01 -0700381 width, height);
Winson Chung043f2af2012-03-01 16:09:54 -0800382 final Rect from = new Rect();
383 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
384
385 // Calculate how far along the velocity vector we should put the intermediate point on
386 // the bezier curve
387 float velocity = Math.abs(vel.length());
388 float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
389 int offsetY = (int) (-from.top * vp);
390 int offsetX = (int) (offsetY / (vel.y / vel.x));
391 final float y2 = from.top + offsetY; // intermediate t/l
392 final float x2 = from.left + offsetX;
393 final float x1 = from.left; // drag view t/l
394 final float y1 = from.top;
395 final float x3 = to.left; // delete target t/l
396 final float y3 = to.top;
397
398 final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {
399 @Override
400 public float getInterpolation(float t) {
401 return t * t * t * t * t * t * t * t;
402 }
403 };
404 return new AnimatorUpdateListener() {
405 @Override
406 public void onAnimationUpdate(ValueAnimator animation) {
407 final DragView dragView = (DragView) dragLayer.getAnimatedView();
408 float t = ((Float) animation.getAnimatedValue()).floatValue();
409 float tp = scaleAlphaInterpolator.getInterpolation(t);
410 float initialScale = dragView.getInitialScale();
411 float finalAlpha = 0.5f;
412 float scale = dragView.getScaleX();
413 float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
414 float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
415 float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) +
416 (t * t) * x3;
417 float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) +
418 (t * t) * y3;
419
420 dragView.setTranslationX(x);
421 dragView.setTranslationY(y);
422 dragView.setScaleX(initialScale * (1f - tp));
423 dragView.setScaleY(initialScale * (1f - tp));
424 dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
425 }
426 };
427 }
428
429 /**
430 * Creates an animation from the current drag view along its current velocity vector.
431 * For this animation, the alpha runs for a fixed duration and we update the position
432 * progressively.
433 */
434 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
Winson Chung043f2af2012-03-01 16:09:54 -0800435 private DragLayer mDragLayer;
436 private PointF mVelocity;
437 private Rect mFrom;
438 private long mPrevTime;
439 private boolean mHasOffsetForScale;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700440 private float mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800441
Winson Chung9658b1e2012-04-09 18:30:07 -0700442 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
Winson Chung043f2af2012-03-01 16:09:54 -0800443
444 public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from,
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700445 long startTime, float friction) {
Winson Chung043f2af2012-03-01 16:09:54 -0800446 mDragLayer = dragLayer;
447 mVelocity = vel;
448 mFrom = from;
449 mPrevTime = startTime;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700450 mFriction = 1f - (dragLayer.getResources().getDisplayMetrics().density * friction);
Winson Chung043f2af2012-03-01 16:09:54 -0800451 }
452
453 @Override
454 public void onAnimationUpdate(ValueAnimator animation) {
455 final DragView dragView = (DragView) mDragLayer.getAnimatedView();
456 float t = ((Float) animation.getAnimatedValue()).floatValue();
457 long curTime = AnimationUtils.currentAnimationTimeMillis();
458
459 if (!mHasOffsetForScale) {
460 mHasOffsetForScale = true;
461 float scale = dragView.getScaleX();
462 float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f;
463 float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f;
464
465 mFrom.left += xOffset;
466 mFrom.top += yOffset;
467 }
468
469 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
470 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
471
472 dragView.setTranslationX(mFrom.left);
473 dragView.setTranslationY(mFrom.top);
474 dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
475
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700476 mVelocity.x *= mFriction;
477 mVelocity.y *= mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800478 mPrevTime = curTime;
479 }
480 };
481 private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer,
482 DragObject d, PointF vel, final long startTime, final int duration,
483 ViewConfiguration config) {
484 final Rect from = new Rect();
485 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
486
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700487 return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime,
488 FLING_TO_DELETE_FRICTION);
Winson Chung043f2af2012-03-01 16:09:54 -0800489 }
490
491 public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
Winson Chunga48487a2012-03-20 16:19:37 -0700492 final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView;
493
Winson Chung043f2af2012-03-01 16:09:54 -0800494 // Don't highlight the icon as it's animating
495 d.dragView.setColor(0);
496 d.dragView.updateInitialScaleToCurrentScale();
Winson Chunga48487a2012-03-20 16:19:37 -0700497 // Don't highlight the target if we are flinging from AllApps
498 if (isAllApps) {
499 resetHoverColor();
500 }
Winson Chung043f2af2012-03-01 16:09:54 -0800501
502 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
503 // Defer animating out the drop target if we are animating to it
504 mSearchDropTargetBar.deferOnDragEnd();
505 mSearchDropTargetBar.finishAnimations();
506 }
507
508 final ViewConfiguration config = ViewConfiguration.get(mLauncher);
509 final DragLayer dragLayer = mLauncher.getDragLayer();
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700510 final int duration = FLING_DELETE_ANIMATION_DURATION;
Winson Chung043f2af2012-03-01 16:09:54 -0800511 final long startTime = AnimationUtils.currentAnimationTimeMillis();
512
513 // NOTE: Because it takes time for the first frame of animation to actually be
514 // called and we expect the animation to be a continuation of the fling, we have
515 // to account for the time that has elapsed since the fling finished. And since
516 // we don't have a startDelay, we will always get call to update when we call
517 // start() (which we want to ignore).
518 final TimeInterpolator tInterpolator = new TimeInterpolator() {
519 private int mCount = -1;
520 private float mOffset = 0f;
521
522 @Override
523 public float getInterpolation(float t) {
524 if (mCount < 0) {
525 mCount++;
526 } else if (mCount == 0) {
527 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
528 startTime) / duration);
529 mCount++;
530 }
531 return Math.min(1f, mOffset + t);
532 }
533 };
534 AnimatorUpdateListener updateCb = null;
535 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
536 updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
537 } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
538 updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime,
539 duration, config);
540 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700541 deferCompleteDropIfUninstalling(d);
542
Winson Chung043f2af2012-03-01 16:09:54 -0800543 Runnable onAnimationEndRunnable = new Runnable() {
544 @Override
545 public void run() {
Winson Chunga48487a2012-03-20 16:19:37 -0700546 // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up
547 // itself, otherwise, complete the drop to initiate the deletion process
548 if (!isAllApps) {
549 mLauncher.exitSpringLoadedDragMode();
550 completeDrop(d);
551 }
552 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800553 }
554 };
555 dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable,
556 DragLayer.ANIMATION_END_DISAPPEAR, null);
557 }
Winson Chung4c98d922011-05-31 16:50:48 -0700558}