blob: d75d7122806e496568db7699c236a0e73a662a44 [file] [log] [blame]
Winson Chung4c98d922011-05-31 16:50:48 -07001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung4c98d922011-05-31 16:50:48 -070018
thiruramff484512020-05-11 11:19:58 -070019import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_DROPPED_ON_CANCEL;
20import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_DROPPED_ON_REMOVE;
Tony Wickham03f27012019-04-25 14:22:54 -070021import static com.android.launcher3.userevent.nano.LauncherLogProto.Action.Touch.TAP;
22import static com.android.launcher3.userevent.nano.LauncherLogProto.ControlType.UNDO;
23
Winson Chung4c98d922011-05-31 16:50:48 -070024import android.content.Context;
Sunny Goyalf37a2142016-03-24 17:28:25 -070025import android.text.TextUtils;
Winson Chung4c98d922011-05-31 16:50:48 -070026import android.util.AttributeSet;
27import android.view.View;
Winson Chung4c98d922011-05-31 16:50:48 -070028
Sunny Goyal0236d0b2017-10-24 14:54:30 -070029import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Sunny Goyal94b510c2016-08-16 15:36:48 -070030import com.android.launcher3.dragndrop.DragOptions;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070031import com.android.launcher3.logging.LoggerUtils;
thiruramff484512020-05-11 11:19:58 -070032import com.android.launcher3.logging.StatsLogManager;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070033import com.android.launcher3.model.ModelWriter;
Sunny Goyale396abf2020-04-06 15:11:17 -070034import com.android.launcher3.model.data.FolderInfo;
35import com.android.launcher3.model.data.ItemInfo;
36import com.android.launcher3.model.data.LauncherAppWidgetInfo;
37import com.android.launcher3.model.data.WorkspaceItemInfo;
Winson Chung1054d4e2018-03-05 19:39:21 +000038import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070039import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070040import com.android.launcher3.views.Snackbar;
Kenny Guyed131872014-04-30 03:02:21 +010041
Winson Chung61fa4192011-06-12 15:15:29 -070042public class DeleteDropTarget extends ButtonDropTarget {
Sunny Goyalfa401a12015-04-10 13:45:42 -070043
thiruramff484512020-05-11 11:19:58 -070044 private final StatsLogManager mStatsLogManager;
45
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070046 private int mControlType = ControlType.DEFAULT_CONTROLTYPE;
47
Winson Chung4c98d922011-05-31 16:50:48 -070048 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);
thiruramff484512020-05-11 11:19:58 -070054 this.mStatsLogManager = StatsLogManager.newInstance(context);
Winson Chung4c98d922011-05-31 16:50:48 -070055 }
56
57 @Override
58 protected void onFinishInflate() {
59 super.onFinishInflate();
Winson Chung4c98d922011-05-31 16:50:48 -070060 // Get the hover color
Sunny Goyalfa401a12015-04-10 13:45:42 -070061 mHoverColor = getResources().getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080062
Sunny Goyalda1dfa32017-04-26 22:34:49 -070063 setDrawable(R.drawable.ic_remove_shadow);
Winson Chung4c98d922011-05-31 16:50:48 -070064 }
65
Tony Wickham9aae47f2015-10-01 13:04:22 -070066 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -070067 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
68 super.onDragStart(dragObject, options);
Sunny Goyal1ce9c472017-10-03 16:17:32 -070069 setTextBasedOnDragSource(dragObject.dragInfo);
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070070 setControlTypeBasedOnDragSource(dragObject.dragInfo);
Tony Wickham9aae47f2015-10-01 13:04:22 -070071 }
72
Sunny Goyal0236d0b2017-10-24 14:54:30 -070073 /**
74 * @return true for items that should have a "Remove" action in accessibility.
75 */
76 @Override
Winson Chung1054d4e2018-03-05 19:39:21 +000077 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
Sunny Goyal95899162019-03-27 16:03:06 -070078 if (info instanceof WorkspaceItemInfo) {
vadimt26850a22019-02-05 14:56:33 -080079 // Support the action unless the item is in a context menu.
Samuel Fufa6eaf9892020-04-01 11:40:40 -070080 return canRemove(info);
vadimt26850a22019-02-05 14:56:33 -080081 }
82
83 return (info instanceof LauncherAppWidgetInfo)
Sunny Goyal1a70cef2015-04-22 11:29:51 -070084 || (info instanceof FolderInfo);
Winson Chunga48487a2012-03-20 16:19:37 -070085 }
86
Winson Chung4c98d922011-05-31 16:50:48 -070087 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070088 public int getAccessibilityAction() {
89 return LauncherAccessibilityDelegate.REMOVE;
90 }
91
92 @Override
Sunny Goyal1ce9c472017-10-03 16:17:32 -070093 protected boolean supportsDrop(ItemInfo info) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070094 return true;
95 }
96
97 /**
Sunny Goyal1ce9c472017-10-03 16:17:32 -070098 * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item.
Tony Wickham9aae47f2015-10-01 13:04:22 -070099 */
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700100 private void setTextBasedOnDragSource(ItemInfo item) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700101 if (!TextUtils.isEmpty(mText)) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700102 mText = getResources().getString(canRemove(item)
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700103 ? R.string.remove_drop_target_label
Tony Wickham9aae47f2015-10-01 13:04:22 -0700104 : android.R.string.cancel);
vadimt422885d2019-02-15 19:55:43 -0800105 setContentDescription(mText);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700106 requestLayout();
Tony Wickham9aae47f2015-10-01 13:04:22 -0700107 }
Winson Chung4c98d922011-05-31 16:50:48 -0700108 }
109
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700110 private boolean canRemove(ItemInfo item) {
111 return item.id != ItemInfo.NO_ID;
112 }
113
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700114 /**
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 Chung4c98d922011-05-31 16:50:48 -0700122 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700123 public void onDrop(DragObject d, DragOptions options) {
124 if (canRemove(d.dragInfo)) {
125 mLauncher.getModelWriter().prepareToUndoDelete();
Samuel Fufa2f0b2d32019-11-26 16:16:02 -0800126 d.dragInfo.container = NO_ID;
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700127 }
128 super.onDrop(d, options);
thiruramff484512020-05-11 11:19:58 -0700129 mStatsLogManager.log(
130 mControlType == ControlType.REMOVE_TARGET
131 ? LAUNCHER_ITEM_DROPPED_ON_REMOVE
132 : LAUNCHER_ITEM_DROPPED_ON_CANCEL,
133 d.logInstanceId);
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700134 }
135
136 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800137 public void completeDrop(DragObject d) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700138 ItemInfo item = d.dragInfo;
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700139 if (canRemove(item)) {
Tonyf80e8932018-12-21 11:58:04 -0800140 int itemPage = mLauncher.getWorkspace().getCurrentPage();
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700141 onAccessibilityDrop(null, item);
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700142 ModelWriter modelWriter = mLauncher.getModelWriter();
Tony Wickham03f27012019-04-25 14:22:54 -0700143 Runnable onUndoClicked = () -> {
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530144 mLauncher.setPageToBindSynchronously(itemPage);
145 modelWriter.abortDelete();
Tony Wickham03f27012019-04-25 14:22:54 -0700146 mLauncher.getUserEventDispatcher().logActionOnControl(TAP, UNDO);
147 };
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700148 Snackbar.show(mLauncher, R.string.item_removed, R.string.undo,
Tony Wickham03f27012019-04-25 14:22:54 -0700149 modelWriter::commitDelete, onUndoClicked);
Winson Chung4c98d922011-05-31 16:50:48 -0700150 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800151 }
152
153 /**
154 * Removes the item from the workspace. If the view is not null, it also removes the view.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800155 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700156 @Override
157 public void onAccessibilityDrop(View view, ItemInfo item) {
Winsonfa56b3f2015-09-14 12:01:13 -0700158 // 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 Goyal0236d0b2017-10-24 14:54:30 -0700161 mLauncher.removeItem(view, item, true /* deleteFromDb */);
162 mLauncher.getWorkspace().stripEmptyScreens();
163 mLauncher.getDragLayer()
164 .announceForAccessibility(getContext().getString(R.string.item_removed));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800165 }
Winson Chung1054d4e2018-03-05 19:39:21 +0000166
167 @Override
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700168 public Target getDropTargetForLogging() {
169 Target t = LoggerUtils.newTarget(Target.Type.CONTROL);
170 t.controlType = mControlType;
171 return t;
Winson Chung1054d4e2018-03-05 19:39:21 +0000172 }
Winson Chung4c98d922011-05-31 16:50:48 -0700173}