blob: 01a20f76b8ca98853ffcb7653eba3364d777d4fa [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;
Jeff Sharkey70864282009-04-07 21:08:40 -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
48 private AnimationSet mInAnimation;
49 private AnimationSet mOutAnimation;
50 private Animation mHandleInAnimation;
51 private Animation mHandleOutAnimation;
52
53 private int mOrientation;
Joe Onorato00acb122009-08-04 16:04:30 -040054 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055
56 private final RectF mRegion = new RectF();
57 private TransitionDrawable mTransition;
58 private View mHandle;
Joe Onorato00acb122009-08-04 16:04:30 -040059 private final Paint mTrashPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060
61 public DeleteZone(Context context, AttributeSet attrs) {
62 this(context, attrs, 0);
63 }
64
65 public DeleteZone(Context context, AttributeSet attrs, int defStyle) {
66 super(context, attrs, defStyle);
67
Joe Onorato00acb122009-08-04 16:04:30 -040068 final int srcColor = context.getResources().getColor(R.color.delete_color_filter);
69 mTrashPaint.setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.SRC_ATOP));
70
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0);
72 mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL);
73 a.recycle();
74 }
75
76 @Override
77 protected void onFinishInflate() {
78 super.onFinishInflate();
Joe Onoratoa9c28f62009-09-14 18:38:49 -040079 mTransition = (TransitionDrawable) getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080 }
81
82 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040083 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 return true;
85 }
86
Joe Onorato00acb122009-08-04 16:04:30 -040087 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
88 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 final ItemInfo item = (ItemInfo) dragInfo;
90
Patrick Dubroybc6840b2010-09-01 11:54:27 -070091 // On x-large screens, you can uninstall an app by dragging from all apps
92 if (item instanceof ApplicationInfo && LauncherApplication.isScreenXLarge()) {
Patrick Dubroy5539af72010-09-07 15:22:01 -070093 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Patrick Dubroybc6840b2010-09-01 11:54:27 -070094 }
95
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 if (item.container == -1) return;
97
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -070099 if (item instanceof LauncherAppWidgetInfo) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400100 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700102 } else if (source instanceof UserFolder) {
103 final UserFolder userFolder = (UserFolder) source;
104 final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder.getInfo();
105 // Item must be a ShortcutInfo otherwise it couldn't have been in the folder
106 // in the first place.
107 userFolderInfo.remove((ShortcutInfo)item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 if (item instanceof UserFolderInfo) {
111 final UserFolderInfo userFolderInfo = (UserFolderInfo)item;
112 LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher, userFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400113 mLauncher.removeFolder(userFolderInfo);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700114 } else if (item instanceof LauncherAppWidgetInfo) {
115 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
116 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
117 if (appWidgetHost != null) {
Brad Fitzpatrick73013bf2010-09-14 12:15:32 -0700118 final int appWidgetId = launcherAppWidgetInfo.appWidgetId;
119 // Deleting an app widget ID is a void call but writes to disk before returning
120 // to the caller...
121 new Thread("deleteAppWidgetId") {
122 public void run() {
123 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
124 }
125 }.start();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 }
127 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 LauncherModel.deleteItemFromDatabase(mLauncher, item);
130 }
131
132 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400133 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 mTransition.reverseTransition(TRANSITION_DURATION);
Joe Onorato00acb122009-08-04 16:04:30 -0400135 dragView.setPaint(mTrashPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 }
137
138 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400139 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 }
141
142 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400143 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 mTransition.reverseTransition(TRANSITION_DURATION);
Joe Onorato00acb122009-08-04 16:04:30 -0400145 dragView.setPaint(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 }
147
Joe Onorato5162ea92009-09-03 09:39:42 -0700148 public void onDragStart(DragSource source, Object info, int dragAction) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 final ItemInfo item = (ItemInfo) info;
150 if (item != null) {
151 mTrashMode = true;
152 createAnimations();
153 final int[] location = mLocation;
154 getLocationOnScreen(location);
155 mRegion.set(location[0], location[1], location[0] + mRight - mLeft,
156 location[1] + mBottom - mTop);
Joe Onorato00acb122009-08-04 16:04:30 -0400157 mDragController.setDeleteRegion(mRegion);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 mTransition.resetTransition();
159 startAnimation(mInAnimation);
160 mHandle.startAnimation(mHandleOutAnimation);
161 setVisibility(VISIBLE);
162 }
163 }
164
165 public void onDragEnd() {
166 if (mTrashMode) {
167 mTrashMode = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400168 mDragController.setDeleteRegion(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 startAnimation(mOutAnimation);
170 mHandle.startAnimation(mHandleInAnimation);
171 setVisibility(GONE);
172 }
173 }
174
175 private void createAnimations() {
176 if (mInAnimation == null) {
177 mInAnimation = new FastAnimationSet();
178 final AnimationSet animationSet = mInAnimation;
179 animationSet.setInterpolator(new AccelerateInterpolator());
180 animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
181 if (mOrientation == ORIENTATION_HORIZONTAL) {
182 animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
183 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
184 Animation.RELATIVE_TO_SELF, 0.0f));
185 } else {
186 animationSet.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF,
187 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
188 Animation.ABSOLUTE, 0.0f));
189 }
190 animationSet.setDuration(ANIMATION_DURATION);
191 }
192 if (mHandleInAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400193 mHandleInAnimation = new AlphaAnimation(0.0f, 1.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 mHandleInAnimation.setDuration(ANIMATION_DURATION);
195 }
196 if (mOutAnimation == null) {
197 mOutAnimation = new FastAnimationSet();
198 final AnimationSet animationSet = mOutAnimation;
199 animationSet.setInterpolator(new AccelerateInterpolator());
200 animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
201 if (mOrientation == ORIENTATION_HORIZONTAL) {
202 animationSet.addAnimation(new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
203 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
204 Animation.RELATIVE_TO_SELF, 1.0f));
205 } else {
206 animationSet.addAnimation(new FastTranslateAnimation(Animation.RELATIVE_TO_SELF,
207 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f,
208 Animation.ABSOLUTE, 0.0f));
209 }
210 animationSet.setDuration(ANIMATION_DURATION);
211 }
212 if (mHandleOutAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400213 mHandleOutAnimation = new AlphaAnimation(1.0f, 0.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 mHandleOutAnimation.setFillAfter(true);
215 mHandleOutAnimation.setDuration(ANIMATION_DURATION);
216 }
217 }
218
219 void setLauncher(Launcher launcher) {
220 mLauncher = launcher;
221 }
222
Joe Onorato00acb122009-08-04 16:04:30 -0400223 void setDragController(DragController dragController) {
224 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 }
226
227 void setHandle(View view) {
228 mHandle = view;
229 }
230
231 private static class FastTranslateAnimation extends TranslateAnimation {
232 public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
233 int fromYType, float fromYValue, int toYType, float toYValue) {
234 super(fromXType, fromXValue, toXType, toXValue,
235 fromYType, fromYValue, toYType, toYValue);
236 }
237
238 @Override
239 public boolean willChangeTransformationMatrix() {
240 return true;
241 }
242
243 @Override
244 public boolean willChangeBounds() {
245 return false;
246 }
247 }
248
249 private static class FastAnimationSet extends AnimationSet {
250 FastAnimationSet() {
251 super(false);
252 }
253
254 @Override
255 public boolean willChangeTransformationMatrix() {
256 return true;
257 }
258
259 @Override
260 public boolean willChangeBounds() {
261 return false;
262 }
263 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700264
265 @Override
266 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
267 DragView dragView, Object dragInfo) {
268 return null;
269 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270}