blob: fdd4f34bd58dfbcda375a929c353a7eaefa6915c [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 Goyal94b510c2016-08-16 15:36:48 -070024import com.android.launcher3.dragndrop.DragOptions;
Sunny Goyal26119432016-02-18 22:09:23 +000025import com.android.launcher3.folder.Folder;
Kenny Guyed131872014-04-30 03:02:21 +010026
Winson Chung61fa4192011-06-12 15:15:29 -070027public class DeleteDropTarget extends ButtonDropTarget {
Sunny Goyalfa401a12015-04-10 13:45:42 -070028
Winson Chung4c98d922011-05-31 16:50:48 -070029 public DeleteDropTarget(Context context, AttributeSet attrs) {
30 this(context, attrs, 0);
31 }
32
33 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
34 super(context, attrs, defStyle);
35 }
36
37 @Override
38 protected void onFinishInflate() {
39 super.onFinishInflate();
Winson Chung4c98d922011-05-31 16:50:48 -070040 // Get the hover color
Sunny Goyalfa401a12015-04-10 13:45:42 -070041 mHoverColor = getResources().getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080042
Sunny Goyalda1dfa32017-04-26 22:34:49 -070043 setDrawable(R.drawable.ic_remove_shadow);
Winson Chung4c98d922011-05-31 16:50:48 -070044 }
45
Tony Wickham9aae47f2015-10-01 13:04:22 -070046 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -070047 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
48 super.onDragStart(dragObject, options);
Sunny Goyal1ce9c472017-10-03 16:17:32 -070049 setTextBasedOnDragSource(dragObject.dragInfo);
Tony Wickham9aae47f2015-10-01 13:04:22 -070050 }
51
52 /** @return true for items that should have a "Remove" action in accessibility. */
53 public static boolean supportsAccessibleDrop(ItemInfo info) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -070054 return (info instanceof ShortcutInfo)
55 || (info instanceof LauncherAppWidgetInfo)
56 || (info instanceof FolderInfo);
Winson Chunga48487a2012-03-20 16:19:37 -070057 }
58
Winson Chung4c98d922011-05-31 16:50:48 -070059 @Override
Sunny Goyal1ce9c472017-10-03 16:17:32 -070060 protected boolean supportsDrop(ItemInfo info) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070061 return true;
62 }
63
64 /**
Sunny Goyal1ce9c472017-10-03 16:17:32 -070065 * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item.
Tony Wickham9aae47f2015-10-01 13:04:22 -070066 */
Sunny Goyal1ce9c472017-10-03 16:17:32 -070067 private void setTextBasedOnDragSource(ItemInfo item) {
Jon Mirandabfaa4a42017-08-21 15:31:51 -070068 if (!TextUtils.isEmpty(mText)) {
Sunny Goyal1ce9c472017-10-03 16:17:32 -070069 mText = getResources().getString(item.id != ItemInfo.NO_ID
Jon Mirandabfaa4a42017-08-21 15:31:51 -070070 ? R.string.remove_drop_target_label
Tony Wickham9aae47f2015-10-01 13:04:22 -070071 : android.R.string.cancel);
Jon Mirandabfaa4a42017-08-21 15:31:51 -070072 requestLayout();
Tony Wickham9aae47f2015-10-01 13:04:22 -070073 }
Winson Chung4c98d922011-05-31 16:50:48 -070074 }
75
76 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -080077 public void completeDrop(DragObject d) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -070078 ItemInfo item = d.dragInfo;
Sunny Goyalfa401a12015-04-10 13:45:42 -070079 if ((d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080080 removeWorkspaceOrFolderItem(mLauncher, item, null);
Winson Chung4c98d922011-05-31 16:50:48 -070081 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080082 }
83
84 /**
85 * Removes the item from the workspace. If the view is not null, it also removes the view.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080086 */
Sunny Goyale78e3d72015-09-24 11:23:31 -070087 public static void removeWorkspaceOrFolderItem(Launcher launcher, ItemInfo item, View view) {
Winsonfa56b3f2015-09-14 12:01:13 -070088 // Remove the item from launcher and the db, we can ignore the containerInfo in this call
89 // because we already remove the drag view from the folder (if the drag originated from
90 // a folder) in Folder.beginDrag()
Winson2949fb52015-09-24 09:56:11 -070091 launcher.removeItem(view, item, true /* deleteFromDb */);
Winsonc0b52fe2015-09-09 16:38:15 -070092 launcher.getWorkspace().stripEmptyScreens();
Sunny Goyale78e3d72015-09-24 11:23:31 -070093 launcher.getDragLayer().announceForAccessibility(launcher.getString(R.string.item_removed));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080094 }
Winson Chung4c98d922011-05-31 16:50:48 -070095}