blob: 773bc6b7dd060a5b86bb47f991ef02e4b00cc736 [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
Joe Onorato00acb122009-08-04 16:04:30 -040062 final int srcColor = context.getResources().getColor(R.color.delete_color_filter);
Winson Chung760e5372010-12-15 13:14:23 -080063 mHoverPaint.setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.SRC_ATOP));
Joe Onorato00acb122009-08-04 16:04:30 -040064
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0);
66 mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL);
67 a.recycle();
Winson Chungbe5212b2010-12-20 11:36:33 -080068
69 int tb = getResources().getDimensionPixelSize(R.dimen.delete_zone_vertical_drag_padding);
70 int lr = getResources().getDimensionPixelSize(R.dimen.delete_zone_horizontal_drag_padding);
71 setDragPadding(tb, lr, tb, lr);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072 }
73
74 @Override
75 protected void onFinishInflate() {
76 super.onFinishInflate();
Joe Onoratoa9c28f62009-09-14 18:38:49 -040077 mTransition = (TransitionDrawable) getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078 }
79
80 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040081 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082 return true;
83 }
84
Joe Onorato00acb122009-08-04 16:04:30 -040085 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
86 DragView dragView, Object dragInfo) {
Adam Cohencdc30d52010-12-01 15:09:47 -080087 if (!mDragAndDropEnabled) return;
88
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 final ItemInfo item = (ItemInfo) dragInfo;
90
Patrick Dubroybc6840b2010-09-01 11:54:27 -070091 // On x-large screens, you can uninstall an app by dragging from all apps
92 if (item instanceof ApplicationInfo && LauncherApplication.isScreenXLarge()) {
Patrick Dubroy5539af72010-09-07 15:22:01 -070093 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Patrick Dubroybc6840b2010-09-01 11:54:27 -070094 }
95
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 if (item.container == -1) return;
97
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -070099 if (item instanceof LauncherAppWidgetInfo) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400100 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700102 } else if (source instanceof UserFolder) {
103 final UserFolder userFolder = (UserFolder) source;
104 final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder.getInfo();
105 // Item must be a ShortcutInfo otherwise it couldn't have been in the folder
106 // in the first place.
107 userFolderInfo.remove((ShortcutInfo)item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 if (item instanceof UserFolderInfo) {
111 final UserFolderInfo userFolderInfo = (UserFolderInfo)item;
112 LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher, userFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400113 mLauncher.removeFolder(userFolderInfo);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700114 } else if (item instanceof LauncherAppWidgetInfo) {
115 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
116 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
117 if (appWidgetHost != null) {
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700118 final int appWidgetId = launcherAppWidgetInfo.appWidgetId;
119 // Deleting an app widget ID is a void call but writes to disk before returning
120 // to the caller...
121 new Thread("deleteAppWidgetId") {
122 public void run() {
123 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
124 }
125 }.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 }
127 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 LauncherModel.deleteItemFromDatabase(mLauncher, item);
130 }
131
132 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400133 DragView dragView, Object dragInfo) {
Winson Chung760e5372010-12-15 13:14:23 -0800134 if (mDragAndDropEnabled) {
135 mTransition.reverseTransition(getTransitionAnimationDuration());
136 super.onDragEnter(source, x, y, xOffset, yOffset, dragView, dragInfo);
137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 }
139
140 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400141 DragView dragView, Object dragInfo) {
Winson Chung760e5372010-12-15 13:14:23 -0800142 if (mDragAndDropEnabled) {
143 mTransition.reverseTransition(getTransitionAnimationDuration());
144 super.onDragExit(source, x, y, xOffset, yOffset, dragView, dragInfo);
145 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 }
147
Joe Onorato5162ea92009-09-03 09:39:42 -0700148 public void onDragStart(DragSource source, Object info, int dragAction) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 final ItemInfo item = (ItemInfo) info;
Adam Cohencdc30d52010-12-01 15:09:47 -0800150 if (item != null && mDragAndDropEnabled) {
Winson Chung760e5372010-12-15 13:14:23 -0800151 mActive = true;
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700152 getHitRect(mRegion);
153 mRegionF.set(mRegion);
154
155 if (LauncherApplication.isScreenXLarge()) {
156 // This region will be a "dead zone" for scrolling; make it extend to the edge of
157 // the screen so users don't accidentally trigger a scroll while deleting items
158 mRegionF.top = mLauncher.getWorkspace().getTop();
159 mRegionF.right = mLauncher.getWorkspace().getRight();
160 }
161
162 mDragController.setDeleteRegion(mRegionF);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700163
164 // Make sure the icon is set to the default drawable, not the hover drawable
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 mTransition.resetTransition();
Patrick Dubroydea9e932010-09-22 15:04:29 -0700166
Adam Cohencdc30d52010-12-01 15:09:47 -0800167 createAnimations();
168 startAnimation(mInAnimation);
169 if (mHandle != null) {
Patrick Dubroydea9e932010-09-22 15:04:29 -0700170 mHandle.startAnimation(mHandleOutAnimation);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700171 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800172 setVisibility(VISIBLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 }
174 }
175
176 public void onDragEnd() {
Winson Chung760e5372010-12-15 13:14:23 -0800177 if (mActive && mDragAndDropEnabled) {
178 mActive = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400179 mDragController.setDeleteRegion(null);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700180
181 if (mOutAnimation != null) startAnimation(mOutAnimation);
Adam Cohencdc30d52010-12-01 15:09:47 -0800182 if (mHandleInAnimation != null && mHandle != null) {
183 mHandle.startAnimation(mHandleInAnimation);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700184 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800185 setVisibility(GONE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 }
187 }
188
189 private void createAnimations() {
Winson Chung760e5372010-12-15 13:14:23 -0800190 int duration = getAnimationDuration();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 if (mHandleInAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400192 mHandleInAnimation = new AlphaAnimation(0.0f, 1.0f);
Winson Chung760e5372010-12-15 13:14:23 -0800193 mHandleInAnimation.setDuration(duration);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700195
196 if (mInAnimation == null) {
197 mInAnimation = new FastAnimationSet();
198 if (!LauncherApplication.isScreenXLarge()) {
199 final AnimationSet animationSet = mInAnimation;
200 animationSet.setInterpolator(new AccelerateInterpolator());
201 animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
202 if (mOrientation == ORIENTATION_HORIZONTAL) {
203 animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
204 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
205 Animation.RELATIVE_TO_SELF, 0.0f));
206 } else {
207 animationSet.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF,
208 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
209 Animation.ABSOLUTE, 0.0f));
210 }
Winson Chung760e5372010-12-15 13:14:23 -0800211 animationSet.setDuration(duration);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 } else {
Adam Lesinski6b879f02010-11-04 16:15:23 -0700213 mInAnimation.addAnimation(mHandleInAnimation);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 if (mHandleOutAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400218 mHandleOutAnimation = new AlphaAnimation(1.0f, 0.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 mHandleOutAnimation.setFillAfter(true);
Winson Chung760e5372010-12-15 13:14:23 -0800220 mHandleOutAnimation.setDuration(duration);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700222
223 if (mOutAnimation == null) {
224 mOutAnimation = new FastAnimationSet();
225 if (!LauncherApplication.isScreenXLarge()) {
226 final AnimationSet animationSet = mOutAnimation;
227 animationSet.setInterpolator(new AccelerateInterpolator());
228 animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
229 if (mOrientation == ORIENTATION_HORIZONTAL) {
230 animationSet.addAnimation(new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
231 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
232 Animation.RELATIVE_TO_SELF, 1.0f));
233 } else {
234 animationSet.addAnimation(new FastTranslateAnimation(Animation.RELATIVE_TO_SELF,
235 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f,
236 Animation.ABSOLUTE, 0.0f));
237 }
Winson Chung760e5372010-12-15 13:14:23 -0800238 animationSet.setDuration(duration);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700239 } else {
240 mOutAnimation.addAnimation(mHandleOutAnimation);
241 }
242 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 }
244
Joe Onorato00acb122009-08-04 16:04:30 -0400245 void setDragController(DragController dragController) {
246 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 }
248
Winson Chung760e5372010-12-15 13:14:23 -0800249 private int getTransitionAnimationDuration() {
250 return LauncherApplication.isScreenXLarge() ?
251 XLARGE_TRANSITION_DURATION : TRANSITION_DURATION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 }
253
Winson Chung760e5372010-12-15 13:14:23 -0800254 private int getAnimationDuration() {
255 return LauncherApplication.isScreenXLarge() ?
256 XLARGE_ANIMATION_DURATION : ANIMATION_DURATION;
Patrick Dubroydea9e932010-09-22 15:04:29 -0700257 }
258
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 private static class FastTranslateAnimation extends TranslateAnimation {
260 public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
261 int fromYType, float fromYValue, int toYType, float toYValue) {
262 super(fromXType, fromXValue, toXType, toXValue,
263 fromYType, fromYValue, toYType, toYValue);
264 }
265
266 @Override
267 public boolean willChangeTransformationMatrix() {
268 return true;
269 }
270
271 @Override
272 public boolean willChangeBounds() {
273 return false;
274 }
275 }
276
277 private static class FastAnimationSet extends AnimationSet {
278 FastAnimationSet() {
279 super(false);
280 }
281
282 @Override
283 public boolean willChangeTransformationMatrix() {
284 return true;
285 }
286
287 @Override
288 public boolean willChangeBounds() {
289 return false;
290 }
291 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292}