blob: 477964a6f4c74bb3c682e8bccbc519af3fb67efb [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 Goyal12e3f1f2021-07-15 14:31:58 -070028import com.android.launcher3.LauncherSettings.Favorites;
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;
thiruramff484512020-05-11 11:19:58 -070031import com.android.launcher3.logging.StatsLogManager;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070032import com.android.launcher3.model.ModelWriter;
Sunny Goyale396abf2020-04-06 15:11:17 -070033import com.android.launcher3.model.data.FolderInfo;
34import com.android.launcher3.model.data.ItemInfo;
35import com.android.launcher3.model.data.LauncherAppWidgetInfo;
36import com.android.launcher3.model.data.WorkspaceItemInfo;
Andras Kloczl55edfe52021-05-14 12:21:30 +020037import com.android.launcher3.util.IntSet;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070038import com.android.launcher3.views.Snackbar;
Kenny Guyed131872014-04-30 03:02:21 +010039
Winson Chung61fa4192011-06-12 15:15:29 -070040public class DeleteDropTarget extends ButtonDropTarget {
Sunny Goyalfa401a12015-04-10 13:45:42 -070041
thiruramff484512020-05-11 11:19:58 -070042 private final StatsLogManager mStatsLogManager;
43
Winson Chungf9935182020-10-23 09:26:44 -070044 private StatsLogManager.LauncherEvent mLauncherEvent;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070045
Winson Chung4c98d922011-05-31 16:50:48 -070046 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);
thiruramff484512020-05-11 11:19:58 -070052 this.mStatsLogManager = StatsLogManager.newInstance(context);
Winson Chung4c98d922011-05-31 16:50:48 -070053 }
54
55 @Override
56 protected void onFinishInflate() {
57 super.onFinishInflate();
Yogisha Dixita3a6f512021-04-28 13:45:37 +010058 setDrawable(R.drawable.ic_remove_no_shadow);
Winson Chung4c98d922011-05-31 16:50:48 -070059 }
60
Tony Wickham9aae47f2015-10-01 13:04:22 -070061 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -070062 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
63 super.onDragStart(dragObject, options);
Sunny Goyal1ce9c472017-10-03 16:17:32 -070064 setTextBasedOnDragSource(dragObject.dragInfo);
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070065 setControlTypeBasedOnDragSource(dragObject.dragInfo);
Tony Wickham9aae47f2015-10-01 13:04:22 -070066 }
67
Sunny Goyal0236d0b2017-10-24 14:54:30 -070068 /**
69 * @return true for items that should have a "Remove" action in accessibility.
70 */
71 @Override
Winson Chung1054d4e2018-03-05 19:39:21 +000072 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
Sunny Goyal95899162019-03-27 16:03:06 -070073 if (info instanceof WorkspaceItemInfo) {
vadimt26850a22019-02-05 14:56:33 -080074 // Support the action unless the item is in a context menu.
Samuel Fufa6eaf9892020-04-01 11:40:40 -070075 return canRemove(info);
vadimt26850a22019-02-05 14:56:33 -080076 }
77
78 return (info instanceof LauncherAppWidgetInfo)
Sunny Goyal1a70cef2015-04-22 11:29:51 -070079 || (info instanceof FolderInfo);
Winson Chunga48487a2012-03-20 16:19:37 -070080 }
81
Winson Chung4c98d922011-05-31 16:50:48 -070082 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070083 public int getAccessibilityAction() {
84 return LauncherAccessibilityDelegate.REMOVE;
85 }
86
87 @Override
Sunny Goyal1ce9c472017-10-03 16:17:32 -070088 protected boolean supportsDrop(ItemInfo info) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070089 return true;
90 }
91
92 /**
Sunny Goyal1ce9c472017-10-03 16:17:32 -070093 * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item.
Tony Wickham9aae47f2015-10-01 13:04:22 -070094 */
Sunny Goyal1ce9c472017-10-03 16:17:32 -070095 private void setTextBasedOnDragSource(ItemInfo item) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -070096 if (!TextUtils.isEmpty(mText)) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -070097 mText = getResources().getString(canRemove(item)
Jon Mirandabfaa4a42017-08-21 15:31:51 -070098 ? R.string.remove_drop_target_label
Tony Wickham9aae47f2015-10-01 13:04:22 -070099 : android.R.string.cancel);
vadimt422885d2019-02-15 19:55:43 -0800100 setContentDescription(mText);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700101 requestLayout();
Tony Wickham9aae47f2015-10-01 13:04:22 -0700102 }
Winson Chung4c98d922011-05-31 16:50:48 -0700103 }
104
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700105 private boolean canRemove(ItemInfo item) {
106 return item.id != ItemInfo.NO_ID;
107 }
108
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700109 /**
110 * Set mControlType depending on the drag item.
111 */
112 private void setControlTypeBasedOnDragSource(ItemInfo item) {
Winson Chungf9935182020-10-23 09:26:44 -0700113 mLauncherEvent = item.id != ItemInfo.NO_ID ? LAUNCHER_ITEM_DROPPED_ON_REMOVE
114 : LAUNCHER_ITEM_DROPPED_ON_CANCEL;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700115 }
116
Winson Chung4c98d922011-05-31 16:50:48 -0700117 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700118 public void onDrop(DragObject d, DragOptions options) {
119 if (canRemove(d.dragInfo)) {
120 mLauncher.getModelWriter().prepareToUndoDelete();
Samuel Fufa2f0b2d32019-11-26 16:16:02 -0800121 d.dragInfo.container = NO_ID;
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700122 }
123 super.onDrop(d, options);
thiruramc6a38ba2020-06-16 18:58:13 -0700124 mStatsLogManager.logger().withInstanceId(d.logInstanceId)
Winson Chungf9935182020-10-23 09:26:44 -0700125 .log(mLauncherEvent);
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700126 }
127
128 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800129 public void completeDrop(DragObject d) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700130 ItemInfo item = d.dragInfo;
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700131 if (canRemove(item)) {
Sunny Goyal12e3f1f2021-07-15 14:31:58 -0700132 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 Goyal0236d0b2017-10-24 14:54:30 -0700143 onAccessibilityDrop(null, item);
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700144 ModelWriter modelWriter = mLauncher.getModelWriter();
Tony Wickham03f27012019-04-25 14:22:54 -0700145 Runnable onUndoClicked = () -> {
Sunny Goyal12e3f1f2021-07-15 14:31:58 -0700146 mLauncher.setPagesToBindSynchronously(pageIds);
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530147 modelWriter.abortDelete();
Winson Chungf9935182020-10-23 09:26:44 -0700148 mLauncher.getStatsLogManager().logger().log(LAUNCHER_UNDO);
Tony Wickham03f27012019-04-25 14:22:54 -0700149 };
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700150 Snackbar.show(mLauncher, R.string.item_removed, R.string.undo,
Tony Wickham03f27012019-04-25 14:22:54 -0700151 modelWriter::commitDelete, onUndoClicked);
Winson Chung4c98d922011-05-31 16:50:48 -0700152 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800153 }
154
155 /**
156 * Removes the item from the workspace. If the view is not null, it also removes the view.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800157 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700158 @Override
159 public void onAccessibilityDrop(View view, ItemInfo item) {
Winsonfa56b3f2015-09-14 12:01:13 -0700160 // 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 Goyal0236d0b2017-10-24 14:54:30 -0700163 mLauncher.removeItem(view, item, true /* deleteFromDb */);
164 mLauncher.getWorkspace().stripEmptyScreens();
165 mLauncher.getDragLayer()
166 .announceForAccessibility(getContext().getString(R.string.item_removed));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800167 }
Winson Chung4c98d922011-05-31 16:50:48 -0700168}