blob: cc119c93613b02f0cc0e611a40cf84dd642ee6d7 [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();
Winson Chung4c98d922011-05-31 16:50:48 -070056 // Get the hover color
Sunny Goyalfa401a12015-04-10 13:45:42 -070057 mHoverColor = getResources().getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080058
Sunny Goyalda1dfa32017-04-26 22:34:49 -070059 setDrawable(R.drawable.ic_remove_shadow);
Winson Chung4c98d922011-05-31 16:50:48 -070060 }
61
Tony Wickham9aae47f2015-10-01 13:04:22 -070062 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -070063 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
64 super.onDragStart(dragObject, options);
Sunny Goyal1ce9c472017-10-03 16:17:32 -070065 setTextBasedOnDragSource(dragObject.dragInfo);
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070066 setControlTypeBasedOnDragSource(dragObject.dragInfo);
Tony Wickham9aae47f2015-10-01 13:04:22 -070067 }
68
Sunny Goyal0236d0b2017-10-24 14:54:30 -070069 /**
70 * @return true for items that should have a "Remove" action in accessibility.
71 */
72 @Override
Winson Chung1054d4e2018-03-05 19:39:21 +000073 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
Sunny Goyal95899162019-03-27 16:03:06 -070074 if (info instanceof WorkspaceItemInfo) {
vadimt26850a22019-02-05 14:56:33 -080075 // Support the action unless the item is in a context menu.
Samuel Fufa6eaf9892020-04-01 11:40:40 -070076 return canRemove(info);
vadimt26850a22019-02-05 14:56:33 -080077 }
78
79 return (info instanceof LauncherAppWidgetInfo)
Sunny Goyal1a70cef2015-04-22 11:29:51 -070080 || (info instanceof FolderInfo);
Winson Chunga48487a2012-03-20 16:19:37 -070081 }
82
Winson Chung4c98d922011-05-31 16:50:48 -070083 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070084 public int getAccessibilityAction() {
85 return LauncherAccessibilityDelegate.REMOVE;
86 }
87
88 @Override
Sunny Goyal1ce9c472017-10-03 16:17:32 -070089 protected boolean supportsDrop(ItemInfo info) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070090 return true;
91 }
92
93 /**
Sunny Goyal1ce9c472017-10-03 16:17:32 -070094 * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item.
Tony Wickham9aae47f2015-10-01 13:04:22 -070095 */
Sunny Goyal1ce9c472017-10-03 16:17:32 -070096 private void setTextBasedOnDragSource(ItemInfo item) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -070097 if (!TextUtils.isEmpty(mText)) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -070098 mText = getResources().getString(canRemove(item)
Jon Mirandabfaa4a42017-08-21 15:31:51 -070099 ? R.string.remove_drop_target_label
Tony Wickham9aae47f2015-10-01 13:04:22 -0700100 : android.R.string.cancel);
vadimt422885d2019-02-15 19:55:43 -0800101 setContentDescription(mText);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700102 requestLayout();
Tony Wickham9aae47f2015-10-01 13:04:22 -0700103 }
Winson Chung4c98d922011-05-31 16:50:48 -0700104 }
105
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700106 private boolean canRemove(ItemInfo item) {
107 return item.id != ItemInfo.NO_ID;
108 }
109
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700110 /**
111 * Set mControlType depending on the drag item.
112 */
113 private void setControlTypeBasedOnDragSource(ItemInfo item) {
Winson Chungf9935182020-10-23 09:26:44 -0700114 mLauncherEvent = item.id != ItemInfo.NO_ID ? LAUNCHER_ITEM_DROPPED_ON_REMOVE
115 : LAUNCHER_ITEM_DROPPED_ON_CANCEL;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700116 }
117
Winson Chung4c98d922011-05-31 16:50:48 -0700118 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700119 public void onDrop(DragObject d, DragOptions options) {
120 if (canRemove(d.dragInfo)) {
121 mLauncher.getModelWriter().prepareToUndoDelete();
Samuel Fufa2f0b2d32019-11-26 16:16:02 -0800122 d.dragInfo.container = NO_ID;
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700123 }
124 super.onDrop(d, options);
thiruramc6a38ba2020-06-16 18:58:13 -0700125 mStatsLogManager.logger().withInstanceId(d.logInstanceId)
Winson Chungf9935182020-10-23 09:26:44 -0700126 .log(mLauncherEvent);
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700127 }
128
129 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800130 public void completeDrop(DragObject d) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700131 ItemInfo item = d.dragInfo;
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700132 if (canRemove(item)) {
Tonyf80e8932018-12-21 11:58:04 -0800133 int itemPage = mLauncher.getWorkspace().getCurrentPage();
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700134 onAccessibilityDrop(null, item);
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700135 ModelWriter modelWriter = mLauncher.getModelWriter();
Tony Wickham03f27012019-04-25 14:22:54 -0700136 Runnable onUndoClicked = () -> {
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530137 mLauncher.setPageToBindSynchronously(itemPage);
138 modelWriter.abortDelete();
Winson Chungf9935182020-10-23 09:26:44 -0700139 mLauncher.getStatsLogManager().logger().log(LAUNCHER_UNDO);
Tony Wickham03f27012019-04-25 14:22:54 -0700140 };
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700141 Snackbar.show(mLauncher, R.string.item_removed, R.string.undo,
Tony Wickham03f27012019-04-25 14:22:54 -0700142 modelWriter::commitDelete, onUndoClicked);
Winson Chung4c98d922011-05-31 16:50:48 -0700143 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800144 }
145
146 /**
147 * Removes the item from the workspace. If the view is not null, it also removes the view.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800148 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700149 @Override
150 public void onAccessibilityDrop(View view, ItemInfo item) {
Winsonfa56b3f2015-09-14 12:01:13 -0700151 // Remove the item from launcher and the db, we can ignore the containerInfo in this call
152 // because we already remove the drag view from the folder (if the drag originated from
153 // a folder) in Folder.beginDrag()
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700154 mLauncher.removeItem(view, item, true /* deleteFromDb */);
155 mLauncher.getWorkspace().stripEmptyScreens();
156 mLauncher.getDragLayer()
157 .announceForAccessibility(getContext().getString(R.string.item_removed));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800158 }
Winson Chung4c98d922011-05-31 16:50:48 -0700159}