blob: fdd41257f93de24bdf551249b44fae2b99361e15 [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
Winson Chungc5536bf2011-03-30 10:39:30 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Adam Cohendf2cc412011-04-27 16:56:57 -070021import android.animation.AnimatorSet;
Winson Chungc5536bf2011-03-30 10:39:30 -070022import android.animation.ObjectAnimator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.Context;
Michael Jurka577d0172010-12-17 20:04:50 -080024import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.res.TypedArray;
Joe Onorato00acb122009-08-04 16:04:30 -040026import android.graphics.PorterDuff;
27import android.graphics.PorterDuffColorFilter;
Michael Jurka3adcb0c2010-10-15 18:07:21 -070028import android.graphics.Rect;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.graphics.RectF;
30import android.graphics.drawable.TransitionDrawable;
Patrick Dubroybc6840b2010-09-01 11:54:27 -070031import android.util.AttributeSet;
Patrick Dubroybc6840b2010-09-01 11:54:27 -070032import android.view.View;
33import android.view.animation.AccelerateInterpolator;
Romain Guyedcce092010-03-04 13:03:17 -080034
Winson Chungc5536bf2011-03-30 10:39:30 -070035import com.android.launcher.R;
36
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
Winson Chungc5536bf2011-03-30 10:39:30 -070045 private AnimatorSet mInAnimation;
46 private AnimatorSet mOutAnimation;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047
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;
Michael Jurka577d0172010-12-17 20:04:50 -080054 private int mTextColor;
55 private int mDragTextColor;
Patrick Dubroydea9e932010-09-22 15:04:29 -070056
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057 public DeleteZone(Context context, AttributeSet attrs) {
58 this(context, attrs, 0);
59 }
60
61 public DeleteZone(Context context, AttributeSet attrs, int defStyle) {
62 super(context, attrs, defStyle);
63
Joe Onorato00acb122009-08-04 16:04:30 -040064 final int srcColor = context.getResources().getColor(R.color.delete_color_filter);
Winson Chung760e5372010-12-15 13:14:23 -080065 mHoverPaint.setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.SRC_ATOP));
Joe Onorato00acb122009-08-04 16:04:30 -040066
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0);
68 mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL);
69 a.recycle();
Winson Chungbe5212b2010-12-20 11:36:33 -080070
Michael Jurkaa2eb1702011-05-12 14:57:05 -070071 if (LauncherApplication.isScreenLarge()) {
Winson Chung59e1f9a2010-12-21 11:31:54 -080072 int tb = getResources().getDimensionPixelSize(
73 R.dimen.delete_zone_vertical_drag_padding);
74 int lr = getResources().getDimensionPixelSize(
75 R.dimen.delete_zone_horizontal_drag_padding);
76 setDragPadding(tb, lr, tb, lr);
77 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078 }
79
80 @Override
81 protected void onFinishInflate() {
82 super.onFinishInflate();
Michael Jurka577d0172010-12-17 20:04:50 -080083 mTransition = (TransitionDrawable) getCompoundDrawables()[LEFT_DRAWABLE];
Michael Jurkaa2eb1702011-05-12 14:57:05 -070084 if (LauncherApplication.isScreenLarge()) {
Michael Jurka577d0172010-12-17 20:04:50 -080085 mTransition.setCrossFadeEnabled(false);
86 }
87
88 Resources r = getResources();
89 mTextColor = r.getColor(R.color.workspace_all_apps_and_delete_zone_text_color);
90 mDragTextColor = r.getColor(R.color.workspace_delete_zone_drag_text_color);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 }
92
93 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040094 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 return true;
96 }
97
Joe Onorato00acb122009-08-04 16:04:30 -040098 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
99 DragView dragView, Object dragInfo) {
Adam Cohencdc30d52010-12-01 15:09:47 -0800100 if (!mDragAndDropEnabled) return;
101
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 final ItemInfo item = (ItemInfo) dragInfo;
103
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700104 // On x-large screens, you can uninstall an app by dragging from all apps
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700105 if (item instanceof ApplicationInfo && LauncherApplication.isScreenLarge()) {
Patrick Dubroy5539af72010-09-07 15:22:01 -0700106 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700107 }
108
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109 if (item.container == -1) return;
110
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700112 if (item instanceof LauncherAppWidgetInfo) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400113 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700115 } else if (source instanceof Folder) {
116 final Folder folder = (Folder) source;
117 final FolderInfo folderInfo = (FolderInfo) folder.getInfo();
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700118 // Item must be a ShortcutInfo otherwise it couldn't have been in the folder
119 // in the first place.
Adam Cohendf2cc412011-04-27 16:56:57 -0700120 folderInfo.remove((ShortcutInfo)item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700122
Adam Cohendf2cc412011-04-27 16:56:57 -0700123 if (item instanceof FolderInfo) {
124 final FolderInfo folderInfo = (FolderInfo)item;
125 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
126 mLauncher.removeFolder(folderInfo);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700127 } else if (item instanceof LauncherAppWidgetInfo) {
128 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
129 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
130 if (appWidgetHost != null) {
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700131 // Deleting an app widget ID is a void call but writes to disk before returning
132 // to the caller...
133 new Thread("deleteAppWidgetId") {
134 public void run() {
135 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
136 }
137 }.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 }
139 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700140
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 LauncherModel.deleteItemFromDatabase(mLauncher, item);
142 }
143
144 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400145 DragView dragView, Object dragInfo) {
Winson Chung760e5372010-12-15 13:14:23 -0800146 if (mDragAndDropEnabled) {
147 mTransition.reverseTransition(getTransitionAnimationDuration());
Michael Jurka577d0172010-12-17 20:04:50 -0800148 setTextColor(mDragTextColor);
Winson Chung760e5372010-12-15 13:14:23 -0800149 super.onDragEnter(source, x, y, xOffset, yOffset, dragView, dragInfo);
150 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 }
152
153 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400154 DragView dragView, Object dragInfo) {
Winson Chung760e5372010-12-15 13:14:23 -0800155 if (mDragAndDropEnabled) {
156 mTransition.reverseTransition(getTransitionAnimationDuration());
Michael Jurka577d0172010-12-17 20:04:50 -0800157 setTextColor(mTextColor);
Winson Chung760e5372010-12-15 13:14:23 -0800158 super.onDragExit(source, x, y, xOffset, yOffset, dragView, dragInfo);
159 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 }
161
Joe Onorato5162ea92009-09-03 09:39:42 -0700162 public void onDragStart(DragSource source, Object info, int dragAction) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 final ItemInfo item = (ItemInfo) info;
Adam Cohencdc30d52010-12-01 15:09:47 -0800164 if (item != null && mDragAndDropEnabled) {
Winson Chung760e5372010-12-15 13:14:23 -0800165 mActive = true;
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700166 getHitRect(mRegion);
167 mRegionF.set(mRegion);
168
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700169 if (LauncherApplication.isScreenLarge()) {
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700170 // This region will be a "dead zone" for scrolling; make it extend to the edge of
171 // the screen so users don't accidentally trigger a scroll while deleting items
172 mRegionF.top = mLauncher.getWorkspace().getTop();
173 mRegionF.right = mLauncher.getWorkspace().getRight();
174 }
175
176 mDragController.setDeleteRegion(mRegionF);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700177
178 // Make sure the icon is set to the default drawable, not the hover drawable
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 mTransition.resetTransition();
Patrick Dubroydea9e932010-09-22 15:04:29 -0700180
Adam Cohencdc30d52010-12-01 15:09:47 -0800181 createAnimations();
Winson Chungc5536bf2011-03-30 10:39:30 -0700182 mInAnimation.start();
Michael Jurkab8e14472010-12-20 16:06:10 -0800183 if (mOverlappingViews != null) {
184 for (View view : mOverlappingViews) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700185 createOutAlphaAnim(view).start();
Michael Jurkab8e14472010-12-20 16:06:10 -0800186 }
Patrick Dubroydea9e932010-09-22 15:04:29 -0700187 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800188 setVisibility(VISIBLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 }
190 }
191
192 public void onDragEnd() {
Winson Chung760e5372010-12-15 13:14:23 -0800193 if (mActive && mDragAndDropEnabled) {
194 mActive = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400195 mDragController.setDeleteRegion(null);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700196
Winson Chungc5536bf2011-03-30 10:39:30 -0700197 mOutAnimation.start();
198 if (mOverlappingViews != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -0800199 for (View view : mOverlappingViews) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700200 createInAlphaAnim(view).start();
Michael Jurkab8e14472010-12-20 16:06:10 -0800201 }
Patrick Dubroydea9e932010-09-22 15:04:29 -0700202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 }
204 }
205
Winson Chungc5536bf2011-03-30 10:39:30 -0700206 private Animator createAlphaAnim(View v, float start, float end) {
207 Animator anim = ObjectAnimator.ofFloat(v, "alpha", start, end);
208 anim.setDuration(getAnimationDuration());
209 return anim;
210 }
211 private Animator createInAlphaAnim(View v) {
212 return createAlphaAnim(v, 0f, 1f);
213 }
214 private Animator createOutAlphaAnim(View v) {
215 return createAlphaAnim(v, 1f, 0f);
216 }
217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 private void createAnimations() {
Winson Chung760e5372010-12-15 13:14:23 -0800219 int duration = getAnimationDuration();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700220
Winson Chungc5536bf2011-03-30 10:39:30 -0700221 Animator inAlphaAnim = createInAlphaAnim(this);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700222 if (mInAnimation == null) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700223 mInAnimation = new AnimatorSet();
224 mInAnimation.setInterpolator(new AccelerateInterpolator());
225 mInAnimation.setDuration(duration);
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700226 if (!LauncherApplication.isScreenLarge()) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700227 Animator translateAnim;
Adam Lesinski6b879f02010-11-04 16:15:23 -0700228 if (mOrientation == ORIENTATION_HORIZONTAL) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700229 translateAnim = ObjectAnimator.ofFloat(this, "translationY",
230 getMeasuredWidth(), 0f);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700231 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700232 translateAnim = ObjectAnimator.ofFloat(this, "translationX",
233 getMeasuredHeight(), 0f);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700234 }
Winson Chungc5536bf2011-03-30 10:39:30 -0700235 mInAnimation.playTogether(translateAnim, inAlphaAnim);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700237 mInAnimation.play(inAlphaAnim);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700240
Winson Chungc5536bf2011-03-30 10:39:30 -0700241 Animator outAlphaAnim = createOutAlphaAnim(this);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700242 if (mOutAnimation == null) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700243 mOutAnimation = new AnimatorSet();
244 mOutAnimation.setInterpolator(new AccelerateInterpolator());
245 mOutAnimation.setDuration(duration);
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700246 if (!LauncherApplication.isScreenLarge()) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700247 Animator translateAnim;
Adam Lesinski6b879f02010-11-04 16:15:23 -0700248 if (mOrientation == ORIENTATION_HORIZONTAL) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700249 translateAnim = ObjectAnimator.ofFloat(this, "translationY", 0f,
250 getMeasuredWidth());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700251 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700252 translateAnim = ObjectAnimator.ofFloat(this, "translationX", 0f,
253 getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700254 }
Winson Chungc5536bf2011-03-30 10:39:30 -0700255 mOutAnimation.playTogether(translateAnim, outAlphaAnim);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700256 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700257 mOutAnimation.addListener(new AnimatorListenerAdapter() {
258 public void onAnimationEnd(Animator animation) {
259 setVisibility(GONE);
260 }
261 });
262 mOutAnimation.play(outAlphaAnim);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700263 }
264 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 }
266
Joe Onorato00acb122009-08-04 16:04:30 -0400267 void setDragController(DragController dragController) {
268 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 }
270
Winson Chung760e5372010-12-15 13:14:23 -0800271 private int getTransitionAnimationDuration() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700272 return LauncherApplication.isScreenLarge() ?
Winson Chung760e5372010-12-15 13:14:23 -0800273 XLARGE_TRANSITION_DURATION : TRANSITION_DURATION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 }
275
Winson Chung760e5372010-12-15 13:14:23 -0800276 private int getAnimationDuration() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700277 return LauncherApplication.isScreenLarge() ?
Winson Chung760e5372010-12-15 13:14:23 -0800278 XLARGE_ANIMATION_DURATION : ANIMATION_DURATION;
Patrick Dubroydea9e932010-09-22 15:04:29 -0700279 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280}