2011-05-31 16:50:48 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-05 22:57:57 -04:00
|
|
|
package com.android.launcher3;
|
2011-05-31 16:50:48 -07:00
|
|
|
|
2019-04-25 14:22:54 -07:00
|
|
|
import static com.android.launcher3.userevent.nano.LauncherLogProto.Action.Touch.TAP;
|
|
|
|
|
import static com.android.launcher3.userevent.nano.LauncherLogProto.ControlType.UNDO;
|
|
|
|
|
|
2011-05-31 16:50:48 -07:00
|
|
|
import android.content.Context;
|
2016-03-24 17:28:25 -07:00
|
|
|
import android.text.TextUtils;
|
2011-05-31 16:50:48 -07:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
|
2017-10-24 14:54:30 -07:00
|
|
|
import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
|
2016-08-16 15:36:48 -07:00
|
|
|
import com.android.launcher3.dragndrop.DragOptions;
|
2018-05-01 19:26:05 -07:00
|
|
|
import com.android.launcher3.logging.LoggerUtils;
|
2018-08-21 11:40:23 -07:00
|
|
|
import com.android.launcher3.model.ModelWriter;
|
2020-04-06 15:11:17 -07:00
|
|
|
import com.android.launcher3.model.data.FolderInfo;
|
|
|
|
|
import com.android.launcher3.model.data.ItemInfo;
|
|
|
|
|
import com.android.launcher3.model.data.LauncherAppWidgetInfo;
|
|
|
|
|
import com.android.launcher3.model.data.WorkspaceItemInfo;
|
2018-03-05 19:39:21 +00:00
|
|
|
import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
|
2018-05-01 19:26:05 -07:00
|
|
|
import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
|
2018-08-21 11:40:23 -07:00
|
|
|
import com.android.launcher3.views.Snackbar;
|
2014-04-30 03:02:21 +01:00
|
|
|
|
2011-06-12 15:15:29 -07:00
|
|
|
public class DeleteDropTarget extends ButtonDropTarget {
|
2015-04-10 13:45:42 -07:00
|
|
|
|
2018-05-01 19:26:05 -07:00
|
|
|
private int mControlType = ControlType.DEFAULT_CONTROLTYPE;
|
|
|
|
|
|
2011-05-31 16:50:48 -07:00
|
|
|
public DeleteDropTarget(Context context, AttributeSet attrs) {
|
|
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onFinishInflate() {
|
|
|
|
|
super.onFinishInflate();
|
|
|
|
|
// Get the hover color
|
2015-04-10 13:45:42 -07:00
|
|
|
mHoverColor = getResources().getColor(R.color.delete_target_hover_tint);
|
2012-03-20 16:19:37 -07:00
|
|
|
|
2017-04-26 22:34:49 -07:00
|
|
|
setDrawable(R.drawable.ic_remove_shadow);
|
2013-06-14 17:42:35 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-01 13:04:22 -07:00
|
|
|
@Override
|
2016-08-16 15:36:48 -07:00
|
|
|
public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
|
|
|
|
|
super.onDragStart(dragObject, options);
|
2017-10-03 16:17:32 -07:00
|
|
|
setTextBasedOnDragSource(dragObject.dragInfo);
|
2018-05-01 19:26:05 -07:00
|
|
|
setControlTypeBasedOnDragSource(dragObject.dragInfo);
|
2015-10-01 13:04:22 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-24 14:54:30 -07:00
|
|
|
/**
|
|
|
|
|
* @return true for items that should have a "Remove" action in accessibility.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2018-03-05 19:39:21 +00:00
|
|
|
public boolean supportsAccessibilityDrop(ItemInfo info, View view) {
|
2019-03-27 16:03:06 -07:00
|
|
|
if (info instanceof WorkspaceItemInfo) {
|
2019-02-05 14:56:33 -08:00
|
|
|
// Support the action unless the item is in a context menu.
|
2020-04-01 11:40:40 -07:00
|
|
|
return canRemove(info);
|
2019-02-05 14:56:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (info instanceof LauncherAppWidgetInfo)
|
2015-04-22 11:29:51 -07:00
|
|
|
|| (info instanceof FolderInfo);
|
2011-05-31 16:50:48 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-24 14:54:30 -07:00
|
|
|
@Override
|
|
|
|
|
public int getAccessibilityAction() {
|
|
|
|
|
return LauncherAccessibilityDelegate.REMOVE;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-31 16:50:48 -07:00
|
|
|
@Override
|
2017-10-03 16:17:32 -07:00
|
|
|
protected boolean supportsDrop(ItemInfo info) {
|
2015-10-01 13:04:22 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-10-03 16:17:32 -07:00
|
|
|
* Set the drop target's text to either "Remove" or "Cancel" depending on the drag item.
|
2015-10-01 13:04:22 -07:00
|
|
|
*/
|
2017-10-03 16:17:32 -07:00
|
|
|
private void setTextBasedOnDragSource(ItemInfo item) {
|
2017-08-21 15:31:51 -07:00
|
|
|
if (!TextUtils.isEmpty(mText)) {
|
2018-08-21 11:40:23 -07:00
|
|
|
mText = getResources().getString(canRemove(item)
|
2017-08-21 15:31:51 -07:00
|
|
|
? R.string.remove_drop_target_label
|
2015-10-01 13:04:22 -07:00
|
|
|
: android.R.string.cancel);
|
2019-02-15 19:55:43 -08:00
|
|
|
setContentDescription(mText);
|
2017-08-21 15:31:51 -07:00
|
|
|
requestLayout();
|
2015-10-01 13:04:22 -07:00
|
|
|
}
|
2011-05-31 16:50:48 -07:00
|
|
|
}
|
|
|
|
|
|
2018-08-21 11:40:23 -07:00
|
|
|
private boolean canRemove(ItemInfo item) {
|
|
|
|
|
return item.id != ItemInfo.NO_ID;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-01 19:26:05 -07:00
|
|
|
/**
|
|
|
|
|
* Set mControlType depending on the drag item.
|
|
|
|
|
*/
|
|
|
|
|
private void setControlTypeBasedOnDragSource(ItemInfo item) {
|
|
|
|
|
mControlType = item.id != ItemInfo.NO_ID ? ControlType.REMOVE_TARGET
|
|
|
|
|
: ControlType.CANCEL_TARGET;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-21 11:40:23 -07:00
|
|
|
@Override
|
|
|
|
|
public void onDrop(DragObject d, DragOptions options) {
|
|
|
|
|
if (canRemove(d.dragInfo)) {
|
|
|
|
|
mLauncher.getModelWriter().prepareToUndoDelete();
|
2019-11-26 16:16:02 -08:00
|
|
|
d.dragInfo.container = NO_ID;
|
2018-08-21 11:40:23 -07:00
|
|
|
}
|
|
|
|
|
super.onDrop(d, options);
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-31 16:50:48 -07:00
|
|
|
@Override
|
2016-12-13 19:37:10 -08:00
|
|
|
public void completeDrop(DragObject d) {
|
2015-06-12 20:04:41 -07:00
|
|
|
ItemInfo item = d.dragInfo;
|
2018-08-21 11:40:23 -07:00
|
|
|
if (canRemove(item)) {
|
2018-12-21 11:58:04 -08:00
|
|
|
int itemPage = mLauncher.getWorkspace().getCurrentPage();
|
2017-10-24 14:54:30 -07:00
|
|
|
onAccessibilityDrop(null, item);
|
2018-08-21 11:40:23 -07:00
|
|
|
ModelWriter modelWriter = mLauncher.getModelWriter();
|
2019-04-25 14:22:54 -07:00
|
|
|
Runnable onUndoClicked = () -> {
|
2020-01-05 15:35:29 +05:30
|
|
|
mLauncher.setPageToBindSynchronously(itemPage);
|
|
|
|
|
modelWriter.abortDelete();
|
2019-04-25 14:22:54 -07:00
|
|
|
mLauncher.getUserEventDispatcher().logActionOnControl(TAP, UNDO);
|
|
|
|
|
};
|
2018-08-21 11:40:23 -07:00
|
|
|
Snackbar.show(mLauncher, R.string.item_removed, R.string.undo,
|
2019-04-25 14:22:54 -07:00
|
|
|
modelWriter::commitDelete, onUndoClicked);
|
2015-01-08 16:59:04 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes the item from the workspace. If the view is not null, it also removes the view.
|
|
|
|
|
*/
|
2017-10-24 14:54:30 -07:00
|
|
|
@Override
|
|
|
|
|
public void onAccessibilityDrop(View view, ItemInfo item) {
|
2015-09-14 12:01:13 -07:00
|
|
|
// Remove the item from launcher and the db, we can ignore the containerInfo in this call
|
|
|
|
|
// because we already remove the drag view from the folder (if the drag originated from
|
|
|
|
|
// a folder) in Folder.beginDrag()
|
2017-10-24 14:54:30 -07:00
|
|
|
mLauncher.removeItem(view, item, true /* deleteFromDb */);
|
|
|
|
|
mLauncher.getWorkspace().stripEmptyScreens();
|
|
|
|
|
mLauncher.getDragLayer()
|
|
|
|
|
.announceForAccessibility(getContext().getString(R.string.item_removed));
|
2011-05-31 16:50:48 -07:00
|
|
|
}
|
2018-03-05 19:39:21 +00:00
|
|
|
|
|
|
|
|
@Override
|
2018-05-01 19:26:05 -07:00
|
|
|
public Target getDropTargetForLogging() {
|
|
|
|
|
Target t = LoggerUtils.newTarget(Target.Type.CONTROL);
|
|
|
|
|
t.controlType = mControlType;
|
|
|
|
|
return t;
|
2018-03-05 19:39:21 +00:00
|
|
|
}
|
2011-05-31 16:50:48 -07:00
|
|
|
}
|