blob: 1f0dad221b4a0e45d3d4908aa39e26b3a5ce19c7 [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;
Adam Cohen091440a2015-03-18 14:16:05 -070044import com.android.launcher3.util.Thunk;
Kenny Guyed131872014-04-30 03:02:21 +010045
Winson Chung61fa4192011-06-12 15:15:29 -070046public class DeleteDropTarget extends ButtonDropTarget {
Winson Chung043f2af2012-03-01 16:09:54 -080047 private static int DELETE_ANIMATION_DURATION = 285;
Winson Chung6e1bdaf2012-05-29 17:03:45 -070048 private static int FLING_DELETE_ANIMATION_DURATION = 350;
49 private static float FLING_TO_DELETE_FRICTION = 0.035f;
Winson Chung043f2af2012-03-01 16:09:54 -080050 private static int MODE_FLING_DELETE_TO_TRASH = 0;
51 private static int MODE_FLING_DELETE_ALONG_VECTOR = 1;
Winson Chung4c98d922011-05-31 16:50:48 -070052
Winson Chung043f2af2012-03-01 16:09:54 -080053 private final int mFlingDeleteMode = MODE_FLING_DELETE_ALONG_VECTOR;
54
Winson Chunga62e9fd2011-07-11 15:20:48 -070055 private ColorStateList mOriginalTextColor;
Adam Cohenebea84d2011-11-09 17:20:41 -080056 private TransitionDrawable mUninstallDrawable;
57 private TransitionDrawable mRemoveDrawable;
58 private TransitionDrawable mCurrentDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070059
Adam Cohen091440a2015-03-18 14:16:05 -070060 @Thunk boolean mWaitingForUninstall = false;
Michael Jurka24715c72013-07-08 18:03:46 -070061
Winson Chung4c98d922011-05-31 16:50:48 -070062 public DeleteDropTarget(Context context, AttributeSet attrs) {
63 this(context, attrs, 0);
64 }
65
66 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
67 super(context, attrs, defStyle);
68 }
69
70 @Override
71 protected void onFinishInflate() {
72 super.onFinishInflate();
73
74 // Get the drawable
Winson Chunga6427b12011-07-27 10:53:39 -070075 mOriginalTextColor = getTextColors();
Winson Chung4c98d922011-05-31 16:50:48 -070076
77 // Get the hover color
78 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070079 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080080 mUninstallDrawable = (TransitionDrawable)
81 r.getDrawable(R.drawable.uninstall_target_selector);
82 mRemoveDrawable = (TransitionDrawable) r.getDrawable(R.drawable.remove_target_selector);
83
84 mRemoveDrawable.setCrossFadeEnabled(true);
85 mUninstallDrawable.setCrossFadeEnabled(true);
86
87 // The current drawable is set to either the remove drawable or the uninstall drawable
88 // and is initially set to the remove drawable, as set in the layout xml.
Winson Chung947245b2012-05-15 16:34:19 -070089 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Winson Chung201bc822011-06-20 15:41:53 -070090
91 // Remove the text in the Phone UI in landscape
92 int orientation = getResources().getConfiguration().orientation;
93 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
Daniel Sandlere4f98912013-06-25 15:13:26 -040094 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070095 setText("");
Winson Chung201bc822011-06-20 15:41:53 -070096 }
97 }
Winson Chung4c98d922011-05-31 16:50:48 -070098 }
99
100 private boolean isAllAppsApplication(DragSource source, Object info) {
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000101 return source.supportsAppInfoDropTarget() && (info instanceof AppInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700102 }
103 private boolean isAllAppsWidget(DragSource source, Object info) {
Winson Chung11a49372012-04-27 15:12:38 -0700104 if (source instanceof AppsCustomizePagedView) {
105 if (info instanceof PendingAddItemInfo) {
106 PendingAddItemInfo addInfo = (PendingAddItemInfo) info;
107 switch (addInfo.itemType) {
108 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
109 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -0800110 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chung11a49372012-04-27 15:12:38 -0700111 return true;
112 }
113 }
114 }
115 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700116 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700117 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
118 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -0700119 }
Winson Chung4c98d922011-05-31 16:50:48 -0700120
Winson Chunga48487a2012-03-20 16:19:37 -0700121 private void setHoverColor() {
Adam Cohenfe9da812014-08-04 17:08:01 -0700122 if (mCurrentDrawable != null) {
123 mCurrentDrawable.startTransition(mTransitionDuration);
124 }
Winson Chunga48487a2012-03-20 16:19:37 -0700125 setTextColor(mHoverColor);
126 }
127 private void resetHoverColor() {
Adam Cohenfe9da812014-08-04 17:08:01 -0700128 if (mCurrentDrawable != null) {
129 mCurrentDrawable.resetTransition();
130 }
Winson Chunga48487a2012-03-20 16:19:37 -0700131 setTextColor(mOriginalTextColor);
132 }
133
Winson Chung4c98d922011-05-31 16:50:48 -0700134 @Override
135 public boolean acceptDrop(DragObject d) {
Adam Cohen7c4c5102013-06-14 17:42:35 -0700136 return willAcceptDrop(d.dragInfo);
137 }
138
139 public static boolean willAcceptDrop(Object info) {
140 if (info instanceof ItemInfo) {
141 ItemInfo item = (ItemInfo) info;
142 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET ||
Adam Cohen59400422014-03-05 18:07:04 -0800143 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET ||
Adam Cohen7c4c5102013-06-14 17:42:35 -0700144 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
145 return true;
146 }
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200147
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800148 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) {
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200149 return true;
150 }
151
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800152 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200153 item instanceof AppInfo) {
154 AppInfo appInfo = (AppInfo) info;
155 return (appInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
156 }
157
Michael Jurka9bd4d282013-09-05 19:21:31 +0200158 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
Michael Jurka1e2f4652013-07-08 18:03:46 -0700159 item instanceof ShortcutInfo) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800160 return true;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700161 }
Adam Cohen947dc542013-06-06 22:43:33 -0700162 }
Adam Cohen7c4c5102013-06-14 17:42:35 -0700163 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700164 }
165
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800166 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4c98d922011-05-31 16:50:48 -0700167 @Override
168 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700169 boolean isVisible = true;
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800170 boolean useUninstallLabel = isAllAppsApplication(source, info);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000171 boolean useDeleteLabel = !useUninstallLabel && source.supportsDeleteDropTarget();
Winson Chung4c98d922011-05-31 16:50:48 -0700172
Winson Chung4c98d922011-05-31 16:50:48 -0700173 // If we are dragging an application from AppsCustomize, only show the control if we can
Winson Chunge01af632013-09-26 10:43:20 -0700174 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case.
175 // Hide the delete target if it is a widget from AppsCustomize.
176 if (!willAcceptDrop(info) || isAllAppsWidget(source, info)) {
Adam Cohen947dc542013-06-06 22:43:33 -0700177 isVisible = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700178 }
Jason Monk950a4672014-07-15 09:22:36 -0400179 if (useUninstallLabel) {
Jason Monk6a2c7782014-09-02 13:13:24 -0400180 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
181 UserManager userManager = (UserManager)
182 getContext().getSystemService(Context.USER_SERVICE);
183 Bundle restrictions = userManager.getUserRestrictions();
184 if (restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
185 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false)) {
186 isVisible = false;
187 }
Jason Monk950a4672014-07-15 09:22:36 -0400188 }
189 }
Winson Chung4c98d922011-05-31 16:50:48 -0700190
Michael Jurkaaddcba62013-10-09 19:04:13 -0700191 if (useUninstallLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800192 setCompoundDrawablesRelativeWithIntrinsicBounds(mUninstallDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000193 } else if (useDeleteLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800194 setCompoundDrawablesRelativeWithIntrinsicBounds(mRemoveDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000195 } else {
196 isVisible = false;
Adam Cohenebea84d2011-11-09 17:20:41 -0800197 }
Winson Chung947245b2012-05-15 16:34:19 -0700198 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Adam Cohenebea84d2011-11-09 17:20:41 -0800199
Winson Chung4c98d922011-05-31 16:50:48 -0700200 mActive = isVisible;
Winson Chunga48487a2012-03-20 16:19:37 -0700201 resetHoverColor();
Winson Chunga6427b12011-07-27 10:53:39 -0700202 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000203 if (isVisible && getText().length() > 0) {
Michael Jurkaaddcba62013-10-09 19:04:13 -0700204 setText(useUninstallLabel ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700205 : R.string.delete_target_label);
206 }
207 }
208
209 @Override
210 public void onDragEnd() {
211 super.onDragEnd();
212 mActive = false;
213 }
214
215 public void onDragEnter(DragObject d) {
216 super.onDragEnter(d);
217
Winson Chunga48487a2012-03-20 16:19:37 -0700218 setHoverColor();
Winson Chung4c98d922011-05-31 16:50:48 -0700219 }
220
221 public void onDragExit(DragObject d) {
222 super.onDragExit(d);
223
Winson Chungaaa530a2011-07-11 21:06:30 -0700224 if (!d.dragComplete) {
Winson Chunga48487a2012-03-20 16:19:37 -0700225 resetHoverColor();
Winson Chung61967cb2012-02-28 18:11:33 -0800226 } else {
227 // Restore the hover color if we are deleting
228 d.dragView.setColor(mHoverColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700229 }
Winson Chung4c98d922011-05-31 16:50:48 -0700230 }
231
Adam Cohened66b2b2012-01-23 17:28:51 -0800232 private void animateToTrashAndCompleteDrop(final DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700233 final DragLayer dragLayer = mLauncher.getDragLayer();
234 final Rect from = new Rect();
Adam Cohened66b2b2012-01-23 17:28:51 -0800235 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Adam Cohenfe9da812014-08-04 17:08:01 -0700236
237 int width = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicWidth();
238 int height = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicHeight();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700239 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Adam Cohenfe9da812014-08-04 17:08:01 -0700240 width, height);
Michael Jurka1e2f4652013-07-08 18:03:46 -0700241 final float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800242
Adam Cohend4d7aa52011-07-19 21:47:37 -0700243 mSearchDropTargetBar.deferOnDragEnd();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700244 deferCompleteDropIfUninstalling(d);
245
Adam Cohend4d7aa52011-07-19 21:47:37 -0700246 Runnable onAnimationEndRunnable = new Runnable() {
247 @Override
248 public void run() {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700249 completeDrop(d);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700250 mSearchDropTargetBar.onDragEnd();
Sunny Goyal8498eb42014-10-16 12:08:41 -0700251 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700252 }
253 };
Winson Chung61967cb2012-02-28 18:11:33 -0800254 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700255 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800256 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800257 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700258 }
259
Michael Jurka1e2f4652013-07-08 18:03:46 -0700260 private void deferCompleteDropIfUninstalling(DragObject d) {
261 mWaitingForUninstall = false;
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200262 if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700263 if (d.dragSource instanceof Folder) {
264 ((Folder) d.dragSource).deferCompleteDropAfterUninstallActivity();
265 } else if (d.dragSource instanceof Workspace) {
266 ((Workspace) d.dragSource).deferCompleteDropAfterUninstallActivity();
267 }
268 mWaitingForUninstall = true;
269 }
270 }
Winson Chung4c98d922011-05-31 16:50:48 -0700271
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200272 private boolean isUninstallFromWorkspace(DragObject d) {
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200273 return false;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700274 }
275
Adam Cohen091440a2015-03-18 14:16:05 -0700276 @Thunk void completeDrop(DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700277 ItemInfo item = (ItemInfo) d.dragInfo;
278 boolean wasWaitingForUninstall = mWaitingForUninstall;
279 mWaitingForUninstall = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700280 if (isAllAppsApplication(d.dragSource, item)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800281 uninstallApp(mLauncher, (AppInfo) item);
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200282 } else if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700283 ShortcutInfo shortcut = (ShortcutInfo) item;
Kenny Guyd31df542014-06-30 15:12:11 +0100284 if (shortcut.intent != null && shortcut.intent.getComponent() != null) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200285 final ComponentName componentName = shortcut.intent.getComponent();
286 final DragSource dragSource = d.dragSource;
Kenny Guyd31df542014-06-30 15:12:11 +0100287 final UserHandleCompat user = shortcut.user;
288 mWaitingForUninstall = mLauncher.startApplicationUninstallActivity(
289 componentName, shortcut.flags, user);
Michael Jurkaf3007582013-08-21 14:33:57 +0200290 if (mWaitingForUninstall) {
291 final Runnable checkIfUninstallWasSuccess = new Runnable() {
292 @Override
293 public void run() {
294 mWaitingForUninstall = false;
295 String packageName = componentName.getPackageName();
Kenny Guyed131872014-04-30 03:02:21 +0100296 boolean uninstallSuccessful = !AllAppsList.packageHasActivities(
Kenny Guyd31df542014-06-30 15:12:11 +0100297 getContext(), packageName, user);
Michael Jurkaf3007582013-08-21 14:33:57 +0200298 if (dragSource instanceof Folder) {
299 ((Folder) dragSource).
300 onUninstallActivityReturned(uninstallSuccessful);
301 } else if (dragSource instanceof Workspace) {
302 ((Workspace) dragSource).
303 onUninstallActivityReturned(uninstallSuccessful);
304 }
305 }
306 };
307 mLauncher.addOnResumeCallback(checkIfUninstallWasSuccess);
308 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700309 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800310 } else if (isDragSourceWorkspaceOrFolder(d)) {
311 removeWorkspaceOrFolderItem(mLauncher, item, null);
Winson Chung4c98d922011-05-31 16:50:48 -0700312 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700313 if (wasWaitingForUninstall && !mWaitingForUninstall) {
314 if (d.dragSource instanceof Folder) {
315 ((Folder) d.dragSource).onUninstallActivityReturned(false);
316 } else if (d.dragSource instanceof Workspace) {
317 ((Workspace) d.dragSource).onUninstallActivityReturned(false);
318 }
319 }
Winson Chung4c98d922011-05-31 16:50:48 -0700320 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700321
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800322 public static void uninstallApp(Launcher launcher, AppInfo info) {
323 launcher.startApplicationUninstallActivity(info.componentName, info.flags, info.user);
324 }
325
326 /**
327 * Removes the item from the workspace. If the view is not null, it also removes the view.
328 * @return true if the item was removed.
329 */
330 public static boolean removeWorkspaceOrFolderItem(Launcher launcher, ItemInfo item, View view) {
331 if (item instanceof ShortcutInfo) {
332 LauncherModel.deleteItemFromDatabase(launcher, item);
333 } else if (item instanceof FolderInfo) {
334 FolderInfo folder = (FolderInfo) item;
335 launcher.removeFolder(folder);
336 LauncherModel.deleteFolderContentsFromDatabase(launcher, folder);
337 } else if (item instanceof LauncherAppWidgetInfo) {
338 final LauncherAppWidgetInfo widget = (LauncherAppWidgetInfo) item;
339
340 // Remove the widget from the workspace
341 launcher.removeAppWidget(widget);
342 LauncherModel.deleteItemFromDatabase(launcher, widget);
343
344 final LauncherAppWidgetHost appWidgetHost = launcher.getAppWidgetHost();
345
346 if (appWidgetHost != null && !widget.isCustomWidget()
347 && widget.isWidgetIdValid()) {
348 // Deleting an app widget ID is a void call but writes to disk before returning
349 // to the caller...
350 new AsyncTask<Void, Void, Void>() {
351 public Void doInBackground(Void ... args) {
352 appWidgetHost.deleteAppWidgetId(widget.appWidgetId);
353 return null;
354 }
355 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
356 }
357 } else {
358 return false;
359 }
360
361 if (view != null) {
362 launcher.getWorkspace().removeWorkspaceItem(view);
363 launcher.getWorkspace().stripEmptyScreens();
364 }
365 return true;
366 }
367
Adam Cohend4d7aa52011-07-19 21:47:37 -0700368 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}