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