blob: 7e4225be4bc40b5f9cedd8390f6e19596d98a1ec [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;
41 private static int MODE_FLING_DELETE_TO_TRASH = 0;
42 private static int MODE_FLING_DELETE_ALONG_VECTOR = 1;
Winson Chung4c98d922011-05-31 16:50:48 -070043
Winson Chung043f2af2012-03-01 16:09:54 -080044 private final int mFlingDeleteMode = MODE_FLING_DELETE_ALONG_VECTOR;
45
Winson Chunga62e9fd2011-07-11 15:20:48 -070046 private ColorStateList mOriginalTextColor;
Adam Cohenebea84d2011-11-09 17:20:41 -080047 private TransitionDrawable mUninstallDrawable;
48 private TransitionDrawable mRemoveDrawable;
49 private TransitionDrawable mCurrentDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070050
51 public DeleteDropTarget(Context context, AttributeSet attrs) {
52 this(context, attrs, 0);
53 }
54
55 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
56 super(context, attrs, defStyle);
57 }
58
59 @Override
60 protected void onFinishInflate() {
61 super.onFinishInflate();
62
63 // Get the drawable
Winson Chunga6427b12011-07-27 10:53:39 -070064 mOriginalTextColor = getTextColors();
Winson Chung4c98d922011-05-31 16:50:48 -070065
66 // Get the hover color
67 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070068 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080069 mUninstallDrawable = (TransitionDrawable)
70 r.getDrawable(R.drawable.uninstall_target_selector);
71 mRemoveDrawable = (TransitionDrawable) r.getDrawable(R.drawable.remove_target_selector);
72
73 mRemoveDrawable.setCrossFadeEnabled(true);
74 mUninstallDrawable.setCrossFadeEnabled(true);
75
76 // The current drawable is set to either the remove drawable or the uninstall drawable
77 // and is initially set to the remove drawable, as set in the layout xml.
78 mCurrentDrawable = (TransitionDrawable) getCompoundDrawables()[0];
Winson Chung201bc822011-06-20 15:41:53 -070079
80 // Remove the text in the Phone UI in landscape
81 int orientation = getResources().getConfiguration().orientation;
82 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
83 if (!LauncherApplication.isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070084 setText("");
Winson Chung201bc822011-06-20 15:41:53 -070085 }
86 }
Winson Chung4c98d922011-05-31 16:50:48 -070087 }
88
89 private boolean isAllAppsApplication(DragSource source, Object info) {
90 return (source instanceof AppsCustomizePagedView) && (info instanceof ApplicationInfo);
91 }
92 private boolean isAllAppsWidget(DragSource source, Object info) {
93 return (source instanceof AppsCustomizePagedView) && (info instanceof PendingAddWidgetInfo);
94 }
Michael Jurka0b4870d2011-07-10 13:39:08 -070095 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
96 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -070097 }
Michael Jurka0b4870d2011-07-10 13:39:08 -070098 private boolean isWorkspaceOrFolderApplication(DragObject d) {
99 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
100 }
101 private boolean isWorkspaceOrFolderWidget(DragObject d) {
102 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700103 }
104 private boolean isWorkspaceFolder(DragObject d) {
105 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
106 }
107
Winson Chunga48487a2012-03-20 16:19:37 -0700108 private void setHoverColor() {
109 mCurrentDrawable.startTransition(mTransitionDuration);
110 setTextColor(mHoverColor);
111 }
112 private void resetHoverColor() {
113 mCurrentDrawable.resetTransition();
114 setTextColor(mOriginalTextColor);
115 }
116
Winson Chung4c98d922011-05-31 16:50:48 -0700117 @Override
118 public boolean acceptDrop(DragObject d) {
119 // We can remove everything including App shortcuts, folders, widgets, etc.
120 return true;
121 }
122
123 @Override
124 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700125 boolean isVisible = true;
126 boolean isUninstall = false;
127
Winson Chungf0ea4d32011-06-06 14:27:16 -0700128 // If we are dragging a widget from AppsCustomize, hide the delete target
Winson Chung4c98d922011-05-31 16:50:48 -0700129 if (isAllAppsWidget(source, info)) {
130 isVisible = false;
131 }
132
133 // If we are dragging an application from AppsCustomize, only show the control if we can
134 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
135 if (isAllAppsApplication(source, info)) {
136 ApplicationInfo appInfo = (ApplicationInfo) info;
137 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
138 isUninstall = true;
139 } else {
140 isVisible = false;
141 }
142 }
143
Adam Cohenebea84d2011-11-09 17:20:41 -0800144 if (isUninstall) {
145 setCompoundDrawablesWithIntrinsicBounds(mUninstallDrawable, null, null, null);
146 } else {
147 setCompoundDrawablesWithIntrinsicBounds(mRemoveDrawable, null, null, null);
148 }
149 mCurrentDrawable = (TransitionDrawable) getCompoundDrawables()[0];
150
Winson Chung4c98d922011-05-31 16:50:48 -0700151 mActive = isVisible;
Winson Chunga48487a2012-03-20 16:19:37 -0700152 resetHoverColor();
Winson Chunga6427b12011-07-27 10:53:39 -0700153 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
154 if (getText().length() > 0) {
155 setText(isUninstall ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700156 : R.string.delete_target_label);
157 }
158 }
159
160 @Override
161 public void onDragEnd() {
162 super.onDragEnd();
163 mActive = false;
164 }
165
166 public void onDragEnter(DragObject d) {
167 super.onDragEnter(d);
168
Winson Chunga48487a2012-03-20 16:19:37 -0700169 setHoverColor();
Winson Chung4c98d922011-05-31 16:50:48 -0700170 }
171
172 public void onDragExit(DragObject d) {
173 super.onDragExit(d);
174
Winson Chungaaa530a2011-07-11 21:06:30 -0700175 if (!d.dragComplete) {
Winson Chunga48487a2012-03-20 16:19:37 -0700176 resetHoverColor();
Winson Chung61967cb2012-02-28 18:11:33 -0800177 } else {
178 // Restore the hover color if we are deleting
179 d.dragView.setColor(mHoverColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700180 }
Winson Chung4c98d922011-05-31 16:50:48 -0700181 }
182
Adam Cohened66b2b2012-01-23 17:28:51 -0800183 private void animateToTrashAndCompleteDrop(final DragObject d) {
184 DragLayer dragLayer = mLauncher.getDragLayer();
185 Rect from = new Rect();
186 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Winson Chung61967cb2012-02-28 18:11:33 -0800187 Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
188 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
189 float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800190
Adam Cohend4d7aa52011-07-19 21:47:37 -0700191 mSearchDropTargetBar.deferOnDragEnd();
192 Runnable onAnimationEndRunnable = new Runnable() {
193 @Override
194 public void run() {
195 mSearchDropTargetBar.onDragEnd();
196 mLauncher.exitSpringLoadedDragMode();
197 completeDrop(d);
198 }
199 };
Winson Chung61967cb2012-02-28 18:11:33 -0800200 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700201 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800202 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800203 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700204 }
205
206 private void completeDrop(DragObject d) {
Winson Chung4c98d922011-05-31 16:50:48 -0700207 ItemInfo item = (ItemInfo) d.dragInfo;
208
209 if (isAllAppsApplication(d.dragSource, item)) {
210 // Uninstall the application if it is being dragged from AppsCustomize
211 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700212 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700213 LauncherModel.deleteItemFromDatabase(mLauncher, item);
214 } else if (isWorkspaceFolder(d)) {
215 // Remove the folder from the workspace and delete the contents from launcher model
216 FolderInfo folderInfo = (FolderInfo) item;
217 mLauncher.removeFolder(folderInfo);
218 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700219 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700220 // Remove the widget from the workspace
221 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
222 LauncherModel.deleteItemFromDatabase(mLauncher, item);
223
224 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
225 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
226 if (appWidgetHost != null) {
227 // Deleting an app widget ID is a void call but writes to disk before returning
228 // to the caller...
229 new Thread("deleteAppWidgetId") {
230 public void run() {
231 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
232 }
233 }.start();
234 }
235 }
236 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700237
238 public void onDrop(DragObject d) {
239 animateToTrashAndCompleteDrop(d);
240 }
Winson Chung043f2af2012-03-01 16:09:54 -0800241
242 /**
243 * Creates an animation from the current drag view to the delete trash icon.
244 */
245 private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer,
246 DragObject d, PointF vel, ViewConfiguration config) {
247 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
248 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
249 final Rect from = new Rect();
250 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
251
252 // Calculate how far along the velocity vector we should put the intermediate point on
253 // the bezier curve
254 float velocity = Math.abs(vel.length());
255 float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
256 int offsetY = (int) (-from.top * vp);
257 int offsetX = (int) (offsetY / (vel.y / vel.x));
258 final float y2 = from.top + offsetY; // intermediate t/l
259 final float x2 = from.left + offsetX;
260 final float x1 = from.left; // drag view t/l
261 final float y1 = from.top;
262 final float x3 = to.left; // delete target t/l
263 final float y3 = to.top;
264
265 final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {
266 @Override
267 public float getInterpolation(float t) {
268 return t * t * t * t * t * t * t * t;
269 }
270 };
271 return new AnimatorUpdateListener() {
272 @Override
273 public void onAnimationUpdate(ValueAnimator animation) {
274 final DragView dragView = (DragView) dragLayer.getAnimatedView();
275 float t = ((Float) animation.getAnimatedValue()).floatValue();
276 float tp = scaleAlphaInterpolator.getInterpolation(t);
277 float initialScale = dragView.getInitialScale();
278 float finalAlpha = 0.5f;
279 float scale = dragView.getScaleX();
280 float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
281 float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
282 float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) +
283 (t * t) * x3;
284 float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) +
285 (t * t) * y3;
286
287 dragView.setTranslationX(x);
288 dragView.setTranslationY(y);
289 dragView.setScaleX(initialScale * (1f - tp));
290 dragView.setScaleY(initialScale * (1f - tp));
291 dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
292 }
293 };
294 }
295
296 /**
297 * Creates an animation from the current drag view along its current velocity vector.
298 * For this animation, the alpha runs for a fixed duration and we update the position
299 * progressively.
300 */
301 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
302 private static float FRICTION = 0.93f;
303
304 private DragLayer mDragLayer;
305 private PointF mVelocity;
306 private Rect mFrom;
307 private long mPrevTime;
308 private boolean mHasOffsetForScale;
309
310 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(1.5f);
311
312 public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from,
313 long startTime) {
314 mDragLayer = dragLayer;
315 mVelocity = vel;
316 mFrom = from;
317 mPrevTime = startTime;
318 }
319
320 @Override
321 public void onAnimationUpdate(ValueAnimator animation) {
322 final DragView dragView = (DragView) mDragLayer.getAnimatedView();
323 float t = ((Float) animation.getAnimatedValue()).floatValue();
324 long curTime = AnimationUtils.currentAnimationTimeMillis();
325
326 if (!mHasOffsetForScale) {
327 mHasOffsetForScale = true;
328 float scale = dragView.getScaleX();
329 float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f;
330 float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f;
331
332 mFrom.left += xOffset;
333 mFrom.top += yOffset;
334 }
335
336 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
337 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
338
339 dragView.setTranslationX(mFrom.left);
340 dragView.setTranslationY(mFrom.top);
341 dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
342
343 mVelocity.x *= FRICTION;
344 mVelocity.y *= FRICTION;
345 mPrevTime = curTime;
346 }
347 };
348 private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer,
349 DragObject d, PointF vel, final long startTime, final int duration,
350 ViewConfiguration config) {
351 final Rect from = new Rect();
352 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
353
354 return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime);
355 }
356
357 public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
Winson Chunga48487a2012-03-20 16:19:37 -0700358 final boolean isAllApps = d.dragSource instanceof AppsCustomizePagedView;
359
Winson Chung043f2af2012-03-01 16:09:54 -0800360 // Don't highlight the icon as it's animating
361 d.dragView.setColor(0);
362 d.dragView.updateInitialScaleToCurrentScale();
Winson Chunga48487a2012-03-20 16:19:37 -0700363 // Don't highlight the target if we are flinging from AllApps
364 if (isAllApps) {
365 resetHoverColor();
366 }
Winson Chung043f2af2012-03-01 16:09:54 -0800367
368 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
369 // Defer animating out the drop target if we are animating to it
370 mSearchDropTargetBar.deferOnDragEnd();
371 mSearchDropTargetBar.finishAnimations();
372 }
373
374 final ViewConfiguration config = ViewConfiguration.get(mLauncher);
375 final DragLayer dragLayer = mLauncher.getDragLayer();
376 final int duration = DELETE_ANIMATION_DURATION;
377 final long startTime = AnimationUtils.currentAnimationTimeMillis();
378
379 // NOTE: Because it takes time for the first frame of animation to actually be
380 // called and we expect the animation to be a continuation of the fling, we have
381 // to account for the time that has elapsed since the fling finished. And since
382 // we don't have a startDelay, we will always get call to update when we call
383 // start() (which we want to ignore).
384 final TimeInterpolator tInterpolator = new TimeInterpolator() {
385 private int mCount = -1;
386 private float mOffset = 0f;
387
388 @Override
389 public float getInterpolation(float t) {
390 if (mCount < 0) {
391 mCount++;
392 } else if (mCount == 0) {
393 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
394 startTime) / duration);
395 mCount++;
396 }
397 return Math.min(1f, mOffset + t);
398 }
399 };
400 AnimatorUpdateListener updateCb = null;
401 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
402 updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
403 } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
404 updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime,
405 duration, config);
406 }
407 Runnable onAnimationEndRunnable = new Runnable() {
408 @Override
409 public void run() {
410 mSearchDropTargetBar.onDragEnd();
Winson Chunga48487a2012-03-20 16:19:37 -0700411
412 // If we are dragging from AllApps, then we allow AppsCustomizePagedView to clean up
413 // itself, otherwise, complete the drop to initiate the deletion process
414 if (!isAllApps) {
415 mLauncher.exitSpringLoadedDragMode();
416 completeDrop(d);
417 }
418 mLauncher.getDragController().onDeferredEndFling(d);
Winson Chung043f2af2012-03-01 16:09:54 -0800419 }
420 };
421 dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable,
422 DragLayer.ANIMATION_END_DISAPPEAR, null);
423 }
Winson Chung4c98d922011-05-31 16:50:48 -0700424}