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; |
Michael Jurka | 4346746 | 2013-11-14 12:03:58 +0100 | [diff] [blame] | 22 | import android.os.AsyncTask; |
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 | |
Sunny Goyal | ddec734 | 2015-04-29 18:12:37 -0700 | [diff] [blame] | 27 | import com.android.launcher3.util.FlingAnimation; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 28 | import com.android.launcher3.util.Thunk; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 29 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 30 | public class DeleteDropTarget extends ButtonDropTarget { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 31 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 32 | public DeleteDropTarget(Context context, AttributeSet attrs) { |
| 33 | this(context, attrs, 0); |
| 34 | } |
| 35 | |
| 36 | public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 37 | super(context, attrs, defStyle); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | protected void onFinishInflate() { |
| 42 | super.onFinishInflate(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 43 | // Get the hover color |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 44 | mHoverColor = getResources().getColor(R.color.delete_target_hover_tint); |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 45 | |
Sunny Goyal | 40b626b | 2015-06-04 22:40:14 -0700 | [diff] [blame] | 46 | setDrawable(R.drawable.ic_remove_launcher); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame^] | 49 | public static boolean supportsDrop(ItemInfo info) { |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 50 | return (info instanceof ShortcutInfo) |
| 51 | || (info instanceof LauncherAppWidgetInfo) |
| 52 | || (info instanceof FolderInfo); |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 55 | @Override |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame^] | 56 | protected boolean supportsDrop(DragSource source, ItemInfo info) { |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 57 | return source.supportsDeleteDropTarget() && supportsDrop(info); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 61 | @Thunk void completeDrop(DragObject d) { |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame^] | 62 | ItemInfo item = d.dragInfo; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 63 | if ((d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder)) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 64 | removeWorkspaceOrFolderItem(mLauncher, item, null); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 65 | } |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Removes the item from the workspace. If the view is not null, it also removes the view. |
| 70 | * @return true if the item was removed. |
| 71 | */ |
| 72 | public static boolean removeWorkspaceOrFolderItem(Launcher launcher, ItemInfo item, View view) { |
| 73 | if (item instanceof ShortcutInfo) { |
| 74 | LauncherModel.deleteItemFromDatabase(launcher, item); |
| 75 | } else if (item instanceof FolderInfo) { |
| 76 | FolderInfo folder = (FolderInfo) item; |
| 77 | launcher.removeFolder(folder); |
| 78 | LauncherModel.deleteFolderContentsFromDatabase(launcher, folder); |
| 79 | } else if (item instanceof LauncherAppWidgetInfo) { |
| 80 | final LauncherAppWidgetInfo widget = (LauncherAppWidgetInfo) item; |
| 81 | |
| 82 | // Remove the widget from the workspace |
| 83 | launcher.removeAppWidget(widget); |
| 84 | LauncherModel.deleteItemFromDatabase(launcher, widget); |
| 85 | |
| 86 | final LauncherAppWidgetHost appWidgetHost = launcher.getAppWidgetHost(); |
| 87 | |
| 88 | if (appWidgetHost != null && !widget.isCustomWidget() |
| 89 | && widget.isWidgetIdValid()) { |
| 90 | // Deleting an app widget ID is a void call but writes to disk before returning |
| 91 | // to the caller... |
| 92 | new AsyncTask<Void, Void, Void>() { |
| 93 | public Void doInBackground(Void ... args) { |
| 94 | appWidgetHost.deleteAppWidgetId(widget.appWidgetId); |
| 95 | return null; |
| 96 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 97 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 98 | } |
| 99 | } else { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | if (view != null) { |
| 104 | launcher.getWorkspace().removeWorkspaceItem(view); |
| 105 | launcher.getWorkspace().stripEmptyScreens(); |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
Sunny Goyal | ddec734 | 2015-04-29 18:12:37 -0700 | [diff] [blame] | 110 | @Override |
| 111 | public void onFlingToDelete(final DragObject d, PointF vel) { |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 112 | // Don't highlight the icon as it's animating |
| 113 | d.dragView.setColor(0); |
| 114 | d.dragView.updateInitialScaleToCurrentScale(); |
| 115 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 116 | final DragLayer dragLayer = mLauncher.getDragLayer(); |
Sunny Goyal | ddec734 | 2015-04-29 18:12:37 -0700 | [diff] [blame] | 117 | FlingAnimation fling = new FlingAnimation(d, vel, |
| 118 | getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(), |
| 119 | mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight()), |
| 120 | dragLayer); |
| 121 | |
| 122 | final int duration = fling.getDuration(); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 123 | final long startTime = AnimationUtils.currentAnimationTimeMillis(); |
| 124 | |
| 125 | // NOTE: Because it takes time for the first frame of animation to actually be |
| 126 | // called and we expect the animation to be a continuation of the fling, we have |
| 127 | // to account for the time that has elapsed since the fling finished. And since |
| 128 | // we don't have a startDelay, we will always get call to update when we call |
| 129 | // start() (which we want to ignore). |
| 130 | final TimeInterpolator tInterpolator = new TimeInterpolator() { |
| 131 | private int mCount = -1; |
| 132 | private float mOffset = 0f; |
| 133 | |
| 134 | @Override |
| 135 | public float getInterpolation(float t) { |
| 136 | if (mCount < 0) { |
| 137 | mCount++; |
| 138 | } else if (mCount == 0) { |
| 139 | mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() - |
| 140 | startTime) / duration); |
| 141 | mCount++; |
| 142 | } |
| 143 | return Math.min(1f, mOffset + t); |
| 144 | } |
| 145 | }; |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 146 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 147 | Runnable onAnimationEndRunnable = new Runnable() { |
| 148 | @Override |
| 149 | public void run() { |
Sunny Goyal | ddec734 | 2015-04-29 18:12:37 -0700 | [diff] [blame] | 150 | mLauncher.exitSpringLoadedDragMode(); |
| 151 | completeDrop(d); |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 152 | mLauncher.getDragController().onDeferredEndFling(d); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 153 | } |
| 154 | }; |
Sunny Goyal | ddec734 | 2015-04-29 18:12:37 -0700 | [diff] [blame] | 155 | |
| 156 | dragLayer.animateView(d.dragView, fling, duration, tInterpolator, onAnimationEndRunnable, |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 157 | DragLayer.ANIMATION_END_DISAPPEAR, null); |
| 158 | } |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 159 | |
| 160 | @Override |
| 161 | protected String getAccessibilityDropConfirmation() { |
| 162 | return getResources().getString(R.string.item_removed); |
| 163 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 164 | } |