Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
Sunny Goyal | f37a214 | 2016-03-24 17:28:25 -0700 | [diff] [blame] | 20 | import android.text.TextUtils; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 21 | import android.util.AttributeSet; |
| 22 | import android.view.View; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 23 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 24 | import com.android.launcher3.accessibility.LauncherAccessibilityDelegate; |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 25 | import com.android.launcher3.dragndrop.DragOptions; |
Sunny Goyal | 2611943 | 2016-02-18 22:09:23 +0000 | [diff] [blame] | 26 | import com.android.launcher3.folder.Folder; |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame^] | 27 | import com.android.launcher3.logging.LoggerUtils; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 28 | import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType; |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame^] | 29 | import com.android.launcher3.userevent.nano.LauncherLogProto.Target; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 30 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 31 | public class DeleteDropTarget extends ButtonDropTarget { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 32 | |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame^] | 33 | private int mControlType = ControlType.DEFAULT_CONTROLTYPE; |
| 34 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 35 | 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 Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 46 | // Get the hover color |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 47 | mHoverColor = getResources().getColor(R.color.delete_target_hover_tint); |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 48 | |
Sunny Goyal | da1dfa3 | 2017-04-26 22:34:49 -0700 | [diff] [blame] | 49 | setDrawable(R.drawable.ic_remove_shadow); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 52 | @Override |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 53 | public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) { |
| 54 | super.onDragStart(dragObject, options); |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 55 | setTextBasedOnDragSource(dragObject.dragInfo); |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame^] | 56 | setControlTypeBasedOnDragSource(dragObject.dragInfo); |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 59 | /** |
| 60 | * @return true for items that should have a "Remove" action in accessibility. |
| 61 | */ |
| 62 | @Override |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 63 | public boolean supportsAccessibilityDrop(ItemInfo info, View view) { |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 64 | return (info instanceof ShortcutInfo) |
| 65 | || (info instanceof LauncherAppWidgetInfo) |
| 66 | || (info instanceof FolderInfo); |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 69 | @Override |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 70 | public int getAccessibilityAction() { |
| 71 | return LauncherAccessibilityDelegate.REMOVE; |
| 72 | } |
| 73 | |
| 74 | @Override |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 75 | protected boolean supportsDrop(ItemInfo info) { |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 76 | return true; |
| 77 | } |
| 78 | |
| 79 | /** |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 80 | * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item. |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 81 | */ |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 82 | private void setTextBasedOnDragSource(ItemInfo item) { |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 83 | if (!TextUtils.isEmpty(mText)) { |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 84 | mText = getResources().getString(item.id != ItemInfo.NO_ID |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 85 | ? R.string.remove_drop_target_label |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 86 | : android.R.string.cancel); |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 87 | requestLayout(); |
Tony Wickham | 9aae47f | 2015-10-01 13:04:22 -0700 | [diff] [blame] | 88 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame^] | 91 | /** |
| 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 Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 99 | @Override |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 100 | public void completeDrop(DragObject d) { |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 101 | ItemInfo item = d.dragInfo; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 102 | if ((d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder)) { |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 103 | onAccessibilityDrop(null, item); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 104 | } |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Removes the item from the workspace. If the view is not null, it also removes the view. |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 109 | */ |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 110 | @Override |
| 111 | public void onAccessibilityDrop(View view, ItemInfo item) { |
Winson | fa56b3f | 2015-09-14 12:01:13 -0700 | [diff] [blame] | 112 | // 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 Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 115 | mLauncher.removeItem(view, item, true /* deleteFromDb */); |
| 116 | mLauncher.getWorkspace().stripEmptyScreens(); |
| 117 | mLauncher.getDragLayer() |
| 118 | .announceForAccessibility(getContext().getString(R.string.item_removed)); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 119 | } |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 120 | |
| 121 | @Override |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame^] | 122 | public Target getDropTargetForLogging() { |
| 123 | Target t = LoggerUtils.newTarget(Target.Type.CONTROL); |
| 124 | t.controlType = mControlType; |
| 125 | return t; |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 126 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 127 | } |