Home | History | Annotate | Download | only in launcher3
      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.accessibility.LauncherAccessibilityDelegate;
     25 import com.android.launcher3.dragndrop.DragOptions;
     26 import com.android.launcher3.folder.Folder;
     27 import com.android.launcher3.logging.LoggerUtils;
     28 import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
     29 import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
     30 
     31 public class DeleteDropTarget extends ButtonDropTarget {
     32 
     33     private int mControlType = ControlType.DEFAULT_CONTROLTYPE;
     34 
     35     public DeleteDropTarget(Context context, AttributeSet attrs) {
     36         this(context, attrs, 0);
     37     }
     38 
     39     public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
     40         super(context, attrs, defStyle);
     41     }
     42 
     43     @Override
     44     protected void onFinishInflate() {
     45         super.onFinishInflate();
     46         // Get the hover color
     47         mHoverColor = getResources().getColor(R.color.delete_target_hover_tint);
     48 
     49         setDrawable(R.drawable.ic_remove_shadow);
     50     }
     51 
     52     @Override
     53     public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
     54         super.onDragStart(dragObject, options);
     55         setTextBasedOnDragSource(dragObject.dragInfo);
     56         setControlTypeBasedOnDragSource(dragObject.dragInfo);
     57     }
     58 
     59     /**
     60      * @return true for items that should have a "Remove" action in accessibility.
     61      */
     62     @Override
     63     public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
     64         return (info instanceof ShortcutInfo)
     65                 || (info instanceof LauncherAppWidgetInfo)
     66                 || (info instanceof FolderInfo);
     67     }
     68 
     69     @Override
     70     public int getAccessibilityAction() {
     71         return LauncherAccessibilityDelegate.REMOVE;
     72     }
     73 
     74     @Override
     75     protected boolean supportsDrop(ItemInfo info) {
     76         return true;
     77     }
     78 
     79     /**
     80      * Set the drop target's text to either "Remove" or "Cancel" depending on the drag item.
     81      */
     82     private void setTextBasedOnDragSource(ItemInfo item) {
     83         if (!TextUtils.isEmpty(mText)) {
     84             mText = getResources().getString(item.id != ItemInfo.NO_ID
     85                     ? R.string.remove_drop_target_label
     86                     : android.R.string.cancel);
     87             requestLayout();
     88         }
     89     }
     90 
     91     /**
     92      * Set mControlType depending on the drag item.
     93      */
     94     private void setControlTypeBasedOnDragSource(ItemInfo item) {
     95         mControlType = item.id != ItemInfo.NO_ID ? ControlType.REMOVE_TARGET
     96                 : ControlType.CANCEL_TARGET;
     97     }
     98 
     99     @Override
    100     public void completeDrop(DragObject d) {
    101         ItemInfo item = d.dragInfo;
    102         if ((d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder)) {
    103             onAccessibilityDrop(null, item);
    104         }
    105     }
    106 
    107     /**
    108      * Removes the item from the workspace. If the view is not null, it also removes the view.
    109      */
    110     @Override
    111     public void onAccessibilityDrop(View view, ItemInfo item) {
    112         // Remove the item from launcher and the db, we can ignore the containerInfo in this call
    113         // because we already remove the drag view from the folder (if the drag originated from
    114         // a folder) in Folder.beginDrag()
    115         mLauncher.removeItem(view, item, true /* deleteFromDb */);
    116         mLauncher.getWorkspace().stripEmptyScreens();
    117         mLauncher.getDragLayer()
    118                 .announceForAccessibility(getContext().getString(R.string.item_removed));
    119     }
    120 
    121     @Override
    122     public Target getDropTargetForLogging() {
    123         Target t = LoggerUtils.newTarget(Target.Type.CONTROL);
    124         t.controlType = mControlType;
    125         return t;
    126     }
    127 }
    128