blob: 025b292ff6860521d21882c9f795fe73814ca924 [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
Winson Chung4c98d922011-05-31 16:50:48 -070064 final int srcColor = context.getResources().getColor(R.color.delete_target_hover_tint);
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
Adam Cohencb3382b2011-05-24 14:07:08 -070093 public boolean acceptDrop(DragObject d) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 return true;
95 }
96
Adam Cohencb3382b2011-05-24 14:07:08 -070097 public void onDrop(DragObject d) {
Adam Cohencdc30d52010-12-01 15:09:47 -080098 if (!mDragAndDropEnabled) return;
99
Adam Cohencb3382b2011-05-24 14:07:08 -0700100 final ItemInfo item = (ItemInfo) d.dragInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700102 // On x-large screens, you can uninstall an app by dragging from all apps
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700103 if (item instanceof ApplicationInfo && LauncherApplication.isScreenLarge()) {
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 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700114
Adam Cohendf2cc412011-04-27 16:56:57 -0700115 if (item instanceof FolderInfo) {
116 final FolderInfo folderInfo = (FolderInfo)item;
117 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
118 mLauncher.removeFolder(folderInfo);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700119 } else if (item instanceof LauncherAppWidgetInfo) {
120 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
121 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
122 if (appWidgetHost != null) {
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700123 // Deleting an app widget ID is a void call but writes to disk before returning
124 // to the caller...
125 new Thread("deleteAppWidgetId") {
126 public void run() {
127 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
128 }
129 }.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 }
131 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700132
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 LauncherModel.deleteItemFromDatabase(mLauncher, item);
134 }
135
Adam Cohencb3382b2011-05-24 14:07:08 -0700136 public void onDragEnter(DragObject d) {
Winson Chung760e5372010-12-15 13:14:23 -0800137 if (mDragAndDropEnabled) {
138 mTransition.reverseTransition(getTransitionAnimationDuration());
Michael Jurka577d0172010-12-17 20:04:50 -0800139 setTextColor(mDragTextColor);
Adam Cohencb3382b2011-05-24 14:07:08 -0700140 super.onDragEnter(d);
Winson Chung760e5372010-12-15 13:14:23 -0800141 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 }
143
Adam Cohencb3382b2011-05-24 14:07:08 -0700144 public void onDragExit(DragObject d) {
Winson Chung760e5372010-12-15 13:14:23 -0800145 if (mDragAndDropEnabled) {
146 mTransition.reverseTransition(getTransitionAnimationDuration());
Michael Jurka577d0172010-12-17 20:04:50 -0800147 setTextColor(mTextColor);
Adam Cohencb3382b2011-05-24 14:07:08 -0700148 super.onDragExit(d);
Winson Chung760e5372010-12-15 13:14:23 -0800149 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 }
151
Joe Onorato5162ea92009-09-03 09:39:42 -0700152 public void onDragStart(DragSource source, Object info, int dragAction) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 final ItemInfo item = (ItemInfo) info;
Adam Cohencdc30d52010-12-01 15:09:47 -0800154 if (item != null && mDragAndDropEnabled) {
Winson Chung760e5372010-12-15 13:14:23 -0800155 mActive = true;
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700156 getHitRect(mRegion);
157 mRegionF.set(mRegion);
158
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700159 if (LauncherApplication.isScreenLarge()) {
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700160 // This region will be a "dead zone" for scrolling; make it extend to the edge of
161 // the screen so users don't accidentally trigger a scroll while deleting items
162 mRegionF.top = mLauncher.getWorkspace().getTop();
163 mRegionF.right = mLauncher.getWorkspace().getRight();
164 }
165
166 mDragController.setDeleteRegion(mRegionF);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700167
168 // Make sure the icon is set to the default drawable, not the hover drawable
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 mTransition.resetTransition();
Patrick Dubroydea9e932010-09-22 15:04:29 -0700170
Adam Cohencdc30d52010-12-01 15:09:47 -0800171 createAnimations();
Winson Chungc5536bf2011-03-30 10:39:30 -0700172 mInAnimation.start();
Michael Jurkab8e14472010-12-20 16:06:10 -0800173 if (mOverlappingViews != null) {
174 for (View view : mOverlappingViews) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700175 createOutAlphaAnim(view).start();
Michael Jurkab8e14472010-12-20 16:06:10 -0800176 }
Patrick Dubroydea9e932010-09-22 15:04:29 -0700177 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800178 setVisibility(VISIBLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 }
180 }
181
182 public void onDragEnd() {
Winson Chung760e5372010-12-15 13:14:23 -0800183 if (mActive && mDragAndDropEnabled) {
184 mActive = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400185 mDragController.setDeleteRegion(null);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700186
Winson Chungc5536bf2011-03-30 10:39:30 -0700187 mOutAnimation.start();
188 if (mOverlappingViews != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -0800189 for (View view : mOverlappingViews) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700190 createInAlphaAnim(view).start();
Michael Jurkab8e14472010-12-20 16:06:10 -0800191 }
Patrick Dubroydea9e932010-09-22 15:04:29 -0700192 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 }
194 }
195
Winson Chungc5536bf2011-03-30 10:39:30 -0700196 private Animator createAlphaAnim(View v, float start, float end) {
197 Animator anim = ObjectAnimator.ofFloat(v, "alpha", start, end);
198 anim.setDuration(getAnimationDuration());
199 return anim;
200 }
201 private Animator createInAlphaAnim(View v) {
202 return createAlphaAnim(v, 0f, 1f);
203 }
204 private Animator createOutAlphaAnim(View v) {
205 return createAlphaAnim(v, 1f, 0f);
206 }
207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private void createAnimations() {
Winson Chung760e5372010-12-15 13:14:23 -0800209 int duration = getAnimationDuration();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700210
Winson Chungc5536bf2011-03-30 10:39:30 -0700211 Animator inAlphaAnim = createInAlphaAnim(this);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700212 if (mInAnimation == null) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700213 mInAnimation = new AnimatorSet();
214 mInAnimation.setInterpolator(new AccelerateInterpolator());
215 mInAnimation.setDuration(duration);
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700216 if (!LauncherApplication.isScreenLarge()) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700217 Animator translateAnim;
Adam Lesinski6b879f02010-11-04 16:15:23 -0700218 if (mOrientation == ORIENTATION_HORIZONTAL) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700219 translateAnim = ObjectAnimator.ofFloat(this, "translationY",
220 getMeasuredWidth(), 0f);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700221 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700222 translateAnim = ObjectAnimator.ofFloat(this, "translationX",
223 getMeasuredHeight(), 0f);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700224 }
Winson Chungc5536bf2011-03-30 10:39:30 -0700225 mInAnimation.playTogether(translateAnim, inAlphaAnim);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700227 mInAnimation.play(inAlphaAnim);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700230
Winson Chungc5536bf2011-03-30 10:39:30 -0700231 Animator outAlphaAnim = createOutAlphaAnim(this);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700232 if (mOutAnimation == null) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700233 mOutAnimation = new AnimatorSet();
234 mOutAnimation.setInterpolator(new AccelerateInterpolator());
235 mOutAnimation.setDuration(duration);
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700236 if (!LauncherApplication.isScreenLarge()) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700237 Animator translateAnim;
Adam Lesinski6b879f02010-11-04 16:15:23 -0700238 if (mOrientation == ORIENTATION_HORIZONTAL) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700239 translateAnim = ObjectAnimator.ofFloat(this, "translationY", 0f,
240 getMeasuredWidth());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700241 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700242 translateAnim = ObjectAnimator.ofFloat(this, "translationX", 0f,
243 getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700244 }
Winson Chungc5536bf2011-03-30 10:39:30 -0700245 mOutAnimation.playTogether(translateAnim, outAlphaAnim);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700246 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700247 mOutAnimation.addListener(new AnimatorListenerAdapter() {
248 public void onAnimationEnd(Animator animation) {
249 setVisibility(GONE);
250 }
251 });
252 mOutAnimation.play(outAlphaAnim);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700253 }
254 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 }
256
Joe Onorato00acb122009-08-04 16:04:30 -0400257 void setDragController(DragController dragController) {
258 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 }
260
Winson Chung760e5372010-12-15 13:14:23 -0800261 private int getTransitionAnimationDuration() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700262 return LauncherApplication.isScreenLarge() ?
Winson Chung760e5372010-12-15 13:14:23 -0800263 XLARGE_TRANSITION_DURATION : TRANSITION_DURATION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 }
265
Winson Chung760e5372010-12-15 13:14:23 -0800266 private int getAnimationDuration() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700267 return LauncherApplication.isScreenLarge() ?
Winson Chung760e5372010-12-15 13:14:23 -0800268 XLARGE_ANIMATION_DURATION : ANIMATION_DURATION;
Patrick Dubroydea9e932010-09-22 15:04:29 -0700269 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270}