blob: 4acde1c508d12f38067c0473b79a624610594288 [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()) {
Michael Jurka774bd372010-10-22 13:40:50 -0700209 // TODO: This is a temporary hack. mManageVisiblity = false when you're in CAB mode.
210 // In that case, this icon is more tightly spaced next to the delete icon so we want
211 // it to have a smaller drag region. When the new drag&drop system comes in, we'll
212 // dispatch the drag/drop by calculating what target you're overlapping
213 final int dragPadding = mManageVisibility ? 50 : 10;
214 outRect.top -= dragPadding;
215 outRect.left -= dragPadding;
216 outRect.bottom += dragPadding;
217 outRect.right += dragPadding;
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700218 }
219 }
220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 private void createAnimations() {
222 if (mInAnimation == null) {
223 mInAnimation = new FastAnimationSet();
224 final AnimationSet animationSet = mInAnimation;
225 animationSet.setInterpolator(new AccelerateInterpolator());
226 animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
227 if (mOrientation == ORIENTATION_HORIZONTAL) {
228 animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
229 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
230 Animation.RELATIVE_TO_SELF, 0.0f));
231 } else {
232 animationSet.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF,
233 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
234 Animation.ABSOLUTE, 0.0f));
235 }
236 animationSet.setDuration(ANIMATION_DURATION);
237 }
238 if (mHandleInAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400239 mHandleInAnimation = new AlphaAnimation(0.0f, 1.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 mHandleInAnimation.setDuration(ANIMATION_DURATION);
241 }
242 if (mOutAnimation == null) {
243 mOutAnimation = new FastAnimationSet();
244 final AnimationSet animationSet = mOutAnimation;
245 animationSet.setInterpolator(new AccelerateInterpolator());
246 animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
247 if (mOrientation == ORIENTATION_HORIZONTAL) {
248 animationSet.addAnimation(new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
249 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
250 Animation.RELATIVE_TO_SELF, 1.0f));
251 } else {
252 animationSet.addAnimation(new FastTranslateAnimation(Animation.RELATIVE_TO_SELF,
253 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f,
254 Animation.ABSOLUTE, 0.0f));
255 }
256 animationSet.setDuration(ANIMATION_DURATION);
257 }
258 if (mHandleOutAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400259 mHandleOutAnimation = new AlphaAnimation(1.0f, 0.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 mHandleOutAnimation.setFillAfter(true);
261 mHandleOutAnimation.setDuration(ANIMATION_DURATION);
262 }
263 }
264
265 void setLauncher(Launcher launcher) {
266 mLauncher = launcher;
267 }
268
Joe Onorato00acb122009-08-04 16:04:30 -0400269 void setDragController(DragController dragController) {
270 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 }
272
273 void setHandle(View view) {
274 mHandle = view;
275 }
276
Patrick Dubroydea9e932010-09-22 15:04:29 -0700277 void setManageVisibility(boolean value) {
278 mManageVisibility = value;
279 }
280
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 private static class FastTranslateAnimation extends TranslateAnimation {
282 public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
283 int fromYType, float fromYValue, int toYType, float toYValue) {
284 super(fromXType, fromXValue, toXType, toXValue,
285 fromYType, fromYValue, toYType, toYValue);
286 }
287
288 @Override
289 public boolean willChangeTransformationMatrix() {
290 return true;
291 }
292
293 @Override
294 public boolean willChangeBounds() {
295 return false;
296 }
297 }
298
299 private static class FastAnimationSet extends AnimationSet {
300 FastAnimationSet() {
301 super(false);
302 }
303
304 @Override
305 public boolean willChangeTransformationMatrix() {
306 return true;
307 }
308
309 @Override
310 public boolean willChangeBounds() {
311 return false;
312 }
313 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700314
315 @Override
316 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
317 DragView dragView, Object dragInfo) {
318 return null;
319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320}