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; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 21 | import android.graphics.PointF; |
Sunny Goyal | f37a214 | 2016-03-24 17:28:25 -0700 | [diff] [blame^] | 22 | import android.text.TextUtils; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 23 | import android.util.AttributeSet; |
| 24 | import android.view.View; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 25 | import android.view.animation.AnimationUtils; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 26 | |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 27 | import com.android.launcher3.dragndrop.DragLayer; |
Sunny Goyal | 2611943 | 2016-02-18 22:09:23 +0000 | [diff] [blame] | 28 | import com.android.launcher3.folder.Folder; |
Sunny Goyal | ddec734 | 2015-04-29 18:12:37 -0700 | [diff] [blame] | 29 | import com.android.launcher3.util.FlingAnimation; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 30 | import com.android.launcher3.util.Thunk; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 31 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 32 | public class DeleteDropTarget extends ButtonDropTarget { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 33 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 34 | public DeleteDropTarget(Context context, AttributeSet attrs) { |
| 35 | this(context, attrs, 0); |
| 36 | } |
| 37 | |
| 38 | public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 39 | super(context, attrs, defStyle); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | protected void onFinishInflate() { |
| 44 | super.onFinishInflate(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 45 | // Get the hover color |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 46 | mHoverColor = getResources().getColor(R.color.delete_target_hover_tint); |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 47 | |
Sunny Goyal | 40b626b | 2015-06-04 22:40:14 -0700 | [diff] [blame] | 48 | setDrawable(R.drawable.ic_remove_launcher); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 51 | @Override |
| 52 | public void onDragStart(DragSource source, ItemInfo info, int dragAction) { |
| 53 | super.onDragStart(source, info, dragAction); |
| 54 | setTextBasedOnDragSource(source); |
| 55 | } |
| 56 | |
| 57 | /** @return true for items that should have a "Remove" action in accessibility. */ |
| 58 | public static boolean supportsAccessibleDrop(ItemInfo info) { |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 59 | return (info instanceof ShortcutInfo) |
| 60 | || (info instanceof LauncherAppWidgetInfo) |
| 61 | || (info instanceof FolderInfo); |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 64 | @Override |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 65 | protected boolean supportsDrop(DragSource source, ItemInfo info) { |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 66 | return true; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Set the drop target's text to either "Remove" or "Cancel" depending on the drag source. |
| 71 | */ |
| 72 | public void setTextBasedOnDragSource(DragSource dragSource) { |
Sunny Goyal | f37a214 | 2016-03-24 17:28:25 -0700 | [diff] [blame^] | 73 | if (!TextUtils.isEmpty(getText())) { |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 74 | setText(dragSource.supportsDeleteDropTarget() ? R.string.remove_drop_target_label |
| 75 | : android.R.string.cancel); |
| 76 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 80 | @Thunk void completeDrop(DragObject d) { |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 81 | ItemInfo item = d.dragInfo; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 82 | if ((d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder)) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 83 | removeWorkspaceOrFolderItem(mLauncher, item, null); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 84 | } |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Removes the item from the workspace. If the view is not null, it also removes the view. |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 89 | */ |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 90 | public static void removeWorkspaceOrFolderItem(Launcher launcher, ItemInfo item, View view) { |
Winson | fa56b3f | 2015-09-14 12:01:13 -0700 | [diff] [blame] | 91 | // Remove the item from launcher and the db, we can ignore the containerInfo in this call |
| 92 | // because we already remove the drag view from the folder (if the drag originated from |
| 93 | // a folder) in Folder.beginDrag() |
Winson | 2949fb5 | 2015-09-24 09:56:11 -0700 | [diff] [blame] | 94 | launcher.removeItem(view, item, true /* deleteFromDb */); |
Winson | c0b52fe | 2015-09-09 16:38:15 -0700 | [diff] [blame] | 95 | launcher.getWorkspace().stripEmptyScreens(); |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 96 | launcher.getDragLayer().announceForAccessibility(launcher.getString(R.string.item_removed)); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Sunny Goyal | ddec734 | 2015-04-29 18:12:37 -0700 | [diff] [blame] | 99 | @Override |
| 100 | public void onFlingToDelete(final DragObject d, PointF vel) { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 101 | // Don't highlight the icon as it's animating |
| 102 | d.dragView.setColor(0); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 103 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 104 | final DragLayer dragLayer = mLauncher.getDragLayer(); |
Sunny Goyal | ddec734 | 2015-04-29 18:12:37 -0700 | [diff] [blame] | 105 | FlingAnimation fling = new FlingAnimation(d, vel, |
| 106 | getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(), |
| 107 | mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight()), |
| 108 | dragLayer); |
| 109 | |
| 110 | final int duration = fling.getDuration(); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 111 | final long startTime = AnimationUtils.currentAnimationTimeMillis(); |
| 112 | |
| 113 | // NOTE: Because it takes time for the first frame of animation to actually be |
| 114 | // called and we expect the animation to be a continuation of the fling, we have |
| 115 | // to account for the time that has elapsed since the fling finished. And since |
| 116 | // we don't have a startDelay, we will always get call to update when we call |
| 117 | // start() (which we want to ignore). |
| 118 | final TimeInterpolator tInterpolator = new TimeInterpolator() { |
| 119 | private int mCount = -1; |
| 120 | private float mOffset = 0f; |
| 121 | |
| 122 | @Override |
| 123 | public float getInterpolation(float t) { |
| 124 | if (mCount < 0) { |
| 125 | mCount++; |
| 126 | } else if (mCount == 0) { |
| 127 | mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() - |
| 128 | startTime) / duration); |
| 129 | mCount++; |
| 130 | } |
| 131 | return Math.min(1f, mOffset + t); |
| 132 | } |
| 133 | }; |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 134 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 135 | Runnable onAnimationEndRunnable = new Runnable() { |
| 136 | @Override |
| 137 | public void run() { |
Sunny Goyal | ddec734 | 2015-04-29 18:12:37 -0700 | [diff] [blame] | 138 | mLauncher.exitSpringLoadedDragMode(); |
| 139 | completeDrop(d); |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 140 | mLauncher.getDragController().onDeferredEndFling(d); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 141 | } |
| 142 | }; |
Sunny Goyal | ddec734 | 2015-04-29 18:12:37 -0700 | [diff] [blame] | 143 | |
| 144 | dragLayer.animateView(d.dragView, fling, duration, tInterpolator, onAnimationEndRunnable, |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 145 | DragLayer.ANIMATION_END_DISAPPEAR, null); |
| 146 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 147 | } |