blob: ebe874f380455a1136318768364bb5fcc7a7d2bc [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;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080022import android.annotation.TargetApi;
Michael Jurka1e2f4652013-07-08 18:03:46 -070023import android.content.ComponentName;
Winson Chung4c98d922011-05-31 16:50:48 -070024import android.content.Context;
Winson Chunga62e9fd2011-07-11 15:20:48 -070025import android.content.res.ColorStateList;
Winson Chung201bc822011-06-20 15:41:53 -070026import android.content.res.Configuration;
Winson Chung4c98d922011-05-31 16:50:48 -070027import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080028import android.graphics.PointF;
Adam Cohend4d7aa52011-07-19 21:47:37 -070029import android.graphics.Rect;
Winson Chung967289b2011-06-30 18:09:30 -070030import android.graphics.drawable.TransitionDrawable;
Michael Jurka43467462013-11-14 12:03:58 +010031import android.os.AsyncTask;
Jason Monk6a2c7782014-09-02 13:13:24 -040032import android.os.Build;
33import android.os.Bundle;
Jason Monk950a4672014-07-15 09:22:36 -040034import android.os.UserManager;
Winson Chung4c98d922011-05-31 16:50:48 -070035import android.util.AttributeSet;
36import android.view.View;
Winson Chung043f2af2012-03-01 16:09:54 -080037import android.view.ViewConfiguration;
Winson Chunga6427b12011-07-27 10:53:39 -070038import android.view.ViewGroup;
Winson Chung043f2af2012-03-01 16:09:54 -080039import android.view.animation.AnimationUtils;
Adam Cohend4d7aa52011-07-19 21:47:37 -070040import android.view.animation.DecelerateInterpolator;
Winson Chung61967cb2012-02-28 18:11:33 -080041import android.view.animation.LinearInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070042
Kenny Guyed131872014-04-30 03:02:21 +010043import com.android.launcher3.compat.UserHandleCompat;
44
Winson Chung61fa4192011-06-12 15:15:29 -070045public class DeleteDropTarget extends ButtonDropTarget {
Winson Chung043f2af2012-03-01 16:09:54 -080046 private static int DELETE_ANIMATION_DURATION = 285;
Winson Chung6e1bdaf2012-05-29 17:03:45 -070047 private static int FLING_DELETE_ANIMATION_DURATION = 350;
48 private static float FLING_TO_DELETE_FRICTION = 0.035f;
Winson Chung043f2af2012-03-01 16:09:54 -080049 private static int MODE_FLING_DELETE_TO_TRASH = 0;
50 private static int MODE_FLING_DELETE_ALONG_VECTOR = 1;
Winson Chung4c98d922011-05-31 16:50:48 -070051
Winson Chung043f2af2012-03-01 16:09:54 -080052 private final int mFlingDeleteMode = MODE_FLING_DELETE_ALONG_VECTOR;
53
Winson Chunga62e9fd2011-07-11 15:20:48 -070054 private ColorStateList mOriginalTextColor;
Adam Cohenebea84d2011-11-09 17:20:41 -080055 private TransitionDrawable mUninstallDrawable;
56 private TransitionDrawable mRemoveDrawable;
57 private TransitionDrawable mCurrentDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070058
Michael Jurka24715c72013-07-08 18:03:46 -070059 private boolean mWaitingForUninstall = false;
60
Winson Chung4c98d922011-05-31 16:50:48 -070061 public DeleteDropTarget(Context context, AttributeSet attrs) {
62 this(context, attrs, 0);
63 }
64
65 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
66 super(context, attrs, defStyle);
67 }
68
69 @Override
70 protected void onFinishInflate() {
71 super.onFinishInflate();
72
73 // Get the drawable
Winson Chunga6427b12011-07-27 10:53:39 -070074 mOriginalTextColor = getTextColors();
Winson Chung4c98d922011-05-31 16:50:48 -070075
76 // Get the hover color
77 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070078 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080079 mUninstallDrawable = (TransitionDrawable)
80 r.getDrawable(R.drawable.uninstall_target_selector);
81 mRemoveDrawable = (TransitionDrawable) r.getDrawable(R.drawable.remove_target_selector);
82
83 mRemoveDrawable.setCrossFadeEnabled(true);
84 mUninstallDrawable.setCrossFadeEnabled(true);
85
86 // The current drawable is set to either the remove drawable or the uninstall drawable
87 // and is initially set to the remove drawable, as set in the layout xml.
Winson Chung947245b2012-05-15 16:34:19 -070088 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Winson Chung201bc822011-06-20 15:41:53 -070089
90 // Remove the text in the Phone UI in landscape
91 int orientation = getResources().getConfiguration().orientation;
92 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
Daniel Sandlere4f98912013-06-25 15:13:26 -040093 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070094 setText("");
Winson Chung201bc822011-06-20 15:41:53 -070095 }
96 }
Winson Chung4c98d922011-05-31 16:50:48 -070097 }
98
99 private boolean isAllAppsApplication(DragSource source, Object info) {
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000100 return source.supportsAppInfoDropTarget() && (info instanceof AppInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700101 }
102 private boolean isAllAppsWidget(DragSource source, Object info) {
Winson Chung11a49372012-04-27 15:12:38 -0700103 if (source instanceof AppsCustomizePagedView) {
104 if (info instanceof PendingAddItemInfo) {
105 PendingAddItemInfo addInfo = (PendingAddItemInfo) info;
106 switch (addInfo.itemType) {
107 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
108 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -0800109 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chung11a49372012-04-27 15:12:38 -0700110 return true;
111 }
112 }
113 }
114 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700115 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700116 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
117 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -0700118 }
Winson Chung4c98d922011-05-31 16:50:48 -0700119
Winson Chunga48487a2012-03-20 16:19:37 -0700120 private void setHoverColor() {
Adam Cohenfe9da812014-08-04 17:08:01 -0700121 if (mCurrentDrawable != null) {
122 mCurrentDrawable.startTransition(mTransitionDuration);
123 }
Winson Chunga48487a2012-03-20 16:19:37 -0700124 setTextColor(mHoverColor);
125 }
126 private void resetHoverColor() {
Adam Cohenfe9da812014-08-04 17:08:01 -0700127 if (mCurrentDrawable != null) {
128 mCurrentDrawable.resetTransition();
129 }
Winson Chunga48487a2012-03-20 16:19:37 -0700130 setTextColor(mOriginalTextColor);
131 }
132
Winson Chung4c98d922011-05-31 16:50:48 -0700133 @Override
134 public boolean acceptDrop(DragObject d) {
Adam Cohen7c4c5102013-06-14 17:42:35 -0700135 return willAcceptDrop(d.dragInfo);
136 }
137
138 public static boolean willAcceptDrop(Object info) {
139 if (info instanceof ItemInfo) {
140 ItemInfo item = (ItemInfo) info;
141 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET ||
Adam Cohen59400422014-03-05 18:07:04 -0800142 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET ||
Adam Cohen7c4c5102013-06-14 17:42:35 -0700143 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
144 return true;
145 }
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200146
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800147 if (!LauncherAppState.isDisableAllApps() &&
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200148 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) {
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200149 return true;
150 }
151
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_APPLICATION &&
154 item instanceof AppInfo) {
155 AppInfo appInfo = (AppInfo) info;
156 return (appInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
157 }
158
Michael Jurka9bd4d282013-09-05 19:21:31 +0200159 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
Michael Jurka1e2f4652013-07-08 18:03:46 -0700160 item instanceof ShortcutInfo) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800161 if (LauncherAppState.isDisableAllApps()) {
Michael Jurka9bd4d282013-09-05 19:21:31 +0200162 ShortcutInfo shortcutInfo = (ShortcutInfo) info;
163 return (shortcutInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
164 } else {
165 return true;
166 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700167 }
Adam Cohen947dc542013-06-06 22:43:33 -0700168 }
Adam Cohen7c4c5102013-06-14 17:42:35 -0700169 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700170 }
171
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800172 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4c98d922011-05-31 16:50:48 -0700173 @Override
174 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700175 boolean isVisible = true;
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800176 boolean useUninstallLabel = !LauncherAppState.isDisableAllApps() &&
Michael Jurkaaddcba62013-10-09 19:04:13 -0700177 isAllAppsApplication(source, info);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000178 boolean useDeleteLabel = !useUninstallLabel && source.supportsDeleteDropTarget();
Winson Chung4c98d922011-05-31 16:50:48 -0700179
Winson Chung4c98d922011-05-31 16:50:48 -0700180 // If we are dragging an application from AppsCustomize, only show the control if we can
Winson Chunge01af632013-09-26 10:43:20 -0700181 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case.
182 // Hide the delete target if it is a widget from AppsCustomize.
183 if (!willAcceptDrop(info) || isAllAppsWidget(source, info)) {
Adam Cohen947dc542013-06-06 22:43:33 -0700184 isVisible = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700185 }
Jason Monk950a4672014-07-15 09:22:36 -0400186 if (useUninstallLabel) {
Jason Monk6a2c7782014-09-02 13:13:24 -0400187 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
188 UserManager userManager = (UserManager)
189 getContext().getSystemService(Context.USER_SERVICE);
190 Bundle restrictions = userManager.getUserRestrictions();
191 if (restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
192 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false)) {
193 isVisible = false;
194 }
Jason Monk950a4672014-07-15 09:22:36 -0400195 }
196 }
Winson Chung4c98d922011-05-31 16:50:48 -0700197
Michael Jurkaaddcba62013-10-09 19:04:13 -0700198 if (useUninstallLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800199 setCompoundDrawablesRelativeWithIntrinsicBounds(mUninstallDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000200 } else if (useDeleteLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800201 setCompoundDrawablesRelativeWithIntrinsicBounds(mRemoveDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000202 } else {
203 isVisible = false;
Adam Cohenebea84d2011-11-09 17:20:41 -0800204 }
Winson Chung947245b2012-05-15 16:34:19 -0700205 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Adam Cohenebea84d2011-11-09 17:20:41 -0800206
Winson Chung4c98d922011-05-31 16:50:48 -0700207 mActive = isVisible;
Winson Chunga48487a2012-03-20 16:19:37 -0700208 resetHoverColor();
Winson Chunga6427b12011-07-27 10:53:39 -0700209 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000210 if (isVisible && getText().length() > 0) {
Michael Jurkaaddcba62013-10-09 19:04:13 -0700211 setText(useUninstallLabel ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700212 : R.string.delete_target_label);
213 }
214 }
215
216 @Override
217 public void onDragEnd() {
218 super.onDragEnd();
219 mActive = false;
220 }
221
222 public void onDragEnter(DragObject d) {
223 super.onDragEnter(d);
224
Winson Chunga48487a2012-03-20 16:19:37 -0700225 setHoverColor();
Winson Chung4c98d922011-05-31 16:50:48 -0700226 }
227
228 public void onDragExit(DragObject d) {
229 super.onDragExit(d);
230
Winson Chungaaa530a2011-07-11 21:06:30 -0700231 if (!d.dragComplete) {
Winson Chunga48487a2012-03-20 16:19:37 -0700232 resetHoverColor();
Winson Chung61967cb2012-02-28 18:11:33 -0800233 } else {
234 // Restore the hover color if we are deleting
235 d.dragView.setColor(mHoverColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700236 }
Winson Chung4c98d922011-05-31 16:50:48 -0700237 }
238
Adam Cohened66b2b2012-01-23 17:28:51 -0800239 private void animateToTrashAndCompleteDrop(final DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700240 final DragLayer dragLayer = mLauncher.getDragLayer();
241 final Rect from = new Rect();
Adam Cohened66b2b2012-01-23 17:28:51 -0800242 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Adam Cohenfe9da812014-08-04 17:08:01 -0700243
244 int width = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicWidth();
245 int height = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicHeight();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700246 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Adam Cohenfe9da812014-08-04 17:08:01 -0700247 width, height);
Michael Jurka1e2f4652013-07-08 18:03:46 -0700248 final float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800249
Adam Cohend4d7aa52011-07-19 21:47:37 -0700250 mSearchDropTargetBar.deferOnDragEnd();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700251 deferCompleteDropIfUninstalling(d);
252
Adam Cohend4d7aa52011-07-19 21:47:37 -0700253 Runnable onAnimationEndRunnable = new Runnable() {
254 @Override
255 public void run() {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700256 completeDrop(d);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700257 mSearchDropTargetBar.onDragEnd();
Sunny Goyal8498eb42014-10-16 12:08:41 -0700258 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700259 }
260 };
Winson Chung61967cb2012-02-28 18:11:33 -0800261 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700262 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800263 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800264 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700265 }
266
Michael Jurka1e2f4652013-07-08 18:03:46 -0700267 private void deferCompleteDropIfUninstalling(DragObject d) {
268 mWaitingForUninstall = false;
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200269 if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700270 if (d.dragSource instanceof Folder) {
271 ((Folder) d.dragSource).deferCompleteDropAfterUninstallActivity();
272 } else if (d.dragSource instanceof Workspace) {
273 ((Workspace) d.dragSource).deferCompleteDropAfterUninstallActivity();
274 }
275 mWaitingForUninstall = true;
276 }
277 }
Winson Chung4c98d922011-05-31 16:50:48 -0700278
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200279 private boolean isUninstallFromWorkspace(DragObject d) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800280 if (LauncherAppState.isDisableAllApps() && isDragSourceWorkspaceOrFolder(d)
281 && (d.dragInfo instanceof ShortcutInfo)) {
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200282 ShortcutInfo shortcut = (ShortcutInfo) d.dragInfo;
Nilesh Agrawaldff0bfe2013-12-17 15:20:50 -0800283 // Only allow manifest shortcuts to initiate an un-install.
284 return !InstallShortcutReceiver.isValidShortcutLaunchIntent(shortcut.intent);
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200285 }
286 return false;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700287 }
288
Michael Jurkaf3007582013-08-21 14:33:57 +0200289 private void completeDrop(DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700290 ItemInfo item = (ItemInfo) d.dragInfo;
291 boolean wasWaitingForUninstall = mWaitingForUninstall;
292 mWaitingForUninstall = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700293 if (isAllAppsApplication(d.dragSource, item)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800294 uninstallApp(mLauncher, (AppInfo) item);
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200295 } else if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700296 ShortcutInfo shortcut = (ShortcutInfo) item;
Kenny Guyd31df542014-06-30 15:12:11 +0100297 if (shortcut.intent != null && shortcut.intent.getComponent() != null) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200298 final ComponentName componentName = shortcut.intent.getComponent();
299 final DragSource dragSource = d.dragSource;
Kenny Guyd31df542014-06-30 15:12:11 +0100300 final UserHandleCompat user = shortcut.user;
301 mWaitingForUninstall = mLauncher.startApplicationUninstallActivity(
302 componentName, shortcut.flags, user);
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(
Kenny Guyd31df542014-06-30 15:12:11 +0100310 getContext(), packageName, user);
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 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800323 } else if (isDragSourceWorkspaceOrFolder(d)) {
324 removeWorkspaceOrFolderItem(mLauncher, item, null);
Winson Chung4c98d922011-05-31 16:50:48 -0700325 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700326 if (wasWaitingForUninstall && !mWaitingForUninstall) {
327 if (d.dragSource instanceof Folder) {
328 ((Folder) d.dragSource).onUninstallActivityReturned(false);
329 } else if (d.dragSource instanceof Workspace) {
330 ((Workspace) d.dragSource).onUninstallActivityReturned(false);
331 }
332 }
Winson Chung4c98d922011-05-31 16:50:48 -0700333 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700334
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800335 public static void uninstallApp(Launcher launcher, AppInfo info) {
336 launcher.startApplicationUninstallActivity(info.componentName, info.flags, info.user);
337 }
338
339 /**
340 * Removes the item from the workspace. If the view is not null, it also removes the view.
341 * @return true if the item was removed.
342 */
343 public static boolean removeWorkspaceOrFolderItem(Launcher launcher, ItemInfo item, View view) {
344 if (item instanceof ShortcutInfo) {
345 LauncherModel.deleteItemFromDatabase(launcher, item);
346 } else if (item instanceof FolderInfo) {
347 FolderInfo folder = (FolderInfo) item;
348 launcher.removeFolder(folder);
349 LauncherModel.deleteFolderContentsFromDatabase(launcher, folder);
350 } else if (item instanceof LauncherAppWidgetInfo) {
351 final LauncherAppWidgetInfo widget = (LauncherAppWidgetInfo) item;
352
353 // Remove the widget from the workspace
354 launcher.removeAppWidget(widget);
355 LauncherModel.deleteItemFromDatabase(launcher, widget);
356
357 final LauncherAppWidgetHost appWidgetHost = launcher.getAppWidgetHost();
358
359 if (appWidgetHost != null && !widget.isCustomWidget()
360 && widget.isWidgetIdValid()) {
361 // Deleting an app widget ID is a void call but writes to disk before returning
362 // to the caller...
363 new AsyncTask<Void, Void, Void>() {
364 public Void doInBackground(Void ... args) {
365 appWidgetHost.deleteAppWidgetId(widget.appWidgetId);
366 return null;
367 }
368 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
369 }
370 } else {
371 return false;
372 }
373
374 if (view != null) {
375 launcher.getWorkspace().removeWorkspaceItem(view);
376 launcher.getWorkspace().stripEmptyScreens();
377 }
378 return true;
379 }
380
Adam Cohend4d7aa52011-07-19 21:47:37 -0700381 public void onDrop(DragObject d) {
382 animateToTrashAndCompleteDrop(d);
383 }
Winson Chung043f2af2012-03-01 16:09:54 -0800384
385 /**
386 * Creates an animation from the current drag view to the delete trash icon.
387 */
388 private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer,
389 DragObject d, PointF vel, ViewConfiguration config) {
Adam Cohenfe9da812014-08-04 17:08:01 -0700390
391 int width = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicWidth();
392 int height = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicHeight();
Winson Chung043f2af2012-03-01 16:09:54 -0800393 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Adam Cohenfe9da812014-08-04 17:08:01 -0700394 width, height);
Winson Chung043f2af2012-03-01 16:09:54 -0800395 final Rect from = new Rect();
396 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
397
398 // Calculate how far along the velocity vector we should put the intermediate point on
399 // the bezier curve
400 float velocity = Math.abs(vel.length());
401 float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
402 int offsetY = (int) (-from.top * vp);
403 int offsetX = (int) (offsetY / (vel.y / vel.x));
404 final float y2 = from.top + offsetY; // intermediate t/l
405 final float x2 = from.left + offsetX;
406 final float x1 = from.left; // drag view t/l
407 final float y1 = from.top;
408 final float x3 = to.left; // delete target t/l
409 final float y3 = to.top;
410
411 final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {
412 @Override
413 public float getInterpolation(float t) {
414 return t * t * t * t * t * t * t * t;
415 }
416 };
417 return new AnimatorUpdateListener() {
418 @Override
419 public void onAnimationUpdate(ValueAnimator animation) {
420 final DragView dragView = (DragView) dragLayer.getAnimatedView();
421 float t = ((Float) animation.getAnimatedValue()).floatValue();
422 float tp = scaleAlphaInterpolator.getInterpolation(t);
423 float initialScale = dragView.getInitialScale();
424 float finalAlpha = 0.5f;
425 float scale = dragView.getScaleX();
426 float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
427 float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
428 float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) +
429 (t * t) * x3;
430 float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) +
431 (t * t) * y3;
432
433 dragView.setTranslationX(x);
434 dragView.setTranslationY(y);
435 dragView.setScaleX(initialScale * (1f - tp));
436 dragView.setScaleY(initialScale * (1f - tp));
437 dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
438 }
439 };
440 }
441
442 /**
443 * Creates an animation from the current drag view along its current velocity vector.
444 * For this animation, the alpha runs for a fixed duration and we update the position
445 * progressively.
446 */
447 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
Winson Chung043f2af2012-03-01 16:09:54 -0800448 private DragLayer mDragLayer;
449 private PointF mVelocity;
450 private Rect mFrom;
451 private long mPrevTime;
452 private boolean mHasOffsetForScale;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700453 private float mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800454
Winson Chung9658b1e2012-04-09 18:30:07 -0700455 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
Winson Chung043f2af2012-03-01 16:09:54 -0800456
457 public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from,
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700458 long startTime, float friction) {
Winson Chung043f2af2012-03-01 16:09:54 -0800459 mDragLayer = dragLayer;
460 mVelocity = vel;
461 mFrom = from;
462 mPrevTime = startTime;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700463 mFriction = 1f - (dragLayer.getResources().getDisplayMetrics().density * friction);
Winson Chung043f2af2012-03-01 16:09:54 -0800464 }
465
466 @Override
467 public void onAnimationUpdate(ValueAnimator animation) {
468 final DragView dragView = (DragView) mDragLayer.getAnimatedView();
469 float t = ((Float) animation.getAnimatedValue()).floatValue();
470 long curTime = AnimationUtils.currentAnimationTimeMillis();
471
472 if (!mHasOffsetForScale) {
473 mHasOffsetForScale = true;
474 float scale = dragView.getScaleX();
475 float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f;
476 float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f;
477
478 mFrom.left += xOffset;
479 mFrom.top += yOffset;
480 }
481
482 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
483 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
484
485 dragView.setTranslationX(mFrom.left);
486 dragView.setTranslationY(mFrom.top);
487 dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
488
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700489 mVelocity.x *= mFriction;
490 mVelocity.y *= mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800491 mPrevTime = curTime;
492 }
493 };
494 private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer,
495 DragObject d, PointF vel, final long startTime, final int duration,
496 ViewConfiguration config) {
497 final Rect from = new Rect();
498 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
499
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700500 return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime,
501 FLING_TO_DELETE_FRICTION);
Winson Chung043f2af2012-03-01 16:09:54 -0800502 }
503
504 public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
Winson Chunga48487a2012-03-20 16:19:37 -0700505 final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView;
506
Winson Chung043f2af2012-03-01 16:09:54 -0800507 // Don't highlight the icon as it's animating
508 d.dragView.setColor(0);
509 d.dragView.updateInitialScaleToCurrentScale();
Winson Chunga48487a2012-03-20 16:19:37 -0700510 // Don't highlight the target if we are flinging from AllApps
511 if (isAllApps) {
512 resetHoverColor();
513 }
Winson Chung043f2af2012-03-01 16:09:54 -0800514
515 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
516 // Defer animating out the drop target if we are animating to it
517 mSearchDropTargetBar.deferOnDragEnd();
518 mSearchDropTargetBar.finishAnimations();
519 }
520
521 final ViewConfiguration config = ViewConfiguration.get(mLauncher);
522 final DragLayer dragLayer = mLauncher.getDragLayer();
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700523 final int duration = FLING_DELETE_ANIMATION_DURATION;
Winson Chung043f2af2012-03-01 16:09:54 -0800524 final long startTime = AnimationUtils.currentAnimationTimeMillis();
525
526 // NOTE: Because it takes time for the first frame of animation to actually be
527 // called and we expect the animation to be a continuation of the fling, we have
528 // to account for the time that has elapsed since the fling finished. And since
529 // we don't have a startDelay, we will always get call to update when we call
530 // start() (which we want to ignore).
531 final TimeInterpolator tInterpolator = new TimeInterpolator() {
532 private int mCount = -1;
533 private float mOffset = 0f;
534
535 @Override
536 public float getInterpolation(float t) {
537 if (mCount < 0) {
538 mCount++;
539 } else if (mCount == 0) {
540 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
541 startTime) / duration);
542 mCount++;
543 }
544 return Math.min(1f, mOffset + t);
545 }
546 };
547 AnimatorUpdateListener updateCb = null;
548 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
549 updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
550 } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
551 updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime,
552 duration, config);
553 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700554 deferCompleteDropIfUninstalling(d);
555
Winson Chung043f2af2012-03-01 16:09:54 -0800556 Runnable onAnimationEndRunnable = new Runnable() {
557 @Override
558 public void run() {
Winson Chunga48487a2012-03-20 16:19:37 -0700559 // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up
560 // itself, otherwise, complete the drop to initiate the deletion process
561 if (!isAllApps) {
562 mLauncher.exitSpringLoadedDragMode();
563 completeDrop(d);
564 }
565 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800566 }
567 };
568 dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable,
569 DragLayer.ANIMATION_END_DISAPPEAR, null);
570 }
Winson Chung4c98d922011-05-31 16:50:48 -0700571}