blob: 6f455905fb136bcc4bf32b38130d0a37b75374b5 [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
108 @Override
109 public boolean acceptDrop(DragObject d) {
110 // We can remove everything including App shortcuts, folders, widgets, etc.
111 return true;
112 }
113
114 @Override
115 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700116 boolean isVisible = true;
117 boolean isUninstall = false;
118
Winson Chungf0ea4d32011-06-06 14:27:16 -0700119 // If we are dragging a widget from AppsCustomize, hide the delete target
Winson Chung4c98d922011-05-31 16:50:48 -0700120 if (isAllAppsWidget(source, info)) {
121 isVisible = false;
122 }
123
124 // If we are dragging an application from AppsCustomize, only show the control if we can
125 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
126 if (isAllAppsApplication(source, info)) {
127 ApplicationInfo appInfo = (ApplicationInfo) info;
128 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
129 isUninstall = true;
130 } else {
131 isVisible = false;
132 }
133 }
134
Adam Cohenebea84d2011-11-09 17:20:41 -0800135 if (isUninstall) {
136 setCompoundDrawablesWithIntrinsicBounds(mUninstallDrawable, null, null, null);
137 } else {
138 setCompoundDrawablesWithIntrinsicBounds(mRemoveDrawable, null, null, null);
139 }
140 mCurrentDrawable = (TransitionDrawable) getCompoundDrawables()[0];
141
Winson Chung4c98d922011-05-31 16:50:48 -0700142 mActive = isVisible;
Adam Cohenebea84d2011-11-09 17:20:41 -0800143 mCurrentDrawable.resetTransition();
Winson Chunga6427b12011-07-27 10:53:39 -0700144 setTextColor(mOriginalTextColor);
145 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
146 if (getText().length() > 0) {
147 setText(isUninstall ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700148 : R.string.delete_target_label);
149 }
150 }
151
152 @Override
153 public void onDragEnd() {
154 super.onDragEnd();
155 mActive = false;
156 }
157
158 public void onDragEnter(DragObject d) {
159 super.onDragEnter(d);
160
Adam Cohenebea84d2011-11-09 17:20:41 -0800161 mCurrentDrawable.startTransition(mTransitionDuration);
Winson Chunga6427b12011-07-27 10:53:39 -0700162 setTextColor(mHoverColor);
Winson Chung4c98d922011-05-31 16:50:48 -0700163 }
164
165 public void onDragExit(DragObject d) {
166 super.onDragExit(d);
167
Winson Chungaaa530a2011-07-11 21:06:30 -0700168 if (!d.dragComplete) {
Adam Cohenebea84d2011-11-09 17:20:41 -0800169 mCurrentDrawable.resetTransition();
Winson Chunga6427b12011-07-27 10:53:39 -0700170 setTextColor(mOriginalTextColor);
Winson Chung61967cb2012-02-28 18:11:33 -0800171 } else {
172 // Restore the hover color if we are deleting
173 d.dragView.setColor(mHoverColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700174 }
Winson Chung4c98d922011-05-31 16:50:48 -0700175 }
176
Adam Cohened66b2b2012-01-23 17:28:51 -0800177 private void animateToTrashAndCompleteDrop(final DragObject d) {
178 DragLayer dragLayer = mLauncher.getDragLayer();
179 Rect from = new Rect();
180 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Winson Chung61967cb2012-02-28 18:11:33 -0800181 Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
182 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
183 float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800184
Adam Cohend4d7aa52011-07-19 21:47:37 -0700185 mSearchDropTargetBar.deferOnDragEnd();
186 Runnable onAnimationEndRunnable = new Runnable() {
187 @Override
188 public void run() {
189 mSearchDropTargetBar.onDragEnd();
190 mLauncher.exitSpringLoadedDragMode();
191 completeDrop(d);
192 }
193 };
Winson Chung61967cb2012-02-28 18:11:33 -0800194 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700195 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800196 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800197 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700198 }
199
200 private void completeDrop(DragObject d) {
Winson Chung4c98d922011-05-31 16:50:48 -0700201 ItemInfo item = (ItemInfo) d.dragInfo;
202
203 if (isAllAppsApplication(d.dragSource, item)) {
204 // Uninstall the application if it is being dragged from AppsCustomize
205 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700206 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700207 LauncherModel.deleteItemFromDatabase(mLauncher, item);
208 } else if (isWorkspaceFolder(d)) {
209 // Remove the folder from the workspace and delete the contents from launcher model
210 FolderInfo folderInfo = (FolderInfo) item;
211 mLauncher.removeFolder(folderInfo);
212 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700213 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700214 // Remove the widget from the workspace
215 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
216 LauncherModel.deleteItemFromDatabase(mLauncher, item);
217
218 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
219 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
220 if (appWidgetHost != null) {
221 // Deleting an app widget ID is a void call but writes to disk before returning
222 // to the caller...
223 new Thread("deleteAppWidgetId") {
224 public void run() {
225 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
226 }
227 }.start();
228 }
229 }
230 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700231
232 public void onDrop(DragObject d) {
233 animateToTrashAndCompleteDrop(d);
234 }
Winson Chung043f2af2012-03-01 16:09:54 -0800235
236 /**
237 * Creates an animation from the current drag view to the delete trash icon.
238 */
239 private AnimatorUpdateListener createFlingToTrashAnimatorListener(final DragLayer dragLayer,
240 DragObject d, PointF vel, ViewConfiguration config) {
241 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
242 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
243 final Rect from = new Rect();
244 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
245
246 // Calculate how far along the velocity vector we should put the intermediate point on
247 // the bezier curve
248 float velocity = Math.abs(vel.length());
249 float vp = Math.min(1f, velocity / (config.getScaledMaximumFlingVelocity() / 2f));
250 int offsetY = (int) (-from.top * vp);
251 int offsetX = (int) (offsetY / (vel.y / vel.x));
252 final float y2 = from.top + offsetY; // intermediate t/l
253 final float x2 = from.left + offsetX;
254 final float x1 = from.left; // drag view t/l
255 final float y1 = from.top;
256 final float x3 = to.left; // delete target t/l
257 final float y3 = to.top;
258
259 final TimeInterpolator scaleAlphaInterpolator = new TimeInterpolator() {
260 @Override
261 public float getInterpolation(float t) {
262 return t * t * t * t * t * t * t * t;
263 }
264 };
265 return new AnimatorUpdateListener() {
266 @Override
267 public void onAnimationUpdate(ValueAnimator animation) {
268 final DragView dragView = (DragView) dragLayer.getAnimatedView();
269 float t = ((Float) animation.getAnimatedValue()).floatValue();
270 float tp = scaleAlphaInterpolator.getInterpolation(t);
271 float initialScale = dragView.getInitialScale();
272 float finalAlpha = 0.5f;
273 float scale = dragView.getScaleX();
274 float x1o = ((1f - scale) * dragView.getMeasuredWidth()) / 2f;
275 float y1o = ((1f - scale) * dragView.getMeasuredHeight()) / 2f;
276 float x = (1f - t) * (1f - t) * (x1 - x1o) + 2 * (1f - t) * t * (x2 - x1o) +
277 (t * t) * x3;
278 float y = (1f - t) * (1f - t) * (y1 - y1o) + 2 * (1f - t) * t * (y2 - x1o) +
279 (t * t) * y3;
280
281 dragView.setTranslationX(x);
282 dragView.setTranslationY(y);
283 dragView.setScaleX(initialScale * (1f - tp));
284 dragView.setScaleY(initialScale * (1f - tp));
285 dragView.setAlpha(finalAlpha + (1f - finalAlpha) * (1f - tp));
286 }
287 };
288 }
289
290 /**
291 * Creates an animation from the current drag view along its current velocity vector.
292 * For this animation, the alpha runs for a fixed duration and we update the position
293 * progressively.
294 */
295 private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener {
296 private static float FRICTION = 0.93f;
297
298 private DragLayer mDragLayer;
299 private PointF mVelocity;
300 private Rect mFrom;
301 private long mPrevTime;
302 private boolean mHasOffsetForScale;
303
304 private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(1.5f);
305
306 public FlingAlongVectorAnimatorUpdateListener(DragLayer dragLayer, PointF vel, Rect from,
307 long startTime) {
308 mDragLayer = dragLayer;
309 mVelocity = vel;
310 mFrom = from;
311 mPrevTime = startTime;
312 }
313
314 @Override
315 public void onAnimationUpdate(ValueAnimator animation) {
316 final DragView dragView = (DragView) mDragLayer.getAnimatedView();
317 float t = ((Float) animation.getAnimatedValue()).floatValue();
318 long curTime = AnimationUtils.currentAnimationTimeMillis();
319
320 if (!mHasOffsetForScale) {
321 mHasOffsetForScale = true;
322 float scale = dragView.getScaleX();
323 float xOffset = ((scale - 1f) * dragView.getMeasuredWidth()) / 2f;
324 float yOffset = ((scale - 1f) * dragView.getMeasuredHeight()) / 2f;
325
326 mFrom.left += xOffset;
327 mFrom.top += yOffset;
328 }
329
330 mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f);
331 mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f);
332
333 dragView.setTranslationX(mFrom.left);
334 dragView.setTranslationY(mFrom.top);
335 dragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t));
336
337 mVelocity.x *= FRICTION;
338 mVelocity.y *= FRICTION;
339 mPrevTime = curTime;
340 }
341 };
342 private AnimatorUpdateListener createFlingAlongVectorAnimatorListener(final DragLayer dragLayer,
343 DragObject d, PointF vel, final long startTime, final int duration,
344 ViewConfiguration config) {
345 final Rect from = new Rect();
346 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
347
348 return new FlingAlongVectorAnimatorUpdateListener(dragLayer, vel, from, startTime);
349 }
350
351 public void onFlingToDelete(final DragObject d, int x, int y, PointF vel) {
352 // Don't highlight the icon as it's animating
353 d.dragView.setColor(0);
354 d.dragView.updateInitialScaleToCurrentScale();
355
356 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
357 // Defer animating out the drop target if we are animating to it
358 mSearchDropTargetBar.deferOnDragEnd();
359 mSearchDropTargetBar.finishAnimations();
360 }
361
362 final ViewConfiguration config = ViewConfiguration.get(mLauncher);
363 final DragLayer dragLayer = mLauncher.getDragLayer();
364 final int duration = DELETE_ANIMATION_DURATION;
365 final long startTime = AnimationUtils.currentAnimationTimeMillis();
366
367 // NOTE: Because it takes time for the first frame of animation to actually be
368 // called and we expect the animation to be a continuation of the fling, we have
369 // to account for the time that has elapsed since the fling finished. And since
370 // we don't have a startDelay, we will always get call to update when we call
371 // start() (which we want to ignore).
372 final TimeInterpolator tInterpolator = new TimeInterpolator() {
373 private int mCount = -1;
374 private float mOffset = 0f;
375
376 @Override
377 public float getInterpolation(float t) {
378 if (mCount < 0) {
379 mCount++;
380 } else if (mCount == 0) {
381 mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() -
382 startTime) / duration);
383 mCount++;
384 }
385 return Math.min(1f, mOffset + t);
386 }
387 };
388 AnimatorUpdateListener updateCb = null;
389 if (mFlingDeleteMode == MODE_FLING_DELETE_TO_TRASH) {
390 updateCb = createFlingToTrashAnimatorListener(dragLayer, d, vel, config);
391 } else if (mFlingDeleteMode == MODE_FLING_DELETE_ALONG_VECTOR) {
392 updateCb = createFlingAlongVectorAnimatorListener(dragLayer, d, vel, startTime,
393 duration, config);
394 }
395 Runnable onAnimationEndRunnable = new Runnable() {
396 @Override
397 public void run() {
398 mSearchDropTargetBar.onDragEnd();
399 mLauncher.exitSpringLoadedDragMode();
400 completeDrop(d);
401 }
402 };
403 dragLayer.animateView(d.dragView, updateCb, duration, tInterpolator, onAnimationEndRunnable,
404 DragLayer.ANIMATION_END_DISAPPEAR, null);
405 }
Winson Chung4c98d922011-05-31 16:50:48 -0700406}