blob: 62aa285abd8bec10c961b83cf69368e29fa74bb5 [file] [log] [blame]
Winson Chung4c98d922011-05-31 16:50:48 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung4c98d922011-05-31 16:50:48 -070018
Winson Chung043f2af2012-03-01 16:09:54 -080019import android.animation.TimeInterpolator;
20import android.animation.ValueAnimator;
21import android.animation.ValueAnimator.AnimatorUpdateListener;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080022import android.annotation.TargetApi;
Michael Jurka1e2f4652013-07-08 18:03:46 -070023import android.content.ComponentName;
Winson Chung4c98d922011-05-31 16:50:48 -070024import android.content.Context;
Winson Chunga62e9fd2011-07-11 15:20:48 -070025import android.content.res.ColorStateList;
Winson Chung201bc822011-06-20 15:41:53 -070026import android.content.res.Configuration;
Winson Chung4c98d922011-05-31 16:50:48 -070027import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080028import android.graphics.PointF;
Adam Cohend4d7aa52011-07-19 21:47:37 -070029import android.graphics.Rect;
Winson Chung967289b2011-06-30 18:09:30 -070030import android.graphics.drawable.TransitionDrawable;
Michael Jurka43467462013-11-14 12:03:58 +010031import android.os.AsyncTask;
Jason Monk6a2c7782014-09-02 13:13:24 -040032import android.os.Build;
33import android.os.Bundle;
Jason Monk950a4672014-07-15 09:22:36 -040034import android.os.UserManager;
Winson Chung4c98d922011-05-31 16:50:48 -070035import android.util.AttributeSet;
36import android.view.View;
Winson Chung043f2af2012-03-01 16:09:54 -080037import android.view.ViewConfiguration;
Winson Chunga6427b12011-07-27 10:53:39 -070038import android.view.ViewGroup;
Winson Chung043f2af2012-03-01 16:09:54 -080039import android.view.animation.AnimationUtils;
Adam Cohend4d7aa52011-07-19 21:47:37 -070040import android.view.animation.DecelerateInterpolator;
Winson Chung61967cb2012-02-28 18:11:33 -080041import android.view.animation.LinearInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070042
Kenny Guyed131872014-04-30 03:02:21 +010043import com.android.launcher3.compat.UserHandleCompat;
Adam Cohen091440a2015-03-18 14:16:05 -070044import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -070045import com.android.launcher3.widget.WidgetsContainerView;
Kenny Guyed131872014-04-30 03:02:21 +010046
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
Adam Cohen091440a2015-03-18 14:16:05 -070061 @Thunk boolean mWaitingForUninstall = false;
Michael Jurka24715c72013-07-08 18:03:46 -070062
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 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700104
105 private boolean isWidget(DragSource source, Object info) {
106 if (source instanceof WidgetsContainerView) {
Winson Chung11a49372012-04-27 15:12:38 -0700107 if (info instanceof PendingAddItemInfo) {
108 PendingAddItemInfo addInfo = (PendingAddItemInfo) info;
109 switch (addInfo.itemType) {
110 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
111 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -0800112 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chung11a49372012-04-27 15:12:38 -0700113 return true;
114 }
115 }
116 }
117 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700118 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700119 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
120 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -0700121 }
Winson Chung4c98d922011-05-31 16:50:48 -0700122
Winson Chunga48487a2012-03-20 16:19:37 -0700123 private void setHoverColor() {
Adam Cohenfe9da812014-08-04 17:08:01 -0700124 if (mCurrentDrawable != null) {
125 mCurrentDrawable.startTransition(mTransitionDuration);
126 }
Winson Chunga48487a2012-03-20 16:19:37 -0700127 setTextColor(mHoverColor);
128 }
129 private void resetHoverColor() {
Adam Cohenfe9da812014-08-04 17:08:01 -0700130 if (mCurrentDrawable != null) {
131 mCurrentDrawable.resetTransition();
132 }
Winson Chunga48487a2012-03-20 16:19:37 -0700133 setTextColor(mOriginalTextColor);
134 }
135
Winson Chung4c98d922011-05-31 16:50:48 -0700136 @Override
137 public boolean acceptDrop(DragObject d) {
Adam Cohen7c4c5102013-06-14 17:42:35 -0700138 return willAcceptDrop(d.dragInfo);
139 }
140
141 public static boolean willAcceptDrop(Object info) {
142 if (info instanceof ItemInfo) {
143 ItemInfo item = (ItemInfo) info;
144 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET ||
Adam Cohen59400422014-03-05 18:07:04 -0800145 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET ||
Adam Cohen7c4c5102013-06-14 17:42:35 -0700146 item.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
147 return true;
148 }
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200149
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800150 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) {
Michael Jurkaed3d4df2013-09-12 18:09:24 +0200151 return true;
152 }
153
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800154 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200155 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) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800162 return true;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700163 }
Adam Cohen947dc542013-06-06 22:43:33 -0700164 }
Adam Cohen7c4c5102013-06-14 17:42:35 -0700165 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700166 }
167
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800168 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4c98d922011-05-31 16:50:48 -0700169 @Override
170 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700171 boolean isVisible = true;
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800172 boolean useUninstallLabel = isAllAppsApplication(source, info);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000173 boolean useDeleteLabel = !useUninstallLabel && source.supportsDeleteDropTarget();
Winson Chung4c98d922011-05-31 16:50:48 -0700174
Winson Chung4c98d922011-05-31 16:50:48 -0700175 // If we are dragging an application from AppsCustomize, only show the control if we can
Winson Chunge01af632013-09-26 10:43:20 -0700176 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case.
177 // Hide the delete target if it is a widget from AppsCustomize.
Hyunyoung Song3f471442015-04-08 19:01:34 -0700178 if (!willAcceptDrop(info) || isWidget(source, info)) {
Adam Cohen947dc542013-06-06 22:43:33 -0700179 isVisible = false;
Winson Chung4c98d922011-05-31 16:50:48 -0700180 }
Jason Monk950a4672014-07-15 09:22:36 -0400181 if (useUninstallLabel) {
Jason Monk6a2c7782014-09-02 13:13:24 -0400182 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
183 UserManager userManager = (UserManager)
184 getContext().getSystemService(Context.USER_SERVICE);
185 Bundle restrictions = userManager.getUserRestrictions();
186 if (restrictions.getBoolean(UserManager.DISALLOW_APPS_CONTROL, false)
187 || restrictions.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS, false)) {
188 isVisible = false;
189 }
Jason Monk950a4672014-07-15 09:22:36 -0400190 }
191 }
Winson Chung4c98d922011-05-31 16:50:48 -0700192
Michael Jurkaaddcba62013-10-09 19:04:13 -0700193 if (useUninstallLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800194 setCompoundDrawablesRelativeWithIntrinsicBounds(mUninstallDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000195 } else if (useDeleteLabel) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800196 setCompoundDrawablesRelativeWithIntrinsicBounds(mRemoveDrawable, null, null, null);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000197 } else {
198 isVisible = false;
Adam Cohenebea84d2011-11-09 17:20:41 -0800199 }
Winson Chung947245b2012-05-15 16:34:19 -0700200 mCurrentDrawable = (TransitionDrawable) getCurrentDrawable();
Adam Cohenebea84d2011-11-09 17:20:41 -0800201
Winson Chung4c98d922011-05-31 16:50:48 -0700202 mActive = isVisible;
Winson Chunga48487a2012-03-20 16:19:37 -0700203 resetHoverColor();
Winson Chunga6427b12011-07-27 10:53:39 -0700204 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000205 if (isVisible && getText().length() > 0) {
Michael Jurkaaddcba62013-10-09 19:04:13 -0700206 setText(useUninstallLabel ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700207 : R.string.delete_target_label);
208 }
209 }
210
211 @Override
212 public void onDragEnd() {
213 super.onDragEnd();
214 mActive = false;
215 }
216
217 public void onDragEnter(DragObject d) {
218 super.onDragEnter(d);
219
Winson Chunga48487a2012-03-20 16:19:37 -0700220 setHoverColor();
Winson Chung4c98d922011-05-31 16:50:48 -0700221 }
222
223 public void onDragExit(DragObject d) {
224 super.onDragExit(d);
225
Winson Chungaaa530a2011-07-11 21:06:30 -0700226 if (!d.dragComplete) {
Winson Chunga48487a2012-03-20 16:19:37 -0700227 resetHoverColor();
Winson Chung61967cb2012-02-28 18:11:33 -0800228 } else {
229 // Restore the hover color if we are deleting
230 d.dragView.setColor(mHoverColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700231 }
Winson Chung4c98d922011-05-31 16:50:48 -0700232 }
233
Adam Cohened66b2b2012-01-23 17:28:51 -0800234 private void animateToTrashAndCompleteDrop(final DragObject d) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700235 final DragLayer dragLayer = mLauncher.getDragLayer();
236 final Rect from = new Rect();
Adam Cohened66b2b2012-01-23 17:28:51 -0800237 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Adam Cohenfe9da812014-08-04 17:08:01 -0700238
239 int width = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicWidth();
240 int height = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicHeight();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700241 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Adam Cohenfe9da812014-08-04 17:08:01 -0700242 width, height);
Michael Jurka1e2f4652013-07-08 18:03:46 -0700243 final float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800244
Adam Cohend4d7aa52011-07-19 21:47:37 -0700245 mSearchDropTargetBar.deferOnDragEnd();
Michael Jurka1e2f4652013-07-08 18:03:46 -0700246 deferCompleteDropIfUninstalling(d);
247
Adam Cohend4d7aa52011-07-19 21:47:37 -0700248 Runnable onAnimationEndRunnable = new Runnable() {
249 @Override
250 public void run() {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700251 completeDrop(d);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700252 mSearchDropTargetBar.onDragEnd();
Sunny Goyal8498eb42014-10-16 12:08:41 -0700253 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700254 }
255 };
Winson Chung61967cb2012-02-28 18:11:33 -0800256 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700257 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800258 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800259 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700260 }
261
Michael Jurka1e2f4652013-07-08 18:03:46 -0700262 private void deferCompleteDropIfUninstalling(DragObject d) {
263 mWaitingForUninstall = false;
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200264 if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700265 if (d.dragSource instanceof Folder) {
266 ((Folder) d.dragSource).deferCompleteDropAfterUninstallActivity();
267 } else if (d.dragSource instanceof Workspace) {
268 ((Workspace) d.dragSource).deferCompleteDropAfterUninstallActivity();
269 }
270 mWaitingForUninstall = true;
271 }
272 }
Winson Chung4c98d922011-05-31 16:50:48 -0700273
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200274 private boolean isUninstallFromWorkspace(DragObject d) {
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200275 return false;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700276 }
277
Adam Cohen091440a2015-03-18 14:16:05 -0700278 @Thunk 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)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800283 uninstallApp(mLauncher, (AppInfo) item);
Michael Jurka5e7af5d2013-09-14 18:35:38 +0200284 } else if (isUninstallFromWorkspace(d)) {
Michael Jurka1e2f4652013-07-08 18:03:46 -0700285 ShortcutInfo shortcut = (ShortcutInfo) item;
Kenny Guyd31df542014-06-30 15:12:11 +0100286 if (shortcut.intent != null && shortcut.intent.getComponent() != null) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200287 final ComponentName componentName = shortcut.intent.getComponent();
288 final DragSource dragSource = d.dragSource;
Kenny Guyd31df542014-06-30 15:12:11 +0100289 final UserHandleCompat user = shortcut.user;
290 mWaitingForUninstall = mLauncher.startApplicationUninstallActivity(
291 componentName, shortcut.flags, user);
Michael Jurkaf3007582013-08-21 14:33:57 +0200292 if (mWaitingForUninstall) {
293 final Runnable checkIfUninstallWasSuccess = new Runnable() {
294 @Override
295 public void run() {
296 mWaitingForUninstall = false;
297 String packageName = componentName.getPackageName();
Kenny Guyed131872014-04-30 03:02:21 +0100298 boolean uninstallSuccessful = !AllAppsList.packageHasActivities(
Kenny Guyd31df542014-06-30 15:12:11 +0100299 getContext(), packageName, user);
Michael Jurkaf3007582013-08-21 14:33:57 +0200300 if (dragSource instanceof Folder) {
301 ((Folder) dragSource).
302 onUninstallActivityReturned(uninstallSuccessful);
303 } else if (dragSource instanceof Workspace) {
304 ((Workspace) dragSource).
305 onUninstallActivityReturned(uninstallSuccessful);
306 }
307 }
308 };
309 mLauncher.addOnResumeCallback(checkIfUninstallWasSuccess);
310 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700311 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800312 } else if (isDragSourceWorkspaceOrFolder(d)) {
313 removeWorkspaceOrFolderItem(mLauncher, item, null);
Winson Chung4c98d922011-05-31 16:50:48 -0700314 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700315 if (wasWaitingForUninstall && !mWaitingForUninstall) {
316 if (d.dragSource instanceof Folder) {
317 ((Folder) d.dragSource).onUninstallActivityReturned(false);
318 } else if (d.dragSource instanceof Workspace) {
319 ((Workspace) d.dragSource).onUninstallActivityReturned(false);
320 }
321 }
Winson Chung4c98d922011-05-31 16:50:48 -0700322 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700323
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800324 public static void uninstallApp(Launcher launcher, AppInfo info) {
325 launcher.startApplicationUninstallActivity(info.componentName, info.flags, info.user);
326 }
327
328 /**
329 * Removes the item from the workspace. If the view is not null, it also removes the view.
330 * @return true if the item was removed.
331 */
332 public static boolean removeWorkspaceOrFolderItem(Launcher launcher, ItemInfo item, View view) {
333 if (item instanceof ShortcutInfo) {
334 LauncherModel.deleteItemFromDatabase(launcher, item);
335 } else if (item instanceof FolderInfo) {
336 FolderInfo folder = (FolderInfo) item;
337 launcher.removeFolder(folder);
338 LauncherModel.deleteFolderContentsFromDatabase(launcher, folder);
339 } else if (item instanceof LauncherAppWidgetInfo) {
340 final LauncherAppWidgetInfo widget = (LauncherAppWidgetInfo) item;
341
342 // Remove the widget from the workspace
343 launcher.removeAppWidget(widget);
344 LauncherModel.deleteItemFromDatabase(launcher, widget);
345
346 final LauncherAppWidgetHost appWidgetHost = launcher.getAppWidgetHost();
347
348 if (appWidgetHost != null && !widget.isCustomWidget()
349 && widget.isWidgetIdValid()) {
350 // Deleting an app widget ID is a void call but writes to disk before returning
351 // to the caller...
352 new AsyncTask<Void, Void, Void>() {
353 public Void doInBackground(Void ... args) {
354 appWidgetHost.deleteAppWidgetId(widget.appWidgetId);
355 return null;
356 }
357 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
358 }
359 } else {
360 return false;
361 }
362
363 if (view != null) {
364 launcher.getWorkspace().removeWorkspaceItem(view);
365 launcher.getWorkspace().stripEmptyScreens();
366 }
367 return true;
368 }
369
Adam Cohend4d7aa52011-07-19 21:47:37 -0700370 public void onDrop(DragObject d) {
371 animateToTrashAndCompleteDrop(d);
372 }
Winson Chung043f2af2012-03-01 16:09:54 -0800373
374 /**
375 * Creates an animation from the current drag view to the delete trash icon.
376 */
377 private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer,
378 DragObject d, PointF vel, ViewConfiguration config) {
Adam Cohenfe9da812014-08-04 17:08:01 -0700379
380 int width = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicWidth();
381 int height = mCurrentDrawable == null ? 0 : mCurrentDrawable.getIntrinsicHeight();
Winson Chung043f2af2012-03-01 16:09:54 -0800382 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
Adam Cohenfe9da812014-08-04 17:08:01 -0700383 width, height);
Winson Chung043f2af2012-03-01 16:09:54 -0800384 final Rect from = new Rect();
385 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
386
387 // Calculate how far along the velocity vector we should put the intermediate point on
388 // the bezier curve
389 float velocity = Math.abs(vel.length());
390 float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
391 int offsetY = (int) (-from.top * vp);
392 int offsetX = (int) (offsetY / (vel.y / vel.x));
393 final float y2 = from.top + offsetY; // intermediate t/l
394 final float x2 = from.left + offsetX;
395 final float x1 = from.left; // drag view t/l
396 final float y1 = from.top;
397 final float x3 = to.left; // delete target t/l
398 final float y3 = to.top;
399
400 final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {
401 @Override
402 public float getInterpolation(float t) {
403 return t * t * t * t * t * t * t * t;
404 }
405 };
406 return new AnimatorUpdateListener() {
407 @Override
408 public void onAnimationUpdate(ValueAnimator animation) {
409 final DragView dragView = (DragView) dragLayer.getAnimatedView();
410 float t = ((Float) animation.getAnimatedValue()).floatValue();
411 float tp = scaleAlphaInterpolator.getInterpolation(t);
412 float initialScale = dragView.getInitialScale();
413 float finalAlpha = 0.5f;
414 float scale = dragView.getScaleX();
415 float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
416 float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
417 float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) +
418 (t * t) * x3;
419 float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) +
420 (t * t) * y3;
421
422 dragView.setTranslationX(x);
423 dragView.setTranslationY(y);
424 dragView.setScaleX(initialScale * (1f - tp));
425 dragView.setScaleY(initialScale * (1f - tp));
426 dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
427 }
428 };
429 }
430
431 /**
432 * Creates an animation from the current drag view along its current velocity vector.
433 * For this animation, the alpha runs for a fixed duration and we update the position
434 * progressively.
435 */
436 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
Winson Chung043f2af2012-03-01 16:09:54 -0800437 private DragLayer mDragLayer;
438 private PointF mVelocity;
439 private Rect mFrom;
440 private long mPrevTime;
441 private boolean mHasOffsetForScale;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700442 private float mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800443
Winson Chung9658b1e2012-04-09 18:30:07 -0700444 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
Winson Chung043f2af2012-03-01 16:09:54 -0800445
446 public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from,
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700447 long startTime, float friction) {
Winson Chung043f2af2012-03-01 16:09:54 -0800448 mDragLayer = dragLayer;
449 mVelocity = vel;
450 mFrom = from;
451 mPrevTime = startTime;
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700452 mFriction = 1f - (dragLayer.getResources().getDisplayMetrics().density * friction);
Winson Chung043f2af2012-03-01 16:09:54 -0800453 }
454
455 @Override
456 public void onAnimationUpdate(ValueAnimator animation) {
457 final DragView dragView = (DragView) mDragLayer.getAnimatedView();
458 float t = ((Float) animation.getAnimatedValue()).floatValue();
459 long curTime = AnimationUtils.currentAnimationTimeMillis();
460
461 if (!mHasOffsetForScale) {
462 mHasOffsetForScale = true;
463 float scale = dragView.getScaleX();
464 float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f;
465 float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f;
466
467 mFrom.left += xOffset;
468 mFrom.top += yOffset;
469 }
470
471 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
472 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
473
474 dragView.setTranslationX(mFrom.left);
475 dragView.setTranslationY(mFrom.top);
476 dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
477
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700478 mVelocity.x *= mFriction;
479 mVelocity.y *= mFriction;
Winson Chung043f2af2012-03-01 16:09:54 -0800480 mPrevTime = curTime;
481 }
482 };
483 private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer,
484 DragObject d, PointF vel, final long startTime, final int duration,
485 ViewConfiguration config) {
486 final Rect from = new Rect();
487 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
488
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700489 return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime,
490 FLING_TO_DELETE_FRICTION);
Winson Chung043f2af2012-03-01 16:09:54 -0800491 }
492
493 public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
Hyunyoung Song3f471442015-04-08 19:01:34 -0700494 final boolean isWidgets = d.dragSource instanceof WidgetsContainerView;
495 final boolean isAllapps = d.dragSource instanceof AppsContainerView;
Winson Chunga48487a2012-03-20 16:19:37 -0700496
Winson Chung043f2af2012-03-01 16:09:54 -0800497 // Don't highlight the icon as it's animating
498 d.dragView.setColor(0);
499 d.dragView.updateInitialScaleToCurrentScale();
Winson Chunga48487a2012-03-20 16:19:37 -0700500 // Don't highlight the target if we are flinging from AllApps
Hyunyoung Song3f471442015-04-08 19:01:34 -0700501 if (isWidgets || isAllapps) {
Winson Chunga48487a2012-03-20 16:19:37 -0700502 resetHoverColor();
503 }
Winson Chung043f2af2012-03-01 16:09:54 -0800504
505 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
506 // Defer animating out the drop target if we are animating to it
507 mSearchDropTargetBar.deferOnDragEnd();
508 mSearchDropTargetBar.finishAnimations();
509 }
510
511 final ViewConfiguration config = ViewConfiguration.get(mLauncher);
512 final DragLayer dragLayer = mLauncher.getDragLayer();
Winson Chung6e1bdaf2012-05-29 17:03:45 -0700513 final int duration = FLING_DELETE_ANIMATION_DURATION;
Winson Chung043f2af2012-03-01 16:09:54 -0800514 final long startTime = AnimationUtils.currentAnimationTimeMillis();
515
516 // NOTE: Because it takes time for the first frame of animation to actually be
517 // called and we expect the animation to be a continuation of the fling, we have
518 // to account for the time that has elapsed since the fling finished. And since
519 // we don't have a startDelay, we will always get call to update when we call
520 // start() (which we want to ignore).
521 final TimeInterpolator tInterpolator = new TimeInterpolator() {
522 private int mCount = -1;
523 private float mOffset = 0f;
524
525 @Override
526 public float getInterpolation(float t) {
527 if (mCount < 0) {
528 mCount++;
529 } else if (mCount == 0) {
530 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
531 startTime) / duration);
532 mCount++;
533 }
534 return Math.min(1f, mOffset + t);
535 }
536 };
537 AnimatorUpdateListener updateCb = null;
538 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
539 updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
540 } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
541 updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime,
542 duration, config);
543 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700544 deferCompleteDropIfUninstalling(d);
545
Winson Chung043f2af2012-03-01 16:09:54 -0800546 Runnable onAnimationEndRunnable = new Runnable() {
547 @Override
548 public void run() {
Winson Chunga48487a2012-03-20 16:19:37 -0700549 // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up
550 // itself, otherwise, complete the drop to initiate the deletion process
Hyunyoung Song3f471442015-04-08 19:01:34 -0700551 if (!isWidgets || !isAllapps) {
Winson Chunga48487a2012-03-20 16:19:37 -0700552 mLauncher.exitSpringLoadedDragMode();
553 completeDrop(d);
554 }
555 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800556 }
557 };
558 dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable,
559 DragLayer.ANIMATION_END_DISAPPEAR, null);
560 }
Winson Chung4c98d922011-05-31 16:50:48 -0700561}