blob: 64a58fb8c25cafe3f4ae9d825e513ebf58d3dc3c [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;
Sunny Goyal26119432016-02-18 22:09:23 +000026import com.android.launcher3.folder.Folder;
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070027import com.android.launcher3.logging.LoggerUtils;
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;
Kenny Guyed131872014-04-30 03:02:21 +010030
Winson Chung61fa4192011-06-12 15:15:29 -070031public class DeleteDropTarget extends ButtonDropTarget {
Sunny Goyalfa401a12015-04-10 13:45:42 -070032
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070033 private int mControlType = ControlType.DEFAULT_CONTROLTYPE;
34
Winson Chung4c98d922011-05-31 16:50:48 -070035 public DeleteDropTarget(Context context, AttributeSet attrs) {
36 this(context, attrs, 0);
37 }
38
39 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
40 super(context, attrs, defStyle);
41 }
42
43 @Override
44 protected void onFinishInflate() {
45 super.onFinishInflate();
Winson Chung4c98d922011-05-31 16:50:48 -070046 // Get the hover color
Sunny Goyalfa401a12015-04-10 13:45:42 -070047 mHoverColor = getResources().getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080048
Sunny Goyalda1dfa32017-04-26 22:34:49 -070049 setDrawable(R.drawable.ic_remove_shadow);
Winson Chung4c98d922011-05-31 16:50:48 -070050 }
51
Tony Wickham9aae47f2015-10-01 13:04:22 -070052 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -070053 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
54 super.onDragStart(dragObject, options);
Sunny Goyal1ce9c472017-10-03 16:17:32 -070055 setTextBasedOnDragSource(dragObject.dragInfo);
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070056 setControlTypeBasedOnDragSource(dragObject.dragInfo);
Tony Wickham9aae47f2015-10-01 13:04:22 -070057 }
58
Sunny Goyal0236d0b2017-10-24 14:54:30 -070059 /**
60 * @return true for items that should have a "Remove" action in accessibility.
61 */
62 @Override
Winson Chung1054d4e2018-03-05 19:39:21 +000063 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -070064 return (info instanceof ShortcutInfo)
65 || (info instanceof LauncherAppWidgetInfo)
66 || (info instanceof FolderInfo);
Winson Chunga48487a2012-03-20 16:19:37 -070067 }
68
Winson Chung4c98d922011-05-31 16:50:48 -070069 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070070 public int getAccessibilityAction() {
71 return LauncherAccessibilityDelegate.REMOVE;
72 }
73
74 @Override
Sunny Goyal1ce9c472017-10-03 16:17:32 -070075 protected boolean supportsDrop(ItemInfo info) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070076 return true;
77 }
78
79 /**
Sunny Goyal1ce9c472017-10-03 16:17:32 -070080 * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item.
Tony Wickham9aae47f2015-10-01 13:04:22 -070081 */
Sunny Goyal1ce9c472017-10-03 16:17:32 -070082 private void setTextBasedOnDragSource(ItemInfo item) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -070083 if (!TextUtils.isEmpty(mText)) {
Sunny Goyal1ce9c472017-10-03 16:17:32 -070084 mText = getResources().getString(item.id != ItemInfo.NO_ID
Jon Mirandabfaa4a42017-08-21 15:31:51 -070085 ? R.string.remove_drop_target_label
Tony Wickham9aae47f2015-10-01 13:04:22 -070086 : android.R.string.cancel);
Jon Mirandabfaa4a42017-08-21 15:31:51 -070087 requestLayout();
Tony Wickham9aae47f2015-10-01 13:04:22 -070088 }
Winson Chung4c98d922011-05-31 16:50:48 -070089 }
90
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -070091 /**
92 * Set mControlType depending on the drag item.
93 */
94 private void setControlTypeBasedOnDragSource(ItemInfo item) {
95 mControlType = item.id != ItemInfo.NO_ID ? ControlType.REMOVE_TARGET
96 : ControlType.CANCEL_TARGET;
97 }
98
Winson Chung4c98d922011-05-31 16:50:48 -070099 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -0800100 public void completeDrop(DragObject d) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700101 ItemInfo item = d.dragInfo;
Sunny Goyalfa401a12015-04-10 13:45:42 -0700102 if ((d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder)) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700103 onAccessibilityDrop(null, item);
Winson Chung4c98d922011-05-31 16:50:48 -0700104 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800105 }
106
107 /**
108 * Removes the item from the workspace. If the view is not null, it also removes the view.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800109 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700110 @Override
111 public void onAccessibilityDrop(View view, ItemInfo item) {
Winsonfa56b3f2015-09-14 12:01:13 -0700112 // Remove the item from launcher and the db, we can ignore the containerInfo in this call
113 // because we already remove the drag view from the folder (if the drag originated from
114 // a folder) in Folder.beginDrag()
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700115 mLauncher.removeItem(view, item, true /* deleteFromDb */);
116 mLauncher.getWorkspace().stripEmptyScreens();
117 mLauncher.getDragLayer()
118 .announceForAccessibility(getContext().getString(R.string.item_removed));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800119 }
Winson Chung1054d4e2018-03-05 19:39:21 +0000120
121 @Override
Mehdi Alizadehbda47cf2018-05-01 19:26:05 -0700122 public Target getDropTargetForLogging() {
123 Target t = LoggerUtils.newTarget(Target.Type.CONTROL);
124 t.controlType = mControlType;
125 return t;
Winson Chung1054d4e2018-03-05 19:39:21 +0000126 }
Winson Chung4c98d922011-05-31 16:50:48 -0700127}