blob: c349e5d727a5159305c464f0bf5e3c9055afa446 [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;
Adam Cohencb3382b2011-05-24 14:07:08 -070036import com.android.launcher2.DropTarget.DragObject;
Winson Chungc5536bf2011-03-30 10:39:30 -070037
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
Winson Chung4c98d922011-05-31 16:50:48 -070065 final int srcColor = context.getResources().getColor(R.color.delete_target_hover_tint);
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
Michael Jurkaa2eb1702011-05-12 14:57:05 -070072 if (LauncherApplication.isScreenLarge()) {
Winson Chung59e1f9a2010-12-21 11:31:54 -080073 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];
Michael Jurkaa2eb1702011-05-12 14:57:05 -070085 if (LauncherApplication.isScreenLarge()) {
Michael Jurka577d0172010-12-17 20:04:50 -080086 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
Adam Cohencb3382b2011-05-24 14:07:08 -070094 public boolean acceptDrop(DragObject d) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 return true;
96 }
97
Adam Cohencb3382b2011-05-24 14:07:08 -070098 public void onDrop(DragObject d) {
Adam Cohencdc30d52010-12-01 15:09:47 -080099 if (!mDragAndDropEnabled) return;
100
Adam Cohencb3382b2011-05-24 14:07:08 -0700101 final ItemInfo item = (ItemInfo) d.dragInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700103 // On x-large screens, you can uninstall an app by dragging from all apps
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700104 if (item instanceof ApplicationInfo && LauncherApplication.isScreenLarge()) {
Patrick Dubroy5539af72010-09-07 15:22:01 -0700105 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700106 }
107
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 if (item.container == -1) return;
109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700111 if (item instanceof LauncherAppWidgetInfo) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400112 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700115
Adam Cohendf2cc412011-04-27 16:56:57 -0700116 if (item instanceof FolderInfo) {
117 final FolderInfo folderInfo = (FolderInfo)item;
118 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
119 mLauncher.removeFolder(folderInfo);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700120 } else if (item instanceof LauncherAppWidgetInfo) {
121 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
122 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
123 if (appWidgetHost != null) {
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700124 // Deleting an app widget ID is a void call but writes to disk before returning
125 // to the caller...
126 new Thread("deleteAppWidgetId") {
127 public void run() {
128 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
129 }
130 }.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 }
132 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700133
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 LauncherModel.deleteItemFromDatabase(mLauncher, item);
135 }
136
Adam Cohencb3382b2011-05-24 14:07:08 -0700137 public void onDragEnter(DragObject d) {
Winson Chung760e5372010-12-15 13:14:23 -0800138 if (mDragAndDropEnabled) {
139 mTransition.reverseTransition(getTransitionAnimationDuration());
Michael Jurka577d0172010-12-17 20:04:50 -0800140 setTextColor(mDragTextColor);
Adam Cohencb3382b2011-05-24 14:07:08 -0700141 super.onDragEnter(d);
Winson Chung760e5372010-12-15 13:14:23 -0800142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 }
144
Adam Cohencb3382b2011-05-24 14:07:08 -0700145 public void onDragExit(DragObject d) {
Winson Chung760e5372010-12-15 13:14:23 -0800146 if (mDragAndDropEnabled) {
147 mTransition.reverseTransition(getTransitionAnimationDuration());
Michael Jurka577d0172010-12-17 20:04:50 -0800148 setTextColor(mTextColor);
Adam Cohencb3382b2011-05-24 14:07:08 -0700149 super.onDragExit(d);
Winson Chung760e5372010-12-15 13:14:23 -0800150 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 }
152
Joe Onorato5162ea92009-09-03 09:39:42 -0700153 public void onDragStart(DragSource source, Object info, int dragAction) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 final ItemInfo item = (ItemInfo) info;
Adam Cohencdc30d52010-12-01 15:09:47 -0800155 if (item != null && mDragAndDropEnabled) {
Winson Chung760e5372010-12-15 13:14:23 -0800156 mActive = true;
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700157 getHitRect(mRegion);
158 mRegionF.set(mRegion);
159
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700160 if (LauncherApplication.isScreenLarge()) {
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700161 // This region will be a "dead zone" for scrolling; make it extend to the edge of
162 // the screen so users don't accidentally trigger a scroll while deleting items
163 mRegionF.top = mLauncher.getWorkspace().getTop();
164 mRegionF.right = mLauncher.getWorkspace().getRight();
165 }
166
167 mDragController.setDeleteRegion(mRegionF);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700168
169 // Make sure the icon is set to the default drawable, not the hover drawable
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 mTransition.resetTransition();
Patrick Dubroydea9e932010-09-22 15:04:29 -0700171
Adam Cohencdc30d52010-12-01 15:09:47 -0800172 createAnimations();
Winson Chungc5536bf2011-03-30 10:39:30 -0700173 mInAnimation.start();
Michael Jurkab8e14472010-12-20 16:06:10 -0800174 if (mOverlappingViews != null) {
175 for (View view : mOverlappingViews) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700176 createOutAlphaAnim(view).start();
Michael Jurkab8e14472010-12-20 16:06:10 -0800177 }
Patrick Dubroydea9e932010-09-22 15:04:29 -0700178 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800179 setVisibility(VISIBLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 }
181 }
182
183 public void onDragEnd() {
Winson Chung760e5372010-12-15 13:14:23 -0800184 if (mActive && mDragAndDropEnabled) {
185 mActive = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400186 mDragController.setDeleteRegion(null);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700187
Winson Chungc5536bf2011-03-30 10:39:30 -0700188 mOutAnimation.start();
189 if (mOverlappingViews != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -0800190 for (View view : mOverlappingViews) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700191 createInAlphaAnim(view).start();
Michael Jurkab8e14472010-12-20 16:06:10 -0800192 }
Patrick Dubroydea9e932010-09-22 15:04:29 -0700193 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 }
195 }
196
Winson Chungc5536bf2011-03-30 10:39:30 -0700197 private Animator createAlphaAnim(View v, float start, float end) {
198 Animator anim = ObjectAnimator.ofFloat(v, "alpha", start, end);
199 anim.setDuration(getAnimationDuration());
200 return anim;
201 }
202 private Animator createInAlphaAnim(View v) {
203 return createAlphaAnim(v, 0f, 1f);
204 }
205 private Animator createOutAlphaAnim(View v) {
206 return createAlphaAnim(v, 1f, 0f);
207 }
208
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 private void createAnimations() {
Winson Chung760e5372010-12-15 13:14:23 -0800210 int duration = getAnimationDuration();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700211
Winson Chungc5536bf2011-03-30 10:39:30 -0700212 Animator inAlphaAnim = createInAlphaAnim(this);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700213 if (mInAnimation == null) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700214 mInAnimation = new AnimatorSet();
215 mInAnimation.setInterpolator(new AccelerateInterpolator());
216 mInAnimation.setDuration(duration);
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700217 if (!LauncherApplication.isScreenLarge()) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700218 Animator translateAnim;
Adam Lesinski6b879f02010-11-04 16:15:23 -0700219 if (mOrientation == ORIENTATION_HORIZONTAL) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700220 translateAnim = ObjectAnimator.ofFloat(this, "translationY",
221 getMeasuredWidth(), 0f);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700222 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700223 translateAnim = ObjectAnimator.ofFloat(this, "translationX",
224 getMeasuredHeight(), 0f);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700225 }
Winson Chungc5536bf2011-03-30 10:39:30 -0700226 mInAnimation.playTogether(translateAnim, inAlphaAnim);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700228 mInAnimation.play(inAlphaAnim);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700231
Winson Chungc5536bf2011-03-30 10:39:30 -0700232 Animator outAlphaAnim = createOutAlphaAnim(this);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700233 if (mOutAnimation == null) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700234 mOutAnimation = new AnimatorSet();
235 mOutAnimation.setInterpolator(new AccelerateInterpolator());
236 mOutAnimation.setDuration(duration);
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700237 if (!LauncherApplication.isScreenLarge()) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700238 Animator translateAnim;
Adam Lesinski6b879f02010-11-04 16:15:23 -0700239 if (mOrientation == ORIENTATION_HORIZONTAL) {
Winson Chungc5536bf2011-03-30 10:39:30 -0700240 translateAnim = ObjectAnimator.ofFloat(this, "translationY", 0f,
241 getMeasuredWidth());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700242 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700243 translateAnim = ObjectAnimator.ofFloat(this, "translationX", 0f,
244 getMeasuredHeight());
Adam Lesinski6b879f02010-11-04 16:15:23 -0700245 }
Winson Chungc5536bf2011-03-30 10:39:30 -0700246 mOutAnimation.playTogether(translateAnim, outAlphaAnim);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700247 } else {
Winson Chungc5536bf2011-03-30 10:39:30 -0700248 mOutAnimation.addListener(new AnimatorListenerAdapter() {
249 public void onAnimationEnd(Animator animation) {
250 setVisibility(GONE);
251 }
252 });
253 mOutAnimation.play(outAlphaAnim);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700254 }
255 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 }
257
Joe Onorato00acb122009-08-04 16:04:30 -0400258 void setDragController(DragController dragController) {
259 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 }
261
Winson Chung760e5372010-12-15 13:14:23 -0800262 private int getTransitionAnimationDuration() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700263 return LauncherApplication.isScreenLarge() ?
Winson Chung760e5372010-12-15 13:14:23 -0800264 XLARGE_TRANSITION_DURATION : TRANSITION_DURATION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 }
266
Winson Chung760e5372010-12-15 13:14:23 -0800267 private int getAnimationDuration() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700268 return LauncherApplication.isScreenLarge() ?
Winson Chung760e5372010-12-15 13:14:23 -0800269 XLARGE_ANIMATION_DURATION : ANIMATION_DURATION;
Patrick Dubroydea9e932010-09-22 15:04:29 -0700270 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271}