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 static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_DROPPED_ON_CANCEL; 20 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_DROPPED_ON_REMOVE; 21 22 import android.content.Context; 23 import android.text.TextUtils; 24 import android.util.AttributeSet; 25 import android.view.View; 26 27 import com.android.launcher3.accessibility.LauncherAccessibilityDelegate; 28 import com.android.launcher3.dragndrop.DragOptions; 29 import com.android.launcher3.logging.StatsLogManager; 30 import com.android.launcher3.model.data.CollectionInfo; 31 import com.android.launcher3.model.data.ItemInfo; 32 import com.android.launcher3.model.data.LauncherAppWidgetInfo; 33 import com.android.launcher3.model.data.WorkspaceItemInfo; 34 35 public class DeleteDropTarget extends ButtonDropTarget { 36 37 private final StatsLogManager mStatsLogManager; 38 39 private StatsLogManager.LauncherEvent mLauncherEvent; 40 DeleteDropTarget(Context context)41 public DeleteDropTarget(Context context) { 42 this(context, null, 0); 43 } 44 DeleteDropTarget(Context context, AttributeSet attrs)45 public DeleteDropTarget(Context context, AttributeSet attrs) { 46 this(context, attrs, 0); 47 } 48 DeleteDropTarget(Context context, AttributeSet attrs, int defStyle)49 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) { 50 super(context, attrs, defStyle); 51 this.mStatsLogManager = StatsLogManager.newInstance(context); 52 } 53 54 @Override onFinishInflate()55 protected void onFinishInflate() { 56 super.onFinishInflate(); 57 setDrawable(R.drawable.ic_remove_no_shadow); 58 } 59 60 @Override onDragStart(DropTarget.DragObject dragObject, DragOptions options)61 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) { 62 super.onDragStart(dragObject, options); 63 setTextBasedOnDragSource(dragObject.dragInfo); 64 setControlTypeBasedOnDragSource(dragObject.dragInfo); 65 } 66 67 /** 68 * @return true for items that should have a "Remove" action in accessibility. 69 */ 70 @Override supportsAccessibilityDrop(ItemInfo info, View view)71 public boolean supportsAccessibilityDrop(ItemInfo info, View view) { 72 if (info instanceof WorkspaceItemInfo) { 73 // Support the action unless the item is in a context menu. 74 return canRemove(info); 75 } 76 77 return (info instanceof LauncherAppWidgetInfo) 78 || (info instanceof CollectionInfo); 79 } 80 81 @Override getAccessibilityAction()82 public int getAccessibilityAction() { 83 return LauncherAccessibilityDelegate.REMOVE; 84 } 85 86 @Override setupItemInfo(ItemInfo info)87 protected void setupItemInfo(ItemInfo info) {} 88 89 @Override supportsDrop(ItemInfo info)90 protected boolean supportsDrop(ItemInfo info) { 91 return true; 92 } 93 94 /** 95 * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item. 96 */ setTextBasedOnDragSource(ItemInfo item)97 private void setTextBasedOnDragSource(ItemInfo item) { 98 if (!TextUtils.isEmpty(mText)) { 99 mText = getResources().getString(canRemove(item) 100 ? R.string.remove_drop_target_label 101 : android.R.string.cancel); 102 setContentDescription(mText); 103 requestLayout(); 104 } 105 } 106 canRemove(ItemInfo item)107 private boolean canRemove(ItemInfo item) { 108 return item.id != ItemInfo.NO_ID; 109 } 110 111 /** 112 * Set mControlType depending on the drag item. 113 */ setControlTypeBasedOnDragSource(ItemInfo item)114 private void setControlTypeBasedOnDragSource(ItemInfo item) { 115 mLauncherEvent = item.id != ItemInfo.NO_ID ? LAUNCHER_ITEM_DROPPED_ON_REMOVE 116 : LAUNCHER_ITEM_DROPPED_ON_CANCEL; 117 } 118 119 @Override onDrop(DragObject d, DragOptions options)120 public void onDrop(DragObject d, DragOptions options) { 121 if (canRemove(d.dragInfo)) { 122 mDropTargetHandler.prepareToUndoDelete(); 123 } 124 super.onDrop(d, options); 125 mStatsLogManager.logger().withInstanceId(d.logInstanceId) 126 .log(mLauncherEvent); 127 } 128 129 @Override completeDrop(DragObject d)130 public void completeDrop(DragObject d) { 131 ItemInfo item = d.dragInfo; 132 if (canRemove(item)) { 133 onAccessibilityDrop(null, item); 134 mDropTargetHandler.onDeleteComplete(item); 135 } 136 } 137 138 /** 139 * Removes the item from the workspace. If the view is not null, it also removes the view. 140 */ 141 @Override onAccessibilityDrop(View view, ItemInfo item)142 public void onAccessibilityDrop(View view, ItemInfo item) { 143 // Remove the item from launcher and the db, we can ignore the containerInfo in this call 144 // because we already remove the drag view from the folder (if the drag originated from 145 // a folder) in Folder.beginDrag() 146 CharSequence announcement = getContext().getString(R.string.item_removed); 147 mDropTargetHandler.onAccessibilityDelete(view, item, announcement); 148 } 149 } 150