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 | |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame^] | 19 | import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_DROPPED_ON_CANCEL; |
| 20 | import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_DROPPED_ON_REMOVE; |
Tony Wickham | 03f2701 | 2019-04-25 14:22:54 -0700 | [diff] [blame] | 21 | import static com.android.launcher3.userevent.nano.LauncherLogProto.Action.Touch.TAP; |
| 22 | import static com.android.launcher3.userevent.nano.LauncherLogProto.ControlType.UNDO; |
| 23 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Sunny Goyal | f37a214 | 2016-03-24 17:28:25 -0700 | [diff] [blame] | 25 | import android.text.TextUtils; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 26 | import android.util.AttributeSet; |
| 27 | import android.view.View; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 28 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 29 | import com.android.launcher3.accessibility.LauncherAccessibilityDelegate; |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 30 | import com.android.launcher3.dragndrop.DragOptions; |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame] | 31 | import com.android.launcher3.logging.LoggerUtils; |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame^] | 32 | import com.android.launcher3.logging.StatsLogManager; |
Tony Wickham | 6a71a5b | 2018-08-21 11:40:23 -0700 | [diff] [blame] | 33 | import com.android.launcher3.model.ModelWriter; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 34 | import com.android.launcher3.model.data.FolderInfo; |
| 35 | import com.android.launcher3.model.data.ItemInfo; |
| 36 | import com.android.launcher3.model.data.LauncherAppWidgetInfo; |
| 37 | import com.android.launcher3.model.data.WorkspaceItemInfo; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 38 | import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType; |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame] | 39 | import com.android.launcher3.userevent.nano.LauncherLogProto.Target; |
Tony Wickham | 6a71a5b | 2018-08-21 11:40:23 -0700 | [diff] [blame] | 40 | import com.android.launcher3.views.Snackbar; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 41 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 42 | public class DeleteDropTarget extends ButtonDropTarget { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 43 | |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame^] | 44 | private final StatsLogManager mStatsLogManager; |
| 45 | |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame] | 46 | private int mControlType = ControlType.DEFAULT_CONTROLTYPE; |
| 47 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 48 | public DeleteDropTarget(Context context, AttributeSet attrs) { |
| 49 | this(context, attrs, 0); |
| 50 | } |
| 51 | |
| 52 | public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 53 | super(context, attrs, defStyle); |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame^] | 54 | this.mStatsLogManager = StatsLogManager.newInstance(context); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Override |
| 58 | protected void onFinishInflate() { |
| 59 | super.onFinishInflate(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 60 | // Get the hover color |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 61 | mHoverColor = getResources().getColor(R.color.delete_target_hover_tint); |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 62 | |
Sunny Goyal | da1dfa3 | 2017-04-26 22:34:49 -0700 | [diff] [blame] | 63 | setDrawable(R.drawable.ic_remove_shadow); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 66 | @Override |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 67 | public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) { |
| 68 | super.onDragStart(dragObject, options); |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 69 | setTextBasedOnDragSource(dragObject.dragInfo); |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame] | 70 | setControlTypeBasedOnDragSource(dragObject.dragInfo); |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 73 | /** |
| 74 | * @return true for items that should have a "Remove" action in accessibility. |
| 75 | */ |
| 76 | @Override |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 77 | public boolean supportsAccessibilityDrop(ItemInfo info, View view) { |
Sunny Goyal | 9589916 | 2019-03-27 16:03:06 -0700 | [diff] [blame] | 78 | if (info instanceof WorkspaceItemInfo) { |
vadimt | 26850a2 | 2019-02-05 14:56:33 -0800 | [diff] [blame] | 79 | // Support the action unless the item is in a context menu. |
Samuel Fufa | 6eaf989 | 2020-04-01 11:40:40 -0700 | [diff] [blame] | 80 | return canRemove(info); |
vadimt | 26850a2 | 2019-02-05 14:56:33 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | return (info instanceof LauncherAppWidgetInfo) |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 84 | || (info instanceof FolderInfo); |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 87 | @Override |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 88 | public int getAccessibilityAction() { |
| 89 | return LauncherAccessibilityDelegate.REMOVE; |
| 90 | } |
| 91 | |
| 92 | @Override |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 93 | protected boolean supportsDrop(ItemInfo info) { |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 94 | return true; |
| 95 | } |
| 96 | |
| 97 | /** |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 98 | * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item. |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 99 | */ |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 100 | private void setTextBasedOnDragSource(ItemInfo item) { |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 101 | if (!TextUtils.isEmpty(mText)) { |
Tony Wickham | 6a71a5b | 2018-08-21 11:40:23 -0700 | [diff] [blame] | 102 | mText = getResources().getString(canRemove(item) |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 103 | ? R.string.remove_drop_target_label |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 104 | : android.R.string.cancel); |
vadimt | 422885d | 2019-02-15 19:55:43 -0800 | [diff] [blame] | 105 | setContentDescription(mText); |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 106 | requestLayout(); |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 107 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Tony Wickham | 6a71a5b | 2018-08-21 11:40:23 -0700 | [diff] [blame] | 110 | private boolean canRemove(ItemInfo item) { |
| 111 | return item.id != ItemInfo.NO_ID; |
| 112 | } |
| 113 | |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Set mControlType depending on the drag item. |
| 116 | */ |
| 117 | private void setControlTypeBasedOnDragSource(ItemInfo item) { |
| 118 | mControlType = item.id != ItemInfo.NO_ID ? ControlType.REMOVE_TARGET |
| 119 | : ControlType.CANCEL_TARGET; |
| 120 | } |
| 121 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 122 | @Override |
Tony Wickham | 6a71a5b | 2018-08-21 11:40:23 -0700 | [diff] [blame] | 123 | public void onDrop(DragObject d, DragOptions options) { |
| 124 | if (canRemove(d.dragInfo)) { |
| 125 | mLauncher.getModelWriter().prepareToUndoDelete(); |
Samuel Fufa | 2f0b2d3 | 2019-11-26 16:16:02 -0800 | [diff] [blame] | 126 | d.dragInfo.container = NO_ID; |
Tony Wickham | 6a71a5b | 2018-08-21 11:40:23 -0700 | [diff] [blame] | 127 | } |
| 128 | super.onDrop(d, options); |
thiruram | ff48451 | 2020-05-11 11:19:58 -0700 | [diff] [blame^] | 129 | mStatsLogManager.log( |
| 130 | mControlType == ControlType.REMOVE_TARGET |
| 131 | ? LAUNCHER_ITEM_DROPPED_ON_REMOVE |
| 132 | : LAUNCHER_ITEM_DROPPED_ON_CANCEL, |
| 133 | d.logInstanceId); |
Tony Wickham | 6a71a5b | 2018-08-21 11:40:23 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 137 | public void completeDrop(DragObject d) { |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 138 | ItemInfo item = d.dragInfo; |
Tony Wickham | 6a71a5b | 2018-08-21 11:40:23 -0700 | [diff] [blame] | 139 | if (canRemove(item)) { |
Tony | f80e893 | 2018-12-21 11:58:04 -0800 | [diff] [blame] | 140 | int itemPage = mLauncher.getWorkspace().getCurrentPage(); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 141 | onAccessibilityDrop(null, item); |
Tony Wickham | 6a71a5b | 2018-08-21 11:40:23 -0700 | [diff] [blame] | 142 | ModelWriter modelWriter = mLauncher.getModelWriter(); |
Tony Wickham | 03f2701 | 2019-04-25 14:22:54 -0700 | [diff] [blame] | 143 | Runnable onUndoClicked = () -> { |
Sunny Goyal | a7a5bf3 | 2020-01-05 15:35:29 +0530 | [diff] [blame] | 144 | mLauncher.setPageToBindSynchronously(itemPage); |
| 145 | modelWriter.abortDelete(); |
Tony Wickham | 03f2701 | 2019-04-25 14:22:54 -0700 | [diff] [blame] | 146 | mLauncher.getUserEventDispatcher().logActionOnControl(TAP, UNDO); |
| 147 | }; |
Tony Wickham | 6a71a5b | 2018-08-21 11:40:23 -0700 | [diff] [blame] | 148 | Snackbar.show(mLauncher, R.string.item_removed, R.string.undo, |
Tony Wickham | 03f2701 | 2019-04-25 14:22:54 -0700 | [diff] [blame] | 149 | modelWriter::commitDelete, onUndoClicked); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 150 | } |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /** |
| 154 | * 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] | 155 | */ |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 156 | @Override |
| 157 | public void onAccessibilityDrop(View view, ItemInfo item) { |
Winson | fa56b3f | 2015-09-14 12:01:13 -0700 | [diff] [blame] | 158 | // Remove the item from launcher and the db, we can ignore the containerInfo in this call |
| 159 | // because we already remove the drag view from the folder (if the drag originated from |
| 160 | // a folder) in Folder.beginDrag() |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 161 | mLauncher.removeItem(view, item, true /* deleteFromDb */); |
| 162 | mLauncher.getWorkspace().stripEmptyScreens(); |
| 163 | mLauncher.getDragLayer() |
| 164 | .announceForAccessibility(getContext().getString(R.string.item_removed)); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 165 | } |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 166 | |
| 167 | @Override |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame] | 168 | public Target getDropTargetForLogging() { |
| 169 | Target t = LoggerUtils.newTarget(Target.Type.CONTROL); |
| 170 | t.controlType = mControlType; |
| 171 | return t; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 172 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 173 | } |