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