blob: c36f701e7c6561799be03025937699351b512495 [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;
Patrick Dubroy39670182010-09-03 16:25:12 -070037import android.widget.Toast;
Romain Guyedcce092010-03-04 13:03:17 -080038
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039public class DeleteZone extends ImageView implements DropTarget, DragController.DragListener {
40 private static final int ORIENTATION_HORIZONTAL = 1;
41 private static final int TRANSITION_DURATION = 250;
42 private static final int ANIMATION_DURATION = 200;
43
44 private final int[] mLocation = new int[2];
45
46 private Launcher mLauncher;
47 private boolean mTrashMode;
48
49 private AnimationSet mInAnimation;
50 private AnimationSet mOutAnimation;
51 private Animation mHandleInAnimation;
52 private Animation mHandleOutAnimation;
53
54 private int mOrientation;
Joe Onorato00acb122009-08-04 16:04:30 -040055 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056
57 private final RectF mRegion = new RectF();
58 private TransitionDrawable mTransition;
59 private View mHandle;
Joe Onorato00acb122009-08-04 16:04:30 -040060 private final Paint mTrashPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061
62 public DeleteZone(Context context, AttributeSet attrs) {
63 this(context, attrs, 0);
64 }
65
66 public DeleteZone(Context context, AttributeSet attrs, int defStyle) {
67 super(context, attrs, defStyle);
68
Joe Onorato00acb122009-08-04 16:04:30 -040069 final int srcColor = context.getResources().getColor(R.color.delete_color_filter);
70 mTrashPaint.setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.SRC_ATOP));
71
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0);
73 mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL);
74 a.recycle();
75 }
76
77 @Override
78 protected void onFinishInflate() {
79 super.onFinishInflate();
Joe Onoratoa9c28f62009-09-14 18:38:49 -040080 mTransition = (TransitionDrawable) getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 }
82
83 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040084 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 return true;
86 }
Jeff Sharkey70864282009-04-07 21:08:40 -070087
Joe Onorato00acb122009-08-04 16:04:30 -040088 public Rect estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset,
89 DragView dragView, Object dragInfo, Rect recycle) {
Jeff Sharkey70864282009-04-07 21:08:40 -070090 return null;
91 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
Joe Onorato00acb122009-08-04 16:04:30 -040093 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
94 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 final ItemInfo item = (ItemInfo) dragInfo;
96
Patrick Dubroybc6840b2010-09-01 11:54:27 -070097 // On x-large screens, you can uninstall an app by dragging from all apps
98 if (item instanceof ApplicationInfo && LauncherApplication.isScreenXLarge()) {
Patrick Dubroy5539af72010-09-07 15:22:01 -070099 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700100 }
101
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 if (item.container == -1) return;
103
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700105 if (item instanceof LauncherAppWidgetInfo) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400106 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700108 } else if (source instanceof UserFolder) {
109 final UserFolder userFolder = (UserFolder) source;
110 final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder.getInfo();
111 // Item must be a ShortcutInfo otherwise it couldn't have been in the folder
112 // in the first place.
113 userFolderInfo.remove((ShortcutInfo)item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 if (item instanceof UserFolderInfo) {
117 final UserFolderInfo userFolderInfo = (UserFolderInfo)item;
118 LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher, userFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 mLauncher.removeFolder(userFolderInfo);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700120 } else if (item instanceof LauncherAppWidgetInfo) {
121 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
122 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
123 if (appWidgetHost != null) {
124 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 }
126 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 LauncherModel.deleteItemFromDatabase(mLauncher, item);
129 }
130
131 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400132 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 mTransition.reverseTransition(TRANSITION_DURATION);
Joe Onorato00acb122009-08-04 16:04:30 -0400134 dragView.setPaint(mTrashPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 }
136
137 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400138 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 }
140
141 public void onDragExit(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(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 }
146
Joe Onorato5162ea92009-09-03 09:39:42 -0700147 public void onDragStart(DragSource source, Object info, int dragAction) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 final ItemInfo item = (ItemInfo) info;
149 if (item != null) {
150 mTrashMode = true;
151 createAnimations();
152 final int[] location = mLocation;
153 getLocationOnScreen(location);
154 mRegion.set(location[0], location[1], location[0] + mRight - mLeft,
155 location[1] + mBottom - mTop);
Joe Onorato00acb122009-08-04 16:04:30 -0400156 mDragController.setDeleteRegion(mRegion);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 mTransition.resetTransition();
158 startAnimation(mInAnimation);
159 mHandle.startAnimation(mHandleOutAnimation);
160 setVisibility(VISIBLE);
161 }
162 }
163
164 public void onDragEnd() {
165 if (mTrashMode) {
166 mTrashMode = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400167 mDragController.setDeleteRegion(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 startAnimation(mOutAnimation);
169 mHandle.startAnimation(mHandleInAnimation);
170 setVisibility(GONE);
171 }
172 }
173
174 private void createAnimations() {
175 if (mInAnimation == null) {
176 mInAnimation = new FastAnimationSet();
177 final AnimationSet animationSet = mInAnimation;
178 animationSet.setInterpolator(new AccelerateInterpolator());
179 animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
180 if (mOrientation == ORIENTATION_HORIZONTAL) {
181 animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
182 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
183 Animation.RELATIVE_TO_SELF, 0.0f));
184 } else {
185 animationSet.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF,
186 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
187 Animation.ABSOLUTE, 0.0f));
188 }
189 animationSet.setDuration(ANIMATION_DURATION);
190 }
191 if (mHandleInAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400192 mHandleInAnimation = new AlphaAnimation(0.0f, 1.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 mHandleInAnimation.setDuration(ANIMATION_DURATION);
194 }
195 if (mOutAnimation == null) {
196 mOutAnimation = new FastAnimationSet();
197 final AnimationSet animationSet = mOutAnimation;
198 animationSet.setInterpolator(new AccelerateInterpolator());
199 animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
200 if (mOrientation == ORIENTATION_HORIZONTAL) {
201 animationSet.addAnimation(new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
202 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
203 Animation.RELATIVE_TO_SELF, 1.0f));
204 } else {
205 animationSet.addAnimation(new FastTranslateAnimation(Animation.RELATIVE_TO_SELF,
206 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f,
207 Animation.ABSOLUTE, 0.0f));
208 }
209 animationSet.setDuration(ANIMATION_DURATION);
210 }
211 if (mHandleOutAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400212 mHandleOutAnimation = new AlphaAnimation(1.0f, 0.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 mHandleOutAnimation.setFillAfter(true);
214 mHandleOutAnimation.setDuration(ANIMATION_DURATION);
215 }
216 }
217
218 void setLauncher(Launcher launcher) {
219 mLauncher = launcher;
220 }
221
Joe Onorato00acb122009-08-04 16:04:30 -0400222 void setDragController(DragController dragController) {
223 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 }
225
226 void setHandle(View view) {
227 mHandle = view;
228 }
229
230 private static class FastTranslateAnimation extends TranslateAnimation {
231 public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
232 int fromYType, float fromYValue, int toYType, float toYValue) {
233 super(fromXType, fromXValue, toXType, toXValue,
234 fromYType, fromYValue, toYType, toYValue);
235 }
236
237 @Override
238 public boolean willChangeTransformationMatrix() {
239 return true;
240 }
241
242 @Override
243 public boolean willChangeBounds() {
244 return false;
245 }
246 }
247
248 private static class FastAnimationSet extends AnimationSet {
249 FastAnimationSet() {
250 super(false);
251 }
252
253 @Override
254 public boolean willChangeTransformationMatrix() {
255 return true;
256 }
257
258 @Override
259 public boolean willChangeBounds() {
260 return false;
261 }
262 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700263
264 @Override
265 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
266 DragView dragView, Object dragInfo) {
267 return null;
268 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269}