blob: b044ea8076fb847cdd6155969cf233f2f4f899ca [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019import android.content.Context;
20import android.content.res.TypedArray;
Joe Onorato00acb122009-08-04 16:04:30 -040021import android.graphics.PorterDuff;
22import android.graphics.PorterDuffColorFilter;
Michael Jurka3adcb0c2010-10-15 18:07:21 -070023import android.graphics.Rect;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.graphics.RectF;
25import android.graphics.drawable.TransitionDrawable;
Patrick Dubroybc6840b2010-09-01 11:54:27 -070026import android.util.AttributeSet;
Patrick Dubroybc6840b2010-09-01 11:54:27 -070027import android.view.View;
28import android.view.animation.AccelerateInterpolator;
29import android.view.animation.AlphaAnimation;
30import android.view.animation.Animation;
31import android.view.animation.AnimationSet;
32import android.view.animation.TranslateAnimation;
Romain Guyedcce092010-03-04 13:03:17 -080033
Adam Cohencdc30d52010-12-01 15:09:47 -080034import com.android.launcher.R;
35
Winson Chung760e5372010-12-15 13:14:23 -080036public class DeleteZone extends IconDropTarget {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037 private static final int ORIENTATION_HORIZONTAL = 1;
38 private static final int TRANSITION_DURATION = 250;
39 private static final int ANIMATION_DURATION = 200;
Winson Chung760e5372010-12-15 13:14:23 -080040 private static final int XLARGE_TRANSITION_DURATION = 150;
41 private static final int XLARGE_ANIMATION_DURATION = 200;
Patrick Dubroydea9e932010-09-22 15:04:29 -070042
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043 private AnimationSet mInAnimation;
44 private AnimationSet mOutAnimation;
45 private Animation mHandleInAnimation;
46 private Animation mHandleOutAnimation;
47
48 private int mOrientation;
Joe Onorato00acb122009-08-04 16:04:30 -040049 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Michael Jurka3adcb0c2010-10-15 18:07:21 -070051 private final RectF mRegionF = new RectF();
52 private final Rect mRegion = new Rect();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053 private TransitionDrawable mTransition;
Patrick Dubroydea9e932010-09-22 15:04:29 -070054
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055 public DeleteZone(Context context, AttributeSet attrs) {
56 this(context, attrs, 0);
57 }
58
59 public DeleteZone(Context context, AttributeSet attrs, int defStyle) {
60 super(context, attrs, defStyle);
61
Winson Chung760e5372010-12-15 13:14:23 -080062 mOuterDragPadding = getResources().getDimensionPixelSize(R.dimen.delete_zone_size);
63 mInnerDragPadding = getResources().getDimensionPixelSize(R.dimen.delete_zone_padding);
64
Joe Onorato00acb122009-08-04 16:04:30 -040065 final int srcColor = context.getResources().getColor(R.color.delete_color_filter);
Winson Chung760e5372010-12-15 13:14:23 -080066 mHoverPaint.setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.SRC_ATOP));
Joe Onorato00acb122009-08-04 16:04:30 -040067
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0);
69 mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL);
70 a.recycle();
71 }
72
73 @Override
74 protected void onFinishInflate() {
75 super.onFinishInflate();
Joe Onoratoa9c28f62009-09-14 18:38:49 -040076 mTransition = (TransitionDrawable) getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077 }
78
79 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040080 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 return true;
82 }
83
Joe Onorato00acb122009-08-04 16:04:30 -040084 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
85 DragView dragView, Object dragInfo) {
Adam Cohencdc30d52010-12-01 15:09:47 -080086 if (!mDragAndDropEnabled) return;
87
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 final ItemInfo item = (ItemInfo) dragInfo;
89
Patrick Dubroybc6840b2010-09-01 11:54:27 -070090 // On x-large screens, you can uninstall an app by dragging from all apps
91 if (item instanceof ApplicationInfo && LauncherApplication.isScreenXLarge()) {
Patrick Dubroy5539af72010-09-07 15:22:01 -070092 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Patrick Dubroybc6840b2010-09-01 11:54:27 -070093 }
94
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 if (item.container == -1) return;
96
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -070098 if (item instanceof LauncherAppWidgetInfo) {
Joe Onorato9c1289c2009-08-17 11:03:03 -040099 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700101 } else if (source instanceof UserFolder) {
102 final UserFolder userFolder = (UserFolder) source;
103 final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder.getInfo();
104 // Item must be a ShortcutInfo otherwise it couldn't have been in the folder
105 // in the first place.
106 userFolderInfo.remove((ShortcutInfo)item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700108
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109 if (item instanceof UserFolderInfo) {
110 final UserFolderInfo userFolderInfo = (UserFolderInfo)item;
111 LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher, userFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400112 mLauncher.removeFolder(userFolderInfo);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700113 } else if (item instanceof LauncherAppWidgetInfo) {
114 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
115 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
116 if (appWidgetHost != null) {
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700117 final int appWidgetId = launcherAppWidgetInfo.appWidgetId;
118 // Deleting an app widget ID is a void call but writes to disk before returning
119 // to the caller...
120 new Thread("deleteAppWidgetId") {
121 public void run() {
122 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
123 }
124 }.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 }
126 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 LauncherModel.deleteItemFromDatabase(mLauncher, item);
129 }
130
131 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400132 DragView dragView, Object dragInfo) {
Winson Chung760e5372010-12-15 13:14:23 -0800133 if (mDragAndDropEnabled) {
134 mTransition.reverseTransition(getTransitionAnimationDuration());
135 super.onDragEnter(source, x, y, xOffset, yOffset, dragView, dragInfo);
136 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 }
138
139 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400140 DragView dragView, Object dragInfo) {
Winson Chung760e5372010-12-15 13:14:23 -0800141 if (mDragAndDropEnabled) {
142 mTransition.reverseTransition(getTransitionAnimationDuration());
143 super.onDragExit(source, x, y, xOffset, yOffset, dragView, dragInfo);
144 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 }
146
Joe Onorato5162ea92009-09-03 09:39:42 -0700147 public void onDragStart(DragSource source, Object info, int dragAction) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 final ItemInfo item = (ItemInfo) info;
Adam Cohencdc30d52010-12-01 15:09:47 -0800149 if (item != null && mDragAndDropEnabled) {
Winson Chung760e5372010-12-15 13:14:23 -0800150 mActive = true;
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700151 getHitRect(mRegion);
152 mRegionF.set(mRegion);
153
154 if (LauncherApplication.isScreenXLarge()) {
155 // This region will be a "dead zone" for scrolling; make it extend to the edge of
156 // the screen so users don't accidentally trigger a scroll while deleting items
157 mRegionF.top = mLauncher.getWorkspace().getTop();
158 mRegionF.right = mLauncher.getWorkspace().getRight();
159 }
160
161 mDragController.setDeleteRegion(mRegionF);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700162
163 // Make sure the icon is set to the default drawable, not the hover drawable
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 mTransition.resetTransition();
Patrick Dubroydea9e932010-09-22 15:04:29 -0700165
Adam Cohencdc30d52010-12-01 15:09:47 -0800166 createAnimations();
167 startAnimation(mInAnimation);
168 if (mHandle != null) {
Patrick Dubroydea9e932010-09-22 15:04:29 -0700169 mHandle.startAnimation(mHandleOutAnimation);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700170 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800171 setVisibility(VISIBLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 }
173 }
174
175 public void onDragEnd() {
Winson Chung760e5372010-12-15 13:14:23 -0800176 if (mActive && mDragAndDropEnabled) {
177 mActive = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400178 mDragController.setDeleteRegion(null);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700179
180 if (mOutAnimation != null) startAnimation(mOutAnimation);
Adam Cohencdc30d52010-12-01 15:09:47 -0800181 if (mHandleInAnimation != null && mHandle != null) {
182 mHandle.startAnimation(mHandleInAnimation);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700183 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800184 setVisibility(GONE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 }
186 }
187
188 private void createAnimations() {
Winson Chung760e5372010-12-15 13:14:23 -0800189 int duration = getAnimationDuration();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 if (mHandleInAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400191 mHandleInAnimation = new AlphaAnimation(0.0f, 1.0f);
Winson Chung760e5372010-12-15 13:14:23 -0800192 mHandleInAnimation.setDuration(duration);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700194
195 if (mInAnimation == null) {
196 mInAnimation = new FastAnimationSet();
197 if (!LauncherApplication.isScreenXLarge()) {
198 final AnimationSet animationSet = mInAnimation;
199 animationSet.setInterpolator(new AccelerateInterpolator());
200 animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
201 if (mOrientation == ORIENTATION_HORIZONTAL) {
202 animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
203 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
204 Animation.RELATIVE_TO_SELF, 0.0f));
205 } else {
206 animationSet.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF,
207 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
208 Animation.ABSOLUTE, 0.0f));
209 }
Winson Chung760e5372010-12-15 13:14:23 -0800210 animationSet.setDuration(duration);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 } else {
Adam Lesinski6b879f02010-11-04 16:15:23 -0700212 mInAnimation.addAnimation(mHandleInAnimation);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 if (mHandleOutAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400217 mHandleOutAnimation = new AlphaAnimation(1.0f, 0.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 mHandleOutAnimation.setFillAfter(true);
Winson Chung760e5372010-12-15 13:14:23 -0800219 mHandleOutAnimation.setDuration(duration);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700221
222 if (mOutAnimation == null) {
223 mOutAnimation = new FastAnimationSet();
224 if (!LauncherApplication.isScreenXLarge()) {
225 final AnimationSet animationSet = mOutAnimation;
226 animationSet.setInterpolator(new AccelerateInterpolator());
227 animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
228 if (mOrientation == ORIENTATION_HORIZONTAL) {
229 animationSet.addAnimation(new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
230 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
231 Animation.RELATIVE_TO_SELF, 1.0f));
232 } else {
233 animationSet.addAnimation(new FastTranslateAnimation(Animation.RELATIVE_TO_SELF,
234 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f,
235 Animation.ABSOLUTE, 0.0f));
236 }
Winson Chung760e5372010-12-15 13:14:23 -0800237 animationSet.setDuration(duration);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700238 } else {
239 mOutAnimation.addAnimation(mHandleOutAnimation);
240 }
241 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 }
243
Joe Onorato00acb122009-08-04 16:04:30 -0400244 void setDragController(DragController dragController) {
245 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 }
247
Winson Chung760e5372010-12-15 13:14:23 -0800248 private int getTransitionAnimationDuration() {
249 return LauncherApplication.isScreenXLarge() ?
250 XLARGE_TRANSITION_DURATION : TRANSITION_DURATION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 }
252
Winson Chung760e5372010-12-15 13:14:23 -0800253 private int getAnimationDuration() {
254 return LauncherApplication.isScreenXLarge() ?
255 XLARGE_ANIMATION_DURATION : ANIMATION_DURATION;
Patrick Dubroydea9e932010-09-22 15:04:29 -0700256 }
257
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 private static class FastTranslateAnimation extends TranslateAnimation {
259 public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
260 int fromYType, float fromYValue, int toYType, float toYValue) {
261 super(fromXType, fromXValue, toXType, toXValue,
262 fromYType, fromYValue, toYType, toYValue);
263 }
264
265 @Override
266 public boolean willChangeTransformationMatrix() {
267 return true;
268 }
269
270 @Override
271 public boolean willChangeBounds() {
272 return false;
273 }
274 }
275
276 private static class FastAnimationSet extends AnimationSet {
277 FastAnimationSet() {
278 super(false);
279 }
280
281 @Override
282 public boolean willChangeTransformationMatrix() {
283 return true;
284 }
285
286 @Override
287 public boolean willChangeBounds() {
288 return false;
289 }
290 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291}