blob: 9097ed23d778938594d8842615cfb3fb07b87aa2 [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 Goyal40b626b2015-06-04 22:40:14 -070043 setDrawable(R.drawable.ic_remove_launcher);
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);
49 setTextBasedOnDragSource(dragObject.dragSource);
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 Goyalaa8ef112015-06-12 20:04:41 -070060 protected boolean supportsDrop(DragSource source, ItemInfo info) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070061 return true;
62 }
63
64 /**
65 * Set the drop target's text to either "Remove" or "Cancel" depending on the drag source.
66 */
67 public void setTextBasedOnDragSource(DragSource dragSource) {
Sunny Goyalf37a2142016-03-24 17:28:25 -070068 if (!TextUtils.isEmpty(getText())) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070069 setText(dragSource.supportsDeleteDropTarget() ? R.string.remove_drop_target_label
70 : android.R.string.cancel);
71 }
Winson Chung4c98d922011-05-31 16:50:48 -070072 }
73
74 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -080075 public void completeDrop(DragObject d) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -070076 ItemInfo item = d.dragInfo;
Sunny Goyalfa401a12015-04-10 13:45:42 -070077 if ((d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080078 removeWorkspaceOrFolderItem(mLauncher, item, null);
Winson Chung4c98d922011-05-31 16:50:48 -070079 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080080 }
81
82 /**
83 * Removes the item from the workspace. If the view is not null, it also removes the view.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080084 */
Sunny Goyale78e3d72015-09-24 11:23:31 -070085 public static void removeWorkspaceOrFolderItem(Launcher launcher, ItemInfo item, View view) {
Winsonfa56b3f2015-09-14 12:01:13 -070086 // Remove the item from launcher and the db, we can ignore the containerInfo in this call
87 // because we already remove the drag view from the folder (if the drag originated from
88 // a folder) in Folder.beginDrag()
Winson2949fb52015-09-24 09:56:11 -070089 launcher.removeItem(view, item, true /* deleteFromDb */);
Winsonc0b52fe2015-09-09 16:38:15 -070090 launcher.getWorkspace().stripEmptyScreens();
Sunny Goyale78e3d72015-09-24 11:23:31 -070091 launcher.getDragLayer().announceForAccessibility(launcher.getString(R.string.item_removed));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080092 }
Winson Chung4c98d922011-05-31 16:50:48 -070093}