Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 18 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 19 | import android.animation.TimeInterpolator; |
| 20 | import android.animation.ValueAnimator; |
| 21 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 22 | import android.content.ComponentName; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Michael Jurka | 5e7af5d | 2013-09-14 18:35:38 +0200 | [diff] [blame] | 24 | import android.content.Intent; |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 25 | import android.content.pm.ResolveInfo; |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 26 | import android.content.res.ColorStateList; |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 27 | import android.content.res.Configuration; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 28 | import android.content.res.Resources; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 29 | import android.graphics.PointF; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 30 | import android.graphics.Rect; |
Winson Chung | 967289b | 2011-06-30 18:09:30 -0700 | [diff] [blame] | 31 | import android.graphics.drawable.TransitionDrawable; |
Michael Jurka | 4346746 | 2013-11-14 12:03:58 +0100 | [diff] [blame] | 32 | import android.os.AsyncTask; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 33 | import android.util.AttributeSet; |
| 34 | import android.view.View; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 35 | import android.view.ViewConfiguration; |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 36 | import android.view.ViewGroup; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 37 | import android.view.animation.AnimationUtils; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 38 | import android.view.animation.DecelerateInterpolator; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 39 | import android.view.animation.LinearInterpolator; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 40 | |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 41 | import java.util.List; |
Michael Jurka | 5e7af5d | 2013-09-14 18:35:38 +0200 | [diff] [blame] | 42 | import java.util.Set; |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 43 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 44 | public class DeleteDropTarget extends ButtonDropTarget { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 45 | private static int DELETE_ANIMATION_DURATION = 285; |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 46 | private static int FLING_DELETE_ANIMATION_DURATION = 350; |
| 47 | private static float FLING_TO_DELETE_FRICTION = 0.035f; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 48 | private static int MODE_FLING_DELETE_TO_TRASH = 0; |
| 49 | private static int MODE_FLING_DELETE_ALONG_VECTOR = 1; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 50 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 51 | private final int mFlingDeleteMode = MODE_FLING_DELETE_ALONG_VECTOR; |
| 52 | |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 53 | private ColorStateList mOriginalTextColor; |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 54 | private TransitionDrawable mUninstallDrawable; |
| 55 | private TransitionDrawable mRemoveDrawable; |
| 56 | private TransitionDrawable mCurrentDrawable; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 57 | |
Michael Jurka | 24715c7 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 58 | private boolean mWaitingForUninstall = false; |
| 59 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 60 | public DeleteDropTarget(Context context, AttributeSet attrs) { |
| 61 | this(context, attrs, 0); |
| 62 | } |
| 63 | |
| 64 | public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 65 | super(context, attrs, defStyle); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected void onFinishInflate() { |
| 70 | super.onFinishInflate(); |
| 71 | |
| 72 | // Get the drawable |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 73 | mOriginalTextColor = getTextColors(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 74 | |
| 75 | // Get the hover color |
| 76 | Resources r = getResources(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 77 | mHoverColor = r.getColor(R.color.delete_target_hover_tint); |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 78 | mUninstallDrawable = (TransitionDrawable) |
| 79 | r.getDrawable(R.drawable.uninstall_target_selector); |
| 80 | mRemoveDrawable = (TransitionDrawable) r.getDrawable(R.drawable.remove_target_selector); |
| 81 | |
| 82 | mRemoveDrawable.setCrossFadeEnabled(true); |
| 83 | mUninstallDrawable.setCrossFadeEnabled(true); |
| 84 | |
| 85 | // The current drawable is set to either the remove drawable or the uninstall drawable |
| 86 | // and is initially set to the remove drawable, as set in the layout xml. |
Winson Chung | 947245b | 2012-05-15 16:34:19 -0700 | [diff] [blame] | 87 | mCurrentDrawable = (TransitionDrawable) getCurrentDrawable(); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 88 | |
| 89 | // Remove the text in the Phone UI in landscape |
| 90 | int orientation = getResources().getConfiguration().orientation; |
| 91 | if (orientation == Configuration.ORIENTATION_LANDSCAPE) { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 92 | if (!LauncherAppState.getInstance().isScreenLarge()) { |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 93 | setText(""); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | private boolean isAllAppsApplication(DragSource source, Object info) { |
Mathew Inwood | 1eeb3fc | 2013-11-25 17:01:34 +0000 | [diff] [blame] | 99 | return source.supportsAppInfoDropTarget() && (info instanceof AppInfo); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 100 | } |
| 101 | private boolean isAllAppsWidget(DragSource source, Object info) { |
Winson Chung | 11a4937 | 2012-04-27 15:12:38 -0700 | [diff] [blame] | 102 | if (source instanceof AppsCustomizePagedView) { |
| 103 | if (info instanceof PendingAddItemInfo) { |
| 104 | PendingAddItemInfo addInfo = (PendingAddItemInfo) info; |
| 105 | switch (addInfo.itemType) { |
| 106 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
| 107 | case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: |
| 108 | return true; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | return false; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 113 | } |
Michael Jurka | 0b4870d | 2011-07-10 13:39:08 -0700 | [diff] [blame] | 114 | private boolean isDragSourceWorkspaceOrFolder(DragObject d) { |
| 115 | return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 116 | } |
Michael Jurka | 0b4870d | 2011-07-10 13:39:08 -0700 | [diff] [blame] | 117 | private boolean isWorkspaceOrFolderApplication(DragObject d) { |
| 118 | return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo); |
| 119 | } |
| 120 | private boolean isWorkspaceOrFolderWidget(DragObject d) { |
| 121 | return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 122 | } |
| 123 | private boolean isWorkspaceFolder(DragObject d) { |
| 124 | return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo); |
| 125 | } |
| 126 | |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 127 | private void setHoverColor() { |
| 128 | mCurrentDrawable.startTransition(mTransitionDuration); |
| 129 | setTextColor(mHoverColor); |
| 130 | } |
| 131 | private void resetHoverColor() { |
| 132 | mCurrentDrawable.resetTransition(); |
| 133 | setTextColor(mOriginalTextColor); |
| 134 | } |
| 135 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 136 | @Override |
| 137 | public boolean acceptDrop(DragObject d) { |
Adam Cohen | 7c4c510 | 2013-06-14 17:42:35 -0700 | [diff] [blame] | 138 | 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 || |
| 145 | item.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) { |
| 146 | return true; |
| 147 | } |
Michael Jurka | ed3d4df | 2013-09-12 18:09:24 +0200 | [diff] [blame] | 148 | |
| 149 | if (!AppsCustomizePagedView.DISABLE_ALL_APPS && |
Michael Jurka | 5e7af5d | 2013-09-14 18:35:38 +0200 | [diff] [blame] | 150 | item.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) { |
Michael Jurka | ed3d4df | 2013-09-12 18:09:24 +0200 | [diff] [blame] | 151 | return true; |
| 152 | } |
| 153 | |
Michael Jurka | 5e7af5d | 2013-09-14 18:35:38 +0200 | [diff] [blame] | 154 | if (!AppsCustomizePagedView.DISABLE_ALL_APPS && |
| 155 | item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION && |
| 156 | item instanceof AppInfo) { |
| 157 | AppInfo appInfo = (AppInfo) info; |
| 158 | return (appInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0; |
| 159 | } |
| 160 | |
Michael Jurka | 9bd4d28 | 2013-09-05 19:21:31 +0200 | [diff] [blame] | 161 | if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION && |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 162 | item instanceof ShortcutInfo) { |
Michael Jurka | 9bd4d28 | 2013-09-05 19:21:31 +0200 | [diff] [blame] | 163 | if (AppsCustomizePagedView.DISABLE_ALL_APPS) { |
| 164 | ShortcutInfo shortcutInfo = (ShortcutInfo) info; |
| 165 | return (shortcutInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0; |
| 166 | } else { |
| 167 | return true; |
| 168 | } |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 169 | } |
Adam Cohen | 947dc54 | 2013-06-06 22:43:33 -0700 | [diff] [blame] | 170 | } |
Adam Cohen | 7c4c510 | 2013-06-14 17:42:35 -0700 | [diff] [blame] | 171 | return false; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void onDragStart(DragSource source, Object info, int dragAction) { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 176 | boolean isVisible = true; |
Michael Jurka | addcba6 | 2013-10-09 19:04:13 -0700 | [diff] [blame] | 177 | boolean useUninstallLabel = !AppsCustomizePagedView.DISABLE_ALL_APPS && |
| 178 | isAllAppsApplication(source, info); |
Mathew Inwood | 1eeb3fc | 2013-11-25 17:01:34 +0000 | [diff] [blame] | 179 | boolean useDeleteLabel = !useUninstallLabel && source.supportsDeleteDropTarget(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 180 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 181 | // If we are dragging an application from AppsCustomize, only show the control if we can |
Winson Chung | e01af63 | 2013-09-26 10:43:20 -0700 | [diff] [blame] | 182 | // delete the app (it was downloaded), and rename the string to "uninstall" in such a case. |
| 183 | // Hide the delete target if it is a widget from AppsCustomize. |
| 184 | if (!willAcceptDrop(info) || isAllAppsWidget(source, info)) { |
Adam Cohen | 947dc54 | 2013-06-06 22:43:33 -0700 | [diff] [blame] | 185 | isVisible = false; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Michael Jurka | addcba6 | 2013-10-09 19:04:13 -0700 | [diff] [blame] | 188 | if (useUninstallLabel) { |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 189 | setCompoundDrawablesRelativeWithIntrinsicBounds(mUninstallDrawable, null, null, null); |
Mathew Inwood | 1eeb3fc | 2013-11-25 17:01:34 +0000 | [diff] [blame] | 190 | } else if (useDeleteLabel) { |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 191 | setCompoundDrawablesRelativeWithIntrinsicBounds(mRemoveDrawable, null, null, null); |
Mathew Inwood | 1eeb3fc | 2013-11-25 17:01:34 +0000 | [diff] [blame] | 192 | } else { |
| 193 | isVisible = false; |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 194 | } |
Winson Chung | 947245b | 2012-05-15 16:34:19 -0700 | [diff] [blame] | 195 | mCurrentDrawable = (TransitionDrawable) getCurrentDrawable(); |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 196 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 197 | mActive = isVisible; |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 198 | resetHoverColor(); |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 199 | ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE); |
Mathew Inwood | 1eeb3fc | 2013-11-25 17:01:34 +0000 | [diff] [blame] | 200 | if (isVisible && getText().length() > 0) { |
Michael Jurka | addcba6 | 2013-10-09 19:04:13 -0700 | [diff] [blame] | 201 | setText(useUninstallLabel ? R.string.delete_target_uninstall_label |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 202 | : R.string.delete_target_label); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void onDragEnd() { |
| 208 | super.onDragEnd(); |
| 209 | mActive = false; |
| 210 | } |
| 211 | |
| 212 | public void onDragEnter(DragObject d) { |
| 213 | super.onDragEnter(d); |
| 214 | |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 215 | setHoverColor(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | public void onDragExit(DragObject d) { |
| 219 | super.onDragExit(d); |
| 220 | |
Winson Chung | aaa530a | 2011-07-11 21:06:30 -0700 | [diff] [blame] | 221 | if (!d.dragComplete) { |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 222 | resetHoverColor(); |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 223 | } else { |
| 224 | // Restore the hover color if we are deleting |
| 225 | d.dragView.setColor(mHoverColor); |
Winson Chung | aaa530a | 2011-07-11 21:06:30 -0700 | [diff] [blame] | 226 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 229 | private void animateToTrashAndCompleteDrop(final DragObject d) { |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 230 | final DragLayer dragLayer = mLauncher.getDragLayer(); |
| 231 | final Rect from = new Rect(); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 232 | dragLayer.getViewRectRelativeToSelf(d.dragView, from); |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 233 | final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(), |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 234 | mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight()); |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 235 | final float scale = (float) to.width() / from.width(); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 236 | |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 237 | mSearchDropTargetBar.deferOnDragEnd(); |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 238 | deferCompleteDropIfUninstalling(d); |
| 239 | |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 240 | Runnable onAnimationEndRunnable = new Runnable() { |
| 241 | @Override |
| 242 | public void run() { |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 243 | completeDrop(d); |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 244 | mSearchDropTargetBar.onDragEnd(); |
| 245 | mLauncher.exitSpringLoadedDragMode(); |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 246 | } |
| 247 | }; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 248 | dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f, |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 249 | DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2), |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 250 | new LinearInterpolator(), onAnimationEndRunnable, |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 251 | DragLayer.ANIMATION_END_DISAPPEAR, null); |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 254 | private void deferCompleteDropIfUninstalling(DragObject d) { |
| 255 | mWaitingForUninstall = false; |
Michael Jurka | 5e7af5d | 2013-09-14 18:35:38 +0200 | [diff] [blame] | 256 | if (isUninstallFromWorkspace(d)) { |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 257 | if (d.dragSource instanceof Folder) { |
| 258 | ((Folder) d.dragSource).deferCompleteDropAfterUninstallActivity(); |
| 259 | } else if (d.dragSource instanceof Workspace) { |
| 260 | ((Workspace) d.dragSource).deferCompleteDropAfterUninstallActivity(); |
| 261 | } |
| 262 | mWaitingForUninstall = true; |
| 263 | } |
| 264 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 265 | |
Michael Jurka | 5e7af5d | 2013-09-14 18:35:38 +0200 | [diff] [blame] | 266 | private boolean isUninstallFromWorkspace(DragObject d) { |
| 267 | if (AppsCustomizePagedView.DISABLE_ALL_APPS && isWorkspaceOrFolderApplication(d)) { |
| 268 | ShortcutInfo shortcut = (ShortcutInfo) d.dragInfo; |
Nilesh Agrawal | dff0bfe | 2013-12-17 15:20:50 -0800 | [diff] [blame] | 269 | // Only allow manifest shortcuts to initiate an un-install. |
| 270 | return !InstallShortcutReceiver.isValidShortcutLaunchIntent(shortcut.intent); |
Michael Jurka | 5e7af5d | 2013-09-14 18:35:38 +0200 | [diff] [blame] | 271 | } |
| 272 | return false; |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Michael Jurka | f300758 | 2013-08-21 14:33:57 +0200 | [diff] [blame] | 275 | private void completeDrop(DragObject d) { |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 276 | ItemInfo item = (ItemInfo) d.dragInfo; |
| 277 | boolean wasWaitingForUninstall = mWaitingForUninstall; |
| 278 | mWaitingForUninstall = false; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 279 | if (isAllAppsApplication(d.dragSource, item)) { |
| 280 | // Uninstall the application if it is being dragged from AppsCustomize |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 281 | AppInfo appInfo = (AppInfo) item; |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 282 | mLauncher.startApplicationUninstallActivity(appInfo.componentName, appInfo.flags); |
Michael Jurka | 5e7af5d | 2013-09-14 18:35:38 +0200 | [diff] [blame] | 283 | } else if (isUninstallFromWorkspace(d)) { |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 284 | ShortcutInfo shortcut = (ShortcutInfo) item; |
| 285 | if (shortcut.intent != null && shortcut.intent.getComponent() != null) { |
Michael Jurka | f300758 | 2013-08-21 14:33:57 +0200 | [diff] [blame] | 286 | final ComponentName componentName = shortcut.intent.getComponent(); |
| 287 | final DragSource dragSource = d.dragSource; |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 288 | int flags = AppInfo.initFlags( |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 289 | ShortcutInfo.getPackageInfo(getContext(), componentName.getPackageName())); |
| 290 | mWaitingForUninstall = |
| 291 | mLauncher.startApplicationUninstallActivity(componentName, flags); |
Michael Jurka | f300758 | 2013-08-21 14:33:57 +0200 | [diff] [blame] | 292 | if (mWaitingForUninstall) { |
| 293 | final Runnable checkIfUninstallWasSuccess = new Runnable() { |
| 294 | @Override |
| 295 | public void run() { |
| 296 | mWaitingForUninstall = false; |
| 297 | String packageName = componentName.getPackageName(); |
| 298 | List<ResolveInfo> activities = |
| 299 | AllAppsList.findActivitiesForPackage(getContext(), packageName); |
| 300 | boolean uninstallSuccessful = activities.size() == 0; |
| 301 | if (dragSource instanceof Folder) { |
| 302 | ((Folder) dragSource). |
| 303 | onUninstallActivityReturned(uninstallSuccessful); |
| 304 | } else if (dragSource instanceof Workspace) { |
| 305 | ((Workspace) dragSource). |
| 306 | onUninstallActivityReturned(uninstallSuccessful); |
| 307 | } |
| 308 | } |
| 309 | }; |
| 310 | mLauncher.addOnResumeCallback(checkIfUninstallWasSuccess); |
| 311 | } |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 312 | } |
Michael Jurka | 0b4870d | 2011-07-10 13:39:08 -0700 | [diff] [blame] | 313 | } else if (isWorkspaceOrFolderApplication(d)) { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 314 | LauncherModel.deleteItemFromDatabase(mLauncher, item); |
| 315 | } else if (isWorkspaceFolder(d)) { |
| 316 | // Remove the folder from the workspace and delete the contents from launcher model |
| 317 | FolderInfo folderInfo = (FolderInfo) item; |
| 318 | mLauncher.removeFolder(folderInfo); |
| 319 | LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo); |
Michael Jurka | 0b4870d | 2011-07-10 13:39:08 -0700 | [diff] [blame] | 320 | } else if (isWorkspaceOrFolderWidget(d)) { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 321 | // Remove the widget from the workspace |
| 322 | mLauncher.removeAppWidget((LauncherAppWidgetInfo) item); |
| 323 | LauncherModel.deleteItemFromDatabase(mLauncher, item); |
| 324 | |
| 325 | final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item; |
| 326 | final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost(); |
| 327 | if (appWidgetHost != null) { |
| 328 | // Deleting an app widget ID is a void call but writes to disk before returning |
| 329 | // to the caller... |
Michael Jurka | 4346746 | 2013-11-14 12:03:58 +0100 | [diff] [blame] | 330 | new AsyncTask<Void, Void, Void>() { |
| 331 | public Void doInBackground(Void ... args) { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 332 | appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId); |
Michael Jurka | 4346746 | 2013-11-14 12:03:58 +0100 | [diff] [blame] | 333 | return null; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 334 | } |
Michael Jurka | 4346746 | 2013-11-14 12:03:58 +0100 | [diff] [blame] | 335 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 336 | } |
| 337 | } |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 338 | if (wasWaitingForUninstall && !mWaitingForUninstall) { |
| 339 | if (d.dragSource instanceof Folder) { |
| 340 | ((Folder) d.dragSource).onUninstallActivityReturned(false); |
| 341 | } else if (d.dragSource instanceof Workspace) { |
| 342 | ((Workspace) d.dragSource).onUninstallActivityReturned(false); |
| 343 | } |
| 344 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 345 | } |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 346 | |
| 347 | public void onDrop(DragObject d) { |
| 348 | animateToTrashAndCompleteDrop(d); |
| 349 | } |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 350 | |
| 351 | /** |
| 352 | * Creates an animation from the current drag view to the delete trash icon. |
| 353 | */ |
| 354 | private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer, |
| 355 | DragObject d, PointF vel, ViewConfiguration config) { |
| 356 | final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(), |
| 357 | mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight()); |
| 358 | final Rect from = new Rect(); |
| 359 | dragLayer.getViewRectRelativeToSelf(d.dragView, from); |
| 360 | |
| 361 | // Calculate how far along the velocity vector we should put the intermediate point on |
| 362 | // the bezier curve |
| 363 | float velocity = Math.abs(vel.length()); |
| 364 | float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f)); |
| 365 | int offsetY = (int) (-from.top * vp); |
| 366 | int offsetX = (int) (offsetY / (vel.y / vel.x)); |
| 367 | final float y2 = from.top + offsetY; // intermediate t/l |
| 368 | final float x2 = from.left + offsetX; |
| 369 | final float x1 = from.left; // drag view t/l |
| 370 | final float y1 = from.top; |
| 371 | final float x3 = to.left; // delete target t/l |
| 372 | final float y3 = to.top; |
| 373 | |
| 374 | final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() { |
| 375 | @Override |
| 376 | public float getInterpolation(float t) { |
| 377 | return t * t * t * t * t * t * t * t; |
| 378 | } |
| 379 | }; |
| 380 | return new AnimatorUpdateListener() { |
| 381 | @Override |
| 382 | public void onAnimationUpdate(ValueAnimator animation) { |
| 383 | final DragView dragView = (DragView) dragLayer.getAnimatedView(); |
| 384 | float t = ((Float) animation.getAnimatedValue()).floatValue(); |
| 385 | float tp = scaleAlphaInterpolator.getInterpolation(t); |
| 386 | float initialScale = dragView.getInitialScale(); |
| 387 | float finalAlpha = 0.5f; |
| 388 | float scale = dragView.getScaleX(); |
| 389 | float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f; |
| 390 | float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f; |
| 391 | float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) + |
| 392 | (t * t) * x3; |
| 393 | float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) + |
| 394 | (t * t) * y3; |
| 395 | |
| 396 | dragView.setTranslationX(x); |
| 397 | dragView.setTranslationY(y); |
| 398 | dragView.setScaleX(initialScale * (1f - tp)); |
| 399 | dragView.setScaleY(initialScale * (1f - tp)); |
| 400 | dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp)); |
| 401 | } |
| 402 | }; |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Creates an animation from the current drag view along its current velocity vector. |
| 407 | * For this animation, the alpha runs for a fixed duration and we update the position |
| 408 | * progressively. |
| 409 | */ |
| 410 | private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 411 | private DragLayer mDragLayer; |
| 412 | private PointF mVelocity; |
| 413 | private Rect mFrom; |
| 414 | private long mPrevTime; |
| 415 | private boolean mHasOffsetForScale; |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 416 | private float mFriction; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 417 | |
Winson Chung | 9658b1e | 2012-04-09 18:30:07 -0700 | [diff] [blame] | 418 | private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 419 | |
| 420 | public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from, |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 421 | long startTime, float friction) { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 422 | mDragLayer = dragLayer; |
| 423 | mVelocity = vel; |
| 424 | mFrom = from; |
| 425 | mPrevTime = startTime; |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 426 | mFriction = 1f - (dragLayer.getResources().getDisplayMetrics().density * friction); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | @Override |
| 430 | public void onAnimationUpdate(ValueAnimator animation) { |
| 431 | final DragView dragView = (DragView) mDragLayer.getAnimatedView(); |
| 432 | float t = ((Float) animation.getAnimatedValue()).floatValue(); |
| 433 | long curTime = AnimationUtils.currentAnimationTimeMillis(); |
| 434 | |
| 435 | if (!mHasOffsetForScale) { |
| 436 | mHasOffsetForScale = true; |
| 437 | float scale = dragView.getScaleX(); |
| 438 | float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f; |
| 439 | float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f; |
| 440 | |
| 441 | mFrom.left += xOffset; |
| 442 | mFrom.top += yOffset; |
| 443 | } |
| 444 | |
| 445 | mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f); |
| 446 | mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f); |
| 447 | |
| 448 | dragView.setTranslationX(mFrom.left); |
| 449 | dragView.setTranslationY(mFrom.top); |
| 450 | dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t)); |
| 451 | |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 452 | mVelocity.x *= mFriction; |
| 453 | mVelocity.y *= mFriction; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 454 | mPrevTime = curTime; |
| 455 | } |
| 456 | }; |
| 457 | private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer, |
| 458 | DragObject d, PointF vel, final long startTime, final int duration, |
| 459 | ViewConfiguration config) { |
| 460 | final Rect from = new Rect(); |
| 461 | dragLayer.getViewRectRelativeToSelf(d.dragView, from); |
| 462 | |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 463 | return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime, |
| 464 | FLING_TO_DELETE_FRICTION); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) { |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 468 | final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView; |
| 469 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 470 | // Don't highlight the icon as it's animating |
| 471 | d.dragView.setColor(0); |
| 472 | d.dragView.updateInitialScaleToCurrentScale(); |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 473 | // Don't highlight the target if we are flinging from AllApps |
| 474 | if (isAllApps) { |
| 475 | resetHoverColor(); |
| 476 | } |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 477 | |
| 478 | if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) { |
| 479 | // Defer animating out the drop target if we are animating to it |
| 480 | mSearchDropTargetBar.deferOnDragEnd(); |
| 481 | mSearchDropTargetBar.finishAnimations(); |
| 482 | } |
| 483 | |
| 484 | final ViewConfiguration config = ViewConfiguration.get(mLauncher); |
| 485 | final DragLayer dragLayer = mLauncher.getDragLayer(); |
Winson Chung | 6e1bdaf | 2012-05-29 17:03:45 -0700 | [diff] [blame] | 486 | final int duration = FLING_DELETE_ANIMATION_DURATION; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 487 | final long startTime = AnimationUtils.currentAnimationTimeMillis(); |
| 488 | |
| 489 | // NOTE: Because it takes time for the first frame of animation to actually be |
| 490 | // called and we expect the animation to be a continuation of the fling, we have |
| 491 | // to account for the time that has elapsed since the fling finished. And since |
| 492 | // we don't have a startDelay, we will always get call to update when we call |
| 493 | // start() (which we want to ignore). |
| 494 | final TimeInterpolator tInterpolator = new TimeInterpolator() { |
| 495 | private int mCount = -1; |
| 496 | private float mOffset = 0f; |
| 497 | |
| 498 | @Override |
| 499 | public float getInterpolation(float t) { |
| 500 | if (mCount < 0) { |
| 501 | mCount++; |
| 502 | } else if (mCount == 0) { |
| 503 | mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() - |
| 504 | startTime) / duration); |
| 505 | mCount++; |
| 506 | } |
| 507 | return Math.min(1f, mOffset + t); |
| 508 | } |
| 509 | }; |
| 510 | AnimatorUpdateListener updateCb = null; |
| 511 | if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) { |
| 512 | updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config); |
| 513 | } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) { |
| 514 | updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime, |
| 515 | duration, config); |
| 516 | } |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 517 | deferCompleteDropIfUninstalling(d); |
| 518 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 519 | Runnable onAnimationEndRunnable = new Runnable() { |
| 520 | @Override |
| 521 | public void run() { |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 522 | // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up |
| 523 | // itself, otherwise, complete the drop to initiate the deletion process |
| 524 | if (!isAllApps) { |
| 525 | mLauncher.exitSpringLoadedDragMode(); |
| 526 | completeDrop(d); |
| 527 | } |
| 528 | mLauncher.getDragController().onDeferredEndFling(d); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 529 | } |
| 530 | }; |
| 531 | dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable, |
| 532 | DragLayer.ANIMATION_END_DISAPPEAR, null); |
| 533 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 534 | } |