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