blob: 705f8410104f778739c5264e43b4f58aa7b936f2 [file] [log] [blame]
Winson Chung4c98d922011-05-31 16:50:48 -07001/*
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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung4c98d922011-05-31 16:50:48 -070018
Winson Chung043f2af2012-03-01 16:09:54 -080019import android.animation.TimeInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070020import android.content.Context;
Winson Chung043f2af2012-03-01 16:09:54 -080021import android.graphics.PointF;
Sunny Goyalf37a2142016-03-24 17:28:25 -070022import android.text.TextUtils;
Winson Chung4c98d922011-05-31 16:50:48 -070023import android.util.AttributeSet;
24import android.view.View;
Winson Chung043f2af2012-03-01 16:09:54 -080025import android.view.animation.AnimationUtils;
Winson Chung4c98d922011-05-31 16:50:48 -070026
Vadim Tryshevfedca432015-08-19 17:55:02 -070027import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070028import com.android.launcher3.dragndrop.DragOptions;
Sunny Goyal26119432016-02-18 22:09:23 +000029import com.android.launcher3.folder.Folder;
Sunny Goyalddec7342015-04-29 18:12:37 -070030import com.android.launcher3.util.FlingAnimation;
Adam Cohen091440a2015-03-18 14:16:05 -070031import com.android.launcher3.util.Thunk;
Kenny Guyed131872014-04-30 03:02:21 +010032
Winson Chung61fa4192011-06-12 15:15:29 -070033public class DeleteDropTarget extends ButtonDropTarget {
Sunny Goyalfa401a12015-04-10 13:45:42 -070034
Winson Chung4c98d922011-05-31 16:50:48 -070035 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();
Winson Chung4c98d922011-05-31 16:50:48 -070046 // Get the hover color
Sunny Goyalfa401a12015-04-10 13:45:42 -070047 mHoverColor = getResources().getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080048
Sunny Goyal40b626b2015-06-04 22:40:14 -070049 setDrawable(R.drawable.ic_remove_launcher);
Winson Chung4c98d922011-05-31 16:50:48 -070050 }
51
Tony Wickham9aae47f2015-10-01 13:04:22 -070052 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -070053 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
54 super.onDragStart(dragObject, options);
55 setTextBasedOnDragSource(dragObject.dragSource);
Tony Wickham9aae47f2015-10-01 13:04:22 -070056 }
57
58 /** @return true for items that should have a "Remove" action in accessibility. */
59 public static boolean supportsAccessibleDrop(ItemInfo info) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -070060 return (info instanceof ShortcutInfo)
61 || (info instanceof LauncherAppWidgetInfo)
62 || (info instanceof FolderInfo);
Winson Chunga48487a2012-03-20 16:19:37 -070063 }
64
Winson Chung4c98d922011-05-31 16:50:48 -070065 @Override
Sunny Goyalaa8ef112015-06-12 20:04:41 -070066 protected boolean supportsDrop(DragSource source, ItemInfo info) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070067 return true;
68 }
69
70 /**
71 * Set the drop target's text to either "Remove" or "Cancel" depending on the drag source.
72 */
73 public void setTextBasedOnDragSource(DragSource dragSource) {
Sunny Goyalf37a2142016-03-24 17:28:25 -070074 if (!TextUtils.isEmpty(getText())) {
Tony Wickham9aae47f2015-10-01 13:04:22 -070075 setText(dragSource.supportsDeleteDropTarget() ? R.string.remove_drop_target_label
76 : android.R.string.cancel);
77 }
Winson Chung4c98d922011-05-31 16:50:48 -070078 }
79
80 @Override
Adam Cohen091440a2015-03-18 14:16:05 -070081 @Thunk void completeDrop(DragObject d) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -070082 ItemInfo item = d.dragInfo;
Sunny Goyalfa401a12015-04-10 13:45:42 -070083 if ((d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080084 removeWorkspaceOrFolderItem(mLauncher, item, null);
Winson Chung4c98d922011-05-31 16:50:48 -070085 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080086 }
87
88 /**
89 * Removes the item from the workspace. If the view is not null, it also removes the view.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080090 */
Sunny Goyale78e3d72015-09-24 11:23:31 -070091 public static void removeWorkspaceOrFolderItem(Launcher launcher, ItemInfo item, View view) {
Winsonfa56b3f2015-09-14 12:01:13 -070092 // Remove the item from launcher and the db, we can ignore the containerInfo in this call
93 // because we already remove the drag view from the folder (if the drag originated from
94 // a folder) in Folder.beginDrag()
Winson2949fb52015-09-24 09:56:11 -070095 launcher.removeItem(view, item, true /* deleteFromDb */);
Winsonc0b52fe2015-09-09 16:38:15 -070096 launcher.getWorkspace().stripEmptyScreens();
Sunny Goyale78e3d72015-09-24 11:23:31 -070097 launcher.getDragLayer().announceForAccessibility(launcher.getString(R.string.item_removed));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080098 }
99
Sunny Goyalddec7342015-04-29 18:12:37 -0700100 @Override
101 public void onFlingToDelete(final DragObject d, PointF vel) {
Winson Chung043f2af2012-03-01 16:09:54 -0800102 // Don't highlight the icon as it's animating
103 d.dragView.setColor(0);
Winson Chung043f2af2012-03-01 16:09:54 -0800104
Winson Chung043f2af2012-03-01 16:09:54 -0800105 final DragLayer dragLayer = mLauncher.getDragLayer();
Sunny Goyalddec7342015-04-29 18:12:37 -0700106 FlingAnimation fling = new FlingAnimation(d, vel,
107 getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
108 mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight()),
109 dragLayer);
110
111 final int duration = fling.getDuration();
Winson Chung043f2af2012-03-01 16:09:54 -0800112 final long startTime = AnimationUtils.currentAnimationTimeMillis();
113
114 // NOTE: Because it takes time for the first frame of animation to actually be
115 // called and we expect the animation to be a continuation of the fling, we have
116 // to account for the time that has elapsed since the fling finished. And since
117 // we don't have a startDelay, we will always get call to update when we call
118 // start() (which we want to ignore).
119 final TimeInterpolator tInterpolator = new TimeInterpolator() {
120 private int mCount = -1;
121 private float mOffset = 0f;
122
123 @Override
124 public float getInterpolation(float t) {
125 if (mCount < 0) {
126 mCount++;
127 } else if (mCount == 0) {
128 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
129 startTime) / duration);
130 mCount++;
131 }
132 return Math.min(1f, mOffset + t);
133 }
134 };
Michael Jurka1e2f4652013-07-08 18:03:46 -0700135
Winson Chung043f2af2012-03-01 16:09:54 -0800136 Runnable onAnimationEndRunnable = new Runnable() {
137 @Override
138 public void run() {
Sunny Goyalddec7342015-04-29 18:12:37 -0700139 mLauncher.exitSpringLoadedDragMode();
140 completeDrop(d);
Winson Chunga48487a2012-03-20 16:19:37 -0700141 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800142 }
143 };
Sunny Goyalddec7342015-04-29 18:12:37 -0700144
145 dragLayer.animateView(d.dragView, fling, duration, tInterpolator, onAnimationEndRunnable,
Winson Chung043f2af2012-03-01 16:09:54 -0800146 DragLayer.ANIMATION_END_DISAPPEAR, null);
147 }
Winson Chung4c98d922011-05-31 16:50:48 -0700148}