blob: 4023dafb658b6bbd86d86c893c25b6cf669941c2 [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) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +020098 return (source instanceof AppsCustomizePagedView) && (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);
Winson Chung4c98d922011-05-31 16:50:48 -0700178
Winson Chung4c98d922011-05-31 16:50:48 -0700179 // If we are dragging an application from AppsCustomize, only show the control if we can
Winson Chunge01af632013-09-26 10:43:20 -0700180 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case.
181 // Hide the delete target if it is a widget from AppsCustomize.
182 if (!willAcceptDrop(info) || isAllAppsWidget(source, info)) {
Adam Cohen947dc542013-06-06 22:43:33 -0700183 isVisible = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700184 }
185
Michael Jurkaaddcba62013-10-09 19:04:13 -0700186 if (useUninstallLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800187 setCompoundDrawablesRelativeWithIntrinsicBounds(mUninstallDrawable, null, null, null);
Adam Cohenebea84d2011-11-09 17:20:41 -0800188 } else {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800189 setCompoundDrawablesRelativeWithIntrinsicBounds(mRemoveDrawable, null, null, null);
Adam Cohenebea84d2011-11-09 17:20:41 -0800190 }
Winson Chung947245b2012-05-15 16:34:19 -0700191 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Adam Cohenebea84d2011-11-09 17:20:41 -0800192
Winson Chung4c98d922011-05-31 16:50:48 -0700193 mActive = isVisible;
Winson Chunga48487a2012-03-20 16:19:37 -0700194 resetHoverColor();
Winson Chunga6427b12011-07-27 10:53:39 -0700195 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
196 if (getText().length() > 0) {
Michael Jurkaaddcba62013-10-09 19:04:13 -0700197 setText(useUninstallLabel ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700198 : R.string.delete_target_label);
199 }
200 }
201
202 @Override
203 public void onDragEnd() {
204 super.onDragEnd();
205 mActive = false;
206 }
207
208 public void onDragEnter(DragObject d) {
209 super.onDragEnter(d);
210
Winson Chunga48487a2012-03-20 16:19:37 -0700211 setHoverColor();
Winson Chung4c98d922011-05-31 16:50:48 -0700212 }
213
214 public void onDragExit(DragObject d) {
215 super.onDragExit(d);
216
Winson Chungaaa530a2011-07-11 21:06:30 -0700217 if (!d.dragComplete) {
Winson Chunga48487a2012-03-20 16:19:37 -0700218 resetHoverColor();
Winson Chung61967cb2012-02-28 18:11:33 -0800219 } else {
220 // Restore the hover color if we are deleting
221 d.dragView.setColor(mHoverColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700222 }
Winson Chung4c98d922011-05-31 16:50:48 -0700223 }
224
Adam Cohened66b2b2012-01-23 17:28:51 -0800225 private void animateToTrashAndCompleteDrop(final DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700226 final DragLayer dragLayer = mLauncher.getDragLayer();
227 final Rect from = new Rect();
Adam Cohened66b2b2012-01-23 17:28:51 -0800228 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Michael Jurka1e2f4652013-07-08 18:03:46 -0700229 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Winson Chung61967cb2012-02-28 18:11:33 -0800230 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
Michael Jurka1e2f4652013-07-08 18:03:46 -0700231 final float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800232
Adam Cohend4d7aa52011-07-19 21:47:37 -0700233 mSearchDropTargetBar.deferOnDragEnd();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700234 deferCompleteDropIfUninstalling(d);
235
Adam Cohend4d7aa52011-07-19 21:47:37 -0700236 Runnable onAnimationEndRunnable = new Runnable() {
237 @Override
238 public void run() {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700239 completeDrop(d);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700240 mSearchDropTargetBar.onDragEnd();
241 mLauncher.exitSpringLoadedDragMode();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700242 }
243 };
Winson Chung61967cb2012-02-28 18:11:33 -0800244 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700245 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800246 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800247 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700248 }
249
Michael Jurka1e2f4652013-07-08 18:03:46 -0700250 private void deferCompleteDropIfUninstalling(DragObject d) {
251 mWaitingForUninstall = false;
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200252 if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700253 if (d.dragSource instanceof Folder) {
254 ((Folder) d.dragSource).deferCompleteDropAfterUninstallActivity();
255 } else if (d.dragSource instanceof Workspace) {
256 ((Workspace) d.dragSource).deferCompleteDropAfterUninstallActivity();
257 }
258 mWaitingForUninstall = true;
259 }
260 }
Winson Chung4c98d922011-05-31 16:50:48 -0700261
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200262 private boolean isUninstallFromWorkspace(DragObject d) {
263 if (AppsCustomizePagedView.DISABLE_ALL_APPS && isWorkspaceOrFolderApplication(d)) {
264 ShortcutInfo shortcut = (ShortcutInfo) d.dragInfo;
265 if (shortcut.intent != null && shortcut.intent.getComponent() != null) {
266 Set<String> categories = shortcut.intent.getCategories();
267 boolean includesLauncherCategory = false;
268 if (categories != null) {
269 for (String category : categories) {
270 if (category.equals(Intent.CATEGORY_LAUNCHER)) {
271 includesLauncherCategory = true;
272 break;
273 }
274 }
275 }
276 return includesLauncherCategory;
277 }
278 }
279 return false;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700280 }
281
Michael Jurkaf3007582013-08-21 14:33:57 +0200282 private void completeDrop(DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700283 ItemInfo item = (ItemInfo) d.dragInfo;
284 boolean wasWaitingForUninstall = mWaitingForUninstall;
285 mWaitingForUninstall = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700286 if (isAllAppsApplication(d.dragSource, item)) {
287 // Uninstall the application if it is being dragged from AppsCustomize
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200288 AppInfo appInfo = (AppInfo) item;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700289 mLauncher.startApplicationUninstallActivity(appInfo.componentName, appInfo.flags);
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200290 } else if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700291 ShortcutInfo shortcut = (ShortcutInfo) item;
292 if (shortcut.intent != null && shortcut.intent.getComponent() != null) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200293 final ComponentName componentName = shortcut.intent.getComponent();
294 final DragSource dragSource = d.dragSource;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200295 int flags = AppInfo.initFlags(
Michael Jurka1e2f4652013-07-08 18:03:46 -0700296 ShortcutInfo.getPackageInfo(getContext(), componentName.getPackageName()));
297 mWaitingForUninstall =
298 mLauncher.startApplicationUninstallActivity(componentName, flags);
Michael Jurkaf3007582013-08-21 14:33:57 +0200299 if (mWaitingForUninstall) {
300 final Runnable checkIfUninstallWasSuccess = new Runnable() {
301 @Override
302 public void run() {
303 mWaitingForUninstall = false;
304 String packageName = componentName.getPackageName();
305 List<ResolveInfo> activities =
306 AllAppsList.findActivitiesForPackage(getContext(), packageName);
307 boolean uninstallSuccessful = activities.size() == 0;
308 if (dragSource instanceof Folder) {
309 ((Folder) dragSource).
310 onUninstallActivityReturned(uninstallSuccessful);
311 } else if (dragSource instanceof Workspace) {
312 ((Workspace) dragSource).
313 onUninstallActivityReturned(uninstallSuccessful);
314 }
315 }
316 };
317 mLauncher.addOnResumeCallback(checkIfUninstallWasSuccess);
318 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700319 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700320 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700321 LauncherModel.deleteItemFromDatabase(mLauncher, item);
322 } else if (isWorkspaceFolder(d)) {
323 // Remove the folder from the workspace and delete the contents from launcher model
324 FolderInfo folderInfo = (FolderInfo) item;
325 mLauncher.removeFolder(folderInfo);
326 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700327 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700328 // Remove the widget from the workspace
329 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
330 LauncherModel.deleteItemFromDatabase(mLauncher, item);
331
332 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
333 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
334 if (appWidgetHost != null) {
335 // Deleting an app widget ID is a void call but writes to disk before returning
336 // to the caller...
337 new Thread("deleteAppWidgetId") {
338 public void run() {
339 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
340 }
341 }.start();
342 }
343 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700344 if (wasWaitingForUninstall && !mWaitingForUninstall) {
345 if (d.dragSource instanceof Folder) {
346 ((Folder) d.dragSource).onUninstallActivityReturned(false);
347 } else if (d.dragSource instanceof Workspace) {
348 ((Workspace) d.dragSource).onUninstallActivityReturned(false);
349 }
350 }
Winson Chung4c98d922011-05-31 16:50:48 -0700351 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700352
353 public void onDrop(DragObject d) {
354 animateToTrashAndCompleteDrop(d);
355 }
Winson Chung043f2af2012-03-01 16:09:54 -0800356
357 /**
358 * Creates an animation from the current drag view to the delete trash icon.
359 */
360 private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer,
361 DragObject d, PointF vel, ViewConfiguration config) {
362 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
363 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
364 final Rect from = new Rect();
365 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
366
367 // Calculate how far along the velocity vector we should put the intermediate point on
368 // the bezier curve
369 float velocity = Math.abs(vel.length());
370 float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
371 int offsetY = (int) (-from.top * vp);
372 int offsetX = (int) (offsetY / (vel.y / vel.x));
373 final float y2 = from.top + offsetY; // intermediate t/l
374 final float x2 = from.left + offsetX;
375 final float x1 = from.left; // drag view t/l
376 final float y1 = from.top;
377 final float x3 = to.left; // delete target t/l
378 final float y3 = to.top;
379
380 final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {
381 @Override
382 public float getInterpolation(float t) {
383 return t * t * t * t * t * t * t * t;
384 }
385 };
386 return new AnimatorUpdateListener() {
387 @Override
388 public void onAnimationUpdate(ValueAnimator animation) {
389 final DragView dragView = (DragView) dragLayer.getAnimatedView();
390 float t = ((Float) animation.getAnimatedValue()).floatValue();
391 float tp = scaleAlphaInterpolator.getInterpolation(t);
392 float initialScale = dragView.getInitialScale();
393 float finalAlpha = 0.5f;
394 float scale = dragView.getScaleX();
395 float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
396 float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
397 float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) +
398 (t * t) * x3;
399 float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) +
400 (t * t) * y3;
401
402 dragView.setTranslationX(x);
403 dragView.setTranslationY(y);
404 dragView.setScaleX(initialScale * (1f - tp));
405 dragView.setScaleY(initialScale * (1f - tp));
406 dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
407 }
408 };
409 }
410
411 /**
412 * Creates an animation from the current drag view along its current velocity vector.
413 * For this animation, the alpha runs for a fixed duration and we update the position
414 * progressively.
415 */
416 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
Winson Chung043f2af2012-03-01 16:09:54 -0800417 private DragLayer mDragLayer;
418 private PointF mVelocity;
419 private Rect mFrom;
420 private long mPrevTime;
421 private boolean mHasOffsetForScale;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700422 private float mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800423
Winson Chung9658b1e2012-04-09 18:30:07 -0700424 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
Winson Chung043f2af2012-03-01 16:09:54 -0800425
426 public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from,
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700427 long startTime, float friction) {
Winson Chung043f2af2012-03-01 16:09:54 -0800428 mDragLayer = dragLayer;
429 mVelocity = vel;
430 mFrom = from;
431 mPrevTime = startTime;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700432 mFriction = 1f - (dragLayer.getResources().getDisplayMetrics().density * friction);
Winson Chung043f2af2012-03-01 16:09:54 -0800433 }
434
435 @Override
436 public void onAnimationUpdate(ValueAnimator animation) {
437 final DragView dragView = (DragView) mDragLayer.getAnimatedView();
438 float t = ((Float) animation.getAnimatedValue()).floatValue();
439 long curTime = AnimationUtils.currentAnimationTimeMillis();
440
441 if (!mHasOffsetForScale) {
442 mHasOffsetForScale = true;
443 float scale = dragView.getScaleX();
444 float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f;
445 float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f;
446
447 mFrom.left += xOffset;
448 mFrom.top += yOffset;
449 }
450
451 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
452 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
453
454 dragView.setTranslationX(mFrom.left);
455 dragView.setTranslationY(mFrom.top);
456 dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
457
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700458 mVelocity.x *= mFriction;
459 mVelocity.y *= mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800460 mPrevTime = curTime;
461 }
462 };
463 private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer,
464 DragObject d, PointF vel, final long startTime, final int duration,
465 ViewConfiguration config) {
466 final Rect from = new Rect();
467 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
468
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700469 return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime,
470 FLING_TO_DELETE_FRICTION);
Winson Chung043f2af2012-03-01 16:09:54 -0800471 }
472
473 public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
Winson Chunga48487a2012-03-20 16:19:37 -0700474 final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView;
475
Winson Chung043f2af2012-03-01 16:09:54 -0800476 // Don't highlight the icon as it's animating
477 d.dragView.setColor(0);
478 d.dragView.updateInitialScaleToCurrentScale();
Winson Chunga48487a2012-03-20 16:19:37 -0700479 // Don't highlight the target if we are flinging from AllApps
480 if (isAllApps) {
481 resetHoverColor();
482 }
Winson Chung043f2af2012-03-01 16:09:54 -0800483
484 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
485 // Defer animating out the drop target if we are animating to it
486 mSearchDropTargetBar.deferOnDragEnd();
487 mSearchDropTargetBar.finishAnimations();
488 }
489
490 final ViewConfiguration config = ViewConfiguration.get(mLauncher);
491 final DragLayer dragLayer = mLauncher.getDragLayer();
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700492 final int duration = FLING_DELETE_ANIMATION_DURATION;
Winson Chung043f2af2012-03-01 16:09:54 -0800493 final long startTime = AnimationUtils.currentAnimationTimeMillis();
494
495 // NOTE: Because it takes time for the first frame of animation to actually be
496 // called and we expect the animation to be a continuation of the fling, we have
497 // to account for the time that has elapsed since the fling finished. And since
498 // we don't have a startDelay, we will always get call to update when we call
499 // start() (which we want to ignore).
500 final TimeInterpolator tInterpolator = new TimeInterpolator() {
501 private int mCount = -1;
502 private float mOffset = 0f;
503
504 @Override
505 public float getInterpolation(float t) {
506 if (mCount < 0) {
507 mCount++;
508 } else if (mCount == 0) {
509 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
510 startTime) / duration);
511 mCount++;
512 }
513 return Math.min(1f, mOffset + t);
514 }
515 };
516 AnimatorUpdateListener updateCb = null;
517 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
518 updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
519 } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
520 updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime,
521 duration, config);
522 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700523 deferCompleteDropIfUninstalling(d);
524
Winson Chung043f2af2012-03-01 16:09:54 -0800525 Runnable onAnimationEndRunnable = new Runnable() {
526 @Override
527 public void run() {
Winson Chunga48487a2012-03-20 16:19:37 -0700528 // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up
529 // itself, otherwise, complete the drop to initiate the deletion process
530 if (!isAllApps) {
531 mLauncher.exitSpringLoadedDragMode();
532 completeDrop(d);
533 }
534 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800535 }
536 };
537 dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable,
538 DragLayer.ANIMATION_END_DISAPPEAR, null);
539 }
Winson Chung4c98d922011-05-31 16:50:48 -0700540}