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