blob: 5d9b5db2de6ad49cdbe906dd6a29b01347154bd4 [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 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700116
Adam Cohendf2cc412011-04-27 16:56:57 -0700117 if (item instanceof FolderInfo) {
118 final FolderInfo folderInfo = (FolderInfo)item;
119 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
120 mLauncher.removeFolder(folderInfo);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700121 } else if (item instanceof LauncherAppWidgetInfo) {
122 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
123 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
124 if (appWidgetHost != null) {
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700125 // Deleting an app widget ID is a void call but writes to disk before returning
126 // to the caller...
127 new Thread("deleteAppWidgetId") {
128 public void run() {
129 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
130 }
131 }.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 }
133 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 LauncherModel.deleteItemFromDatabase(mLauncher, item);
136 }
137
138 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400139 DragView dragView, Object dragInfo) {
Winson Chung760e5372010-12-15 13:14:23 -0800140 if (mDragAndDropEnabled) {
141 mTransition.reverseTransition(getTransitionAnimationDuration());
Michael Jurka577d0172010-12-17 20:04:50 -0800142 setTextColor(mDragTextColor);
Winson Chung760e5372010-12-15 13:14:23 -0800143 super.onDragEnter(source, x, y, xOffset, yOffset, dragView, dragInfo);
144 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 }
146
147 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400148 DragView dragView, Object dragInfo) {
Winson Chung760e5372010-12-15 13:14:23 -0800149 if (mDragAndDropEnabled) {
150 mTransition.reverseTransition(getTransitionAnimationDuration());
Michael Jurka577d0172010-12-17 20:04:50 -0800151 setTextColor(mTextColor);
Winson Chung760e5372010-12-15 13:14:23 -0800152 super.onDragExit(source, x, y, xOffset, yOffset, dragView, dragInfo);
153 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 }
155
Joe Onorato5162ea92009-09-03 09:39:42 -0700156 public void onDragStart(DragSource source, Object info, int dragAction) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 final ItemInfo item = (ItemInfo) info;
Adam Cohencdc30d52010-12-01 15:09:47 -0800158 if (item != null && mDragAndDropEnabled) {
Winson Chung760e5372010-12-15 13:14:23 -0800159 mActive = true;
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700160 getHitRect(mRegion);
161 mRegionF.set(mRegion);
162
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700163 if (LauncherApplication.isScreenLarge()) {
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700164 // This region will be a "dead zone" for scrolling; make it extend to the edge of
165 // the screen so users don't accidentally trigger a scroll while deleting items
166 mRegionF.top = mLauncher.getWorkspace().getTop();
167 mRegionF.right = mLauncher.getWorkspace().getRight();
168 }
169
170 mDragController.setDeleteRegion(mRegionF);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700171
172 // Make sure the icon is set to the default drawable, not the hover drawable
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 mTransition.resetTransition();
Patrick Dubroydea9e932010-09-22 15:04:29 -0700174
Adam Cohencdc30d52010-12-01 15:09:47 -0800175 createAnimations();
Winson Chungc5536bf2011-03-30 10:39:30 -0700176 mInAnimation.start();
Michael Jurkab8e14472010-12-20 16:06:10 -0800177 if (mOverlappingViews != null) {
178 for (View view : mOverlappingViews) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700179 createOutAlphaAnim(view).start();
Michael Jurkab8e14472010-12-20 16:06:10 -0800180 }
Patrick Dubroydea9e932010-09-22 15:04:29 -0700181 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800182 setVisibility(VISIBLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 }
184 }
185
186 public void onDragEnd() {
Winson Chung760e5372010-12-15 13:14:23 -0800187 if (mActive && mDragAndDropEnabled) {
188 mActive = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400189 mDragController.setDeleteRegion(null);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700190
Winson Chungc5536bf2011-03-30 10:39:30 -0700191 mOutAnimation.start();
192 if (mOverlappingViews != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -0800193 for (View view : mOverlappingViews) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700194 createInAlphaAnim(view).start();
Michael Jurkab8e14472010-12-20 16:06:10 -0800195 }
Patrick Dubroydea9e932010-09-22 15:04:29 -0700196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 }
198 }
199
Winson Chungc5536bf2011-03-30 10:39:30 -0700200 private Animator createAlphaAnim(View v, float start, float end) {
201 Animator anim = ObjectAnimator.ofFloat(v, "alpha", start, end);
202 anim.setDuration(getAnimationDuration());
203 return anim;
204 }
205 private Animator createInAlphaAnim(View v) {
206 return createAlphaAnim(v, 0f, 1f);
207 }
208 private Animator createOutAlphaAnim(View v) {
209 return createAlphaAnim(v, 1f, 0f);
210 }
211
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 private void createAnimations() {
Winson Chung760e5372010-12-15 13:14:23 -0800213 int duration = getAnimationDuration();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700214
Winson Chungc5536bf2011-03-30 10:39:30 -0700215 Animator inAlphaAnim = createInAlphaAnim(this);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700216 if (mInAnimation == null) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700217 mInAnimation = new AnimatorSet();
218 mInAnimation.setInterpolator(new AccelerateInterpolator());
219 mInAnimation.setDuration(duration);
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700220 if (!LauncherApplication.isScreenLarge()) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700221 Animator translateAnim;
Adam Lesinski6b879f02010-11-04 16:15:23 -0700222 if (mOrientation == ORIENTATION_HORIZONTAL) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700223 translateAnim = ObjectAnimator.ofFloat(this, "translationY",
224 getMeasuredWidth(), 0f);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700225 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700226 translateAnim = ObjectAnimator.ofFloat(this, "translationX",
227 getMeasuredHeight(), 0f);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700228 }
Winson Chungc5536bf2011-03-30 10:39:30 -0700229 mInAnimation.playTogether(translateAnim, inAlphaAnim);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700231 mInAnimation.play(inAlphaAnim);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700234
Winson Chungc5536bf2011-03-30 10:39:30 -0700235 Animator outAlphaAnim = createOutAlphaAnim(this);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700236 if (mOutAnimation == null) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700237 mOutAnimation = new AnimatorSet();
238 mOutAnimation.setInterpolator(new AccelerateInterpolator());
239 mOutAnimation.setDuration(duration);
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700240 if (!LauncherApplication.isScreenLarge()) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700241 Animator translateAnim;
Adam Lesinski6b879f02010-11-04 16:15:23 -0700242 if (mOrientation == ORIENTATION_HORIZONTAL) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700243 translateAnim = ObjectAnimator.ofFloat(this, "translationY", 0f,
244 getMeasuredWidth());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700245 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700246 translateAnim = ObjectAnimator.ofFloat(this, "translationX", 0f,
247 getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700248 }
Winson Chungc5536bf2011-03-30 10:39:30 -0700249 mOutAnimation.playTogether(translateAnim, outAlphaAnim);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700250 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700251 mOutAnimation.addListener(new AnimatorListenerAdapter() {
252 public void onAnimationEnd(Animator animation) {
253 setVisibility(GONE);
254 }
255 });
256 mOutAnimation.play(outAlphaAnim);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700257 }
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() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700266 return LauncherApplication.isScreenLarge() ?
Winson Chung760e5372010-12-15 13:14:23 -0800267 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() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700271 return LauncherApplication.isScreenLarge() ?
Winson Chung760e5372010-12-15 13:14:23 -0800272 XLARGE_ANIMATION_DURATION : ANIMATION_DURATION;
Patrick Dubroydea9e932010-09-22 15:04:29 -0700273 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274}