blob: aeaf5a37e7fa4cd0844186ff6d290ee139a29ab2 [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
Patrick Dubroybc6840b2010-09-01 11:54:27 -070019import com.android.launcher.R;
20
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.content.Context;
22import android.content.res.TypedArray;
Joe Onorato00acb122009-08-04 16:04:30 -040023import android.graphics.Paint;
24import android.graphics.PorterDuff;
25import android.graphics.PorterDuffColorFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.graphics.RectF;
27import android.graphics.drawable.TransitionDrawable;
Patrick Dubroybc6840b2010-09-01 11:54:27 -070028import android.util.AttributeSet;
Patrick Dubroybc6840b2010-09-01 11:54:27 -070029import android.view.View;
30import android.view.animation.AccelerateInterpolator;
31import android.view.animation.AlphaAnimation;
32import android.view.animation.Animation;
33import android.view.animation.AnimationSet;
34import android.view.animation.TranslateAnimation;
35import android.widget.ImageView;
Romain Guyedcce092010-03-04 13:03:17 -080036
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037public class DeleteZone extends ImageView implements DropTarget, DragController.DragListener {
38 private static final int ORIENTATION_HORIZONTAL = 1;
39 private static final int TRANSITION_DURATION = 250;
40 private static final int ANIMATION_DURATION = 200;
41
42 private final int[] mLocation = new int[2];
43
44 private Launcher mLauncher;
45 private boolean mTrashMode;
46
Patrick Dubroydea9e932010-09-22 15:04:29 -070047 /**
48 * If true, this View responsible for managing its own visibility, and that of its handle.
49 * This is generally the case, but it will be set to false when this is part of the
50 * Contextual Action Bar.
51 */
52 private boolean mManageVisibility = true;
53
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054 private AnimationSet mInAnimation;
55 private AnimationSet mOutAnimation;
56 private Animation mHandleInAnimation;
57 private Animation mHandleOutAnimation;
58
59 private int mOrientation;
Joe Onorato00acb122009-08-04 16:04:30 -040060 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061
62 private final RectF mRegion = new RectF();
63 private TransitionDrawable mTransition;
Joe Onorato00acb122009-08-04 16:04:30 -040064 private final Paint mTrashPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065
Patrick Dubroydea9e932010-09-22 15:04:29 -070066 /** The View that this view will replace. */
67 private View mHandle = null;
68
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 public DeleteZone(Context context, AttributeSet attrs) {
70 this(context, attrs, 0);
71 }
72
73 public DeleteZone(Context context, AttributeSet attrs, int defStyle) {
74 super(context, attrs, defStyle);
75
Joe Onorato00acb122009-08-04 16:04:30 -040076 final int srcColor = context.getResources().getColor(R.color.delete_color_filter);
77 mTrashPaint.setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.SRC_ATOP));
78
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0);
80 mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL);
81 a.recycle();
Patrick Dubroydea9e932010-09-22 15:04:29 -070082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 }
84
85 @Override
86 protected void onFinishInflate() {
87 super.onFinishInflate();
Joe Onoratoa9c28f62009-09-14 18:38:49 -040088 mTransition = (TransitionDrawable) getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 }
90
91 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040092 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 return true;
94 }
95
Joe Onorato00acb122009-08-04 16:04:30 -040096 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
97 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098 final ItemInfo item = (ItemInfo) dragInfo;
99
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700100 // On x-large screens, you can uninstall an app by dragging from all apps
101 if (item instanceof ApplicationInfo && LauncherApplication.isScreenXLarge()) {
Patrick Dubroy5539af72010-09-07 15:22:01 -0700102 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700103 }
104
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 if (item.container == -1) return;
106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700108 if (item instanceof LauncherAppWidgetInfo) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400109 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700111 } else if (source instanceof UserFolder) {
112 final UserFolder userFolder = (UserFolder) source;
113 final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder.getInfo();
114 // Item must be a ShortcutInfo otherwise it couldn't have been in the folder
115 // in the first place.
116 userFolderInfo.remove((ShortcutInfo)item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700118
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 if (item instanceof UserFolderInfo) {
120 final UserFolderInfo userFolderInfo = (UserFolderInfo)item;
121 LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher, userFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400122 mLauncher.removeFolder(userFolderInfo);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700123 } else if (item instanceof LauncherAppWidgetInfo) {
124 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
125 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
126 if (appWidgetHost != null) {
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700127 final int appWidgetId = launcherAppWidgetInfo.appWidgetId;
128 // Deleting an app widget ID is a void call but writes to disk before returning
129 // to the caller...
130 new Thread("deleteAppWidgetId") {
131 public void run() {
132 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
133 }
134 }.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 }
136 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700137
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 LauncherModel.deleteItemFromDatabase(mLauncher, item);
139 }
140
141 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400142 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 mTransition.reverseTransition(TRANSITION_DURATION);
Joe Onorato00acb122009-08-04 16:04:30 -0400144 dragView.setPaint(mTrashPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 }
146
147 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400148 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 }
150
151 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400152 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 mTransition.reverseTransition(TRANSITION_DURATION);
Joe Onorato00acb122009-08-04 16:04:30 -0400154 dragView.setPaint(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 }
156
Joe Onorato5162ea92009-09-03 09:39:42 -0700157 public void onDragStart(DragSource source, Object info, int dragAction) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 final ItemInfo item = (ItemInfo) info;
159 if (item != null) {
160 mTrashMode = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 final int[] location = mLocation;
162 getLocationOnScreen(location);
163 mRegion.set(location[0], location[1], location[0] + mRight - mLeft,
164 location[1] + mBottom - mTop);
Joe Onorato00acb122009-08-04 16:04:30 -0400165 mDragController.setDeleteRegion(mRegion);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700166
167 // Make sure the icon is set to the default drawable, not the hover drawable
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 mTransition.resetTransition();
Patrick Dubroydea9e932010-09-22 15:04:29 -0700169
170 if (mManageVisibility) {
171 createAnimations();
172 startAnimation(mInAnimation);
173 mHandle.startAnimation(mHandleOutAnimation);
174 setVisibility(VISIBLE);
175 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 }
177 }
178
179 public void onDragEnd() {
180 if (mTrashMode) {
181 mTrashMode = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400182 mDragController.setDeleteRegion(null);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700183
184 if (mOutAnimation != null) startAnimation(mOutAnimation);
185 if (mHandleInAnimation != null) mHandle.startAnimation(mHandleInAnimation);
186
187 if (mManageVisibility) {
188 setVisibility(GONE);
189 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 }
191 }
192
193 private void createAnimations() {
194 if (mInAnimation == null) {
195 mInAnimation = new FastAnimationSet();
196 final AnimationSet animationSet = mInAnimation;
197 animationSet.setInterpolator(new AccelerateInterpolator());
198 animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
199 if (mOrientation == ORIENTATION_HORIZONTAL) {
200 animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
201 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
202 Animation.RELATIVE_TO_SELF, 0.0f));
203 } else {
204 animationSet.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF,
205 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
206 Animation.ABSOLUTE, 0.0f));
207 }
208 animationSet.setDuration(ANIMATION_DURATION);
209 }
210 if (mHandleInAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400211 mHandleInAnimation = new AlphaAnimation(0.0f, 1.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 mHandleInAnimation.setDuration(ANIMATION_DURATION);
213 }
214 if (mOutAnimation == null) {
215 mOutAnimation = new FastAnimationSet();
216 final AnimationSet animationSet = mOutAnimation;
217 animationSet.setInterpolator(new AccelerateInterpolator());
218 animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
219 if (mOrientation == ORIENTATION_HORIZONTAL) {
220 animationSet.addAnimation(new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
221 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
222 Animation.RELATIVE_TO_SELF, 1.0f));
223 } else {
224 animationSet.addAnimation(new FastTranslateAnimation(Animation.RELATIVE_TO_SELF,
225 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f,
226 Animation.ABSOLUTE, 0.0f));
227 }
228 animationSet.setDuration(ANIMATION_DURATION);
229 }
230 if (mHandleOutAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400231 mHandleOutAnimation = new AlphaAnimation(1.0f, 0.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 mHandleOutAnimation.setFillAfter(true);
233 mHandleOutAnimation.setDuration(ANIMATION_DURATION);
234 }
235 }
236
237 void setLauncher(Launcher launcher) {
238 mLauncher = launcher;
239 }
240
Joe Onorato00acb122009-08-04 16:04:30 -0400241 void setDragController(DragController dragController) {
242 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 }
244
245 void setHandle(View view) {
246 mHandle = view;
247 }
248
Patrick Dubroydea9e932010-09-22 15:04:29 -0700249 void setManageVisibility(boolean value) {
250 mManageVisibility = value;
251 }
252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 private static class FastTranslateAnimation extends TranslateAnimation {
254 public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
255 int fromYType, float fromYValue, int toYType, float toYValue) {
256 super(fromXType, fromXValue, toXType, toXValue,
257 fromYType, fromYValue, toYType, toYValue);
258 }
259
260 @Override
261 public boolean willChangeTransformationMatrix() {
262 return true;
263 }
264
265 @Override
266 public boolean willChangeBounds() {
267 return false;
268 }
269 }
270
271 private static class FastAnimationSet extends AnimationSet {
272 FastAnimationSet() {
273 super(false);
274 }
275
276 @Override
277 public boolean willChangeTransformationMatrix() {
278 return true;
279 }
280
281 @Override
282 public boolean willChangeBounds() {
283 return false;
284 }
285 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700286
287 @Override
288 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
289 DragView dragView, Object dragInfo) {
290 return null;
291 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292}