blob: c80f96bb801c5f35b08ed10fa5602abfe0803e1a [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 Goyal1a70cef2015-04-22 11:29:51 -070065 return (info instanceof ShortcutInfo)
66 || (info instanceof LauncherAppWidgetInfo)
67 || (info instanceof FolderInfo);
Winson Chunga48487a2012-03-20 16:19:37 -070068 }
69
Winson Chung4c98d922011-05-31 16:50:48 -070070 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070071 public int getAccessibilityAction() {
72 return LauncherAccessibilityDelegate.REMOVE;
73 }
74
75 @Override
Sunny Goyal1ce9c472017-10-03 16:17:32 -070076 protected boolean supportsDrop(ItemInfo info) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070077 return true;
78 }
79
80 /**
Sunny Goyal1ce9c472017-10-03 16:17:32 -070081 * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item.
Tony Wickham9aae47f2015-10-01 13:04:22 -070082 */
Sunny Goyal1ce9c472017-10-03 16:17:32 -070083 private void setTextBasedOnDragSource(ItemInfo item) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -070084 if (!TextUtils.isEmpty(mText)) {
Tony Wickham6a71a5b2018-08-21 11:40:23 -070085 mText = getResources().getString(canRemove(item)
Jon Mirandabfaa4a42017-08-21 15:31:51 -070086 ? R.string.remove_drop_target_label
Tony Wickham9aae47f2015-10-01 13:04:22 -070087 : android.R.string.cancel);
Jon Mirandabfaa4a42017-08-21 15:31:51 -070088 requestLayout();
Tony Wickham9aae47f2015-10-01 13:04:22 -070089 }
Winson Chung4c98d922011-05-31 16:50:48 -070090 }
91
Tony Wickham6a71a5b2018-08-21 11:40:23 -070092 private boolean canRemove(ItemInfo item) {
93 return item.id != ItemInfo.NO_ID;
94 }
95
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070096 /**
97 * Set mControlType depending on the drag item.
98 */
99 private void setControlTypeBasedOnDragSource(ItemInfo item) {
100 mControlType = item.id != ItemInfo.NO_ID ? ControlType.REMOVE_TARGET
101 : ControlType.CANCEL_TARGET;
102 }
103
Winson Chung4c98d922011-05-31 16:50:48 -0700104 @Override
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700105 public void onDrop(DragObject d, DragOptions options) {
106 if (canRemove(d.dragInfo)) {
107 mLauncher.getModelWriter().prepareToUndoDelete();
108 }
109 super.onDrop(d, options);
110 }
111
112 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800113 public void completeDrop(DragObject d) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700114 ItemInfo item = d.dragInfo;
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700115 if (canRemove(item)) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700116 onAccessibilityDrop(null, item);
Tony Wickham6a71a5b2018-08-21 11:40:23 -0700117 ModelWriter modelWriter = mLauncher.getModelWriter();
118 Snackbar.show(mLauncher, R.string.item_removed, R.string.undo,
119 modelWriter::commitDelete, modelWriter::abortDelete);
Winson Chung4c98d922011-05-31 16:50:48 -0700120 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800121 }
122
123 /**
124 * Removes the item from the workspace. If the view is not null, it also removes the view.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800125 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700126 @Override
127 public void onAccessibilityDrop(View view, ItemInfo item) {
Winsonfa56b3f2015-09-14 12:01:13 -0700128 // Remove the item from launcher and the db, we can ignore the containerInfo in this call
129 // because we already remove the drag view from the folder (if the drag originated from
130 // a folder) in Folder.beginDrag()
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700131 mLauncher.removeItem(view, item, true /* deleteFromDb */);
132 mLauncher.getWorkspace().stripEmptyScreens();
133 mLauncher.getDragLayer()
134 .announceForAccessibility(getContext().getString(R.string.item_removed));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800135 }
Winson Chung1054d4e2018-03-05 19:39:21 +0000136
137 @Override
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700138 public Target getDropTargetForLogging() {
139 Target t = LoggerUtils.newTarget(Target.Type.CONTROL);
140 t.controlType = mControlType;
141 return t;
Winson Chung1054d4e2018-03-05 19:39:21 +0000142 }
Winson Chung4c98d922011-05-31 16:50:48 -0700143}