blob: bf5bc178e74998f551039d451367c9bbab7e73c6 [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;
Michael Jurka43467462013-11-14 12:03:58 +010022import android.os.AsyncTask;
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
Sunny Goyalddec7342015-04-29 18:12:37 -070027import com.android.launcher3.util.FlingAnimation;
Adam Cohen091440a2015-03-18 14:16:05 -070028import com.android.launcher3.util.Thunk;
Kenny Guyed131872014-04-30 03:02:21 +010029
Winson Chung61fa4192011-06-12 15:15:29 -070030public class DeleteDropTarget extends ButtonDropTarget {
Sunny Goyalfa401a12015-04-10 13:45:42 -070031
Winson Chung4c98d922011-05-31 16:50:48 -070032 public DeleteDropTarget(Context context, AttributeSet attrs) {
33 this(context, attrs, 0);
34 }
35
36 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
37 super(context, attrs, defStyle);
38 }
39
40 @Override
41 protected void onFinishInflate() {
42 super.onFinishInflate();
Winson Chung4c98d922011-05-31 16:50:48 -070043 // Get the hover color
Sunny Goyalfa401a12015-04-10 13:45:42 -070044 mHoverColor = getResources().getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080045
Sunny Goyal40b626b2015-06-04 22:40:14 -070046 setDrawable(R.drawable.ic_remove_launcher);
Winson Chung4c98d922011-05-31 16:50:48 -070047 }
48
Sunny Goyalaa8ef112015-06-12 20:04:41 -070049 public static boolean supportsDrop(ItemInfo info) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -070050 return (info instanceof ShortcutInfo)
51 || (info instanceof LauncherAppWidgetInfo)
52 || (info instanceof FolderInfo);
Winson Chunga48487a2012-03-20 16:19:37 -070053 }
54
Winson Chung4c98d922011-05-31 16:50:48 -070055 @Override
Sunny Goyalaa8ef112015-06-12 20:04:41 -070056 protected boolean supportsDrop(DragSource source, ItemInfo info) {
Sunny Goyal1a70cef2015-04-22 11:29:51 -070057 return source.supportsDeleteDropTarget() && supportsDrop(info);
Winson Chung4c98d922011-05-31 16:50:48 -070058 }
59
60 @Override
Adam Cohen091440a2015-03-18 14:16:05 -070061 @Thunk void completeDrop(DragObject d) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -070062 ItemInfo item = d.dragInfo;
Sunny Goyalfa401a12015-04-10 13:45:42 -070063 if ((d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080064 removeWorkspaceOrFolderItem(mLauncher, item, null);
Winson Chung4c98d922011-05-31 16:50:48 -070065 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080066 }
67
68 /**
69 * Removes the item from the workspace. If the view is not null, it also removes the view.
70 * @return true if the item was removed.
71 */
72 public static boolean removeWorkspaceOrFolderItem(Launcher launcher, ItemInfo item, View view) {
73 if (item instanceof ShortcutInfo) {
74 LauncherModel.deleteItemFromDatabase(launcher, item);
75 } else if (item instanceof FolderInfo) {
76 FolderInfo folder = (FolderInfo) item;
77 launcher.removeFolder(folder);
78 LauncherModel.deleteFolderContentsFromDatabase(launcher, folder);
79 } else if (item instanceof LauncherAppWidgetInfo) {
80 final LauncherAppWidgetInfo widget = (LauncherAppWidgetInfo) item;
81
82 // Remove the widget from the workspace
83 launcher.removeAppWidget(widget);
84 LauncherModel.deleteItemFromDatabase(launcher, widget);
85
86 final LauncherAppWidgetHost appWidgetHost = launcher.getAppWidgetHost();
87
88 if (appWidgetHost != null && !widget.isCustomWidget()
89 && widget.isWidgetIdValid()) {
90 // Deleting an app widget ID is a void call but writes to disk before returning
91 // to the caller...
92 new AsyncTask<Void, Void, Void>() {
93 public Void doInBackground(Void ... args) {
94 appWidgetHost.deleteAppWidgetId(widget.appWidgetId);
95 return null;
96 }
Sunny Goyalfa401a12015-04-10 13:45:42 -070097 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080098 }
99 } else {
100 return false;
101 }
102
103 if (view != null) {
104 launcher.getWorkspace().removeWorkspaceItem(view);
105 launcher.getWorkspace().stripEmptyScreens();
106 }
107 return true;
108 }
109
Sunny Goyalddec7342015-04-29 18:12:37 -0700110 @Override
111 public void onFlingToDelete(final DragObject d, PointF vel) {
Winson Chung043f2af2012-03-01 16:09:54 -0800112 // Don't highlight the icon as it's animating
113 d.dragView.setColor(0);
Winson Chung043f2af2012-03-01 16:09:54 -0800114
Winson Chung043f2af2012-03-01 16:09:54 -0800115 final DragLayer dragLayer = mLauncher.getDragLayer();
Sunny Goyalddec7342015-04-29 18:12:37 -0700116 FlingAnimation fling = new FlingAnimation(d, vel,
117 getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
118 mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight()),
119 dragLayer);
120
121 final int duration = fling.getDuration();
Winson Chung043f2af2012-03-01 16:09:54 -0800122 final long startTime = AnimationUtils.currentAnimationTimeMillis();
123
124 // NOTE: Because it takes time for the first frame of animation to actually be
125 // called and we expect the animation to be a continuation of the fling, we have
126 // to account for the time that has elapsed since the fling finished. And since
127 // we don't have a startDelay, we will always get call to update when we call
128 // start() (which we want to ignore).
129 final TimeInterpolator tInterpolator = new TimeInterpolator() {
130 private int mCount = -1;
131 private float mOffset = 0f;
132
133 @Override
134 public float getInterpolation(float t) {
135 if (mCount < 0) {
136 mCount++;
137 } else if (mCount == 0) {
138 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
139 startTime) / duration);
140 mCount++;
141 }
142 return Math.min(1f, mOffset + t);
143 }
144 };
Michael Jurka1e2f4652013-07-08 18:03:46 -0700145
Winson Chung043f2af2012-03-01 16:09:54 -0800146 Runnable onAnimationEndRunnable = new Runnable() {
147 @Override
148 public void run() {
Sunny Goyalddec7342015-04-29 18:12:37 -0700149 mLauncher.exitSpringLoadedDragMode();
150 completeDrop(d);
Winson Chunga48487a2012-03-20 16:19:37 -0700151 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800152 }
153 };
Sunny Goyalddec7342015-04-29 18:12:37 -0700154
155 dragLayer.animateView(d.dragView, fling, duration, tInterpolator, onAnimationEndRunnable,
Winson Chung043f2af2012-03-01 16:09:54 -0800156 DragLayer.ANIMATION_END_DISAPPEAR, null);
157 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700158
159 @Override
160 protected String getAccessibilityDropConfirmation() {
161 return getResources().getString(R.string.item_removed);
162 }
Winson Chung4c98d922011-05-31 16:50:48 -0700163}