blob: 28d11291b9594ad207978d0aa5bfb5efbdd20396 [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;
Winson Chung1054d4e2018-03-05 19:39:21 +000027import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Kenny Guyed131872014-04-30 03:02:21 +010028
Winson Chung61fa4192011-06-12 15:15:29 -070029public class DeleteDropTarget extends ButtonDropTarget {
Sunny Goyalfa401a12015-04-10 13:45:42 -070030
Winson Chung4c98d922011-05-31 16:50:48 -070031 public DeleteDropTarget(Context context, AttributeSet attrs) {
32 this(context, attrs, 0);
33 }
34
35 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
36 super(context, attrs, defStyle);
37 }
38
39 @Override
40 protected void onFinishInflate() {
41 super.onFinishInflate();
Winson Chung4c98d922011-05-31 16:50:48 -070042 // Get the hover color
Sunny Goyalfa401a12015-04-10 13:45:42 -070043 mHoverColor = getResources().getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080044
Sunny Goyalda1dfa32017-04-26 22:34:49 -070045 setDrawable(R.drawable.ic_remove_shadow);
Winson Chung4c98d922011-05-31 16:50:48 -070046 }
47
Tony Wickham9aae47f2015-10-01 13:04:22 -070048 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -070049 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
50 super.onDragStart(dragObject, options);
Sunny Goyal1ce9c472017-10-03 16:17:32 -070051 setTextBasedOnDragSource(dragObject.dragInfo);
Tony Wickham9aae47f2015-10-01 13:04:22 -070052 }
53
Sunny Goyal0236d0b2017-10-24 14:54:30 -070054 /**
55 * @return true for items that should have a "Remove" action in accessibility.
56 */
57 @Override
Winson Chung1054d4e2018-03-05 19:39:21 +000058 public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -070059 return (info instanceof ShortcutInfo)
60 || (info instanceof LauncherAppWidgetInfo)
61 || (info instanceof FolderInfo);
Winson Chunga48487a2012-03-20 16:19:37 -070062 }
63
Winson Chung4c98d922011-05-31 16:50:48 -070064 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070065 public int getAccessibilityAction() {
66 return LauncherAccessibilityDelegate.REMOVE;
67 }
68
69 @Override
Sunny Goyal1ce9c472017-10-03 16:17:32 -070070 protected boolean supportsDrop(ItemInfo info) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070071 return true;
72 }
73
74 /**
Sunny Goyal1ce9c472017-10-03 16:17:32 -070075 * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item.
Tony Wickham9aae47f2015-10-01 13:04:22 -070076 */
Sunny Goyal1ce9c472017-10-03 16:17:32 -070077 private void setTextBasedOnDragSource(ItemInfo item) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -070078 if (!TextUtils.isEmpty(mText)) {
Sunny Goyal1ce9c472017-10-03 16:17:32 -070079 mText = getResources().getString(item.id != ItemInfo.NO_ID
Jon Mirandabfaa4a42017-08-21 15:31:51 -070080 ? R.string.remove_drop_target_label
Tony Wickham9aae47f2015-10-01 13:04:22 -070081 : android.R.string.cancel);
Jon Mirandabfaa4a42017-08-21 15:31:51 -070082 requestLayout();
Tony Wickham9aae47f2015-10-01 13:04:22 -070083 }
Winson Chung4c98d922011-05-31 16:50:48 -070084 }
85
86 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -080087 public void completeDrop(DragObject d) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -070088 ItemInfo item = d.dragInfo;
Sunny Goyalfa401a12015-04-10 13:45:42 -070089 if ((d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder)) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -070090 onAccessibilityDrop(null, item);
Winson Chung4c98d922011-05-31 16:50:48 -070091 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080092 }
93
94 /**
95 * Removes the item from the workspace. If the view is not null, it also removes the view.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080096 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -070097 @Override
98 public void onAccessibilityDrop(View view, ItemInfo item) {
Winsonfa56b3f2015-09-14 12:01:13 -070099 // Remove the item from launcher and the db, we can ignore the containerInfo in this call
100 // because we already remove the drag view from the folder (if the drag originated from
101 // a folder) in Folder.beginDrag()
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700102 mLauncher.removeItem(view, item, true /* deleteFromDb */);
103 mLauncher.getWorkspace().stripEmptyScreens();
104 mLauncher.getDragLayer()
105 .announceForAccessibility(getContext().getString(R.string.item_removed));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800106 }
Winson Chung1054d4e2018-03-05 19:39:21 +0000107
108 @Override
109 public int getControlTypeForLogging() {
110 return ControlType.REMOVE_TARGET;
111 }
Winson Chung4c98d922011-05-31 16:50:48 -0700112}