blob: e8f160f3ce2e560f22afbbbc33358c4dc3ebe7d1 [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;
Michael Jurka3adcb0c2010-10-15 18:07:21 -070026import android.graphics.Rect;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.graphics.RectF;
28import android.graphics.drawable.TransitionDrawable;
Patrick Dubroybc6840b2010-09-01 11:54:27 -070029import android.util.AttributeSet;
Patrick Dubroybc6840b2010-09-01 11:54:27 -070030import android.view.View;
31import android.view.animation.AccelerateInterpolator;
32import android.view.animation.AlphaAnimation;
33import android.view.animation.Animation;
34import android.view.animation.AnimationSet;
35import android.view.animation.TranslateAnimation;
36import android.widget.ImageView;
Romain Guyedcce092010-03-04 13:03:17 -080037
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038public class DeleteZone extends ImageView implements DropTarget, DragController.DragListener {
39 private static final int ORIENTATION_HORIZONTAL = 1;
40 private static final int TRANSITION_DURATION = 250;
41 private static final int ANIMATION_DURATION = 200;
42
43 private final int[] mLocation = new int[2];
44
45 private Launcher mLauncher;
46 private boolean mTrashMode;
47
Patrick Dubroydea9e932010-09-22 15:04:29 -070048 /**
49 * If true, this View responsible for managing its own visibility, and that of its handle.
50 * This is generally the case, but it will be set to false when this is part of the
51 * Contextual Action Bar.
52 */
53 private boolean mManageVisibility = true;
54
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055 private AnimationSet mInAnimation;
56 private AnimationSet mOutAnimation;
57 private Animation mHandleInAnimation;
58 private Animation mHandleOutAnimation;
59
60 private int mOrientation;
Joe Onorato00acb122009-08-04 16:04:30 -040061 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062
Michael Jurka3adcb0c2010-10-15 18:07:21 -070063 private final RectF mRegionF = new RectF();
64 private final Rect mRegion = new Rect();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 private TransitionDrawable mTransition;
Joe Onorato00acb122009-08-04 16:04:30 -040066 private final Paint mTrashPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
Patrick Dubroydea9e932010-09-22 15:04:29 -070068 /** The View that this view will replace. */
69 private View mHandle = null;
70
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071 public DeleteZone(Context context, AttributeSet attrs) {
72 this(context, attrs, 0);
73 }
74
75 public DeleteZone(Context context, AttributeSet attrs, int defStyle) {
76 super(context, attrs, defStyle);
77
Joe Onorato00acb122009-08-04 16:04:30 -040078 final int srcColor = context.getResources().getColor(R.color.delete_color_filter);
79 mTrashPaint.setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.SRC_ATOP));
80
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0);
82 mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL);
83 a.recycle();
Patrick Dubroydea9e932010-09-22 15:04:29 -070084
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 }
86
87 @Override
88 protected void onFinishInflate() {
89 super.onFinishInflate();
Joe Onoratoa9c28f62009-09-14 18:38:49 -040090 mTransition = (TransitionDrawable) getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 }
92
93 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040094 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 return true;
96 }
97
Joe Onorato00acb122009-08-04 16:04:30 -040098 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
99 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 final ItemInfo item = (ItemInfo) dragInfo;
101
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700102 // On x-large screens, you can uninstall an app by dragging from all apps
103 if (item instanceof ApplicationInfo && LauncherApplication.isScreenXLarge()) {
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 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700113 } else if (source instanceof UserFolder) {
114 final UserFolder userFolder = (UserFolder) source;
115 final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder.getInfo();
116 // Item must be a ShortcutInfo otherwise it couldn't have been in the folder
117 // in the first place.
118 userFolderInfo.remove((ShortcutInfo)item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 if (item instanceof UserFolderInfo) {
122 final UserFolderInfo userFolderInfo = (UserFolderInfo)item;
123 LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher, userFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400124 mLauncher.removeFolder(userFolderInfo);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700125 } else if (item instanceof LauncherAppWidgetInfo) {
126 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
127 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
128 if (appWidgetHost != null) {
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700129 final int appWidgetId = launcherAppWidgetInfo.appWidgetId;
130 // Deleting an app widget ID is a void call but writes to disk before returning
131 // to the caller...
132 new Thread("deleteAppWidgetId") {
133 public void run() {
134 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
135 }
136 }.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 }
138 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700139
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 LauncherModel.deleteItemFromDatabase(mLauncher, item);
141 }
142
143 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400144 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 mTransition.reverseTransition(TRANSITION_DURATION);
Joe Onorato00acb122009-08-04 16:04:30 -0400146 dragView.setPaint(mTrashPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 }
148
149 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400150 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 }
152
153 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400154 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 mTransition.reverseTransition(TRANSITION_DURATION);
Joe Onorato00acb122009-08-04 16:04:30 -0400156 dragView.setPaint(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 }
158
Joe Onorato5162ea92009-09-03 09:39:42 -0700159 public void onDragStart(DragSource source, Object info, int dragAction) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 final ItemInfo item = (ItemInfo) info;
161 if (item != null) {
162 mTrashMode = true;
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700163 getHitRect(mRegion);
164 mRegionF.set(mRegion);
165
166 if (LauncherApplication.isScreenXLarge()) {
167 // This region will be a "dead zone" for scrolling; make it extend to the edge of
168 // the screen so users don't accidentally trigger a scroll while deleting items
169 mRegionF.top = mLauncher.getWorkspace().getTop();
170 mRegionF.right = mLauncher.getWorkspace().getRight();
171 }
172
173 mDragController.setDeleteRegion(mRegionF);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700174
175 // Make sure the icon is set to the default drawable, not the hover drawable
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 mTransition.resetTransition();
Patrick Dubroydea9e932010-09-22 15:04:29 -0700177
178 if (mManageVisibility) {
179 createAnimations();
180 startAnimation(mInAnimation);
181 mHandle.startAnimation(mHandleOutAnimation);
182 setVisibility(VISIBLE);
183 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 }
185 }
186
187 public void onDragEnd() {
188 if (mTrashMode) {
189 mTrashMode = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400190 mDragController.setDeleteRegion(null);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700191
192 if (mOutAnimation != null) startAnimation(mOutAnimation);
193 if (mHandleInAnimation != null) mHandle.startAnimation(mHandleInAnimation);
194
195 if (mManageVisibility) {
196 setVisibility(GONE);
197 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 }
199 }
200
Michael Jurka0280c3b2010-09-17 15:00:07 -0700201 public boolean isDropEnabled() {
202 return true;
203 }
204
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700205 @Override
206 public void getHitRect(Rect outRect) {
207 super.getHitRect(outRect);
208 if (LauncherApplication.isScreenXLarge()) {
209 outRect.top -= 50;
210 outRect.left -= 50;
211 outRect.bottom += 50;
212 outRect.right += 50;
213 }
214 }
215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 private void createAnimations() {
217 if (mInAnimation == null) {
218 mInAnimation = new FastAnimationSet();
219 final AnimationSet animationSet = mInAnimation;
220 animationSet.setInterpolator(new AccelerateInterpolator());
221 animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
222 if (mOrientation == ORIENTATION_HORIZONTAL) {
223 animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
224 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
225 Animation.RELATIVE_TO_SELF, 0.0f));
226 } else {
227 animationSet.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF,
228 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
229 Animation.ABSOLUTE, 0.0f));
230 }
231 animationSet.setDuration(ANIMATION_DURATION);
232 }
233 if (mHandleInAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400234 mHandleInAnimation = new AlphaAnimation(0.0f, 1.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 mHandleInAnimation.setDuration(ANIMATION_DURATION);
236 }
237 if (mOutAnimation == null) {
238 mOutAnimation = new FastAnimationSet();
239 final AnimationSet animationSet = mOutAnimation;
240 animationSet.setInterpolator(new AccelerateInterpolator());
241 animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
242 if (mOrientation == ORIENTATION_HORIZONTAL) {
243 animationSet.addAnimation(new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
244 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
245 Animation.RELATIVE_TO_SELF, 1.0f));
246 } else {
247 animationSet.addAnimation(new FastTranslateAnimation(Animation.RELATIVE_TO_SELF,
248 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f,
249 Animation.ABSOLUTE, 0.0f));
250 }
251 animationSet.setDuration(ANIMATION_DURATION);
252 }
253 if (mHandleOutAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400254 mHandleOutAnimation = new AlphaAnimation(1.0f, 0.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 mHandleOutAnimation.setFillAfter(true);
256 mHandleOutAnimation.setDuration(ANIMATION_DURATION);
257 }
258 }
259
260 void setLauncher(Launcher launcher) {
261 mLauncher = launcher;
262 }
263
Joe Onorato00acb122009-08-04 16:04:30 -0400264 void setDragController(DragController dragController) {
265 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 }
267
268 void setHandle(View view) {
269 mHandle = view;
270 }
271
Patrick Dubroydea9e932010-09-22 15:04:29 -0700272 void setManageVisibility(boolean value) {
273 mManageVisibility = value;
274 }
275
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 private static class FastTranslateAnimation extends TranslateAnimation {
277 public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
278 int fromYType, float fromYValue, int toYType, float toYValue) {
279 super(fromXType, fromXValue, toXType, toXValue,
280 fromYType, fromYValue, toYType, toYValue);
281 }
282
283 @Override
284 public boolean willChangeTransformationMatrix() {
285 return true;
286 }
287
288 @Override
289 public boolean willChangeBounds() {
290 return false;
291 }
292 }
293
294 private static class FastAnimationSet extends AnimationSet {
295 FastAnimationSet() {
296 super(false);
297 }
298
299 @Override
300 public boolean willChangeTransformationMatrix() {
301 return true;
302 }
303
304 @Override
305 public boolean willChangeBounds() {
306 return false;
307 }
308 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700309
310 @Override
311 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
312 DragView dragView, Object dragInfo) {
313 return null;
314 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315}