blob: 6d3bed545e4b079c3bf03186e12c58f126ec27d1 [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.AnimatorSet;
20import android.animation.Animator;
21import android.animation.AnimatorListenerAdapter;
22import 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;
Patrick Dubroybc6840b2010-09-01 11:54:27 -070034import android.view.animation.TranslateAnimation;
Romain Guyedcce092010-03-04 13:03:17 -080035
Winson Chungc5536bf2011-03-30 10:39:30 -070036import com.android.launcher.R;
37
Winson Chung760e5372010-12-15 13:14:23 -080038public class DeleteZone extends IconDropTarget {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039 private static final int ORIENTATION_HORIZONTAL = 1;
40 private static final int TRANSITION_DURATION = 250;
41 private static final int ANIMATION_DURATION = 200;
Winson Chung760e5372010-12-15 13:14:23 -080042 private static final int XLARGE_TRANSITION_DURATION = 150;
43 private static final int XLARGE_ANIMATION_DURATION = 200;
Michael Jurka577d0172010-12-17 20:04:50 -080044 private static final int LEFT_DRAWABLE = 0;
Patrick Dubroydea9e932010-09-22 15:04:29 -070045
Winson Chungc5536bf2011-03-30 10:39:30 -070046 private AnimatorSet mInAnimation;
47 private AnimatorSet mOutAnimation;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
49 private int mOrientation;
Joe Onorato00acb122009-08-04 16:04:30 -040050 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051
Michael Jurka3adcb0c2010-10-15 18:07:21 -070052 private final RectF mRegionF = new RectF();
53 private final Rect mRegion = new Rect();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054 private TransitionDrawable mTransition;
Michael Jurka577d0172010-12-17 20:04:50 -080055 private int mTextColor;
56 private int mDragTextColor;
Patrick Dubroydea9e932010-09-22 15:04:29 -070057
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058 public DeleteZone(Context context, AttributeSet attrs) {
59 this(context, attrs, 0);
60 }
61
62 public DeleteZone(Context context, AttributeSet attrs, int defStyle) {
63 super(context, attrs, defStyle);
64
Joe Onorato00acb122009-08-04 16:04:30 -040065 final int srcColor = context.getResources().getColor(R.color.delete_color_filter);
Winson Chung760e5372010-12-15 13:14:23 -080066 mHoverPaint.setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.SRC_ATOP));
Joe Onorato00acb122009-08-04 16:04:30 -040067
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0);
69 mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL);
70 a.recycle();
Winson Chungbe5212b2010-12-20 11:36:33 -080071
Winson Chung59e1f9a2010-12-21 11:31:54 -080072 if (LauncherApplication.isScreenXLarge()) {
73 int tb = getResources().getDimensionPixelSize(
74 R.dimen.delete_zone_vertical_drag_padding);
75 int lr = getResources().getDimensionPixelSize(
76 R.dimen.delete_zone_horizontal_drag_padding);
77 setDragPadding(tb, lr, tb, lr);
78 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079 }
80
81 @Override
82 protected void onFinishInflate() {
83 super.onFinishInflate();
Michael Jurka577d0172010-12-17 20:04:50 -080084 mTransition = (TransitionDrawable) getCompoundDrawables()[LEFT_DRAWABLE];
85 if (LauncherApplication.isScreenXLarge()) {
86 mTransition.setCrossFadeEnabled(false);
87 }
88
89 Resources r = getResources();
90 mTextColor = r.getColor(R.color.workspace_all_apps_and_delete_zone_text_color);
91 mDragTextColor = r.getColor(R.color.workspace_delete_zone_drag_text_color);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 }
93
94 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040095 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 return true;
97 }
98
Joe Onorato00acb122009-08-04 16:04:30 -040099 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
100 DragView dragView, Object dragInfo) {
Adam Cohencdc30d52010-12-01 15:09:47 -0800101 if (!mDragAndDropEnabled) return;
102
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 final ItemInfo item = (ItemInfo) dragInfo;
104
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700105 // On x-large screens, you can uninstall an app by dragging from all apps
106 if (item instanceof ApplicationInfo && LauncherApplication.isScreenXLarge()) {
Patrick Dubroy5539af72010-09-07 15:22:01 -0700107 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700108 }
109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 if (item.container == -1) return;
111
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700113 if (item instanceof LauncherAppWidgetInfo) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400114 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700116 } else if (source instanceof UserFolder) {
117 final UserFolder userFolder = (UserFolder) source;
118 final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder.getInfo();
119 // Item must be a ShortcutInfo otherwise it couldn't have been in the folder
120 // in the first place.
121 userFolderInfo.remove((ShortcutInfo)item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700123
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 if (item instanceof UserFolderInfo) {
125 final UserFolderInfo userFolderInfo = (UserFolderInfo)item;
126 LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher, userFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400127 mLauncher.removeFolder(userFolderInfo);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700128 } else if (item instanceof LauncherAppWidgetInfo) {
129 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
130 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
131 if (appWidgetHost != null) {
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700132 // Deleting an app widget ID is a void call but writes to disk before returning
133 // to the caller...
134 new Thread("deleteAppWidgetId") {
135 public void run() {
136 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
137 }
138 }.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 }
140 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700141
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 LauncherModel.deleteItemFromDatabase(mLauncher, item);
143 }
144
145 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400146 DragView dragView, Object dragInfo) {
Winson Chung760e5372010-12-15 13:14:23 -0800147 if (mDragAndDropEnabled) {
148 mTransition.reverseTransition(getTransitionAnimationDuration());
Michael Jurka577d0172010-12-17 20:04:50 -0800149 setTextColor(mDragTextColor);
Winson Chung760e5372010-12-15 13:14:23 -0800150 super.onDragEnter(source, x, y, xOffset, yOffset, dragView, dragInfo);
151 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 }
153
154 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400155 DragView dragView, Object dragInfo) {
Winson Chung760e5372010-12-15 13:14:23 -0800156 if (mDragAndDropEnabled) {
157 mTransition.reverseTransition(getTransitionAnimationDuration());
Michael Jurka577d0172010-12-17 20:04:50 -0800158 setTextColor(mTextColor);
Winson Chung760e5372010-12-15 13:14:23 -0800159 super.onDragExit(source, x, y, xOffset, yOffset, dragView, dragInfo);
160 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 }
162
Joe Onorato5162ea92009-09-03 09:39:42 -0700163 public void onDragStart(DragSource source, Object info, int dragAction) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 final ItemInfo item = (ItemInfo) info;
Adam Cohencdc30d52010-12-01 15:09:47 -0800165 if (item != null && mDragAndDropEnabled) {
Winson Chung760e5372010-12-15 13:14:23 -0800166 mActive = true;
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700167 getHitRect(mRegion);
168 mRegionF.set(mRegion);
169
170 if (LauncherApplication.isScreenXLarge()) {
171 // This region will be a "dead zone" for scrolling; make it extend to the edge of
172 // the screen so users don't accidentally trigger a scroll while deleting items
173 mRegionF.top = mLauncher.getWorkspace().getTop();
174 mRegionF.right = mLauncher.getWorkspace().getRight();
175 }
176
177 mDragController.setDeleteRegion(mRegionF);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700178
179 // Make sure the icon is set to the default drawable, not the hover drawable
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 mTransition.resetTransition();
Patrick Dubroydea9e932010-09-22 15:04:29 -0700181
Adam Cohencdc30d52010-12-01 15:09:47 -0800182 createAnimations();
Winson Chungc5536bf2011-03-30 10:39:30 -0700183 mInAnimation.start();
Michael Jurkab8e14472010-12-20 16:06:10 -0800184 if (mOverlappingViews != null) {
185 for (View view : mOverlappingViews) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700186 createOutAlphaAnim(view).start();
Michael Jurkab8e14472010-12-20 16:06:10 -0800187 }
Patrick Dubroydea9e932010-09-22 15:04:29 -0700188 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800189 setVisibility(VISIBLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 }
191 }
192
193 public void onDragEnd() {
Winson Chung760e5372010-12-15 13:14:23 -0800194 if (mActive && mDragAndDropEnabled) {
195 mActive = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400196 mDragController.setDeleteRegion(null);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700197
Winson Chungc5536bf2011-03-30 10:39:30 -0700198 mOutAnimation.start();
199 if (mOverlappingViews != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -0800200 for (View view : mOverlappingViews) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700201 createInAlphaAnim(view).start();
Michael Jurkab8e14472010-12-20 16:06:10 -0800202 }
Patrick Dubroydea9e932010-09-22 15:04:29 -0700203 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 }
205 }
206
Winson Chungc5536bf2011-03-30 10:39:30 -0700207 private Animator createAlphaAnim(View v, float start, float end) {
208 Animator anim = ObjectAnimator.ofFloat(v, "alpha", start, end);
209 anim.setDuration(getAnimationDuration());
210 return anim;
211 }
212 private Animator createInAlphaAnim(View v) {
213 return createAlphaAnim(v, 0f, 1f);
214 }
215 private Animator createOutAlphaAnim(View v) {
216 return createAlphaAnim(v, 1f, 0f);
217 }
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private void createAnimations() {
Winson Chung760e5372010-12-15 13:14:23 -0800220 int duration = getAnimationDuration();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700221
Winson Chungc5536bf2011-03-30 10:39:30 -0700222 Animator inAlphaAnim = createInAlphaAnim(this);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700223 if (mInAnimation == null) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700224 mInAnimation = new AnimatorSet();
225 mInAnimation.setInterpolator(new AccelerateInterpolator());
226 mInAnimation.setDuration(duration);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700227 if (!LauncherApplication.isScreenXLarge()) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700228 Animator translateAnim;
Adam Lesinski6b879f02010-11-04 16:15:23 -0700229 if (mOrientation == ORIENTATION_HORIZONTAL) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700230 translateAnim = ObjectAnimator.ofFloat(this, "translationY",
231 getMeasuredWidth(), 0f);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700232 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700233 translateAnim = ObjectAnimator.ofFloat(this, "translationX",
234 getMeasuredHeight(), 0f);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700235 }
Winson Chungc5536bf2011-03-30 10:39:30 -0700236 mInAnimation.playTogether(translateAnim, inAlphaAnim);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700238 mInAnimation.play(inAlphaAnim);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700241
Winson Chungc5536bf2011-03-30 10:39:30 -0700242 Animator outAlphaAnim = createOutAlphaAnim(this);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700243 if (mOutAnimation == null) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700244 mOutAnimation = new AnimatorSet();
245 mOutAnimation.setInterpolator(new AccelerateInterpolator());
246 mOutAnimation.setDuration(duration);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700247 if (!LauncherApplication.isScreenXLarge()) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700248 Animator translateAnim;
Adam Lesinski6b879f02010-11-04 16:15:23 -0700249 if (mOrientation == ORIENTATION_HORIZONTAL) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700250 translateAnim = ObjectAnimator.ofFloat(this, "translationY", 0f,
251 getMeasuredWidth());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700252 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700253 translateAnim = ObjectAnimator.ofFloat(this, "translationX", 0f,
254 getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700255 }
Winson Chungc5536bf2011-03-30 10:39:30 -0700256 mOutAnimation.playTogether(translateAnim, outAlphaAnim);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700257 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700258 mOutAnimation.addListener(new AnimatorListenerAdapter() {
259 public void onAnimationEnd(Animator animation) {
260 setVisibility(GONE);
261 }
262 });
263 mOutAnimation.play(outAlphaAnim);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700264 }
265 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 }
267
Joe Onorato00acb122009-08-04 16:04:30 -0400268 void setDragController(DragController dragController) {
269 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 }
271
Winson Chung760e5372010-12-15 13:14:23 -0800272 private int getTransitionAnimationDuration() {
273 return LauncherApplication.isScreenXLarge() ?
274 XLARGE_TRANSITION_DURATION : TRANSITION_DURATION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 }
276
Winson Chung760e5372010-12-15 13:14:23 -0800277 private int getAnimationDuration() {
278 return LauncherApplication.isScreenXLarge() ?
279 XLARGE_ANIMATION_DURATION : ANIMATION_DURATION;
Patrick Dubroydea9e932010-09-22 15:04:29 -0700280 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281}