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