blob: 8cbad20b57fa764742760c272e27a1e358fd8160 [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
19import android.content.Context;
Sunny Goyalf37a2142016-03-24 17:28:25 -070020import android.text.TextUtils;
Winson Chung4c98d922011-05-31 16:50:48 -070021import android.util.AttributeSet;
22import android.view.View;
Winson Chung4c98d922011-05-31 16:50:48 -070023
Sunny Goyal0236d0b2017-10-24 14:54:30 -070024import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Sunny Goyal94b510c2016-08-16 15:36:48 -070025import com.android.launcher3.dragndrop.DragOptions;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070026import com.android.launcher3.logging.LoggerUtils;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070027import com.android.launcher3.model.ModelWriter;
Winson Chung1054d4e2018-03-05 19:39:21 +000028import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070029import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
Tony Wickham6a71a5b2018-08-21 11:40:23 -070030import com.android.launcher3.views.Snackbar;
Kenny Guyed131872014-04-30 03:02:21 +010031
Winson Chung61fa4192011-06-12 15:15:29 -070032public class DeleteDropTarget extends ButtonDropTarget {
Sunny Goyalfa401a12015-04-10 13:45:42 -070033
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070034 private int mControlType = ControlType.DEFAULT_CONTROLTYPE;
35
Winson Chung4c98d922011-05-31 16:50:48 -070036 public DeleteDropTarget(Context context, AttributeSet attrs) {
37 this(context, attrs, 0);
38 }
39
40 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
41 super(context, attrs, defStyle);
42 }
43
44 @Override
45 protected void onFinishInflate() {
46 super.onFinishInflate();
Winson Chung4c98d922011-05-31 16:50:48 -070047 // Get the hover color
Sunny Goyalfa401a12015-04-10 13:45:42 -070048 mHoverColor = getResources().getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080049
Sunny Goyalda1dfa32017-04-26 22:34:49 -070050 setDrawable(R.drawable.ic_remove_shadow);
Winson Chung4c98d922011-05-31 16:50:48 -070051 }
52
Tony Wickham9aae47f2015-10-01 13:04:22 -070053 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -070054 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
55 super.onDragStart(dragObject, options);
Sunny Goyal1ce9c472017-10-03 16:17:32 -070056 setTextBasedOnDragSource(dragObject.dragInfo);
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070057 setControlTypeBasedOnDragSource(dragObject.dragInfo);
Tony Wickham9aae47f2015-10-01 13:04:22 -070058 }
59
Sunny Goyal0236d0b2017-10-24 14:54:30 -070060 /**
61 * @return true for items that should have a "Remove" action in accessibility.
62 */
63 @Override
Winson Chung1054d4e2018-03-05 19:39:21 +000064 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
Sunny Goyal95899162019-03-27 16:03:06 -070065 if (info instanceof WorkspaceItemInfo) {
vadimt26850a22019-02-05 14:56:33 -080066 // Support the action unless the item is in a context menu.
67 return info.screenId >= 0;
68 }
69
70 return (info instanceof LauncherAppWidgetInfo)
Sunny Goyal1a70cef2015-04-22 11:29:51 -070071 || (info instanceof FolderInfo);
Winson Chunga48487a2012-03-20 16:19:37 -070072 }
73
Winson Chung4c98d922011-05-31 16:50:48 -070074 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070075 public int getAccessibilityAction() {
76 return LauncherAccessibilityDelegate.REMOVE;
77 }
78
79 @Override
Sunny Goyal1ce9c472017-10-03 16:17:32 -070080 protected boolean supportsDrop(ItemInfo info) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070081 return true;
82 }
83
84 /**
Sunny Goyal1ce9c472017-10-03 16:17:32 -070085 * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item.
Tony Wickham9aae47f2015-10-01 13:04:22 -070086 */
Sunny Goyal1ce9c472017-10-03 16:17:32 -070087 private void setTextBasedOnDragSource(ItemInfo item) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -070088 if (!TextUtils.isEmpty(mText)) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -070089 mText = getResources().getString(canRemove(item)
Jon Mirandabfaa4a42017-08-21 15:31:51 -070090 ? R.string.remove_drop_target_label
Tony Wickham9aae47f2015-10-01 13:04:22 -070091 : android.R.string.cancel);
vadimt422885d2019-02-15 19:55:43 -080092 setContentDescription(mText);
Jon Mirandabfaa4a42017-08-21 15:31:51 -070093 requestLayout();
Tony Wickham9aae47f2015-10-01 13:04:22 -070094 }
Winson Chung4c98d922011-05-31 16:50:48 -070095 }
96
Tony Wickham6a71a5b2018-08-21 11:40:23 -070097 private boolean canRemove(ItemInfo item) {
98 return item.id != ItemInfo.NO_ID;
99 }
100
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700101 /**
102 * Set mControlType depending on the drag item.
103 */
104 private void setControlTypeBasedOnDragSource(ItemInfo item) {
105 mControlType = item.id != ItemInfo.NO_ID ? ControlType.REMOVE_TARGET
106 : ControlType.CANCEL_TARGET;
107 }
108
Winson Chung4c98d922011-05-31 16:50:48 -0700109 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700110 public void onDrop(DragObject d, DragOptions options) {
111 if (canRemove(d.dragInfo)) {
112 mLauncher.getModelWriter().prepareToUndoDelete();
113 }
114 super.onDrop(d, options);
115 }
116
117 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800118 public void completeDrop(DragObject d) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700119 ItemInfo item = d.dragInfo;
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700120 if (canRemove(item)) {
Tonyf80e8932018-12-21 11:58:04 -0800121 int itemPage = mLauncher.getWorkspace().getCurrentPage();
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700122 onAccessibilityDrop(null, item);
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700123 ModelWriter modelWriter = mLauncher.getModelWriter();
124 Snackbar.show(mLauncher, R.string.item_removed, R.string.undo,
Tonyf80e8932018-12-21 11:58:04 -0800125 modelWriter::commitDelete, () -> modelWriter.abortDelete(itemPage));
Winson Chung4c98d922011-05-31 16:50:48 -0700126 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800127 }
128
129 /**
130 * Removes the item from the workspace. If the view is not null, it also removes the view.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800131 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700132 @Override
133 public void onAccessibilityDrop(View view, ItemInfo item) {
Winsonfa56b3f2015-09-14 12:01:13 -0700134 // Remove the item from launcher and the db, we can ignore the containerInfo in this call
135 // because we already remove the drag view from the folder (if the drag originated from
136 // a folder) in Folder.beginDrag()
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700137 mLauncher.removeItem(view, item, true /* deleteFromDb */);
138 mLauncher.getWorkspace().stripEmptyScreens();
139 mLauncher.getDragLayer()
140 .announceForAccessibility(getContext().getString(R.string.item_removed));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800141 }
Winson Chung1054d4e2018-03-05 19:39:21 +0000142
143 @Override
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700144 public Target getDropTargetForLogging() {
145 Target t = LoggerUtils.newTarget(Target.Type.CONTROL);
146 t.controlType = mControlType;
147 return t;
Winson Chung1054d4e2018-03-05 19:39:21 +0000148 }
Winson Chung4c98d922011-05-31 16:50:48 -0700149}