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
|
|
|
|
2012-03-01 16:09:54 -08:00
|
|
|
import android.animation.TimeInterpolator;
|
2011-05-31 16:50:48 -07:00
|
|
|
import android.content.Context;
|
2012-03-01 16:09:54 -08:00
|
|
|
import android.graphics.PointF;
|
2011-05-31 16:50:48 -07:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
|
import android.view.View;
|
2012-03-01 16:09:54 -08:00
|
|
|
import android.view.animation.AnimationUtils;
|
2011-05-31 16:50:48 -07:00
|
|
|
|
2015-08-19 17:55:02 -07:00
|
|
|
import com.android.launcher3.dragndrop.DragLayer;
|
2015-04-29 18:12:37 -07:00
|
|
|
import com.android.launcher3.util.FlingAnimation;
|
2015-03-18 14:16:05 -07:00
|
|
|
import com.android.launcher3.util.Thunk;
|
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
|
|
|
|
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
|
|
|
|
2015-06-04 22:40:14 -07:00
|
|
|
setDrawable(R.drawable.ic_remove_launcher);
|
2013-06-14 17:42:35 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-12 20:04:41 -07:00
|
|
|
public static boolean supportsDrop(ItemInfo info) {
|
2015-04-22 11:29:51 -07:00
|
|
|
return (info instanceof ShortcutInfo)
|
|
|
|
|
|| (info instanceof LauncherAppWidgetInfo)
|
|
|
|
|
|| (info instanceof FolderInfo);
|
2011-05-31 16:50:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2015-06-12 20:04:41 -07:00
|
|
|
protected boolean supportsDrop(DragSource source, ItemInfo info) {
|
2015-04-22 11:29:51 -07:00
|
|
|
return source.supportsDeleteDropTarget() && supportsDrop(info);
|
2011-05-31 16:50:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2015-03-18 14:16:05 -07:00
|
|
|
@Thunk void completeDrop(DragObject d) {
|
2015-06-12 20:04:41 -07:00
|
|
|
ItemInfo item = d.dragInfo;
|
2015-04-10 13:45:42 -07:00
|
|
|
if ((d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder)) {
|
2015-01-08 16:59:04 -08:00
|
|
|
removeWorkspaceOrFolderItem(mLauncher, item, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes the item from the workspace. If the view is not null, it also removes the view.
|
|
|
|
|
*/
|
2015-09-24 11:23:31 -07:00
|
|
|
public static void removeWorkspaceOrFolderItem(Launcher launcher, ItemInfo item, View view) {
|
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()
|
2015-09-24 09:56:11 -07:00
|
|
|
launcher.removeItem(view, item, true /* deleteFromDb */);
|
2015-09-09 16:38:15 -07:00
|
|
|
launcher.getWorkspace().stripEmptyScreens();
|
2015-09-24 11:23:31 -07:00
|
|
|
launcher.getDragLayer().announceForAccessibility(launcher.getString(R.string.item_removed));
|
2011-05-31 16:50:48 -07:00
|
|
|
}
|
2011-07-19 21:47:37 -07:00
|
|
|
|
2015-04-29 18:12:37 -07:00
|
|
|
@Override
|
|
|
|
|
public void onFlingToDelete(final DragObject d, PointF vel) {
|
2012-03-01 16:09:54 -08:00
|
|
|
// Don't highlight the icon as it's animating
|
|
|
|
|
d.dragView.setColor(0);
|
|
|
|
|
|
|
|
|
|
final DragLayer dragLayer = mLauncher.getDragLayer();
|
2015-04-29 18:12:37 -07:00
|
|
|
FlingAnimation fling = new FlingAnimation(d, vel,
|
|
|
|
|
getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
|
|
|
|
|
mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight()),
|
|
|
|
|
dragLayer);
|
|
|
|
|
|
|
|
|
|
final int duration = fling.getDuration();
|
2012-03-01 16:09:54 -08:00
|
|
|
final long startTime = AnimationUtils.currentAnimationTimeMillis();
|
|
|
|
|
|
|
|
|
|
// NOTE: Because it takes time for the first frame of animation to actually be
|
|
|
|
|
// called and we expect the animation to be a continuation of the fling, we have
|
|
|
|
|
// to account for the time that has elapsed since the fling finished. And since
|
|
|
|
|
// we don't have a startDelay, we will always get call to update when we call
|
|
|
|
|
// start() (which we want to ignore).
|
|
|
|
|
final TimeInterpolator tInterpolator = new TimeInterpolator() {
|
|
|
|
|
private int mCount = -1;
|
|
|
|
|
private float mOffset = 0f;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public float getInterpolation(float t) {
|
|
|
|
|
if (mCount < 0) {
|
|
|
|
|
mCount++;
|
|
|
|
|
} else if (mCount == 0) {
|
|
|
|
|
mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
|
|
|
|
|
startTime) / duration);
|
|
|
|
|
mCount++;
|
|
|
|
|
}
|
|
|
|
|
return Math.min(1f, mOffset + t);
|
|
|
|
|
}
|
|
|
|
|
};
|
2013-07-08 18:03:46 -07:00
|
|
|
|
2012-03-01 16:09:54 -08:00
|
|
|
Runnable onAnimationEndRunnable = new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2015-04-29 18:12:37 -07:00
|
|
|
mLauncher.exitSpringLoadedDragMode();
|
|
|
|
|
completeDrop(d);
|
2012-03-20 16:19:37 -07:00
|
|
|
mLauncher.getDragController().onDeferredEndFling(d);
|
2012-03-01 16:09:54 -08:00
|
|
|
}
|
|
|
|
|
};
|
2015-04-29 18:12:37 -07:00
|
|
|
|
|
|
|
|
dragLayer.animateView(d.dragView, fling, duration, tInterpolator, onAnimationEndRunnable,
|
2012-03-01 16:09:54 -08:00
|
|
|
DragLayer.ANIMATION_END_DISAPPEAR, null);
|
|
|
|
|
}
|
2011-05-31 16:50:48 -07:00
|
|
|
}
|