blob: d8ea6ef5cec429b268267fff906ee366d2b5cafe [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) {
94 return (source instanceof AppsCustomizePagedView) && (info instanceof PendingAddWidgetInfo);
95 }
Michael Jurka0b4870d2011-07-10 13:39:08 -070096 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
97 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -070098 }
Michael Jurka0b4870d2011-07-10 13:39:08 -070099 private boolean isWorkspaceOrFolderApplication(DragObject d) {
100 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
101 }
102 private boolean isWorkspaceOrFolderWidget(DragObject d) {
103 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700104 }
105 private boolean isWorkspaceFolder(DragObject d) {
106 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
107 }
108
Winson Chunga48487a2012-03-20 16:19:37 -0700109 private void setHoverColor() {
110 mCurrentDrawable.startTransition(mTransitionDuration);
111 setTextColor(mHoverColor);
112 }
113 private void resetHoverColor() {
114 mCurrentDrawable.resetTransition();
115 setTextColor(mOriginalTextColor);
116 }
117
Winson Chung4c98d922011-05-31 16:50:48 -0700118 @Override
119 public boolean acceptDrop(DragObject d) {
120 // We can remove everything including App shortcuts, folders, widgets, etc.
121 return true;
122 }
123
124 @Override
125 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700126 boolean isVisible = true;
127 boolean isUninstall = false;
128
Winson Chungf0ea4d32011-06-06 14:27:16 -0700129 // If we are dragging a widget from AppsCustomize, hide the delete target
Winson Chung4c98d922011-05-31 16:50:48 -0700130 if (isAllAppsWidget(source, info)) {
131 isVisible = false;
132 }
133
134 // If we are dragging an application from AppsCustomize, only show the control if we can
135 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
136 if (isAllAppsApplication(source, info)) {
137 ApplicationInfo appInfo = (ApplicationInfo) info;
138 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
139 isUninstall = true;
140 } else {
141 isVisible = false;
142 }
143 }
144
Adam Cohenebea84d2011-11-09 17:20:41 -0800145 if (isUninstall) {
146 setCompoundDrawablesWithIntrinsicBounds(mUninstallDrawable, null, null, null);
147 } else {
148 setCompoundDrawablesWithIntrinsicBounds(mRemoveDrawable, null, null, null);
149 }
150 mCurrentDrawable = (TransitionDrawable) getCompoundDrawables()[0];
151
Winson Chung4c98d922011-05-31 16:50:48 -0700152 mActive = isVisible;
Winson Chunga48487a2012-03-20 16:19:37 -0700153 resetHoverColor();
Winson Chunga6427b12011-07-27 10:53:39 -0700154 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
155 if (getText().length() > 0) {
156 setText(isUninstall ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700157 : R.string.delete_target_label);
158 }
159 }
160
161 @Override
162 public void onDragEnd() {
163 super.onDragEnd();
164 mActive = false;
165 }
166
167 public void onDragEnter(DragObject d) {
168 super.onDragEnter(d);
169
Winson Chunga48487a2012-03-20 16:19:37 -0700170 setHoverColor();
Winson Chung4c98d922011-05-31 16:50:48 -0700171 }
172
173 public void onDragExit(DragObject d) {
174 super.onDragExit(d);
175
Winson Chungaaa530a2011-07-11 21:06:30 -0700176 if (!d.dragComplete) {
Winson Chunga48487a2012-03-20 16:19:37 -0700177 resetHoverColor();
Winson Chung61967cb2012-02-28 18:11:33 -0800178 } else {
179 // Restore the hover color if we are deleting
180 d.dragView.setColor(mHoverColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700181 }
Winson Chung4c98d922011-05-31 16:50:48 -0700182 }
183
Adam Cohened66b2b2012-01-23 17:28:51 -0800184 private void animateToTrashAndCompleteDrop(final DragObject d) {
185 DragLayer dragLayer = mLauncher.getDragLayer();
186 Rect from = new Rect();
187 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Winson Chung61967cb2012-02-28 18:11:33 -0800188 Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
189 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
190 float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800191
Adam Cohend4d7aa52011-07-19 21:47:37 -0700192 mSearchDropTargetBar.deferOnDragEnd();
193 Runnable onAnimationEndRunnable = new Runnable() {
194 @Override
195 public void run() {
196 mSearchDropTargetBar.onDragEnd();
197 mLauncher.exitSpringLoadedDragMode();
198 completeDrop(d);
199 }
200 };
Winson Chung61967cb2012-02-28 18:11:33 -0800201 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700202 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800203 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800204 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700205 }
206
207 private void completeDrop(DragObject d) {
Winson Chung4c98d922011-05-31 16:50:48 -0700208 ItemInfo item = (ItemInfo) d.dragInfo;
209
210 if (isAllAppsApplication(d.dragSource, item)) {
211 // Uninstall the application if it is being dragged from AppsCustomize
212 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700213 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700214 LauncherModel.deleteItemFromDatabase(mLauncher, item);
215 } else if (isWorkspaceFolder(d)) {
216 // Remove the folder from the workspace and delete the contents from launcher model
217 FolderInfo folderInfo = (FolderInfo) item;
218 mLauncher.removeFolder(folderInfo);
219 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700220 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700221 // Remove the widget from the workspace
222 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
223 LauncherModel.deleteItemFromDatabase(mLauncher, item);
224
225 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
226 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
227 if (appWidgetHost != null) {
228 // Deleting an app widget ID is a void call but writes to disk before returning
229 // to the caller...
230 new Thread("deleteAppWidgetId") {
231 public void run() {
232 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
233 }
234 }.start();
235 }
236 }
237 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700238
239 public void onDrop(DragObject d) {
240 animateToTrashAndCompleteDrop(d);
241 }
Winson Chung043f2af2012-03-01 16:09:54 -0800242
243 /**
244 * Creates an animation from the current drag view to the delete trash icon.
245 */
246 private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer,
247 DragObject d, PointF vel, ViewConfiguration config) {
248 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
249 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
250 final Rect from = new Rect();
251 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
252
253 // Calculate how far along the velocity vector we should put the intermediate point on
254 // the bezier curve
255 float velocity = Math.abs(vel.length());
256 float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
257 int offsetY = (int) (-from.top * vp);
258 int offsetX = (int) (offsetY / (vel.y / vel.x));
259 final float y2 = from.top + offsetY; // intermediate t/l
260 final float x2 = from.left + offsetX;
261 final float x1 = from.left; // drag view t/l
262 final float y1 = from.top;
263 final float x3 = to.left; // delete target t/l
264 final float y3 = to.top;
265
266 final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {
267 @Override
268 public float getInterpolation(float t) {
269 return t * t * t * t * t * t * t * t;
270 }
271 };
272 return new AnimatorUpdateListener() {
273 @Override
274 public void onAnimationUpdate(ValueAnimator animation) {
275 final DragView dragView = (DragView) dragLayer.getAnimatedView();
276 float t = ((Float) animation.getAnimatedValue()).floatValue();
277 float tp = scaleAlphaInterpolator.getInterpolation(t);
278 float initialScale = dragView.getInitialScale();
279 float finalAlpha = 0.5f;
280 float scale = dragView.getScaleX();
281 float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
282 float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
283 float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) +
284 (t * t) * x3;
285 float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) +
286 (t * t) * y3;
287
288 dragView.setTranslationX(x);
289 dragView.setTranslationY(y);
290 dragView.setScaleX(initialScale * (1f - tp));
291 dragView.setScaleY(initialScale * (1f - tp));
292 dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
293 }
294 };
295 }
296
297 /**
298 * Creates an animation from the current drag view along its current velocity vector.
299 * For this animation, the alpha runs for a fixed duration and we update the position
300 * progressively.
301 */
302 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
303 private static float FRICTION = 0.93f;
304
305 private DragLayer mDragLayer;
306 private PointF mVelocity;
307 private Rect mFrom;
308 private long mPrevTime;
309 private boolean mHasOffsetForScale;
310
Winson Chung9658b1e2012-04-09 18:30:07 -0700311 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f);
Winson Chung043f2af2012-03-01 16:09:54 -0800312
313 public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from,
314 long startTime) {
315 mDragLayer = dragLayer;
316 mVelocity = vel;
317 mFrom = from;
318 mPrevTime = startTime;
319 }
320
321 @Override
322 public void onAnimationUpdate(ValueAnimator animation) {
323 final DragView dragView = (DragView) mDragLayer.getAnimatedView();
324 float t = ((Float) animation.getAnimatedValue()).floatValue();
325 long curTime = AnimationUtils.currentAnimationTimeMillis();
326
327 if (!mHasOffsetForScale) {
328 mHasOffsetForScale = true;
329 float scale = dragView.getScaleX();
330 float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f;
331 float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f;
332
333 mFrom.left += xOffset;
334 mFrom.top += yOffset;
335 }
336
337 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
338 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
339
340 dragView.setTranslationX(mFrom.left);
341 dragView.setTranslationY(mFrom.top);
342 dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
343
344 mVelocity.x *= FRICTION;
345 mVelocity.y *= FRICTION;
346 mPrevTime = curTime;
347 }
348 };
349 private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer,
350 DragObject d, PointF vel, final long startTime, final int duration,
351 ViewConfiguration config) {
352 final Rect from = new Rect();
353 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
354
355 return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime);
356 }
357
358 public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
Winson Chunga48487a2012-03-20 16:19:37 -0700359 final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView;
360
Winson Chung043f2af2012-03-01 16:09:54 -0800361 // Don't highlight the icon as it's animating
362 d.dragView.setColor(0);
363 d.dragView.updateInitialScaleToCurrentScale();
Winson Chunga48487a2012-03-20 16:19:37 -0700364 // Don't highlight the target if we are flinging from AllApps
365 if (isAllApps) {
366 resetHoverColor();
367 }
Winson Chung043f2af2012-03-01 16:09:54 -0800368
369 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
370 // Defer animating out the drop target if we are animating to it
371 mSearchDropTargetBar.deferOnDragEnd();
372 mSearchDropTargetBar.finishAnimations();
373 }
374
375 final ViewConfiguration config = ViewConfiguration.get(mLauncher);
376 final DragLayer dragLayer = mLauncher.getDragLayer();
Winson Chung9658b1e2012-04-09 18:30:07 -0700377 final int duration = FLIND_DELETE_ANIMATION_DURATION;
Winson Chung043f2af2012-03-01 16:09:54 -0800378 final long startTime = AnimationUtils.currentAnimationTimeMillis();
379
380 // NOTE: Because it takes time for the first frame of animation to actually be
381 // called and we expect the animation to be a continuation of the fling, we have
382 // to account for the time that has elapsed since the fling finished. And since
383 // we don't have a startDelay, we will always get call to update when we call
384 // start() (which we want to ignore).
385 final TimeInterpolator tInterpolator = new TimeInterpolator() {
386 private int mCount = -1;
387 private float mOffset = 0f;
388
389 @Override
390 public float getInterpolation(float t) {
391 if (mCount < 0) {
392 mCount++;
393 } else if (mCount == 0) {
394 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
395 startTime) / duration);
396 mCount++;
397 }
398 return Math.min(1f, mOffset + t);
399 }
400 };
401 AnimatorUpdateListener updateCb = null;
402 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
403 updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
404 } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
405 updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime,
406 duration, config);
407 }
408 Runnable onAnimationEndRunnable = new Runnable() {
409 @Override
410 public void run() {
411 mSearchDropTargetBar.onDragEnd();
Winson Chunga48487a2012-03-20 16:19:37 -0700412
413 // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up
414 // itself, otherwise, complete the drop to initiate the deletion process
415 if (!isAllApps) {
416 mLauncher.exitSpringLoadedDragMode();
417 completeDrop(d);
418 }
419 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800420 }
421 };
422 dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable,
423 DragLayer.ANIMATION_END_DISAPPEAR, null);
424 }
Winson Chung4c98d922011-05-31 16:50:48 -0700425}