blob: ba55834e993e4f3d2b105cdfb0709ed41782613d [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;
Winson Chungf9935182020-10-23 09:26:44 -070021import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_UNDO;
Tony Wickham03f27012019-04-25 14:22:54 -070022
Winson Chung4c98d922011-05-31 16:50:48 -070023import android.content.Context;
Sunny Goyalf37a2142016-03-24 17:28:25 -070024import android.text.TextUtils;
Winson Chung4c98d922011-05-31 16:50:48 -070025import android.util.AttributeSet;
26import android.view.View;
Winson Chung4c98d922011-05-31 16:50:48 -070027
Sunny Goyal0236d0b2017-10-24 14:54:30 -070028import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Sunny Goyal94b510c2016-08-16 15:36:48 -070029import com.android.launcher3.dragndrop.DragOptions;
thiruramff484512020-05-11 11:19:58 -070030import com.android.launcher3.logging.StatsLogManager;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070031import com.android.launcher3.model.ModelWriter;
Sunny Goyale396abf2020-04-06 15:11:17 -070032import com.android.launcher3.model.data.FolderInfo;
33import com.android.launcher3.model.data.ItemInfo;
34import com.android.launcher3.model.data.LauncherAppWidgetInfo;
35import com.android.launcher3.model.data.WorkspaceItemInfo;
Andras Kloczl55edfe52021-05-14 12:21:30 +020036import com.android.launcher3.util.IntSet;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070037import com.android.launcher3.views.Snackbar;
Kenny Guyed131872014-04-30 03:02:21 +010038
Winson Chung61fa4192011-06-12 15:15:29 -070039public class DeleteDropTarget extends ButtonDropTarget {
Sunny Goyalfa401a12015-04-10 13:45:42 -070040
thiruramff484512020-05-11 11:19:58 -070041 private final StatsLogManager mStatsLogManager;
42
Winson Chungf9935182020-10-23 09:26:44 -070043 private StatsLogManager.LauncherEvent mLauncherEvent;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070044
Winson Chung4c98d922011-05-31 16:50:48 -070045 public DeleteDropTarget(Context context, AttributeSet attrs) {
46 this(context, attrs, 0);
47 }
48
49 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
50 super(context, attrs, defStyle);
thiruramff484512020-05-11 11:19:58 -070051 this.mStatsLogManager = StatsLogManager.newInstance(context);
Winson Chung4c98d922011-05-31 16:50:48 -070052 }
53
54 @Override
55 protected void onFinishInflate() {
56 super.onFinishInflate();
Yogisha Dixita3a6f512021-04-28 13:45:37 +010057 setDrawable(R.drawable.ic_remove_no_shadow);
Winson Chung4c98d922011-05-31 16:50:48 -070058 }
59
Tony Wickham9aae47f2015-10-01 13:04:22 -070060 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -070061 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
62 super.onDragStart(dragObject, options);
Sunny Goyal1ce9c472017-10-03 16:17:32 -070063 setTextBasedOnDragSource(dragObject.dragInfo);
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070064 setControlTypeBasedOnDragSource(dragObject.dragInfo);
Tony Wickham9aae47f2015-10-01 13:04:22 -070065 }
66
Sunny Goyal0236d0b2017-10-24 14:54:30 -070067 /**
68 * @return true for items that should have a "Remove" action in accessibility.
69 */
70 @Override
Winson Chung1054d4e2018-03-05 19:39:21 +000071 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
Sunny Goyal95899162019-03-27 16:03:06 -070072 if (info instanceof WorkspaceItemInfo) {
vadimt26850a22019-02-05 14:56:33 -080073 // Support the action unless the item is in a context menu.
Samuel Fufa6eaf9892020-04-01 11:40:40 -070074 return canRemove(info);
vadimt26850a22019-02-05 14:56:33 -080075 }
76
77 return (info instanceof LauncherAppWidgetInfo)
Sunny Goyal1a70cef2015-04-22 11:29:51 -070078 || (info instanceof FolderInfo);
Winson Chunga48487a2012-03-20 16:19:37 -070079 }
80
Winson Chung4c98d922011-05-31 16:50:48 -070081 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070082 public int getAccessibilityAction() {
83 return LauncherAccessibilityDelegate.REMOVE;
84 }
85
86 @Override
Sunny Goyal1ce9c472017-10-03 16:17:32 -070087 protected boolean supportsDrop(ItemInfo info) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070088 return true;
89 }
90
91 /**
Sunny Goyal1ce9c472017-10-03 16:17:32 -070092 * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item.
Tony Wickham9aae47f2015-10-01 13:04:22 -070093 */
Sunny Goyal1ce9c472017-10-03 16:17:32 -070094 private void setTextBasedOnDragSource(ItemInfo item) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -070095 if (!TextUtils.isEmpty(mText)) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -070096 mText = getResources().getString(canRemove(item)
Jon Mirandabfaa4a42017-08-21 15:31:51 -070097 ? R.string.remove_drop_target_label
Tony Wickham9aae47f2015-10-01 13:04:22 -070098 : android.R.string.cancel);
vadimt422885d2019-02-15 19:55:43 -080099 setContentDescription(mText);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700100 requestLayout();
Tony Wickham9aae47f2015-10-01 13:04:22 -0700101 }
Winson Chung4c98d922011-05-31 16:50:48 -0700102 }
103
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700104 private boolean canRemove(ItemInfo item) {
105 return item.id != ItemInfo.NO_ID;
106 }
107
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700108 /**
109 * Set mControlType depending on the drag item.
110 */
111 private void setControlTypeBasedOnDragSource(ItemInfo item) {
Winson Chungf9935182020-10-23 09:26:44 -0700112 mLauncherEvent = item.id != ItemInfo.NO_ID ? LAUNCHER_ITEM_DROPPED_ON_REMOVE
113 : LAUNCHER_ITEM_DROPPED_ON_CANCEL;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700114 }
115
Winson Chung4c98d922011-05-31 16:50:48 -0700116 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700117 public void onDrop(DragObject d, DragOptions options) {
118 if (canRemove(d.dragInfo)) {
119 mLauncher.getModelWriter().prepareToUndoDelete();
Samuel Fufa2f0b2d32019-11-26 16:16:02 -0800120 d.dragInfo.container = NO_ID;
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700121 }
122 super.onDrop(d, options);
thiruramc6a38ba2020-06-16 18:58:13 -0700123 mStatsLogManager.logger().withInstanceId(d.logInstanceId)
Winson Chungf9935182020-10-23 09:26:44 -0700124 .log(mLauncherEvent);
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700125 }
126
127 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800128 public void completeDrop(DragObject d) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700129 ItemInfo item = d.dragInfo;
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700130 if (canRemove(item)) {
Tonyf80e8932018-12-21 11:58:04 -0800131 int itemPage = mLauncher.getWorkspace().getCurrentPage();
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700132 onAccessibilityDrop(null, item);
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700133 ModelWriter modelWriter = mLauncher.getModelWriter();
Tony Wickham03f27012019-04-25 14:22:54 -0700134 Runnable onUndoClicked = () -> {
Andras Kloczl55edfe52021-05-14 12:21:30 +0200135 mLauncher.setPagesToBindSynchronously(IntSet.wrap(itemPage));
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530136 modelWriter.abortDelete();
Winson Chungf9935182020-10-23 09:26:44 -0700137 mLauncher.getStatsLogManager().logger().log(LAUNCHER_UNDO);
Tony Wickham03f27012019-04-25 14:22:54 -0700138 };
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700139 Snackbar.show(mLauncher, R.string.item_removed, R.string.undo,
Tony Wickham03f27012019-04-25 14:22:54 -0700140 modelWriter::commitDelete, onUndoClicked);
Winson Chung4c98d922011-05-31 16:50:48 -0700141 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800142 }
143
144 /**
145 * Removes the item from the workspace. If the view is not null, it also removes the view.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800146 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700147 @Override
148 public void onAccessibilityDrop(View view, ItemInfo item) {
Winsonfa56b3f2015-09-14 12:01:13 -0700149 // Remove the item from launcher and the db, we can ignore the containerInfo in this call
150 // because we already remove the drag view from the folder (if the drag originated from
151 // a folder) in Folder.beginDrag()
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700152 mLauncher.removeItem(view, item, true /* deleteFromDb */);
153 mLauncher.getWorkspace().stripEmptyScreens();
154 mLauncher.getDragLayer()
155 .announceForAccessibility(getContext().getString(R.string.item_removed));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800156 }
Winson Chung4c98d922011-05-31 16:50:48 -0700157}