blob: 423f2bbf1c13e658e7c6d5f627b25326ccac85a2 [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
Tony Wickham03f27012019-04-25 14:22:54 -070019import static com.android.launcher3.userevent.nano.LauncherLogProto.Action.Touch.TAP;
20import static com.android.launcher3.userevent.nano.LauncherLogProto.ControlType.UNDO;
21
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;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070029import com.android.launcher3.logging.LoggerUtils;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070030import com.android.launcher3.model.ModelWriter;
Winson Chung1054d4e2018-03-05 19:39:21 +000031import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070032import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070033import com.android.launcher3.views.Snackbar;
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
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070037 private int mControlType = ControlType.DEFAULT_CONTROLTYPE;
38
Winson Chung4c98d922011-05-31 16:50:48 -070039 public DeleteDropTarget(Context context, AttributeSet attrs) {
40 this(context, attrs, 0);
41 }
42
43 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
44 super(context, attrs, defStyle);
45 }
46
47 @Override
48 protected void onFinishInflate() {
49 super.onFinishInflate();
Winson Chung4c98d922011-05-31 16:50:48 -070050 // Get the hover color
Sunny Goyalfa401a12015-04-10 13:45:42 -070051 mHoverColor = getResources().getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080052
Sunny Goyalda1dfa32017-04-26 22:34:49 -070053 setDrawable(R.drawable.ic_remove_shadow);
Winson Chung4c98d922011-05-31 16:50:48 -070054 }
55
Tony Wickham9aae47f2015-10-01 13:04:22 -070056 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -070057 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
58 super.onDragStart(dragObject, options);
Sunny Goyal1ce9c472017-10-03 16:17:32 -070059 setTextBasedOnDragSource(dragObject.dragInfo);
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070060 setControlTypeBasedOnDragSource(dragObject.dragInfo);
Tony Wickham9aae47f2015-10-01 13:04:22 -070061 }
62
Sunny Goyal0236d0b2017-10-24 14:54:30 -070063 /**
64 * @return true for items that should have a "Remove" action in accessibility.
65 */
66 @Override
Winson Chung1054d4e2018-03-05 19:39:21 +000067 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
Sunny Goyal95899162019-03-27 16:03:06 -070068 if (info instanceof WorkspaceItemInfo) {
vadimt26850a22019-02-05 14:56:33 -080069 // Support the action unless the item is in a context menu.
70 return info.screenId >= 0;
71 }
72
73 return (info instanceof LauncherAppWidgetInfo)
Sunny Goyal1a70cef2015-04-22 11:29:51 -070074 || (info instanceof FolderInfo);
Winson Chunga48487a2012-03-20 16:19:37 -070075 }
76
Winson Chung4c98d922011-05-31 16:50:48 -070077 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070078 public int getAccessibilityAction() {
79 return LauncherAccessibilityDelegate.REMOVE;
80 }
81
82 @Override
Sunny Goyal1ce9c472017-10-03 16:17:32 -070083 protected boolean supportsDrop(ItemInfo info) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070084 return true;
85 }
86
87 /**
Sunny Goyal1ce9c472017-10-03 16:17:32 -070088 * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item.
Tony Wickham9aae47f2015-10-01 13:04:22 -070089 */
Sunny Goyal1ce9c472017-10-03 16:17:32 -070090 private void setTextBasedOnDragSource(ItemInfo item) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -070091 if (!TextUtils.isEmpty(mText)) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -070092 mText = getResources().getString(canRemove(item)
Jon Mirandabfaa4a42017-08-21 15:31:51 -070093 ? R.string.remove_drop_target_label
Tony Wickham9aae47f2015-10-01 13:04:22 -070094 : android.R.string.cancel);
vadimt422885d2019-02-15 19:55:43 -080095 setContentDescription(mText);
Jon Mirandabfaa4a42017-08-21 15:31:51 -070096 requestLayout();
Tony Wickham9aae47f2015-10-01 13:04:22 -070097 }
Winson Chung4c98d922011-05-31 16:50:48 -070098 }
99
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700100 private boolean canRemove(ItemInfo item) {
101 return item.id != ItemInfo.NO_ID;
102 }
103
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700104 /**
105 * Set mControlType depending on the drag item.
106 */
107 private void setControlTypeBasedOnDragSource(ItemInfo item) {
108 mControlType = item.id != ItemInfo.NO_ID ? ControlType.REMOVE_TARGET
109 : ControlType.CANCEL_TARGET;
110 }
111
Winson Chung4c98d922011-05-31 16:50:48 -0700112 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700113 public void onDrop(DragObject d, DragOptions options) {
114 if (canRemove(d.dragInfo)) {
115 mLauncher.getModelWriter().prepareToUndoDelete();
Samuel Fufa2f0b2d32019-11-26 16:16:02 -0800116 d.dragInfo.container = NO_ID;
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700117 }
118 super.onDrop(d, options);
119 }
120
121 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800122 public void completeDrop(DragObject d) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700123 ItemInfo item = d.dragInfo;
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700124 if (canRemove(item)) {
Tonyf80e8932018-12-21 11:58:04 -0800125 int itemPage = mLauncher.getWorkspace().getCurrentPage();
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700126 onAccessibilityDrop(null, item);
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700127 ModelWriter modelWriter = mLauncher.getModelWriter();
Tony Wickham03f27012019-04-25 14:22:54 -0700128 Runnable onUndoClicked = () -> {
Sunny Goyala7a5bf32020-01-05 15:35:29 +0530129 mLauncher.setPageToBindSynchronously(itemPage);
130 modelWriter.abortDelete();
Tony Wickham03f27012019-04-25 14:22:54 -0700131 mLauncher.getUserEventDispatcher().logActionOnControl(TAP, UNDO);
132 };
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700133 Snackbar.show(mLauncher, R.string.item_removed, R.string.undo,
Tony Wickham03f27012019-04-25 14:22:54 -0700134 modelWriter::commitDelete, onUndoClicked);
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()
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700146 mLauncher.removeItem(view, item, true /* deleteFromDb */);
147 mLauncher.getWorkspace().stripEmptyScreens();
148 mLauncher.getDragLayer()
149 .announceForAccessibility(getContext().getString(R.string.item_removed));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800150 }
Winson Chung1054d4e2018-03-05 19:39:21 +0000151
152 @Override
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700153 public Target getDropTargetForLogging() {
154 Target t = LoggerUtils.newTarget(Target.Type.CONTROL);
155 t.controlType = mControlType;
156 return t;
Winson Chung1054d4e2018-03-05 19:39:21 +0000157 }
Winson Chung4c98d922011-05-31 16:50:48 -0700158}