blob: 4bed84c5cc2fc013c6f30201c3bea2786c18ecfd [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
Michael Jurka577d0172010-12-17 20:04:50 -080019import com.android.launcher.R;
20
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.content.Context;
Michael Jurka577d0172010-12-17 20:04:50 -080022import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.res.TypedArray;
Joe Onorato00acb122009-08-04 16:04:30 -040024import android.graphics.PorterDuff;
25import android.graphics.PorterDuffColorFilter;
Michael Jurka3adcb0c2010-10-15 18:07:21 -070026import android.graphics.Rect;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.graphics.RectF;
28import android.graphics.drawable.TransitionDrawable;
Patrick Dubroybc6840b2010-09-01 11:54:27 -070029import android.util.AttributeSet;
Patrick Dubroybc6840b2010-09-01 11:54:27 -070030import android.view.View;
31import android.view.animation.AccelerateInterpolator;
32import android.view.animation.AlphaAnimation;
33import android.view.animation.Animation;
34import android.view.animation.AnimationSet;
35import android.view.animation.TranslateAnimation;
Romain Guyedcce092010-03-04 13:03:17 -080036
Winson Chung760e5372010-12-15 13:14:23 -080037public class DeleteZone extends IconDropTarget {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038 private static final int ORIENTATION_HORIZONTAL = 1;
39 private static final int TRANSITION_DURATION = 250;
40 private static final int ANIMATION_DURATION = 200;
Winson Chung760e5372010-12-15 13:14:23 -080041 private static final int XLARGE_TRANSITION_DURATION = 150;
42 private static final int XLARGE_ANIMATION_DURATION = 200;
Michael Jurka577d0172010-12-17 20:04:50 -080043 private static final int LEFT_DRAWABLE = 0;
Patrick Dubroydea9e932010-09-22 15:04:29 -070044
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045 private AnimationSet mInAnimation;
46 private AnimationSet mOutAnimation;
47 private Animation mHandleInAnimation;
48 private Animation mHandleOutAnimation;
49
50 private int mOrientation;
Joe Onorato00acb122009-08-04 16:04:30 -040051 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052
Michael Jurka3adcb0c2010-10-15 18:07:21 -070053 private final RectF mRegionF = new RectF();
54 private final Rect mRegion = new Rect();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055 private TransitionDrawable mTransition;
Michael Jurka577d0172010-12-17 20:04:50 -080056 private int mTextColor;
57 private int mDragTextColor;
Patrick Dubroydea9e932010-09-22 15:04:29 -070058
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059 public DeleteZone(Context context, AttributeSet attrs) {
60 this(context, attrs, 0);
61 }
62
63 public DeleteZone(Context context, AttributeSet attrs, int defStyle) {
64 super(context, attrs, defStyle);
65
Joe Onorato00acb122009-08-04 16:04:30 -040066 final int srcColor = context.getResources().getColor(R.color.delete_color_filter);
Winson Chung760e5372010-12-15 13:14:23 -080067 mHoverPaint.setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.SRC_ATOP));
Joe Onorato00acb122009-08-04 16:04:30 -040068
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0);
70 mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL);
71 a.recycle();
Winson Chungbe5212b2010-12-20 11:36:33 -080072
73 int tb = getResources().getDimensionPixelSize(R.dimen.delete_zone_vertical_drag_padding);
74 int lr = getResources().getDimensionPixelSize(R.dimen.delete_zone_horizontal_drag_padding);
75 setDragPadding(tb, lr, tb, lr);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076 }
77
78 @Override
79 protected void onFinishInflate() {
80 super.onFinishInflate();
Michael Jurka577d0172010-12-17 20:04:50 -080081 mTransition = (TransitionDrawable) getCompoundDrawables()[LEFT_DRAWABLE];
82 if (LauncherApplication.isScreenXLarge()) {
83 mTransition.setCrossFadeEnabled(false);
84 }
85
86 Resources r = getResources();
87 mTextColor = r.getColor(R.color.workspace_all_apps_and_delete_zone_text_color);
88 mDragTextColor = r.getColor(R.color.workspace_delete_zone_drag_text_color);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 }
90
91 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040092 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 return true;
94 }
95
Joe Onorato00acb122009-08-04 16:04:30 -040096 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
97 DragView dragView, Object dragInfo) {
Adam Cohencdc30d52010-12-01 15:09:47 -080098 if (!mDragAndDropEnabled) return;
99
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 final ItemInfo item = (ItemInfo) dragInfo;
101
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700102 // On x-large screens, you can uninstall an app by dragging from all apps
103 if (item instanceof ApplicationInfo && LauncherApplication.isScreenXLarge()) {
Patrick Dubroy5539af72010-09-07 15:22:01 -0700104 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700105 }
106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 if (item.container == -1) return;
108
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700110 if (item instanceof LauncherAppWidgetInfo) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400111 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700113 } else if (source instanceof UserFolder) {
114 final UserFolder userFolder = (UserFolder) source;
115 final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder.getInfo();
116 // Item must be a ShortcutInfo otherwise it couldn't have been in the folder
117 // in the first place.
118 userFolderInfo.remove((ShortcutInfo)item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 if (item instanceof UserFolderInfo) {
122 final UserFolderInfo userFolderInfo = (UserFolderInfo)item;
123 LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher, userFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400124 mLauncher.removeFolder(userFolderInfo);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700125 } else if (item instanceof LauncherAppWidgetInfo) {
126 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
127 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
128 if (appWidgetHost != null) {
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700129 // Deleting an app widget ID is a void call but writes to disk before returning
130 // to the caller...
131 new Thread("deleteAppWidgetId") {
132 public void run() {
133 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
134 }
135 }.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 }
137 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 LauncherModel.deleteItemFromDatabase(mLauncher, item);
140 }
141
142 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400143 DragView dragView, Object dragInfo) {
Winson Chung760e5372010-12-15 13:14:23 -0800144 if (mDragAndDropEnabled) {
145 mTransition.reverseTransition(getTransitionAnimationDuration());
Michael Jurka577d0172010-12-17 20:04:50 -0800146 setTextColor(mDragTextColor);
Winson Chung760e5372010-12-15 13:14:23 -0800147 super.onDragEnter(source, x, y, xOffset, yOffset, dragView, dragInfo);
148 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 }
150
151 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400152 DragView dragView, Object dragInfo) {
Winson Chung760e5372010-12-15 13:14:23 -0800153 if (mDragAndDropEnabled) {
154 mTransition.reverseTransition(getTransitionAnimationDuration());
Michael Jurka577d0172010-12-17 20:04:50 -0800155 setTextColor(mTextColor);
Winson Chung760e5372010-12-15 13:14:23 -0800156 super.onDragExit(source, x, y, xOffset, yOffset, dragView, dragInfo);
157 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 }
159
Joe Onorato5162ea92009-09-03 09:39:42 -0700160 public void onDragStart(DragSource source, Object info, int dragAction) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 final ItemInfo item = (ItemInfo) info;
Adam Cohencdc30d52010-12-01 15:09:47 -0800162 if (item != null && mDragAndDropEnabled) {
Winson Chung760e5372010-12-15 13:14:23 -0800163 mActive = true;
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700164 getHitRect(mRegion);
165 mRegionF.set(mRegion);
166
167 if (LauncherApplication.isScreenXLarge()) {
168 // This region will be a "dead zone" for scrolling; make it extend to the edge of
169 // the screen so users don't accidentally trigger a scroll while deleting items
170 mRegionF.top = mLauncher.getWorkspace().getTop();
171 mRegionF.right = mLauncher.getWorkspace().getRight();
172 }
173
174 mDragController.setDeleteRegion(mRegionF);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700175
176 // Make sure the icon is set to the default drawable, not the hover drawable
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 mTransition.resetTransition();
Patrick Dubroydea9e932010-09-22 15:04:29 -0700178
Adam Cohencdc30d52010-12-01 15:09:47 -0800179 createAnimations();
180 startAnimation(mInAnimation);
Michael Jurkab8e14472010-12-20 16:06:10 -0800181 if (mOverlappingViews != null) {
182 for (View view : mOverlappingViews) {
183 view.startAnimation(mHandleOutAnimation);
184 }
Patrick Dubroydea9e932010-09-22 15:04:29 -0700185 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800186 setVisibility(VISIBLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 }
188 }
189
190 public void onDragEnd() {
Winson Chung760e5372010-12-15 13:14:23 -0800191 if (mActive && mDragAndDropEnabled) {
192 mActive = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400193 mDragController.setDeleteRegion(null);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700194
195 if (mOutAnimation != null) startAnimation(mOutAnimation);
Michael Jurkab8e14472010-12-20 16:06:10 -0800196 if (mHandleInAnimation != null && mOverlappingViews != null) {
197 for (View view : mOverlappingViews) {
198 view.startAnimation(mHandleInAnimation);
199 }
Patrick Dubroydea9e932010-09-22 15:04:29 -0700200 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800201 setVisibility(GONE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 }
203 }
204
205 private void createAnimations() {
Winson Chung760e5372010-12-15 13:14:23 -0800206 int duration = getAnimationDuration();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 if (mHandleInAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400208 mHandleInAnimation = new AlphaAnimation(0.0f, 1.0f);
Winson Chung760e5372010-12-15 13:14:23 -0800209 mHandleInAnimation.setDuration(duration);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700211
212 if (mInAnimation == null) {
213 mInAnimation = new FastAnimationSet();
214 if (!LauncherApplication.isScreenXLarge()) {
215 final AnimationSet animationSet = mInAnimation;
216 animationSet.setInterpolator(new AccelerateInterpolator());
217 animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
218 if (mOrientation == ORIENTATION_HORIZONTAL) {
219 animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
220 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
221 Animation.RELATIVE_TO_SELF, 0.0f));
222 } else {
223 animationSet.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF,
224 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
225 Animation.ABSOLUTE, 0.0f));
226 }
Winson Chung760e5372010-12-15 13:14:23 -0800227 animationSet.setDuration(duration);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 } else {
Adam Lesinski6b879f02010-11-04 16:15:23 -0700229 mInAnimation.addAnimation(mHandleInAnimation);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700232
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 if (mHandleOutAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400234 mHandleOutAnimation = new AlphaAnimation(1.0f, 0.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 mHandleOutAnimation.setFillAfter(true);
Winson Chung760e5372010-12-15 13:14:23 -0800236 mHandleOutAnimation.setDuration(duration);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700238
239 if (mOutAnimation == null) {
240 mOutAnimation = new FastAnimationSet();
241 if (!LauncherApplication.isScreenXLarge()) {
242 final AnimationSet animationSet = mOutAnimation;
243 animationSet.setInterpolator(new AccelerateInterpolator());
244 animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
245 if (mOrientation == ORIENTATION_HORIZONTAL) {
246 animationSet.addAnimation(new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
247 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
248 Animation.RELATIVE_TO_SELF, 1.0f));
249 } else {
250 animationSet.addAnimation(new FastTranslateAnimation(Animation.RELATIVE_TO_SELF,
251 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f,
252 Animation.ABSOLUTE, 0.0f));
253 }
Winson Chung760e5372010-12-15 13:14:23 -0800254 animationSet.setDuration(duration);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700255 } else {
256 mOutAnimation.addAnimation(mHandleOutAnimation);
257 }
258 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 }
260
Joe Onorato00acb122009-08-04 16:04:30 -0400261 void setDragController(DragController dragController) {
262 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 }
264
Winson Chung760e5372010-12-15 13:14:23 -0800265 private int getTransitionAnimationDuration() {
266 return LauncherApplication.isScreenXLarge() ?
267 XLARGE_TRANSITION_DURATION : TRANSITION_DURATION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 }
269
Winson Chung760e5372010-12-15 13:14:23 -0800270 private int getAnimationDuration() {
271 return LauncherApplication.isScreenXLarge() ?
272 XLARGE_ANIMATION_DURATION : ANIMATION_DURATION;
Patrick Dubroydea9e932010-09-22 15:04:29 -0700273 }
274
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 private static class FastTranslateAnimation extends TranslateAnimation {
276 public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
277 int fromYType, float fromYValue, int toYType, float toYValue) {
278 super(fromXType, fromXValue, toXType, toXValue,
279 fromYType, fromYValue, toYType, toYValue);
280 }
281
282 @Override
283 public boolean willChangeTransformationMatrix() {
284 return true;
285 }
286
287 @Override
288 public boolean willChangeBounds() {
289 return false;
290 }
291 }
292
293 private static class FastAnimationSet extends AnimationSet {
294 FastAnimationSet() {
295 super(false);
296 }
297
298 @Override
299 public boolean willChangeTransformationMatrix() {
300 return true;
301 }
302
303 @Override
304 public boolean willChangeBounds() {
305 return false;
306 }
307 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308}