blob: 4c3388e053b8c7872429f97f0ba4765aea5ed399 [file] [log] [blame]
Winson Chung4c98d922011-05-31 16:50:48 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung4c98d922011-05-31 16:50:48 -070018
Winson Chung043f2af2012-03-01 16:09:54 -080019import android.animation.TimeInterpolator;
20import android.animation.ValueAnimator;
21import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka1e2f4652013-07-08 18:03:46 -070022import android.content.ComponentName;
Winson Chung4c98d922011-05-31 16:50:48 -070023import android.content.Context;
Michael Jurka5e7af5d2013-09-14 18:35:38 +020024import android.content.Intent;
Michael Jurka1e2f4652013-07-08 18:03:46 -070025import android.content.pm.ResolveInfo;
Winson Chunga62e9fd2011-07-11 15:20:48 -070026import android.content.res.ColorStateList;
Winson Chung201bc822011-06-20 15:41:53 -070027import android.content.res.Configuration;
Winson Chung4c98d922011-05-31 16:50:48 -070028import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080029import android.graphics.PointF;
Adam Cohend4d7aa52011-07-19 21:47:37 -070030import android.graphics.Rect;
Winson Chung967289b2011-06-30 18:09:30 -070031import android.graphics.drawable.TransitionDrawable;
Michael Jurka43467462013-11-14 12:03:58 +010032import android.os.AsyncTask;
Winson Chung4c98d922011-05-31 16:50:48 -070033import android.util.AttributeSet;
34import android.view.View;
Winson Chung043f2af2012-03-01 16:09:54 -080035import android.view.ViewConfiguration;
Winson Chunga6427b12011-07-27 10:53:39 -070036import android.view.ViewGroup;
Winson Chung043f2af2012-03-01 16:09:54 -080037import android.view.animation.AnimationUtils;
Adam Cohend4d7aa52011-07-19 21:47:37 -070038import android.view.animation.DecelerateInterpolator;
Winson Chung61967cb2012-02-28 18:11:33 -080039import android.view.animation.LinearInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070040
Kenny Guyed131872014-04-30 03:02:21 +010041import com.android.launcher3.compat.LauncherAppsCompat;
42import com.android.launcher3.compat.UserHandleCompat;
43
Michael Jurka1e2f4652013-07-08 18:03:46 -070044import java.util.List;
Michael Jurka5e7af5d2013-09-14 18:35:38 +020045import java.util.Set;
Michael Jurka1e2f4652013-07-08 18:03:46 -070046
Winson Chung61fa4192011-06-12 15:15:29 -070047public class DeleteDropTarget extends ButtonDropTarget {
Winson Chung043f2af2012-03-01 16:09:54 -080048 private static int DELETE_ANIMATION_DURATION = 285;
Winson Chung6e1bdaf2012-05-29 17:03:45 -070049 private static int FLING_DELETE_ANIMATION_DURATION = 350;
50 private static float FLING_TO_DELETE_FRICTION = 0.035f;
Winson Chung043f2af2012-03-01 16:09:54 -080051 private static int MODE_FLING_DELETE_TO_TRASH = 0;
52 private static int MODE_FLING_DELETE_ALONG_VECTOR = 1;
Winson Chung4c98d922011-05-31 16:50:48 -070053
Winson Chung043f2af2012-03-01 16:09:54 -080054 private final int mFlingDeleteMode = MODE_FLING_DELETE_ALONG_VECTOR;
55
Winson Chunga62e9fd2011-07-11 15:20:48 -070056 private ColorStateList mOriginalTextColor;
Adam Cohenebea84d2011-11-09 17:20:41 -080057 private TransitionDrawable mUninstallDrawable;
58 private TransitionDrawable mRemoveDrawable;
59 private TransitionDrawable mCurrentDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070060
Michael Jurka24715c72013-07-08 18:03:46 -070061 private boolean mWaitingForUninstall = false;
62
Winson Chung4c98d922011-05-31 16:50:48 -070063 public DeleteDropTarget(Context context, AttributeSet attrs) {
64 this(context, attrs, 0);
65 }
66
67 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
68 super(context, attrs, defStyle);
69 }
70
71 @Override
72 protected void onFinishInflate() {
73 super.onFinishInflate();
74
75 // Get the drawable
Winson Chunga6427b12011-07-27 10:53:39 -070076 mOriginalTextColor = getTextColors();
Winson Chung4c98d922011-05-31 16:50:48 -070077
78 // Get the hover color
79 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070080 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080081 mUninstallDrawable = (TransitionDrawable)
82 r.getDrawable(R.drawable.uninstall_target_selector);
83 mRemoveDrawable = (TransitionDrawable) r.getDrawable(R.drawable.remove_target_selector);
84
85 mRemoveDrawable.setCrossFadeEnabled(true);
86 mUninstallDrawable.setCrossFadeEnabled(true);
87
88 // The current drawable is set to either the remove drawable or the uninstall drawable
89 // and is initially set to the remove drawable, as set in the layout xml.
Winson Chung947245b2012-05-15 16:34:19 -070090 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Winson Chung201bc822011-06-20 15:41:53 -070091
92 // Remove the text in the Phone UI in landscape
93 int orientation = getResources().getConfiguration().orientation;
94 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
Daniel Sandlere4f98912013-06-25 15:13:26 -040095 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070096 setText("");
Winson Chung201bc822011-06-20 15:41:53 -070097 }
98 }
Winson Chung4c98d922011-05-31 16:50:48 -070099 }
100
101 private boolean isAllAppsApplication(DragSource source, Object info) {
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000102 return source.supportsAppInfoDropTarget() && (info instanceof AppInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700103 }
104 private boolean isAllAppsWidget(DragSource source, Object info) {
Winson Chung11a49372012-04-27 15:12:38 -0700105 if (source instanceof AppsCustomizePagedView) {
106 if (info instanceof PendingAddItemInfo) {
107 PendingAddItemInfo addInfo = (PendingAddItemInfo) info;
108 switch (addInfo.itemType) {
109 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
110 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
111 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 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700120 private boolean isWorkspaceOrFolderApplication(DragObject d) {
121 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
122 }
123 private boolean isWorkspaceOrFolderWidget(DragObject d) {
124 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700125 }
126 private boolean isWorkspaceFolder(DragObject d) {
127 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
128 }
129
Winson Chunga48487a2012-03-20 16:19:37 -0700130 private void setHoverColor() {
131 mCurrentDrawable.startTransition(mTransitionDuration);
132 setTextColor(mHoverColor);
133 }
134 private void resetHoverColor() {
135 mCurrentDrawable.resetTransition();
136 setTextColor(mOriginalTextColor);
137 }
138
Winson Chung4c98d922011-05-31 16:50:48 -0700139 @Override
140 public boolean acceptDrop(DragObject d) {
Adam Cohen7c4c5102013-06-14 17:42:35 -0700141 return willAcceptDrop(d.dragInfo);
142 }
143
144 public static boolean willAcceptDrop(Object info) {
145 if (info instanceof ItemInfo) {
146 ItemInfo item = (ItemInfo) info;
147 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET ||
148 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
149 return true;
150 }
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200151
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_FOLDER) {
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200154 return true;
155 }
156
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800157 if (!LauncherAppState.isDisableAllApps() &&
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200158 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
159 item instanceof AppInfo) {
160 AppInfo appInfo = (AppInfo) info;
161 return (appInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
162 }
163
Michael Jurka9bd4d282013-09-05 19:21:31 +0200164 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
Michael Jurka1e2f4652013-07-08 18:03:46 -0700165 item instanceof ShortcutInfo) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800166 if (LauncherAppState.isDisableAllApps()) {
Michael Jurka9bd4d282013-09-05 19:21:31 +0200167 ShortcutInfo shortcutInfo = (ShortcutInfo) info;
168 return (shortcutInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
169 } else {
170 return true;
171 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700172 }
Adam Cohen947dc542013-06-06 22:43:33 -0700173 }
Adam Cohen7c4c5102013-06-14 17:42:35 -0700174 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700175 }
176
177 @Override
178 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700179 boolean isVisible = true;
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800180 boolean useUninstallLabel = !LauncherAppState.isDisableAllApps() &&
Michael Jurkaaddcba62013-10-09 19:04:13 -0700181 isAllAppsApplication(source, info);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000182 boolean useDeleteLabel = !useUninstallLabel && source.supportsDeleteDropTarget();
Winson Chung4c98d922011-05-31 16:50:48 -0700183
Winson Chung4c98d922011-05-31 16:50:48 -0700184 // If we are dragging an application from AppsCustomize, only show the control if we can
Winson Chunge01af632013-09-26 10:43:20 -0700185 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case.
186 // Hide the delete target if it is a widget from AppsCustomize.
187 if (!willAcceptDrop(info) || isAllAppsWidget(source, info)) {
Adam Cohen947dc542013-06-06 22:43:33 -0700188 isVisible = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700189 }
190
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);
Michael Jurka1e2f4652013-07-08 18:03:46 -0700236 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Winson Chung61967cb2012-02-28 18:11:33 -0800237 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
Michael Jurka1e2f4652013-07-08 18:03:46 -0700238 final float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800239
Adam Cohend4d7aa52011-07-19 21:47:37 -0700240 mSearchDropTargetBar.deferOnDragEnd();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700241 deferCompleteDropIfUninstalling(d);
242
Adam Cohend4d7aa52011-07-19 21:47:37 -0700243 Runnable onAnimationEndRunnable = new Runnable() {
244 @Override
245 public void run() {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700246 completeDrop(d);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700247 mSearchDropTargetBar.onDragEnd();
248 mLauncher.exitSpringLoadedDragMode();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700249 }
250 };
Winson Chung61967cb2012-02-28 18:11:33 -0800251 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700252 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800253 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800254 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700255 }
256
Michael Jurka1e2f4652013-07-08 18:03:46 -0700257 private void deferCompleteDropIfUninstalling(DragObject d) {
258 mWaitingForUninstall = false;
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200259 if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700260 if (d.dragSource instanceof Folder) {
261 ((Folder) d.dragSource).deferCompleteDropAfterUninstallActivity();
262 } else if (d.dragSource instanceof Workspace) {
263 ((Workspace) d.dragSource).deferCompleteDropAfterUninstallActivity();
264 }
265 mWaitingForUninstall = true;
266 }
267 }
Winson Chung4c98d922011-05-31 16:50:48 -0700268
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200269 private boolean isUninstallFromWorkspace(DragObject d) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800270 if (LauncherAppState.isDisableAllApps() && isWorkspaceOrFolderApplication(d)) {
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200271 ShortcutInfo shortcut = (ShortcutInfo) d.dragInfo;
Nilesh Agrawaldff0bfe2013-12-17 15:20:50 -0800272 // Only allow manifest shortcuts to initiate an un-install.
273 return !InstallShortcutReceiver.isValidShortcutLaunchIntent(shortcut.intent);
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200274 }
275 return false;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700276 }
277
Michael Jurkaf3007582013-08-21 14:33:57 +0200278 private void completeDrop(DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700279 ItemInfo item = (ItemInfo) d.dragInfo;
280 boolean wasWaitingForUninstall = mWaitingForUninstall;
281 mWaitingForUninstall = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700282 if (isAllAppsApplication(d.dragSource, item)) {
283 // Uninstall the application if it is being dragged from AppsCustomize
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200284 AppInfo appInfo = (AppInfo) item;
Kenny Guyd31df542014-06-30 15:12:11 +0100285 mLauncher.startApplicationUninstallActivity(appInfo.componentName, appInfo.flags,
286 appInfo.user);
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200287 } else if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700288 ShortcutInfo shortcut = (ShortcutInfo) item;
Kenny Guyd31df542014-06-30 15:12:11 +0100289 if (shortcut.intent != null && shortcut.intent.getComponent() != null) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200290 final ComponentName componentName = shortcut.intent.getComponent();
291 final DragSource dragSource = d.dragSource;
Kenny Guyd31df542014-06-30 15:12:11 +0100292 final UserHandleCompat user = shortcut.user;
293 mWaitingForUninstall = mLauncher.startApplicationUninstallActivity(
294 componentName, shortcut.flags, user);
Michael Jurkaf3007582013-08-21 14:33:57 +0200295 if (mWaitingForUninstall) {
296 final Runnable checkIfUninstallWasSuccess = new Runnable() {
297 @Override
298 public void run() {
299 mWaitingForUninstall = false;
300 String packageName = componentName.getPackageName();
Kenny Guyed131872014-04-30 03:02:21 +0100301 boolean uninstallSuccessful = !AllAppsList.packageHasActivities(
Kenny Guyd31df542014-06-30 15:12:11 +0100302 getContext(), packageName, user);
Michael Jurkaf3007582013-08-21 14:33:57 +0200303 if (dragSource instanceof Folder) {
304 ((Folder) dragSource).
305 onUninstallActivityReturned(uninstallSuccessful);
306 } else if (dragSource instanceof Workspace) {
307 ((Workspace) dragSource).
308 onUninstallActivityReturned(uninstallSuccessful);
309 }
310 }
311 };
312 mLauncher.addOnResumeCallback(checkIfUninstallWasSuccess);
313 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700314 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700315 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700316 LauncherModel.deleteItemFromDatabase(mLauncher, item);
317 } else if (isWorkspaceFolder(d)) {
318 // Remove the folder from the workspace and delete the contents from launcher model
319 FolderInfo folderInfo = (FolderInfo) item;
320 mLauncher.removeFolder(folderInfo);
321 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700322 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700323 // Remove the widget from the workspace
324 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
325 LauncherModel.deleteItemFromDatabase(mLauncher, item);
326
327 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
328 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
Sunny Goyal651077b2014-06-30 14:15:31 -0700329 if ((appWidgetHost != null) && launcherAppWidgetInfo.isWidgetIdValid()) {
Winson Chung4c98d922011-05-31 16:50:48 -0700330 // Deleting an app widget ID is a void call but writes to disk before returning
331 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +0100332 new AsyncTask<Void, Void, Void>() {
333 public Void doInBackground(Void ... args) {
Winson Chung4c98d922011-05-31 16:50:48 -0700334 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +0100335 return null;
Winson Chung4c98d922011-05-31 16:50:48 -0700336 }
Michael Jurka43467462013-11-14 12:03:58 +0100337 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chung4c98d922011-05-31 16:50:48 -0700338 }
339 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700340 if (wasWaitingForUninstall && !mWaitingForUninstall) {
341 if (d.dragSource instanceof Folder) {
342 ((Folder) d.dragSource).onUninstallActivityReturned(false);
343 } else if (d.dragSource instanceof Workspace) {
344 ((Workspace) d.dragSource).onUninstallActivityReturned(false);
345 }
346 }
Winson Chung4c98d922011-05-31 16:50:48 -0700347 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700348
349 public void onDrop(DragObject d) {
350 animateToTrashAndCompleteDrop(d);
351 }
Winson Chung043f2af2012-03-01 16:09:54 -0800352
353 /**
354 * Creates an animation from the current drag view to the delete trash icon.
355 */
356 private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer,
357 DragObject d, PointF vel, ViewConfiguration config) {
358 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
359 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
360 final Rect from = new Rect();
361 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
362
363 // Calculate how far along the velocity vector we should put the intermediate point on
364 // the bezier curve
365 float velocity = Math.abs(vel.length());
366 float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
367 int offsetY = (int) (-from.top * vp);
368 int offsetX = (int) (offsetY / (vel.y / vel.x));
369 final float y2 = from.top + offsetY; // intermediate t/l
370 final float x2 = from.left + offsetX;
371 final float x1 = from.left; // drag view t/l
372 final float y1 = from.top;
373 final float x3 = to.left; // delete target t/l
374 final float y3 = to.top;
375
376 final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {
377 @Override
378 public float getInterpolation(float t) {
379 return t * t * t * t * t * t * t * t;
380 }
381 };
382 return new AnimatorUpdateListener() {
383 @Override
384 public void onAnimationUpdate(ValueAnimator animation) {
385 final DragView dragView = (DragView) dragLayer.getAnimatedView();
386 float t = ((Float) animation.getAnimatedValue()).floatValue();
387 float tp = scaleAlphaInterpolator.getInterpolation(t);
388 float initialScale = dragView.getInitialScale();
389 float finalAlpha = 0.5f;
390 float scale = dragView.getScaleX();
391 float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
392 float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
393 float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) +
394 (t * t) * x3;
395 float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) +
396 (t * t) * y3;
397
398 dragView.setTranslationX(x);
399 dragView.setTranslationY(y);
400 dragView.setScaleX(initialScale * (1f - tp));
401 dragView.setScaleY(initialScale * (1f - tp));
402 dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
403 }
404 };
405 }
406
407 /**
408 * Creates an animation from the current drag view along its current velocity vector.
409 * For this animation, the alpha runs for a fixed duration and we update the position
410 * progressively.
411 */
412 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
Winson Chung043f2af2012-03-01 16:09:54 -0800413 private DragLayer mDragLayer;
414 private PointF mVelocity;
415 private Rect mFrom;
416 private long mPrevTime;
417 private boolean mHasOffsetForScale;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700418 private float mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800419
Winson Chung9658b1e2012-04-09 18:30:07 -0700420 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
Winson Chung043f2af2012-03-01 16:09:54 -0800421
422 public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from,
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700423 long startTime, float friction) {
Winson Chung043f2af2012-03-01 16:09:54 -0800424 mDragLayer = dragLayer;
425 mVelocity = vel;
426 mFrom = from;
427 mPrevTime = startTime;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700428 mFriction = 1f - (dragLayer.getResources().getDisplayMetrics().density * friction);
Winson Chung043f2af2012-03-01 16:09:54 -0800429 }
430
431 @Override
432 public void onAnimationUpdate(ValueAnimator animation) {
433 final DragView dragView = (DragView) mDragLayer.getAnimatedView();
434 float t = ((Float) animation.getAnimatedValue()).floatValue();
435 long curTime = AnimationUtils.currentAnimationTimeMillis();
436
437 if (!mHasOffsetForScale) {
438 mHasOffsetForScale = true;
439 float scale = dragView.getScaleX();
440 float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f;
441 float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f;
442
443 mFrom.left += xOffset;
444 mFrom.top += yOffset;
445 }
446
447 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
448 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
449
450 dragView.setTranslationX(mFrom.left);
451 dragView.setTranslationY(mFrom.top);
452 dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
453
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700454 mVelocity.x *= mFriction;
455 mVelocity.y *= mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800456 mPrevTime = curTime;
457 }
458 };
459 private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer,
460 DragObject d, PointF vel, final long startTime, final int duration,
461 ViewConfiguration config) {
462 final Rect from = new Rect();
463 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
464
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700465 return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime,
466 FLING_TO_DELETE_FRICTION);
Winson Chung043f2af2012-03-01 16:09:54 -0800467 }
468
469 public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
Winson Chunga48487a2012-03-20 16:19:37 -0700470 final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView;
471
Winson Chung043f2af2012-03-01 16:09:54 -0800472 // Don't highlight the icon as it's animating
473 d.dragView.setColor(0);
474 d.dragView.updateInitialScaleToCurrentScale();
Winson Chunga48487a2012-03-20 16:19:37 -0700475 // Don't highlight the target if we are flinging from AllApps
476 if (isAllApps) {
477 resetHoverColor();
478 }
Winson Chung043f2af2012-03-01 16:09:54 -0800479
480 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
481 // Defer animating out the drop target if we are animating to it
482 mSearchDropTargetBar.deferOnDragEnd();
483 mSearchDropTargetBar.finishAnimations();
484 }
485
486 final ViewConfiguration config = ViewConfiguration.get(mLauncher);
487 final DragLayer dragLayer = mLauncher.getDragLayer();
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700488 final int duration = FLING_DELETE_ANIMATION_DURATION;
Winson Chung043f2af2012-03-01 16:09:54 -0800489 final long startTime = AnimationUtils.currentAnimationTimeMillis();
490
491 // NOTE: Because it takes time for the first frame of animation to actually be
492 // called and we expect the animation to be a continuation of the fling, we have
493 // to account for the time that has elapsed since the fling finished. And since
494 // we don't have a startDelay, we will always get call to update when we call
495 // start() (which we want to ignore).
496 final TimeInterpolator tInterpolator = new TimeInterpolator() {
497 private int mCount = -1;
498 private float mOffset = 0f;
499
500 @Override
501 public float getInterpolation(float t) {
502 if (mCount < 0) {
503 mCount++;
504 } else if (mCount == 0) {
505 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
506 startTime) / duration);
507 mCount++;
508 }
509 return Math.min(1f, mOffset + t);
510 }
511 };
512 AnimatorUpdateListener updateCb = null;
513 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
514 updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
515 } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
516 updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime,
517 duration, config);
518 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700519 deferCompleteDropIfUninstalling(d);
520
Winson Chung043f2af2012-03-01 16:09:54 -0800521 Runnable onAnimationEndRunnable = new Runnable() {
522 @Override
523 public void run() {
Winson Chunga48487a2012-03-20 16:19:37 -0700524 // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up
525 // itself, otherwise, complete the drop to initiate the deletion process
526 if (!isAllApps) {
527 mLauncher.exitSpringLoadedDragMode();
528 completeDrop(d);
529 }
530 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800531 }
532 };
533 dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable,
534 DragLayer.ANIMATION_END_DISAPPEAR, null);
535 }
Winson Chung4c98d922011-05-31 16:50:48 -0700536}