blob: 1ada1a912f07f039b61dbbb777d37c0b1830c7cc [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
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800147 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) {
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200148 return true;
149 }
150
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800151 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200152 item instanceof AppInfo) {
153 AppInfo appInfo = (AppInfo) info;
154 return (appInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
155 }
156
Michael Jurka9bd4d282013-09-05 19:21:31 +0200157 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
Michael Jurka1e2f4652013-07-08 18:03:46 -0700158 item instanceof ShortcutInfo) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800159 return true;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700160 }
Adam Cohen947dc542013-06-06 22:43:33 -0700161 }
Adam Cohen7c4c5102013-06-14 17:42:35 -0700162 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700163 }
164
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800165 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4c98d922011-05-31 16:50:48 -0700166 @Override
167 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700168 boolean isVisible = true;
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800169 boolean useUninstallLabel = isAllAppsApplication(source, info);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000170 boolean useDeleteLabel = !useUninstallLabel && source.supportsDeleteDropTarget();
Winson Chung4c98d922011-05-31 16:50:48 -0700171
Winson Chung4c98d922011-05-31 16:50:48 -0700172 // If we are dragging an application from AppsCustomize, only show the control if we can
Winson Chunge01af632013-09-26 10:43:20 -0700173 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case.
174 // Hide the delete target if it is a widget from AppsCustomize.
175 if (!willAcceptDrop(info) || isAllAppsWidget(source, info)) {
Adam Cohen947dc542013-06-06 22:43:33 -0700176 isVisible = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700177 }
Jason Monk950a4672014-07-15 09:22:36 -0400178 if (useUninstallLabel) {
Jason Monk6a2c7782014-09-02 13:13:24 -0400179 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
180 UserManager userManager = (UserManager)
181 getContext().getSystemService(Context.USER_SERVICE);
182 Bundle restrictions = userManager.getUserRestrictions();
183 if (restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
184 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false)) {
185 isVisible = false;
186 }
Jason Monk950a4672014-07-15 09:22:36 -0400187 }
188 }
Winson Chung4c98d922011-05-31 16:50:48 -0700189
Michael Jurkaaddcba62013-10-09 19:04:13 -0700190 if (useUninstallLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800191 setCompoundDrawablesRelativeWithIntrinsicBounds(mUninstallDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000192 } else if (useDeleteLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800193 setCompoundDrawablesRelativeWithIntrinsicBounds(mRemoveDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000194 } else {
195 isVisible = false;
Adam Cohenebea84d2011-11-09 17:20:41 -0800196 }
Winson Chung947245b2012-05-15 16:34:19 -0700197 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Adam Cohenebea84d2011-11-09 17:20:41 -0800198
Winson Chung4c98d922011-05-31 16:50:48 -0700199 mActive = isVisible;
Winson Chunga48487a2012-03-20 16:19:37 -0700200 resetHoverColor();
Winson Chunga6427b12011-07-27 10:53:39 -0700201 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000202 if (isVisible && getText().length() > 0) {
Michael Jurkaaddcba62013-10-09 19:04:13 -0700203 setText(useUninstallLabel ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700204 : R.string.delete_target_label);
205 }
206 }
207
208 @Override
209 public void onDragEnd() {
210 super.onDragEnd();
211 mActive = false;
212 }
213
214 public void onDragEnter(DragObject d) {
215 super.onDragEnter(d);
216
Winson Chunga48487a2012-03-20 16:19:37 -0700217 setHoverColor();
Winson Chung4c98d922011-05-31 16:50:48 -0700218 }
219
220 public void onDragExit(DragObject d) {
221 super.onDragExit(d);
222
Winson Chungaaa530a2011-07-11 21:06:30 -0700223 if (!d.dragComplete) {
Winson Chunga48487a2012-03-20 16:19:37 -0700224 resetHoverColor();
Winson Chung61967cb2012-02-28 18:11:33 -0800225 } else {
226 // Restore the hover color if we are deleting
227 d.dragView.setColor(mHoverColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700228 }
Winson Chung4c98d922011-05-31 16:50:48 -0700229 }
230
Adam Cohened66b2b2012-01-23 17:28:51 -0800231 private void animateToTrashAndCompleteDrop(final DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700232 final DragLayer dragLayer = mLauncher.getDragLayer();
233 final Rect from = new Rect();
Adam Cohened66b2b2012-01-23 17:28:51 -0800234 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Adam Cohenfe9da812014-08-04 17:08:01 -0700235
236 int width = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicWidth();
237 int height = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicHeight();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700238 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Adam Cohenfe9da812014-08-04 17:08:01 -0700239 width, height);
Michael Jurka1e2f4652013-07-08 18:03:46 -0700240 final float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800241
Adam Cohend4d7aa52011-07-19 21:47:37 -0700242 mSearchDropTargetBar.deferOnDragEnd();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700243 deferCompleteDropIfUninstalling(d);
244
Adam Cohend4d7aa52011-07-19 21:47:37 -0700245 Runnable onAnimationEndRunnable = new Runnable() {
246 @Override
247 public void run() {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700248 completeDrop(d);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700249 mSearchDropTargetBar.onDragEnd();
Sunny Goyal8498eb42014-10-16 12:08:41 -0700250 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700251 }
252 };
Winson Chung61967cb2012-02-28 18:11:33 -0800253 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700254 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800255 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800256 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700257 }
258
Michael Jurka1e2f4652013-07-08 18:03:46 -0700259 private void deferCompleteDropIfUninstalling(DragObject d) {
260 mWaitingForUninstall = false;
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200261 if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700262 if (d.dragSource instanceof Folder) {
263 ((Folder) d.dragSource).deferCompleteDropAfterUninstallActivity();
264 } else if (d.dragSource instanceof Workspace) {
265 ((Workspace) d.dragSource).deferCompleteDropAfterUninstallActivity();
266 }
267 mWaitingForUninstall = true;
268 }
269 }
Winson Chung4c98d922011-05-31 16:50:48 -0700270
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200271 private boolean isUninstallFromWorkspace(DragObject d) {
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200272 return false;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700273 }
274
Michael Jurkaf3007582013-08-21 14:33:57 +0200275 private void completeDrop(DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700276 ItemInfo item = (ItemInfo) d.dragInfo;
277 boolean wasWaitingForUninstall = mWaitingForUninstall;
278 mWaitingForUninstall = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700279 if (isAllAppsApplication(d.dragSource, item)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800280 uninstallApp(mLauncher, (AppInfo) item);
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200281 } else if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700282 ShortcutInfo shortcut = (ShortcutInfo) item;
Kenny Guyd31df542014-06-30 15:12:11 +0100283 if (shortcut.intent != null && shortcut.intent.getComponent() != null) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200284 final ComponentName componentName = shortcut.intent.getComponent();
285 final DragSource dragSource = d.dragSource;
Kenny Guyd31df542014-06-30 15:12:11 +0100286 final UserHandleCompat user = shortcut.user;
287 mWaitingForUninstall = mLauncher.startApplicationUninstallActivity(
288 componentName, shortcut.flags, user);
Michael Jurkaf3007582013-08-21 14:33:57 +0200289 if (mWaitingForUninstall) {
290 final Runnable checkIfUninstallWasSuccess = new Runnable() {
291 @Override
292 public void run() {
293 mWaitingForUninstall = false;
294 String packageName = componentName.getPackageName();
Kenny Guyed131872014-04-30 03:02:21 +0100295 boolean uninstallSuccessful = !AllAppsList.packageHasActivities(
Kenny Guyd31df542014-06-30 15:12:11 +0100296 getContext(), packageName, user);
Michael Jurkaf3007582013-08-21 14:33:57 +0200297 if (dragSource instanceof Folder) {
298 ((Folder) dragSource).
299 onUninstallActivityReturned(uninstallSuccessful);
300 } else if (dragSource instanceof Workspace) {
301 ((Workspace) dragSource).
302 onUninstallActivityReturned(uninstallSuccessful);
303 }
304 }
305 };
306 mLauncher.addOnResumeCallback(checkIfUninstallWasSuccess);
307 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700308 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800309 } else if (isDragSourceWorkspaceOrFolder(d)) {
310 removeWorkspaceOrFolderItem(mLauncher, item, null);
Winson Chung4c98d922011-05-31 16:50:48 -0700311 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700312 if (wasWaitingForUninstall && !mWaitingForUninstall) {
313 if (d.dragSource instanceof Folder) {
314 ((Folder) d.dragSource).onUninstallActivityReturned(false);
315 } else if (d.dragSource instanceof Workspace) {
316 ((Workspace) d.dragSource).onUninstallActivityReturned(false);
317 }
318 }
Winson Chung4c98d922011-05-31 16:50:48 -0700319 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700320
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800321 public static void uninstallApp(Launcher launcher, AppInfo info) {
322 launcher.startApplicationUninstallActivity(info.componentName, info.flags, info.user);
323 }
324
325 /**
326 * Removes the item from the workspace. If the view is not null, it also removes the view.
327 * @return true if the item was removed.
328 */
329 public static boolean removeWorkspaceOrFolderItem(Launcher launcher, ItemInfo item, View view) {
330 if (item instanceof ShortcutInfo) {
331 LauncherModel.deleteItemFromDatabase(launcher, item);
332 } else if (item instanceof FolderInfo) {
333 FolderInfo folder = (FolderInfo) item;
334 launcher.removeFolder(folder);
335 LauncherModel.deleteFolderContentsFromDatabase(launcher, folder);
336 } else if (item instanceof LauncherAppWidgetInfo) {
337 final LauncherAppWidgetInfo widget = (LauncherAppWidgetInfo) item;
338
339 // Remove the widget from the workspace
340 launcher.removeAppWidget(widget);
341 LauncherModel.deleteItemFromDatabase(launcher, widget);
342
343 final LauncherAppWidgetHost appWidgetHost = launcher.getAppWidgetHost();
344
345 if (appWidgetHost != null && !widget.isCustomWidget()
346 && widget.isWidgetIdValid()) {
347 // Deleting an app widget ID is a void call but writes to disk before returning
348 // to the caller...
349 new AsyncTask<Void, Void, Void>() {
350 public Void doInBackground(Void ... args) {
351 appWidgetHost.deleteAppWidgetId(widget.appWidgetId);
352 return null;
353 }
354 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
355 }
356 } else {
357 return false;
358 }
359
360 if (view != null) {
361 launcher.getWorkspace().removeWorkspaceItem(view);
362 launcher.getWorkspace().stripEmptyScreens();
363 }
364 return true;
365 }
366
Adam Cohend4d7aa52011-07-19 21:47:37 -0700367 public void onDrop(DragObject d) {
368 animateToTrashAndCompleteDrop(d);
369 }
Winson Chung043f2af2012-03-01 16:09:54 -0800370
371 /**
372 * Creates an animation from the current drag view to the delete trash icon.
373 */
374 private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer,
375 DragObject d, PointF vel, ViewConfiguration config) {
Adam Cohenfe9da812014-08-04 17:08:01 -0700376
377 int width = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicWidth();
378 int height = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicHeight();
Winson Chung043f2af2012-03-01 16:09:54 -0800379 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Adam Cohenfe9da812014-08-04 17:08:01 -0700380 width, height);
Winson Chung043f2af2012-03-01 16:09:54 -0800381 final Rect from = new Rect();
382 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
383
384 // Calculate how far along the velocity vector we should put the intermediate point on
385 // the bezier curve
386 float velocity = Math.abs(vel.length());
387 float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
388 int offsetY = (int) (-from.top * vp);
389 int offsetX = (int) (offsetY / (vel.y / vel.x));
390 final float y2 = from.top + offsetY; // intermediate t/l
391 final float x2 = from.left + offsetX;
392 final float x1 = from.left; // drag view t/l
393 final float y1 = from.top;
394 final float x3 = to.left; // delete target t/l
395 final float y3 = to.top;
396
397 final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {
398 @Override
399 public float getInterpolation(float t) {
400 return t * t * t * t * t * t * t * t;
401 }
402 };
403 return new AnimatorUpdateListener() {
404 @Override
405 public void onAnimationUpdate(ValueAnimator animation) {
406 final DragView dragView = (DragView) dragLayer.getAnimatedView();
407 float t = ((Float) animation.getAnimatedValue()).floatValue();
408 float tp = scaleAlphaInterpolator.getInterpolation(t);
409 float initialScale = dragView.getInitialScale();
410 float finalAlpha = 0.5f;
411 float scale = dragView.getScaleX();
412 float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
413 float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
414 float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) +
415 (t * t) * x3;
416 float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) +
417 (t * t) * y3;
418
419 dragView.setTranslationX(x);
420 dragView.setTranslationY(y);
421 dragView.setScaleX(initialScale * (1f - tp));
422 dragView.setScaleY(initialScale * (1f - tp));
423 dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
424 }
425 };
426 }
427
428 /**
429 * Creates an animation from the current drag view along its current velocity vector.
430 * For this animation, the alpha runs for a fixed duration and we update the position
431 * progressively.
432 */
433 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
Winson Chung043f2af2012-03-01 16:09:54 -0800434 private DragLayer mDragLayer;
435 private PointF mVelocity;
436 private Rect mFrom;
437 private long mPrevTime;
438 private boolean mHasOffsetForScale;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700439 private float mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800440
Winson Chung9658b1e2012-04-09 18:30:07 -0700441 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
Winson Chung043f2af2012-03-01 16:09:54 -0800442
443 public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from,
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700444 long startTime, float friction) {
Winson Chung043f2af2012-03-01 16:09:54 -0800445 mDragLayer = dragLayer;
446 mVelocity = vel;
447 mFrom = from;
448 mPrevTime = startTime;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700449 mFriction = 1f - (dragLayer.getResources().getDisplayMetrics().density * friction);
Winson Chung043f2af2012-03-01 16:09:54 -0800450 }
451
452 @Override
453 public void onAnimationUpdate(ValueAnimator animation) {
454 final DragView dragView = (DragView) mDragLayer.getAnimatedView();
455 float t = ((Float) animation.getAnimatedValue()).floatValue();
456 long curTime = AnimationUtils.currentAnimationTimeMillis();
457
458 if (!mHasOffsetForScale) {
459 mHasOffsetForScale = true;
460 float scale = dragView.getScaleX();
461 float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f;
462 float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f;
463
464 mFrom.left += xOffset;
465 mFrom.top += yOffset;
466 }
467
468 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
469 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
470
471 dragView.setTranslationX(mFrom.left);
472 dragView.setTranslationY(mFrom.top);
473 dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
474
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700475 mVelocity.x *= mFriction;
476 mVelocity.y *= mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800477 mPrevTime = curTime;
478 }
479 };
480 private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer,
481 DragObject d, PointF vel, final long startTime, final int duration,
482 ViewConfiguration config) {
483 final Rect from = new Rect();
484 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
485
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700486 return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime,
487 FLING_TO_DELETE_FRICTION);
Winson Chung043f2af2012-03-01 16:09:54 -0800488 }
489
490 public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
Winson Chunga48487a2012-03-20 16:19:37 -0700491 final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView;
492
Winson Chung043f2af2012-03-01 16:09:54 -0800493 // Don't highlight the icon as it's animating
494 d.dragView.setColor(0);
495 d.dragView.updateInitialScaleToCurrentScale();
Winson Chunga48487a2012-03-20 16:19:37 -0700496 // Don't highlight the target if we are flinging from AllApps
497 if (isAllApps) {
498 resetHoverColor();
499 }
Winson Chung043f2af2012-03-01 16:09:54 -0800500
501 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
502 // Defer animating out the drop target if we are animating to it
503 mSearchDropTargetBar.deferOnDragEnd();
504 mSearchDropTargetBar.finishAnimations();
505 }
506
507 final ViewConfiguration config = ViewConfiguration.get(mLauncher);
508 final DragLayer dragLayer = mLauncher.getDragLayer();
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700509 final int duration = FLING_DELETE_ANIMATION_DURATION;
Winson Chung043f2af2012-03-01 16:09:54 -0800510 final long startTime = AnimationUtils.currentAnimationTimeMillis();
511
512 // NOTE: Because it takes time for the first frame of animation to actually be
513 // called and we expect the animation to be a continuation of the fling, we have
514 // to account for the time that has elapsed since the fling finished. And since
515 // we don't have a startDelay, we will always get call to update when we call
516 // start() (which we want to ignore).
517 final TimeInterpolator tInterpolator = new TimeInterpolator() {
518 private int mCount = -1;
519 private float mOffset = 0f;
520
521 @Override
522 public float getInterpolation(float t) {
523 if (mCount < 0) {
524 mCount++;
525 } else if (mCount == 0) {
526 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
527 startTime) / duration);
528 mCount++;
529 }
530 return Math.min(1f, mOffset + t);
531 }
532 };
533 AnimatorUpdateListener updateCb = null;
534 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
535 updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
536 } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
537 updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime,
538 duration, config);
539 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700540 deferCompleteDropIfUninstalling(d);
541
Winson Chung043f2af2012-03-01 16:09:54 -0800542 Runnable onAnimationEndRunnable = new Runnable() {
543 @Override
544 public void run() {
Winson Chunga48487a2012-03-20 16:19:37 -0700545 // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up
546 // itself, otherwise, complete the drop to initiate the deletion process
547 if (!isAllApps) {
548 mLauncher.exitSpringLoadedDragMode();
549 completeDrop(d);
550 }
551 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800552 }
553 };
554 dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable,
555 DragLayer.ANIMATION_END_DISAPPEAR, null);
556 }
Winson Chung4c98d922011-05-31 16:50:48 -0700557}