blob: eb831f6e91a2ac046e77382ed5e59de8893b2d90 [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
17package com.android.launcher2;
18
Winson Chung043f2af2012-03-01 16:09:54 -080019import android.animation.TimeInterpolator;
20import android.animation.ValueAnimator;
21import android.animation.ValueAnimator.AnimatorUpdateListener;
Winson Chung4c98d922011-05-31 16:50:48 -070022import android.content.Context;
Winson Chunga62e9fd2011-07-11 15:20:48 -070023import android.content.res.ColorStateList;
Winson Chung201bc822011-06-20 15:41:53 -070024import android.content.res.Configuration;
Winson Chung4c98d922011-05-31 16:50:48 -070025import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080026import android.graphics.PointF;
Adam Cohend4d7aa52011-07-19 21:47:37 -070027import android.graphics.Rect;
Winson Chung967289b2011-06-30 18:09:30 -070028import android.graphics.drawable.TransitionDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070029import android.util.AttributeSet;
30import android.view.View;
Winson Chung043f2af2012-03-01 16:09:54 -080031import android.view.ViewConfiguration;
Winson Chunga6427b12011-07-27 10:53:39 -070032import android.view.ViewGroup;
Winson Chung043f2af2012-03-01 16:09:54 -080033import android.view.animation.AnimationUtils;
Adam Cohend4d7aa52011-07-19 21:47:37 -070034import android.view.animation.DecelerateInterpolator;
Winson Chung61967cb2012-02-28 18:11:33 -080035import android.view.animation.LinearInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070036
37import com.android.launcher.R;
38
Winson Chung61fa4192011-06-12 15:15:29 -070039public class DeleteDropTarget extends ButtonDropTarget {
Winson Chung043f2af2012-03-01 16:09:54 -080040 private static int DELETE_ANIMATION_DURATION = 285;
Winson Chung9658b1e2012-04-09 18:30:07 -070041 private static int FLIND_DELETE_ANIMATION_DURATION = 350;
Winson Chung043f2af2012-03-01 16:09:54 -080042 private static int MODE_FLING_DELETE_TO_TRASH = 0;
43 private static int MODE_FLING_DELETE_ALONG_VECTOR = 1;
Winson Chung4c98d922011-05-31 16:50:48 -070044
Winson Chung043f2af2012-03-01 16:09:54 -080045 private final int mFlingDeleteMode = MODE_FLING_DELETE_ALONG_VECTOR;
46
Winson Chunga62e9fd2011-07-11 15:20:48 -070047 private ColorStateList mOriginalTextColor;
Adam Cohenebea84d2011-11-09 17:20:41 -080048 private TransitionDrawable mUninstallDrawable;
49 private TransitionDrawable mRemoveDrawable;
50 private TransitionDrawable mCurrentDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070051
52 public DeleteDropTarget(Context context, AttributeSet attrs) {
53 this(context, attrs, 0);
54 }
55
56 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
57 super(context, attrs, defStyle);
58 }
59
60 @Override
61 protected void onFinishInflate() {
62 super.onFinishInflate();
63
64 // Get the drawable
Winson Chunga6427b12011-07-27 10:53:39 -070065 mOriginalTextColor = getTextColors();
Winson Chung4c98d922011-05-31 16:50:48 -070066
67 // Get the hover color
68 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070069 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080070 mUninstallDrawable = (TransitionDrawable)
71 r.getDrawable(R.drawable.uninstall_target_selector);
72 mRemoveDrawable = (TransitionDrawable) r.getDrawable(R.drawable.remove_target_selector);
73
74 mRemoveDrawable.setCrossFadeEnabled(true);
75 mUninstallDrawable.setCrossFadeEnabled(true);
76
77 // The current drawable is set to either the remove drawable or the uninstall drawable
78 // and is initially set to the remove drawable, as set in the layout xml.
79 mCurrentDrawable = (TransitionDrawable) getCompoundDrawables()[0];
Winson Chung201bc822011-06-20 15:41:53 -070080
81 // Remove the text in the Phone UI in landscape
82 int orientation = getResources().getConfiguration().orientation;
83 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
84 if (!LauncherApplication.isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070085 setText("");
Winson Chung201bc822011-06-20 15:41:53 -070086 }
87 }
Winson Chung4c98d922011-05-31 16:50:48 -070088 }
89
90 private boolean isAllAppsApplication(DragSource source, Object info) {
91 return (source instanceof AppsCustomizePagedView) && (info instanceof ApplicationInfo);
92 }
93 private boolean isAllAppsWidget(DragSource source, Object info) {
Winson Chung11a49372012-04-27 15:12:38 -070094 if (source instanceof AppsCustomizePagedView) {
95 if (info instanceof PendingAddItemInfo) {
96 PendingAddItemInfo addInfo = (PendingAddItemInfo) info;
97 switch (addInfo.itemType) {
98 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
99 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
100 return true;
101 }
102 }
103 }
104 return false;
Winson Chung4c98d922011-05-31 16:50:48 -0700105 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700106 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
107 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -0700108 }
Michael Jurka0b4870d2011-07-10 13:39:08 -0700109 private boolean isWorkspaceOrFolderApplication(DragObject d) {
110 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
111 }
112 private boolean isWorkspaceOrFolderWidget(DragObject d) {
113 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700114 }
115 private boolean isWorkspaceFolder(DragObject d) {
116 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
117 }
118
Winson Chunga48487a2012-03-20 16:19:37 -0700119 private void setHoverColor() {
120 mCurrentDrawable.startTransition(mTransitionDuration);
121 setTextColor(mHoverColor);
122 }
123 private void resetHoverColor() {
124 mCurrentDrawable.resetTransition();
125 setTextColor(mOriginalTextColor);
126 }
127
Winson Chung4c98d922011-05-31 16:50:48 -0700128 @Override
129 public boolean acceptDrop(DragObject d) {
130 // We can remove everything including App shortcuts, folders, widgets, etc.
131 return true;
132 }
133
134 @Override
135 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700136 boolean isVisible = true;
137 boolean isUninstall = false;
138
Winson Chungf0ea4d32011-06-06 14:27:16 -0700139 // If we are dragging a widget from AppsCustomize, hide the delete target
Winson Chung4c98d922011-05-31 16:50:48 -0700140 if (isAllAppsWidget(source, info)) {
141 isVisible = false;
142 }
143
144 // If we are dragging an application from AppsCustomize, only show the control if we can
145 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
146 if (isAllAppsApplication(source, info)) {
147 ApplicationInfo appInfo = (ApplicationInfo) info;
148 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
149 isUninstall = true;
150 } else {
151 isVisible = false;
152 }
153 }
154
Adam Cohenebea84d2011-11-09 17:20:41 -0800155 if (isUninstall) {
156 setCompoundDrawablesWithIntrinsicBounds(mUninstallDrawable, null, null, null);
157 } else {
158 setCompoundDrawablesWithIntrinsicBounds(mRemoveDrawable, null, null, null);
159 }
160 mCurrentDrawable = (TransitionDrawable) getCompoundDrawables()[0];
161
Winson Chung4c98d922011-05-31 16:50:48 -0700162 mActive = isVisible;
Winson Chunga48487a2012-03-20 16:19:37 -0700163 resetHoverColor();
Winson Chunga6427b12011-07-27 10:53:39 -0700164 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
165 if (getText().length() > 0) {
166 setText(isUninstall ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700167 : R.string.delete_target_label);
168 }
169 }
170
171 @Override
172 public void onDragEnd() {
173 super.onDragEnd();
174 mActive = false;
175 }
176
177 public void onDragEnter(DragObject d) {
178 super.onDragEnter(d);
179
Winson Chunga48487a2012-03-20 16:19:37 -0700180 setHoverColor();
Winson Chung4c98d922011-05-31 16:50:48 -0700181 }
182
183 public void onDragExit(DragObject d) {
184 super.onDragExit(d);
185
Winson Chungaaa530a2011-07-11 21:06:30 -0700186 if (!d.dragComplete) {
Winson Chunga48487a2012-03-20 16:19:37 -0700187 resetHoverColor();
Winson Chung61967cb2012-02-28 18:11:33 -0800188 } else {
189 // Restore the hover color if we are deleting
190 d.dragView.setColor(mHoverColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700191 }
Winson Chung4c98d922011-05-31 16:50:48 -0700192 }
193
Adam Cohened66b2b2012-01-23 17:28:51 -0800194 private void animateToTrashAndCompleteDrop(final DragObject d) {
195 DragLayer dragLayer = mLauncher.getDragLayer();
196 Rect from = new Rect();
197 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Winson Chung61967cb2012-02-28 18:11:33 -0800198 Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
199 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
200 float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800201
Adam Cohend4d7aa52011-07-19 21:47:37 -0700202 mSearchDropTargetBar.deferOnDragEnd();
203 Runnable onAnimationEndRunnable = new Runnable() {
204 @Override
205 public void run() {
206 mSearchDropTargetBar.onDragEnd();
207 mLauncher.exitSpringLoadedDragMode();
208 completeDrop(d);
209 }
210 };
Winson Chung61967cb2012-02-28 18:11:33 -0800211 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700212 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800213 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800214 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700215 }
216
217 private void completeDrop(DragObject d) {
Winson Chung4c98d922011-05-31 16:50:48 -0700218 ItemInfo item = (ItemInfo) d.dragInfo;
219
220 if (isAllAppsApplication(d.dragSource, item)) {
221 // Uninstall the application if it is being dragged from AppsCustomize
222 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700223 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700224 LauncherModel.deleteItemFromDatabase(mLauncher, item);
225 } else if (isWorkspaceFolder(d)) {
226 // Remove the folder from the workspace and delete the contents from launcher model
227 FolderInfo folderInfo = (FolderInfo) item;
228 mLauncher.removeFolder(folderInfo);
229 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700230 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700231 // Remove the widget from the workspace
232 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
233 LauncherModel.deleteItemFromDatabase(mLauncher, item);
234
235 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
236 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
237 if (appWidgetHost != null) {
238 // Deleting an app widget ID is a void call but writes to disk before returning
239 // to the caller...
240 new Thread("deleteAppWidgetId") {
241 public void run() {
242 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
243 }
244 }.start();
245 }
246 }
247 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700248
249 public void onDrop(DragObject d) {
250 animateToTrashAndCompleteDrop(d);
251 }
Winson Chung043f2af2012-03-01 16:09:54 -0800252
253 /**
254 * Creates an animation from the current drag view to the delete trash icon.
255 */
256 private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer,
257 DragObject d, PointF vel, ViewConfiguration config) {
258 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
259 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
260 final Rect from = new Rect();
261 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
262
263 // Calculate how far along the velocity vector we should put the intermediate point on
264 // the bezier curve
265 float velocity = Math.abs(vel.length());
266 float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
267 int offsetY = (int) (-from.top * vp);
268 int offsetX = (int) (offsetY / (vel.y / vel.x));
269 final float y2 = from.top + offsetY; // intermediate t/l
270 final float x2 = from.left + offsetX;
271 final float x1 = from.left; // drag view t/l
272 final float y1 = from.top;
273 final float x3 = to.left; // delete target t/l
274 final float y3 = to.top;
275
276 final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {
277 @Override
278 public float getInterpolation(float t) {
279 return t * t * t * t * t * t * t * t;
280 }
281 };
282 return new AnimatorUpdateListener() {
283 @Override
284 public void onAnimationUpdate(ValueAnimator animation) {
285 final DragView dragView = (DragView) dragLayer.getAnimatedView();
286 float t = ((Float) animation.getAnimatedValue()).floatValue();
287 float tp = scaleAlphaInterpolator.getInterpolation(t);
288 float initialScale = dragView.getInitialScale();
289 float finalAlpha = 0.5f;
290 float scale = dragView.getScaleX();
291 float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
292 float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
293 float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) +
294 (t * t) * x3;
295 float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) +
296 (t * t) * y3;
297
298 dragView.setTranslationX(x);
299 dragView.setTranslationY(y);
300 dragView.setScaleX(initialScale * (1f - tp));
301 dragView.setScaleY(initialScale * (1f - tp));
302 dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
303 }
304 };
305 }
306
307 /**
308 * Creates an animation from the current drag view along its current velocity vector.
309 * For this animation, the alpha runs for a fixed duration and we update the position
310 * progressively.
311 */
312 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
313 private static float FRICTION = 0.93f;
314
315 private DragLayer mDragLayer;
316 private PointF mVelocity;
317 private Rect mFrom;
318 private long mPrevTime;
319 private boolean mHasOffsetForScale;
320
Winson Chung9658b1e2012-04-09 18:30:07 -0700321 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
Winson Chung043f2af2012-03-01 16:09:54 -0800322
323 public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from,
324 long startTime) {
325 mDragLayer = dragLayer;
326 mVelocity = vel;
327 mFrom = from;
328 mPrevTime = startTime;
329 }
330
331 @Override
332 public void onAnimationUpdate(ValueAnimator animation) {
333 final DragView dragView = (DragView) mDragLayer.getAnimatedView();
334 float t = ((Float) animation.getAnimatedValue()).floatValue();
335 long curTime = AnimationUtils.currentAnimationTimeMillis();
336
337 if (!mHasOffsetForScale) {
338 mHasOffsetForScale = true;
339 float scale = dragView.getScaleX();
340 float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f;
341 float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f;
342
343 mFrom.left += xOffset;
344 mFrom.top += yOffset;
345 }
346
347 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
348 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
349
350 dragView.setTranslationX(mFrom.left);
351 dragView.setTranslationY(mFrom.top);
352 dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
353
354 mVelocity.x *= FRICTION;
355 mVelocity.y *= FRICTION;
356 mPrevTime = curTime;
357 }
358 };
359 private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer,
360 DragObject d, PointF vel, final long startTime, final int duration,
361 ViewConfiguration config) {
362 final Rect from = new Rect();
363 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
364
365 return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime);
366 }
367
368 public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
Winson Chunga48487a2012-03-20 16:19:37 -0700369 final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView;
370
Winson Chung043f2af2012-03-01 16:09:54 -0800371 // Don't highlight the icon as it's animating
372 d.dragView.setColor(0);
373 d.dragView.updateInitialScaleToCurrentScale();
Winson Chunga48487a2012-03-20 16:19:37 -0700374 // Don't highlight the target if we are flinging from AllApps
375 if (isAllApps) {
376 resetHoverColor();
377 }
Winson Chung043f2af2012-03-01 16:09:54 -0800378
379 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
380 // Defer animating out the drop target if we are animating to it
381 mSearchDropTargetBar.deferOnDragEnd();
382 mSearchDropTargetBar.finishAnimations();
383 }
384
385 final ViewConfiguration config = ViewConfiguration.get(mLauncher);
386 final DragLayer dragLayer = mLauncher.getDragLayer();
Winson Chung9658b1e2012-04-09 18:30:07 -0700387 final int duration = FLIND_DELETE_ANIMATION_DURATION;
Winson Chung043f2af2012-03-01 16:09:54 -0800388 final long startTime = AnimationUtils.currentAnimationTimeMillis();
389
390 // NOTE: Because it takes time for the first frame of animation to actually be
391 // called and we expect the animation to be a continuation of the fling, we have
392 // to account for the time that has elapsed since the fling finished. And since
393 // we don't have a startDelay, we will always get call to update when we call
394 // start() (which we want to ignore).
395 final TimeInterpolator tInterpolator = new TimeInterpolator() {
396 private int mCount = -1;
397 private float mOffset = 0f;
398
399 @Override
400 public float getInterpolation(float t) {
401 if (mCount < 0) {
402 mCount++;
403 } else if (mCount == 0) {
404 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
405 startTime) / duration);
406 mCount++;
407 }
408 return Math.min(1f, mOffset + t);
409 }
410 };
411 AnimatorUpdateListener updateCb = null;
412 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
413 updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
414 } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
415 updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime,
416 duration, config);
417 }
418 Runnable onAnimationEndRunnable = new Runnable() {
419 @Override
420 public void run() {
421 mSearchDropTargetBar.onDragEnd();
Winson Chunga48487a2012-03-20 16:19:37 -0700422
423 // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up
424 // itself, otherwise, complete the drop to initiate the deletion process
425 if (!isAllApps) {
426 mLauncher.exitSpringLoadedDragMode();
427 completeDrop(d);
428 }
429 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800430 }
431 };
432 dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable,
433 DragLayer.ANIMATION_END_DISAPPEAR, null);
434 }
Winson Chung4c98d922011-05-31 16:50:48 -0700435}