blob: 4c6824a5ceac3f32f42c1520793778af59cd912a [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
vadimt26850a22019-02-05 14:56:33 -080019import static com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT;
20
Winson Chung4c98d922011-05-31 16:50:48 -070021import android.content.Context;
Sunny Goyalf37a2142016-03-24 17:28:25 -070022import android.text.TextUtils;
Winson Chung4c98d922011-05-31 16:50:48 -070023import android.util.AttributeSet;
24import android.view.View;
Winson Chung4c98d922011-05-31 16:50:48 -070025
Sunny Goyal0236d0b2017-10-24 14:54:30 -070026import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Sunny Goyal94b510c2016-08-16 15:36:48 -070027import com.android.launcher3.dragndrop.DragOptions;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070028import com.android.launcher3.logging.LoggerUtils;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070029import com.android.launcher3.model.ModelWriter;
Winson Chung1054d4e2018-03-05 19:39:21 +000030import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070031import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070032import com.android.launcher3.views.Snackbar;
Kenny Guyed131872014-04-30 03:02:21 +010033
Winson Chung61fa4192011-06-12 15:15:29 -070034public class DeleteDropTarget extends ButtonDropTarget {
Sunny Goyalfa401a12015-04-10 13:45:42 -070035
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070036 private int mControlType = ControlType.DEFAULT_CONTROLTYPE;
37
Winson Chung4c98d922011-05-31 16:50:48 -070038 public DeleteDropTarget(Context context, AttributeSet attrs) {
39 this(context, attrs, 0);
40 }
41
42 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
43 super(context, attrs, defStyle);
44 }
45
46 @Override
47 protected void onFinishInflate() {
48 super.onFinishInflate();
Winson Chung4c98d922011-05-31 16:50:48 -070049 // Get the hover color
Sunny Goyalfa401a12015-04-10 13:45:42 -070050 mHoverColor = getResources().getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080051
Sunny Goyalda1dfa32017-04-26 22:34:49 -070052 setDrawable(R.drawable.ic_remove_shadow);
Winson Chung4c98d922011-05-31 16:50:48 -070053 }
54
Tony Wickham9aae47f2015-10-01 13:04:22 -070055 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -070056 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
57 super.onDragStart(dragObject, options);
Sunny Goyal1ce9c472017-10-03 16:17:32 -070058 setTextBasedOnDragSource(dragObject.dragInfo);
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070059 setControlTypeBasedOnDragSource(dragObject.dragInfo);
Tony Wickham9aae47f2015-10-01 13:04:22 -070060 }
61
Sunny Goyal0236d0b2017-10-24 14:54:30 -070062 /**
63 * @return true for items that should have a "Remove" action in accessibility.
64 */
65 @Override
Winson Chung1054d4e2018-03-05 19:39:21 +000066 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
vadimt26850a22019-02-05 14:56:33 -080067 if (info instanceof ShortcutInfo) {
68 // Support the action unless the item is in a context menu.
69 return info.screenId >= 0;
70 }
71
72 return (info instanceof LauncherAppWidgetInfo)
Sunny Goyal1a70cef2015-04-22 11:29:51 -070073 || (info instanceof FolderInfo);
Winson Chunga48487a2012-03-20 16:19:37 -070074 }
75
Winson Chung4c98d922011-05-31 16:50:48 -070076 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070077 public int getAccessibilityAction() {
78 return LauncherAccessibilityDelegate.REMOVE;
79 }
80
81 @Override
Sunny Goyal1ce9c472017-10-03 16:17:32 -070082 protected boolean supportsDrop(ItemInfo info) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070083 return true;
84 }
85
86 /**
Sunny Goyal1ce9c472017-10-03 16:17:32 -070087 * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item.
Tony Wickham9aae47f2015-10-01 13:04:22 -070088 */
Sunny Goyal1ce9c472017-10-03 16:17:32 -070089 private void setTextBasedOnDragSource(ItemInfo item) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -070090 if (!TextUtils.isEmpty(mText)) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -070091 mText = getResources().getString(canRemove(item)
Jon Mirandabfaa4a42017-08-21 15:31:51 -070092 ? R.string.remove_drop_target_label
Tony Wickham9aae47f2015-10-01 13:04:22 -070093 : android.R.string.cancel);
vadimt422885d2019-02-15 19:55:43 -080094 setContentDescription(mText);
Jon Mirandabfaa4a42017-08-21 15:31:51 -070095 requestLayout();
Tony Wickham9aae47f2015-10-01 13:04:22 -070096 }
Winson Chung4c98d922011-05-31 16:50:48 -070097 }
98
Tony Wickham6a71a5b2018-08-21 11:40:23 -070099 private boolean canRemove(ItemInfo item) {
100 return item.id != ItemInfo.NO_ID;
101 }
102
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700103 /**
104 * Set mControlType depending on the drag item.
105 */
106 private void setControlTypeBasedOnDragSource(ItemInfo item) {
107 mControlType = item.id != ItemInfo.NO_ID ? ControlType.REMOVE_TARGET
108 : ControlType.CANCEL_TARGET;
109 }
110
Winson Chung4c98d922011-05-31 16:50:48 -0700111 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700112 public void onDrop(DragObject d, DragOptions options) {
113 if (canRemove(d.dragInfo)) {
114 mLauncher.getModelWriter().prepareToUndoDelete();
115 }
116 super.onDrop(d, options);
117 }
118
119 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800120 public void completeDrop(DragObject d) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700121 ItemInfo item = d.dragInfo;
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700122 if (canRemove(item)) {
Tonyf80e8932018-12-21 11:58:04 -0800123 int itemPage = mLauncher.getWorkspace().getCurrentPage();
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700124 onAccessibilityDrop(null, item);
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700125 ModelWriter modelWriter = mLauncher.getModelWriter();
126 Snackbar.show(mLauncher, R.string.item_removed, R.string.undo,
Tonyf80e8932018-12-21 11:58:04 -0800127 modelWriter::commitDelete, () -> modelWriter.abortDelete(itemPage));
Winson Chung4c98d922011-05-31 16:50:48 -0700128 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800129 }
130
131 /**
132 * Removes the item from the workspace. If the view is not null, it also removes the view.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800133 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700134 @Override
135 public void onAccessibilityDrop(View view, ItemInfo item) {
Winsonfa56b3f2015-09-14 12:01:13 -0700136 // Remove the item from launcher and the db, we can ignore the containerInfo in this call
137 // because we already remove the drag view from the folder (if the drag originated from
138 // a folder) in Folder.beginDrag()
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700139 mLauncher.removeItem(view, item, true /* deleteFromDb */);
140 mLauncher.getWorkspace().stripEmptyScreens();
141 mLauncher.getDragLayer()
142 .announceForAccessibility(getContext().getString(R.string.item_removed));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800143 }
Winson Chung1054d4e2018-03-05 19:39:21 +0000144
145 @Override
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700146 public Target getDropTargetForLogging() {
147 Target t = LoggerUtils.newTarget(Target.Type.CONTROL);
148 t.controlType = mControlType;
149 return t;
Winson Chung1054d4e2018-03-05 19:39:21 +0000150 }
Winson Chung4c98d922011-05-31 16:50:48 -0700151}