blob: 1f54b366beca4894012b888db46fd2b98b20136b [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
Michael Jurka0280c3b2010-09-17 15:00:07 -0700193 public boolean isDropEnabled() {
194 return true;
195 }
196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 private void createAnimations() {
198 if (mInAnimation == null) {
199 mInAnimation = new FastAnimationSet();
200 final AnimationSet animationSet = mInAnimation;
201 animationSet.setInterpolator(new AccelerateInterpolator());
202 animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
203 if (mOrientation == ORIENTATION_HORIZONTAL) {
204 animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
205 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
206 Animation.RELATIVE_TO_SELF, 0.0f));
207 } else {
208 animationSet.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF,
209 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
210 Animation.ABSOLUTE, 0.0f));
211 }
212 animationSet.setDuration(ANIMATION_DURATION);
213 }
214 if (mHandleInAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400215 mHandleInAnimation = new AlphaAnimation(0.0f, 1.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 mHandleInAnimation.setDuration(ANIMATION_DURATION);
217 }
218 if (mOutAnimation == null) {
219 mOutAnimation = new FastAnimationSet();
220 final AnimationSet animationSet = mOutAnimation;
221 animationSet.setInterpolator(new AccelerateInterpolator());
222 animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
223 if (mOrientation == ORIENTATION_HORIZONTAL) {
224 animationSet.addAnimation(new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
225 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
226 Animation.RELATIVE_TO_SELF, 1.0f));
227 } else {
228 animationSet.addAnimation(new FastTranslateAnimation(Animation.RELATIVE_TO_SELF,
229 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f,
230 Animation.ABSOLUTE, 0.0f));
231 }
232 animationSet.setDuration(ANIMATION_DURATION);
233 }
234 if (mHandleOutAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400235 mHandleOutAnimation = new AlphaAnimation(1.0f, 0.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 mHandleOutAnimation.setFillAfter(true);
237 mHandleOutAnimation.setDuration(ANIMATION_DURATION);
238 }
239 }
240
241 void setLauncher(Launcher launcher) {
242 mLauncher = launcher;
243 }
244
Joe Onorato00acb122009-08-04 16:04:30 -0400245 void setDragController(DragController dragController) {
246 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 }
248
249 void setHandle(View view) {
250 mHandle = view;
251 }
252
Patrick Dubroydea9e932010-09-22 15:04:29 -0700253 void setManageVisibility(boolean value) {
254 mManageVisibility = value;
255 }
256
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 private static class FastTranslateAnimation extends TranslateAnimation {
258 public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
259 int fromYType, float fromYValue, int toYType, float toYValue) {
260 super(fromXType, fromXValue, toXType, toXValue,
261 fromYType, fromYValue, toYType, toYValue);
262 }
263
264 @Override
265 public boolean willChangeTransformationMatrix() {
266 return true;
267 }
268
269 @Override
270 public boolean willChangeBounds() {
271 return false;
272 }
273 }
274
275 private static class FastAnimationSet extends AnimationSet {
276 FastAnimationSet() {
277 super(false);
278 }
279
280 @Override
281 public boolean willChangeTransformationMatrix() {
282 return true;
283 }
284
285 @Override
286 public boolean willChangeBounds() {
287 return false;
288 }
289 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700290
291 @Override
292 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
293 DragView dragView, Object dragInfo) {
294 return null;
295 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296}