blob: e46aad2bcff7e953bc0b0f707d3227b8f3423656 [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;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070036import com.android.launcher3.views.Snackbar;
Kenny Guyed131872014-04-30 03:02:21 +010037
Winson Chung61fa4192011-06-12 15:15:29 -070038public class DeleteDropTarget extends ButtonDropTarget {
Sunny Goyalfa401a12015-04-10 13:45:42 -070039
thiruramff484512020-05-11 11:19:58 -070040 private final StatsLogManager mStatsLogManager;
41
Winson Chungf9935182020-10-23 09:26:44 -070042 private StatsLogManager.LauncherEvent mLauncherEvent;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070043
Winson Chung4c98d922011-05-31 16:50:48 -070044 public DeleteDropTarget(Context context, AttributeSet attrs) {
45 this(context, attrs, 0);
46 }
47
48 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
49 super(context, attrs, defStyle);
thiruramff484512020-05-11 11:19:58 -070050 this.mStatsLogManager = StatsLogManager.newInstance(context);
Winson Chung4c98d922011-05-31 16:50:48 -070051 }
52
53 @Override
54 protected void onFinishInflate() {
55 super.onFinishInflate();
Sunny Goyalda1dfa32017-04-26 22:34:49 -070056 setDrawable(R.drawable.ic_remove_shadow);
Winson Chung4c98d922011-05-31 16:50:48 -070057 }
58
Tony Wickham9aae47f2015-10-01 13:04:22 -070059 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -070060 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
61 super.onDragStart(dragObject, options);
Sunny Goyal1ce9c472017-10-03 16:17:32 -070062 setTextBasedOnDragSource(dragObject.dragInfo);
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070063 setControlTypeBasedOnDragSource(dragObject.dragInfo);
Tony Wickham9aae47f2015-10-01 13:04:22 -070064 }
65
Sunny Goyal0236d0b2017-10-24 14:54:30 -070066 /**
67 * @return true for items that should have a "Remove" action in accessibility.
68 */
69 @Override
Winson Chung1054d4e2018-03-05 19:39:21 +000070 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
Sunny Goyal95899162019-03-27 16:03:06 -070071 if (info instanceof WorkspaceItemInfo) {
vadimt26850a22019-02-05 14:56:33 -080072 // Support the action unless the item is in a context menu.
Samuel Fufa6eaf9892020-04-01 11:40:40 -070073 return canRemove(info);
vadimt26850a22019-02-05 14:56:33 -080074 }
75
76 return (info instanceof LauncherAppWidgetInfo)
Sunny Goyal1a70cef2015-04-22 11:29:51 -070077 || (info instanceof FolderInfo);
Winson Chunga48487a2012-03-20 16:19:37 -070078 }
79
Winson Chung4c98d922011-05-31 16:50:48 -070080 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070081 public int getAccessibilityAction() {
82 return LauncherAccessibilityDelegate.REMOVE;
83 }
84
85 @Override
Sunny Goyal1ce9c472017-10-03 16:17:32 -070086 protected boolean supportsDrop(ItemInfo info) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070087 return true;
88 }
89
90 /**
Sunny Goyal1ce9c472017-10-03 16:17:32 -070091 * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item.
Tony Wickham9aae47f2015-10-01 13:04:22 -070092 */
Sunny Goyal1ce9c472017-10-03 16:17:32 -070093 private void setTextBasedOnDragSource(ItemInfo item) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -070094 if (!TextUtils.isEmpty(mText)) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -070095 mText = getResources().getString(canRemove(item)
Jon Mirandabfaa4a42017-08-21 15:31:51 -070096 ? R.string.remove_drop_target_label
Tony Wickham9aae47f2015-10-01 13:04:22 -070097 : android.R.string.cancel);
vadimt422885d2019-02-15 19:55:43 -080098 setContentDescription(mText);
Jon Mirandabfaa4a42017-08-21 15:31:51 -070099 requestLayout();
Tony Wickham9aae47f2015-10-01 13:04:22 -0700100 }
Winson Chung4c98d922011-05-31 16:50:48 -0700101 }
102
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700103 private boolean canRemove(ItemInfo item) {
104 return item.id != ItemInfo.NO_ID;
105 }
106
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700107 /**
108 * Set mControlType depending on the drag item.
109 */
110 private void setControlTypeBasedOnDragSource(ItemInfo item) {
Winson Chungf9935182020-10-23 09:26:44 -0700111 mLauncherEvent = item.id != ItemInfo.NO_ID ? LAUNCHER_ITEM_DROPPED_ON_REMOVE
112 : LAUNCHER_ITEM_DROPPED_ON_CANCEL;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700113 }
114
Winson Chung4c98d922011-05-31 16:50:48 -0700115 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700116 public void onDrop(DragObject d, DragOptions options) {
117 if (canRemove(d.dragInfo)) {
118 mLauncher.getModelWriter().prepareToUndoDelete();
Samuel Fufa2f0b2d32019-11-26 16:16:02 -0800119 d.dragInfo.container = NO_ID;
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700120 }
121 super.onDrop(d, options);
thiruramc6a38ba2020-06-16 18:58:13 -0700122 mStatsLogManager.logger().withInstanceId(d.logInstanceId)
Winson Chungf9935182020-10-23 09:26:44 -0700123 .log(mLauncherEvent);
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700124 }
125
126 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800127 public void completeDrop(DragObject d) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700128 ItemInfo item = d.dragInfo;
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700129 if (canRemove(item)) {
Tonyf80e8932018-12-21 11:58:04 -0800130 int itemPage = mLauncher.getWorkspace().getCurrentPage();
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700131 onAccessibilityDrop(null, item);
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700132 ModelWriter modelWriter = mLauncher.getModelWriter();
Tony Wickham03f27012019-04-25 14:22:54 -0700133 Runnable onUndoClicked = () -> {
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530134 mLauncher.setPageToBindSynchronously(itemPage);
135 modelWriter.abortDelete();
Winson Chungf9935182020-10-23 09:26:44 -0700136 mLauncher.getStatsLogManager().logger().log(LAUNCHER_UNDO);
Tony Wickham03f27012019-04-25 14:22:54 -0700137 };
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700138 Snackbar.show(mLauncher, R.string.item_removed, R.string.undo,
Tony Wickham03f27012019-04-25 14:22:54 -0700139 modelWriter::commitDelete, onUndoClicked);
Winson Chung4c98d922011-05-31 16:50:48 -0700140 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800141 }
142
143 /**
144 * Removes the item from the workspace. If the view is not null, it also removes the view.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800145 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700146 @Override
147 public void onAccessibilityDrop(View view, ItemInfo item) {
Winsonfa56b3f2015-09-14 12:01:13 -0700148 // Remove the item from launcher and the db, we can ignore the containerInfo in this call
149 // because we already remove the drag view from the folder (if the drag originated from
150 // a folder) in Folder.beginDrag()
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700151 mLauncher.removeItem(view, item, true /* deleteFromDb */);
152 mLauncher.getWorkspace().stripEmptyScreens();
153 mLauncher.getDragLayer()
154 .announceForAccessibility(getContext().getString(R.string.item_removed));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800155 }
Winson Chung4c98d922011-05-31 16:50:48 -0700156}