blob: 7e0e5ebd7af444786009d6a2945cb1f3500c2302 [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;
Winson Chung4c98d922011-05-31 16:50:48 -070032import android.util.AttributeSet;
33import android.view.View;
Winson Chung043f2af2012-03-01 16:09:54 -080034import android.view.ViewConfiguration;
Winson Chunga6427b12011-07-27 10:53:39 -070035import android.view.ViewGroup;
Winson Chung043f2af2012-03-01 16:09:54 -080036import android.view.animation.AnimationUtils;
Adam Cohend4d7aa52011-07-19 21:47:37 -070037import android.view.animation.DecelerateInterpolator;
Winson Chung61967cb2012-02-28 18:11:33 -080038import android.view.animation.LinearInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070039
Michael Jurka1e2f4652013-07-08 18:03:46 -070040import java.util.List;
Michael Jurka5e7af5d2013-09-14 18:35:38 +020041import java.util.Set;
Michael Jurka1e2f4652013-07-08 18:03:46 -070042
Winson Chung61fa4192011-06-12 15:15:29 -070043public class DeleteDropTarget extends ButtonDropTarget {
Winson Chung043f2af2012-03-01 16:09:54 -080044 private static int DELETE_ANIMATION_DURATION = 285;
Winson Chung6e1bdaf2012-05-29 17:03:45 -070045 private static int FLING_DELETE_ANIMATION_DURATION = 350;
46 private static float FLING_TO_DELETE_FRICTION = 0.035f;
Winson Chung043f2af2012-03-01 16:09:54 -080047 private static int MODE_FLING_DELETE_TO_TRASH = 0;
48 private static int MODE_FLING_DELETE_ALONG_VECTOR = 1;
Winson Chung4c98d922011-05-31 16:50:48 -070049
Winson Chung043f2af2012-03-01 16:09:54 -080050 private final int mFlingDeleteMode = MODE_FLING_DELETE_ALONG_VECTOR;
51
Winson Chunga62e9fd2011-07-11 15:20:48 -070052 private ColorStateList mOriginalTextColor;
Adam Cohenebea84d2011-11-09 17:20:41 -080053 private TransitionDrawable mUninstallDrawable;
54 private TransitionDrawable mRemoveDrawable;
55 private TransitionDrawable mCurrentDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070056
Michael Jurka24715c72013-07-08 18:03:46 -070057 private boolean mWaitingForUninstall = false;
58
Winson Chung4c98d922011-05-31 16:50:48 -070059 public DeleteDropTarget(Context context, AttributeSet attrs) {
60 this(context, attrs, 0);
61 }
62
63 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
64 super(context, attrs, defStyle);
65 }
66
67 @Override
68 protected void onFinishInflate() {
69 super.onFinishInflate();
70
71 // Get the drawable
Winson Chunga6427b12011-07-27 10:53:39 -070072 mOriginalTextColor = getTextColors();
Winson Chung4c98d922011-05-31 16:50:48 -070073
74 // Get the hover color
75 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070076 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080077 mUninstallDrawable = (TransitionDrawable)
78 r.getDrawable(R.drawable.uninstall_target_selector);
79 mRemoveDrawable = (TransitionDrawable) r.getDrawable(R.drawable.remove_target_selector);
80
81 mRemoveDrawable.setCrossFadeEnabled(true);
82 mUninstallDrawable.setCrossFadeEnabled(true);
83
84 // The current drawable is set to either the remove drawable or the uninstall drawable
85 // and is initially set to the remove drawable, as set in the layout xml.
Winson Chung947245b2012-05-15 16:34:19 -070086 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Winson Chung201bc822011-06-20 15:41:53 -070087
88 // Remove the text in the Phone UI in landscape
89 int orientation = getResources().getConfiguration().orientation;
90 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
Daniel Sandlere4f98912013-06-25 15:13:26 -040091 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070092 setText("");
Winson Chung201bc822011-06-20 15:41:53 -070093 }
94 }
Winson Chung4c98d922011-05-31 16:50:48 -070095 }
96
97 private boolean isAllAppsApplication(DragSource source, Object info) {
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +000098 return source.supportsAppInfoDropTarget() && (info instanceof AppInfo);
Winson Chung4c98d922011-05-31 16:50:48 -070099 }
100 private boolean isAllAppsWidget(DragSource source, Object info) {
Winson Chung11a49372012-04-27 15:12:38 -0700101 if (source instanceof AppsCustomizePagedView) {
102 if (info instanceof PendingAddItemInfo) {
103 PendingAddItemInfo addInfo = (PendingAddItemInfo) info;
104 switch (addInfo.itemType) {
105 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
106 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
107 return true;
108 }
109 }
110 }
111 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700112 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700113 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
114 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -0700115 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700116 private boolean isWorkspaceOrFolderApplication(DragObject d) {
117 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
118 }
119 private boolean isWorkspaceOrFolderWidget(DragObject d) {
120 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700121 }
122 private boolean isWorkspaceFolder(DragObject d) {
123 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
124 }
125
Winson Chunga48487a2012-03-20 16:19:37 -0700126 private void setHoverColor() {
127 mCurrentDrawable.startTransition(mTransitionDuration);
128 setTextColor(mHoverColor);
129 }
130 private void resetHoverColor() {
131 mCurrentDrawable.resetTransition();
132 setTextColor(mOriginalTextColor);
133 }
134
Winson Chung4c98d922011-05-31 16:50:48 -0700135 @Override
136 public boolean acceptDrop(DragObject d) {
Adam Cohen7c4c5102013-06-14 17:42:35 -0700137 return willAcceptDrop(d.dragInfo);
138 }
139
140 public static boolean willAcceptDrop(Object info) {
141 if (info instanceof ItemInfo) {
142 ItemInfo item = (ItemInfo) info;
143 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET ||
144 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
145 return true;
146 }
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200147
148 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200149 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) {
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200150 return true;
151 }
152
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200153 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
154 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
155 item instanceof AppInfo) {
156 AppInfo appInfo = (AppInfo) info;
157 return (appInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
158 }
159
Michael Jurka9bd4d282013-09-05 19:21:31 +0200160 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
Michael Jurka1e2f4652013-07-08 18:03:46 -0700161 item instanceof ShortcutInfo) {
Michael Jurka9bd4d282013-09-05 19:21:31 +0200162 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
163 ShortcutInfo shortcutInfo = (ShortcutInfo) info;
164 return (shortcutInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
165 } else {
166 return true;
167 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700168 }
Adam Cohen947dc542013-06-06 22:43:33 -0700169 }
Adam Cohen7c4c5102013-06-14 17:42:35 -0700170 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700171 }
172
173 @Override
174 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700175 boolean isVisible = true;
Michael Jurkaaddcba62013-10-09 19:04:13 -0700176 boolean useUninstallLabel = !AppsCustomizePagedView.DISABLE_ALL_APPS &&
177 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 }
186
Michael Jurkaaddcba62013-10-09 19:04:13 -0700187 if (useUninstallLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800188 setCompoundDrawablesRelativeWithIntrinsicBounds(mUninstallDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000189 } else if (useDeleteLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800190 setCompoundDrawablesRelativeWithIntrinsicBounds(mRemoveDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000191 } else {
192 isVisible = false;
Adam Cohenebea84d2011-11-09 17:20:41 -0800193 }
Winson Chung947245b2012-05-15 16:34:19 -0700194 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Adam Cohenebea84d2011-11-09 17:20:41 -0800195
Winson Chung4c98d922011-05-31 16:50:48 -0700196 mActive = isVisible;
Winson Chunga48487a2012-03-20 16:19:37 -0700197 resetHoverColor();
Winson Chunga6427b12011-07-27 10:53:39 -0700198 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000199 if (isVisible && getText().length() > 0) {
Michael Jurkaaddcba62013-10-09 19:04:13 -0700200 setText(useUninstallLabel ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700201 : R.string.delete_target_label);
202 }
203 }
204
205 @Override
206 public void onDragEnd() {
207 super.onDragEnd();
208 mActive = false;
209 }
210
211 public void onDragEnter(DragObject d) {
212 super.onDragEnter(d);
213
Winson Chunga48487a2012-03-20 16:19:37 -0700214 setHoverColor();
Winson Chung4c98d922011-05-31 16:50:48 -0700215 }
216
217 public void onDragExit(DragObject d) {
218 super.onDragExit(d);
219
Winson Chungaaa530a2011-07-11 21:06:30 -0700220 if (!d.dragComplete) {
Winson Chunga48487a2012-03-20 16:19:37 -0700221 resetHoverColor();
Winson Chung61967cb2012-02-28 18:11:33 -0800222 } else {
223 // Restore the hover color if we are deleting
224 d.dragView.setColor(mHoverColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700225 }
Winson Chung4c98d922011-05-31 16:50:48 -0700226 }
227
Adam Cohened66b2b2012-01-23 17:28:51 -0800228 private void animateToTrashAndCompleteDrop(final DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700229 final DragLayer dragLayer = mLauncher.getDragLayer();
230 final Rect from = new Rect();
Adam Cohened66b2b2012-01-23 17:28:51 -0800231 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Michael Jurka1e2f4652013-07-08 18:03:46 -0700232 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Winson Chung61967cb2012-02-28 18:11:33 -0800233 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
Michael Jurka1e2f4652013-07-08 18:03:46 -0700234 final float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800235
Adam Cohend4d7aa52011-07-19 21:47:37 -0700236 mSearchDropTargetBar.deferOnDragEnd();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700237 deferCompleteDropIfUninstalling(d);
238
Adam Cohend4d7aa52011-07-19 21:47:37 -0700239 Runnable onAnimationEndRunnable = new Runnable() {
240 @Override
241 public void run() {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700242 completeDrop(d);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700243 mSearchDropTargetBar.onDragEnd();
244 mLauncher.exitSpringLoadedDragMode();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700245 }
246 };
Winson Chung61967cb2012-02-28 18:11:33 -0800247 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700248 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800249 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800250 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700251 }
252
Michael Jurka1e2f4652013-07-08 18:03:46 -0700253 private void deferCompleteDropIfUninstalling(DragObject d) {
254 mWaitingForUninstall = false;
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200255 if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700256 if (d.dragSource instanceof Folder) {
257 ((Folder) d.dragSource).deferCompleteDropAfterUninstallActivity();
258 } else if (d.dragSource instanceof Workspace) {
259 ((Workspace) d.dragSource).deferCompleteDropAfterUninstallActivity();
260 }
261 mWaitingForUninstall = true;
262 }
263 }
Winson Chung4c98d922011-05-31 16:50:48 -0700264
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200265 private boolean isUninstallFromWorkspace(DragObject d) {
266 if (AppsCustomizePagedView.DISABLE_ALL_APPS && isWorkspaceOrFolderApplication(d)) {
267 ShortcutInfo shortcut = (ShortcutInfo) d.dragInfo;
268 if (shortcut.intent != null && shortcut.intent.getComponent() != null) {
269 Set<String> categories = shortcut.intent.getCategories();
270 boolean includesLauncherCategory = false;
271 if (categories != null) {
272 for (String category : categories) {
273 if (category.equals(Intent.CATEGORY_LAUNCHER)) {
274 includesLauncherCategory = true;
275 break;
276 }
277 }
278 }
279 return includesLauncherCategory;
280 }
281 }
282 return false;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700283 }
284
Michael Jurkaf3007582013-08-21 14:33:57 +0200285 private void completeDrop(DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700286 ItemInfo item = (ItemInfo) d.dragInfo;
287 boolean wasWaitingForUninstall = mWaitingForUninstall;
288 mWaitingForUninstall = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700289 if (isAllAppsApplication(d.dragSource, item)) {
290 // Uninstall the application if it is being dragged from AppsCustomize
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200291 AppInfo appInfo = (AppInfo) item;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700292 mLauncher.startApplicationUninstallActivity(appInfo.componentName, appInfo.flags);
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200293 } else if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700294 ShortcutInfo shortcut = (ShortcutInfo) item;
295 if (shortcut.intent != null && shortcut.intent.getComponent() != null) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200296 final ComponentName componentName = shortcut.intent.getComponent();
297 final DragSource dragSource = d.dragSource;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200298 int flags = AppInfo.initFlags(
Michael Jurka1e2f4652013-07-08 18:03:46 -0700299 ShortcutInfo.getPackageInfo(getContext(), componentName.getPackageName()));
300 mWaitingForUninstall =
301 mLauncher.startApplicationUninstallActivity(componentName, flags);
Michael Jurkaf3007582013-08-21 14:33:57 +0200302 if (mWaitingForUninstall) {
303 final Runnable checkIfUninstallWasSuccess = new Runnable() {
304 @Override
305 public void run() {
306 mWaitingForUninstall = false;
307 String packageName = componentName.getPackageName();
308 List<ResolveInfo> activities =
309 AllAppsList.findActivitiesForPackage(getContext(), packageName);
310 boolean uninstallSuccessful = activities.size() == 0;
311 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 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700323 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700324 LauncherModel.deleteItemFromDatabase(mLauncher, item);
325 } else if (isWorkspaceFolder(d)) {
326 // Remove the folder from the workspace and delete the contents from launcher model
327 FolderInfo folderInfo = (FolderInfo) item;
328 mLauncher.removeFolder(folderInfo);
329 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700330 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700331 // Remove the widget from the workspace
332 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
333 LauncherModel.deleteItemFromDatabase(mLauncher, item);
334
335 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
336 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
337 if (appWidgetHost != null) {
338 // Deleting an app widget ID is a void call but writes to disk before returning
339 // to the caller...
340 new Thread("deleteAppWidgetId") {
341 public void run() {
342 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
343 }
344 }.start();
345 }
346 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700347 if (wasWaitingForUninstall && !mWaitingForUninstall) {
348 if (d.dragSource instanceof Folder) {
349 ((Folder) d.dragSource).onUninstallActivityReturned(false);
350 } else if (d.dragSource instanceof Workspace) {
351 ((Workspace) d.dragSource).onUninstallActivityReturned(false);
352 }
353 }
Winson Chung4c98d922011-05-31 16:50:48 -0700354 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700355
356 public void onDrop(DragObject d) {
357 animateToTrashAndCompleteDrop(d);
358 }
Winson Chung043f2af2012-03-01 16:09:54 -0800359
360 /**
361 * Creates an animation from the current drag view to the delete trash icon.
362 */
363 private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer,
364 DragObject d, PointF vel, ViewConfiguration config) {
365 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
366 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
367 final Rect from = new Rect();
368 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
369
370 // Calculate how far along the velocity vector we should put the intermediate point on
371 // the bezier curve
372 float velocity = Math.abs(vel.length());
373 float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
374 int offsetY = (int) (-from.top * vp);
375 int offsetX = (int) (offsetY / (vel.y / vel.x));
376 final float y2 = from.top + offsetY; // intermediate t/l
377 final float x2 = from.left + offsetX;
378 final float x1 = from.left; // drag view t/l
379 final float y1 = from.top;
380 final float x3 = to.left; // delete target t/l
381 final float y3 = to.top;
382
383 final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {
384 @Override
385 public float getInterpolation(float t) {
386 return t * t * t * t * t * t * t * t;
387 }
388 };
389 return new AnimatorUpdateListener() {
390 @Override
391 public void onAnimationUpdate(ValueAnimator animation) {
392 final DragView dragView = (DragView) dragLayer.getAnimatedView();
393 float t = ((Float) animation.getAnimatedValue()).floatValue();
394 float tp = scaleAlphaInterpolator.getInterpolation(t);
395 float initialScale = dragView.getInitialScale();
396 float finalAlpha = 0.5f;
397 float scale = dragView.getScaleX();
398 float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
399 float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
400 float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) +
401 (t * t) * x3;
402 float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) +
403 (t * t) * y3;
404
405 dragView.setTranslationX(x);
406 dragView.setTranslationY(y);
407 dragView.setScaleX(initialScale * (1f - tp));
408 dragView.setScaleY(initialScale * (1f - tp));
409 dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
410 }
411 };
412 }
413
414 /**
415 * Creates an animation from the current drag view along its current velocity vector.
416 * For this animation, the alpha runs for a fixed duration and we update the position
417 * progressively.
418 */
419 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
Winson Chung043f2af2012-03-01 16:09:54 -0800420 private DragLayer mDragLayer;
421 private PointF mVelocity;
422 private Rect mFrom;
423 private long mPrevTime;
424 private boolean mHasOffsetForScale;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700425 private float mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800426
Winson Chung9658b1e2012-04-09 18:30:07 -0700427 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
Winson Chung043f2af2012-03-01 16:09:54 -0800428
429 public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from,
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700430 long startTime, float friction) {
Winson Chung043f2af2012-03-01 16:09:54 -0800431 mDragLayer = dragLayer;
432 mVelocity = vel;
433 mFrom = from;
434 mPrevTime = startTime;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700435 mFriction = 1f - (dragLayer.getResources().getDisplayMetrics().density * friction);
Winson Chung043f2af2012-03-01 16:09:54 -0800436 }
437
438 @Override
439 public void onAnimationUpdate(ValueAnimator animation) {
440 final DragView dragView = (DragView) mDragLayer.getAnimatedView();
441 float t = ((Float) animation.getAnimatedValue()).floatValue();
442 long curTime = AnimationUtils.currentAnimationTimeMillis();
443
444 if (!mHasOffsetForScale) {
445 mHasOffsetForScale = true;
446 float scale = dragView.getScaleX();
447 float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f;
448 float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f;
449
450 mFrom.left += xOffset;
451 mFrom.top += yOffset;
452 }
453
454 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
455 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
456
457 dragView.setTranslationX(mFrom.left);
458 dragView.setTranslationY(mFrom.top);
459 dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
460
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700461 mVelocity.x *= mFriction;
462 mVelocity.y *= mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800463 mPrevTime = curTime;
464 }
465 };
466 private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer,
467 DragObject d, PointF vel, final long startTime, final int duration,
468 ViewConfiguration config) {
469 final Rect from = new Rect();
470 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
471
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700472 return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime,
473 FLING_TO_DELETE_FRICTION);
Winson Chung043f2af2012-03-01 16:09:54 -0800474 }
475
476 public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
Winson Chunga48487a2012-03-20 16:19:37 -0700477 final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView;
478
Winson Chung043f2af2012-03-01 16:09:54 -0800479 // Don't highlight the icon as it's animating
480 d.dragView.setColor(0);
481 d.dragView.updateInitialScaleToCurrentScale();
Winson Chunga48487a2012-03-20 16:19:37 -0700482 // Don't highlight the target if we are flinging from AllApps
483 if (isAllApps) {
484 resetHoverColor();
485 }
Winson Chung043f2af2012-03-01 16:09:54 -0800486
487 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
488 // Defer animating out the drop target if we are animating to it
489 mSearchDropTargetBar.deferOnDragEnd();
490 mSearchDropTargetBar.finishAnimations();
491 }
492
493 final ViewConfiguration config = ViewConfiguration.get(mLauncher);
494 final DragLayer dragLayer = mLauncher.getDragLayer();
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700495 final int duration = FLING_DELETE_ANIMATION_DURATION;
Winson Chung043f2af2012-03-01 16:09:54 -0800496 final long startTime = AnimationUtils.currentAnimationTimeMillis();
497
498 // NOTE: Because it takes time for the first frame of animation to actually be
499 // called and we expect the animation to be a continuation of the fling, we have
500 // to account for the time that has elapsed since the fling finished. And since
501 // we don't have a startDelay, we will always get call to update when we call
502 // start() (which we want to ignore).
503 final TimeInterpolator tInterpolator = new TimeInterpolator() {
504 private int mCount = -1;
505 private float mOffset = 0f;
506
507 @Override
508 public float getInterpolation(float t) {
509 if (mCount < 0) {
510 mCount++;
511 } else if (mCount == 0) {
512 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
513 startTime) / duration);
514 mCount++;
515 }
516 return Math.min(1f, mOffset + t);
517 }
518 };
519 AnimatorUpdateListener updateCb = null;
520 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
521 updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
522 } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
523 updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime,
524 duration, config);
525 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700526 deferCompleteDropIfUninstalling(d);
527
Winson Chung043f2af2012-03-01 16:09:54 -0800528 Runnable onAnimationEndRunnable = new Runnable() {
529 @Override
530 public void run() {
Winson Chunga48487a2012-03-20 16:19:37 -0700531 // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up
532 // itself, otherwise, complete the drop to initiate the deletion process
533 if (!isAllApps) {
534 mLauncher.exitSpringLoadedDragMode();
535 completeDrop(d);
536 }
537 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800538 }
539 };
540 dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable,
541 DragLayer.ANIMATION_END_DISAPPEAR, null);
542 }
Winson Chung4c98d922011-05-31 16:50:48 -0700543}