blob: c12ea57c37f13b335d4ec311fb55db7cc74f50c0 [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;
Kenny Guyed131872014-04-30 03:02:21 +010027
Winson Chung61fa4192011-06-12 15:15:29 -070028public class DeleteDropTarget extends ButtonDropTarget {
Sunny Goyalfa401a12015-04-10 13:45:42 -070029
Winson Chung4c98d922011-05-31 16:50:48 -070030 public DeleteDropTarget(Context context, AttributeSet attrs) {
31 this(context, attrs, 0);
32 }
33
34 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
35 super(context, attrs, defStyle);
36 }
37
38 @Override
39 protected void onFinishInflate() {
40 super.onFinishInflate();
Winson Chung4c98d922011-05-31 16:50:48 -070041 // Get the hover color
Sunny Goyalfa401a12015-04-10 13:45:42 -070042 mHoverColor = getResources().getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080043
Sunny Goyalda1dfa32017-04-26 22:34:49 -070044 setDrawable(R.drawable.ic_remove_shadow);
Winson Chung4c98d922011-05-31 16:50:48 -070045 }
46
Tony Wickham9aae47f2015-10-01 13:04:22 -070047 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -070048 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
49 super.onDragStart(dragObject, options);
Sunny Goyal1ce9c472017-10-03 16:17:32 -070050 setTextBasedOnDragSource(dragObject.dragInfo);
Tony Wickham9aae47f2015-10-01 13:04:22 -070051 }
52
Sunny Goyal0236d0b2017-10-24 14:54:30 -070053 /**
54 * @return true for items that should have a "Remove" action in accessibility.
55 */
56 @Override
Winson Chungf502e5f2018-03-05 18:06:52 +000057 public boolean supportsAccessibilityDrop(ItemInfo info) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -070058 return (info instanceof ShortcutInfo)
59 || (info instanceof LauncherAppWidgetInfo)
60 || (info instanceof FolderInfo);
Winson Chunga48487a2012-03-20 16:19:37 -070061 }
62
Winson Chung4c98d922011-05-31 16:50:48 -070063 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070064 public int getAccessibilityAction() {
65 return LauncherAccessibilityDelegate.REMOVE;
66 }
67
68 @Override
Sunny Goyal1ce9c472017-10-03 16:17:32 -070069 protected boolean supportsDrop(ItemInfo info) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070070 return true;
71 }
72
73 /**
Sunny Goyal1ce9c472017-10-03 16:17:32 -070074 * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item.
Tony Wickham9aae47f2015-10-01 13:04:22 -070075 */
Sunny Goyal1ce9c472017-10-03 16:17:32 -070076 private void setTextBasedOnDragSource(ItemInfo item) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -070077 if (!TextUtils.isEmpty(mText)) {
Sunny Goyal1ce9c472017-10-03 16:17:32 -070078 mText = getResources().getString(item.id != ItemInfo.NO_ID
Jon Mirandabfaa4a42017-08-21 15:31:51 -070079 ? R.string.remove_drop_target_label
Tony Wickham9aae47f2015-10-01 13:04:22 -070080 : android.R.string.cancel);
Jon Mirandabfaa4a42017-08-21 15:31:51 -070081 requestLayout();
Tony Wickham9aae47f2015-10-01 13:04:22 -070082 }
Winson Chung4c98d922011-05-31 16:50:48 -070083 }
84
85 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -080086 public void completeDrop(DragObject d) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -070087 ItemInfo item = d.dragInfo;
Sunny Goyalfa401a12015-04-10 13:45:42 -070088 if ((d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder)) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -070089 onAccessibilityDrop(null, item);
Winson Chung4c98d922011-05-31 16:50:48 -070090 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080091 }
92
93 /**
94 * Removes the item from the workspace. If the view is not null, it also removes the view.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080095 */
Sunny Goyal0236d0b2017-10-24 14:54:30 -070096 @Override
97 public void onAccessibilityDrop(View view, ItemInfo item) {
Winsonfa56b3f2015-09-14 12:01:13 -070098 // Remove the item from launcher and the db, we can ignore the containerInfo in this call
99 // because we already remove the drag view from the folder (if the drag originated from
100 // a folder) in Folder.beginDrag()
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700101 mLauncher.removeItem(view, item, true /* deleteFromDb */);
102 mLauncher.getWorkspace().stripEmptyScreens();
103 mLauncher.getDragLayer()
104 .announceForAccessibility(getContext().getString(R.string.item_removed));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800105 }
Winson Chung4c98d922011-05-31 16:50:48 -0700106}