blob: 98d2b8366145b52655c133b1ddcc6e7528fe6fd8 [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
Winson Chung59e1f9a2010-12-21 11:31:54 -080073 if (LauncherApplication.isScreenXLarge()) {
74 int tb = getResources().getDimensionPixelSize(
75 R.dimen.delete_zone_vertical_drag_padding);
76 int lr = getResources().getDimensionPixelSize(
77 R.dimen.delete_zone_horizontal_drag_padding);
78 setDragPadding(tb, lr, tb, lr);
79 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080 }
81
82 @Override
83 protected void onFinishInflate() {
84 super.onFinishInflate();
Michael Jurka577d0172010-12-17 20:04:50 -080085 mTransition = (TransitionDrawable) getCompoundDrawables()[LEFT_DRAWABLE];
86 if (LauncherApplication.isScreenXLarge()) {
87 mTransition.setCrossFadeEnabled(false);
88 }
89
90 Resources r = getResources();
91 mTextColor = r.getColor(R.color.workspace_all_apps_and_delete_zone_text_color);
92 mDragTextColor = r.getColor(R.color.workspace_delete_zone_drag_text_color);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 }
94
95 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040096 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 return true;
98 }
99
Joe Onorato00acb122009-08-04 16:04:30 -0400100 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
101 DragView dragView, Object dragInfo) {
Adam Cohencdc30d52010-12-01 15:09:47 -0800102 if (!mDragAndDropEnabled) return;
103
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 final ItemInfo item = (ItemInfo) dragInfo;
105
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700106 // On x-large screens, you can uninstall an app by dragging from all apps
107 if (item instanceof ApplicationInfo && LauncherApplication.isScreenXLarge()) {
Patrick Dubroy5539af72010-09-07 15:22:01 -0700108 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700109 }
110
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 if (item.container == -1) return;
112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700114 if (item instanceof LauncherAppWidgetInfo) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400115 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700117 } else if (source instanceof UserFolder) {
118 final UserFolder userFolder = (UserFolder) source;
119 final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder.getInfo();
120 // Item must be a ShortcutInfo otherwise it couldn't have been in the folder
121 // in the first place.
122 userFolderInfo.remove((ShortcutInfo)item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700124
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 if (item instanceof UserFolderInfo) {
126 final UserFolderInfo userFolderInfo = (UserFolderInfo)item;
127 LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher, userFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400128 mLauncher.removeFolder(userFolderInfo);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700129 } else if (item instanceof LauncherAppWidgetInfo) {
130 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
131 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
132 if (appWidgetHost != null) {
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700133 // Deleting an app widget ID is a void call but writes to disk before returning
134 // to the caller...
135 new Thread("deleteAppWidgetId") {
136 public void run() {
137 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
138 }
139 }.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 }
141 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700142
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 LauncherModel.deleteItemFromDatabase(mLauncher, item);
144 }
145
146 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400147 DragView dragView, Object dragInfo) {
Winson Chung760e5372010-12-15 13:14:23 -0800148 if (mDragAndDropEnabled) {
149 mTransition.reverseTransition(getTransitionAnimationDuration());
Michael Jurka577d0172010-12-17 20:04:50 -0800150 setTextColor(mDragTextColor);
Winson Chung760e5372010-12-15 13:14:23 -0800151 super.onDragEnter(source, x, y, xOffset, yOffset, dragView, dragInfo);
152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 }
154
155 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400156 DragView dragView, Object dragInfo) {
Winson Chung760e5372010-12-15 13:14:23 -0800157 if (mDragAndDropEnabled) {
158 mTransition.reverseTransition(getTransitionAnimationDuration());
Michael Jurka577d0172010-12-17 20:04:50 -0800159 setTextColor(mTextColor);
Winson Chung760e5372010-12-15 13:14:23 -0800160 super.onDragExit(source, x, y, xOffset, yOffset, dragView, dragInfo);
161 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 }
163
Joe Onorato5162ea92009-09-03 09:39:42 -0700164 public void onDragStart(DragSource source, Object info, int dragAction) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 final ItemInfo item = (ItemInfo) info;
Adam Cohencdc30d52010-12-01 15:09:47 -0800166 if (item != null && mDragAndDropEnabled) {
Winson Chung760e5372010-12-15 13:14:23 -0800167 mActive = true;
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700168 getHitRect(mRegion);
169 mRegionF.set(mRegion);
170
171 if (LauncherApplication.isScreenXLarge()) {
172 // This region will be a "dead zone" for scrolling; make it extend to the edge of
173 // the screen so users don't accidentally trigger a scroll while deleting items
174 mRegionF.top = mLauncher.getWorkspace().getTop();
175 mRegionF.right = mLauncher.getWorkspace().getRight();
176 }
177
178 mDragController.setDeleteRegion(mRegionF);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700179
180 // Make sure the icon is set to the default drawable, not the hover drawable
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 mTransition.resetTransition();
Patrick Dubroydea9e932010-09-22 15:04:29 -0700182
Adam Cohencdc30d52010-12-01 15:09:47 -0800183 createAnimations();
184 startAnimation(mInAnimation);
Michael Jurkab8e14472010-12-20 16:06:10 -0800185 if (mOverlappingViews != null) {
186 for (View view : mOverlappingViews) {
187 view.startAnimation(mHandleOutAnimation);
188 }
Patrick Dubroydea9e932010-09-22 15:04:29 -0700189 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800190 setVisibility(VISIBLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 }
192 }
193
194 public void onDragEnd() {
Winson Chung760e5372010-12-15 13:14:23 -0800195 if (mActive && mDragAndDropEnabled) {
196 mActive = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400197 mDragController.setDeleteRegion(null);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700198
199 if (mOutAnimation != null) startAnimation(mOutAnimation);
Michael Jurkab8e14472010-12-20 16:06:10 -0800200 if (mHandleInAnimation != null && mOverlappingViews != null) {
201 for (View view : mOverlappingViews) {
202 view.startAnimation(mHandleInAnimation);
203 }
Patrick Dubroydea9e932010-09-22 15:04:29 -0700204 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800205 setVisibility(GONE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 }
207 }
208
209 private void createAnimations() {
Winson Chung760e5372010-12-15 13:14:23 -0800210 int duration = getAnimationDuration();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 if (mHandleInAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400212 mHandleInAnimation = new AlphaAnimation(0.0f, 1.0f);
Winson Chung760e5372010-12-15 13:14:23 -0800213 mHandleInAnimation.setDuration(duration);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700215
216 if (mInAnimation == null) {
217 mInAnimation = new FastAnimationSet();
218 if (!LauncherApplication.isScreenXLarge()) {
219 final AnimationSet animationSet = mInAnimation;
220 animationSet.setInterpolator(new AccelerateInterpolator());
221 animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
222 if (mOrientation == ORIENTATION_HORIZONTAL) {
223 animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
224 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
225 Animation.RELATIVE_TO_SELF, 0.0f));
226 } else {
227 animationSet.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF,
228 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
229 Animation.ABSOLUTE, 0.0f));
230 }
Winson Chung760e5372010-12-15 13:14:23 -0800231 animationSet.setDuration(duration);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 } else {
Adam Lesinski6b879f02010-11-04 16:15:23 -0700233 mInAnimation.addAnimation(mHandleInAnimation);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700236
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 if (mHandleOutAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400238 mHandleOutAnimation = new AlphaAnimation(1.0f, 0.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 mHandleOutAnimation.setFillAfter(true);
Winson Chung760e5372010-12-15 13:14:23 -0800240 mHandleOutAnimation.setDuration(duration);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700242
243 if (mOutAnimation == null) {
244 mOutAnimation = new FastAnimationSet();
245 if (!LauncherApplication.isScreenXLarge()) {
246 final AnimationSet animationSet = mOutAnimation;
247 animationSet.setInterpolator(new AccelerateInterpolator());
248 animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
249 if (mOrientation == ORIENTATION_HORIZONTAL) {
250 animationSet.addAnimation(new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
251 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
252 Animation.RELATIVE_TO_SELF, 1.0f));
253 } else {
254 animationSet.addAnimation(new FastTranslateAnimation(Animation.RELATIVE_TO_SELF,
255 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f,
256 Animation.ABSOLUTE, 0.0f));
257 }
Winson Chung760e5372010-12-15 13:14:23 -0800258 animationSet.setDuration(duration);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700259 } else {
260 mOutAnimation.addAnimation(mHandleOutAnimation);
261 }
262 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 }
264
Joe Onorato00acb122009-08-04 16:04:30 -0400265 void setDragController(DragController dragController) {
266 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 }
268
Winson Chung760e5372010-12-15 13:14:23 -0800269 private int getTransitionAnimationDuration() {
270 return LauncherApplication.isScreenXLarge() ?
271 XLARGE_TRANSITION_DURATION : TRANSITION_DURATION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 }
273
Winson Chung760e5372010-12-15 13:14:23 -0800274 private int getAnimationDuration() {
275 return LauncherApplication.isScreenXLarge() ?
276 XLARGE_ANIMATION_DURATION : ANIMATION_DURATION;
Patrick Dubroydea9e932010-09-22 15:04:29 -0700277 }
278
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 private static class FastTranslateAnimation extends TranslateAnimation {
280 public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
281 int fromYType, float fromYValue, int toYType, float toYValue) {
282 super(fromXType, fromXValue, toXType, toXValue,
283 fromYType, fromYValue, toYType, toYValue);
284 }
285
286 @Override
287 public boolean willChangeTransformationMatrix() {
288 return true;
289 }
290
291 @Override
292 public boolean willChangeBounds() {
293 return false;
294 }
295 }
296
297 private static class FastAnimationSet extends AnimationSet {
298 FastAnimationSet() {
299 super(false);
300 }
301
302 @Override
303 public boolean willChangeTransformationMatrix() {
304 return true;
305 }
306
307 @Override
308 public boolean willChangeBounds() {
309 return false;
310 }
311 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312}