blob: 58789fd8f2b2e8a2da30bf9e393884cfd8adacd5 [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 -070021
Winson Chung4c98d922011-05-31 16:50:48 -070022import android.content.Context;
Sunny Goyalf37a2142016-03-24 17:28:25 -070023import android.text.TextUtils;
Winson Chung4c98d922011-05-31 16:50:48 -070024import android.util.AttributeSet;
25import android.view.View;
Winson Chung4c98d922011-05-31 16:50:48 -070026
Sunny Goyal0236d0b2017-10-24 14:54:30 -070027import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Sunny Goyal94b510c2016-08-16 15:36:48 -070028import com.android.launcher3.dragndrop.DragOptions;
thiruramff484512020-05-11 11:19:58 -070029import com.android.launcher3.logging.StatsLogManager;
Jeremy Sim742630c2024-03-22 22:11:18 -070030import com.android.launcher3.model.data.CollectionInfo;
Sunny Goyale396abf2020-04-06 15:11:17 -070031import com.android.launcher3.model.data.ItemInfo;
32import com.android.launcher3.model.data.LauncherAppWidgetInfo;
33import com.android.launcher3.model.data.WorkspaceItemInfo;
Kenny Guyed131872014-04-30 03:02:21 +010034
Winson Chung61fa4192011-06-12 15:15:29 -070035public class DeleteDropTarget extends ButtonDropTarget {
Sunny Goyalfa401a12015-04-10 13:45:42 -070036
thiruramff484512020-05-11 11:19:58 -070037 private final StatsLogManager mStatsLogManager;
38
Winson Chungf9935182020-10-23 09:26:44 -070039 private StatsLogManager.LauncherEvent mLauncherEvent;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070040
Andrew Cole44898c32023-04-05 13:49:32 -070041 public DeleteDropTarget(Context context) {
42 this(context, null, 0);
43 }
44
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)
Jeremy Sim742630c2024-03-22 22:11:18 -070078 || (info instanceof CollectionInfo);
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
Alex Chau158caf42022-05-24 17:09:32 +010087 protected void setupItemInfo(ItemInfo info) {}
88
89 @Override
Sunny Goyal1ce9c472017-10-03 16:17:32 -070090 protected boolean supportsDrop(ItemInfo info) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070091 return true;
92 }
93
94 /**
Sunny Goyal1ce9c472017-10-03 16:17:32 -070095 * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item.
Tony Wickham9aae47f2015-10-01 13:04:22 -070096 */
Sunny Goyal1ce9c472017-10-03 16:17:32 -070097 private void setTextBasedOnDragSource(ItemInfo item) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -070098 if (!TextUtils.isEmpty(mText)) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -070099 mText = getResources().getString(canRemove(item)
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700100 ? R.string.remove_drop_target_label
Tony Wickham9aae47f2015-10-01 13:04:22 -0700101 : android.R.string.cancel);
vadimt422885d2019-02-15 19:55:43 -0800102 setContentDescription(mText);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700103 requestLayout();
Tony Wickham9aae47f2015-10-01 13:04:22 -0700104 }
Winson Chung4c98d922011-05-31 16:50:48 -0700105 }
106
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700107 private boolean canRemove(ItemInfo item) {
108 return item.id != ItemInfo.NO_ID;
109 }
110
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700111 /**
112 * Set mControlType depending on the drag item.
113 */
114 private void setControlTypeBasedOnDragSource(ItemInfo item) {
Winson Chungf9935182020-10-23 09:26:44 -0700115 mLauncherEvent = item.id != ItemInfo.NO_ID ? LAUNCHER_ITEM_DROPPED_ON_REMOVE
116 : LAUNCHER_ITEM_DROPPED_ON_CANCEL;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700117 }
118
Winson Chung4c98d922011-05-31 16:50:48 -0700119 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700120 public void onDrop(DragObject d, DragOptions options) {
121 if (canRemove(d.dragInfo)) {
Andrew Cole44898c32023-04-05 13:49:32 -0700122 mDropTargetHandler.prepareToUndoDelete();
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)) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700133 onAccessibilityDrop(null, item);
Andrew Cole44898c32023-04-05 13:49:32 -0700134 mDropTargetHandler.onDeleteComplete(item);
Winson Chung4c98d922011-05-31 16:50:48 -0700135 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800136 }
137
138 /**
139 * Removes the item from the workspace. If the view is not null, it also removes the view.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800140 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700141 @Override
142 public void onAccessibilityDrop(View view, ItemInfo item) {
Winsonfa56b3f2015-09-14 12:01:13 -0700143 // Remove the item from launcher and the db, we can ignore the containerInfo in this call
144 // because we already remove the drag view from the folder (if the drag originated from
145 // a folder) in Folder.beginDrag()
Andrew Cole44898c32023-04-05 13:49:32 -0700146 CharSequence announcement = getContext().getString(R.string.item_removed);
147 mDropTargetHandler.onAccessibilityDelete(view, item, announcement);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800148 }
Winson Chung4c98d922011-05-31 16:50:48 -0700149}