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 17 package com.android.launcher3; 18 19 import android.content.Context; 20 import android.text.TextUtils; 21 import android.util.AttributeSet; 22 import android.view.View; 23 24 import com.android.launcher3.dragndrop.DragOptions; 25 import com.android.launcher3.folder.Folder; 26 27 public class DeleteDropTarget extends ButtonDropTarget { 28 29 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(); 40 // Get the hover color 41 mHoverColor = getResources().getColor(R.color.delete_target_hover_tint); 42 43 setDrawable(R.drawable.ic_remove_launcher); 44 } 45 46 @Override 47 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) { 48 super.onDragStart(dragObject, options); 49 setTextBasedOnDragSource(dragObject.dragSource); 50 } 51 52 /** @return true for items that should have a "Remove" action in accessibility. */ 53 public static boolean supportsAccessibleDrop(ItemInfo info) { 54 return (info instanceof ShortcutInfo) 55 || (info instanceof LauncherAppWidgetInfo) 56 || (info instanceof FolderInfo); 57 } 58 59 @Override 60 protected boolean supportsDrop(DragSource source, ItemInfo info) { 61 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) { 68 if (!TextUtils.isEmpty(getText())) { 69 setText(dragSource.supportsDeleteDropTarget() ? R.string.remove_drop_target_label 70 : android.R.string.cancel); 71 } 72 } 73 74 @Override 75 public void completeDrop(DragObject d) { 76 ItemInfo item = d.dragInfo; 77 if ((d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder)) { 78 removeWorkspaceOrFolderItem(mLauncher, item, null); 79 } 80 } 81 82 /** 83 * Removes the item from the workspace. If the view is not null, it also removes the view. 84 */ 85 public static void removeWorkspaceOrFolderItem(Launcher launcher, ItemInfo item, View view) { 86 // 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() 89 launcher.removeItem(view, item, true /* deleteFromDb */); 90 launcher.getWorkspace().stripEmptyScreens(); 91 launcher.getDragLayer().announceForAccessibility(launcher.getString(R.string.item_removed)); 92 } 93 } 94