blob: 5322f6bc4266d2db0eeb5042e857482edcf03a38 [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
Patrick Dubroy39670182010-09-03 16:25:12 -070062 private Context mContext = null;
63
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064 public DeleteZone(Context context, AttributeSet attrs) {
65 this(context, attrs, 0);
66 }
67
68 public DeleteZone(Context context, AttributeSet attrs, int defStyle) {
69 super(context, attrs, defStyle);
70
Patrick Dubroy39670182010-09-03 16:25:12 -070071 mContext = context;
72
Joe Onorato00acb122009-08-04 16:04:30 -040073 final int srcColor = context.getResources().getColor(R.color.delete_color_filter);
74 mTrashPaint.setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.SRC_ATOP));
75
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0);
77 mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL);
78 a.recycle();
79 }
80
81 @Override
82 protected void onFinishInflate() {
83 super.onFinishInflate();
Joe Onoratoa9c28f62009-09-14 18:38:49 -040084 mTransition = (TransitionDrawable) getDrawable();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 }
86
87 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040088 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 return true;
90 }
Jeff Sharkey70864282009-04-07 21:08:40 -070091
Joe Onorato00acb122009-08-04 16:04:30 -040092 public Rect estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset,
93 DragView dragView, Object dragInfo, Rect recycle) {
Jeff Sharkey70864282009-04-07 21:08:40 -070094 return null;
95 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096
Joe Onorato00acb122009-08-04 16:04:30 -040097 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
98 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 final ItemInfo item = (ItemInfo) dragInfo;
100
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700101 // On x-large screens, you can uninstall an app by dragging from all apps
102 if (item instanceof ApplicationInfo && LauncherApplication.isScreenXLarge()) {
103 ApplicationInfo appInfo = (ApplicationInfo)item;
Patrick Dubroy39670182010-09-03 16:25:12 -0700104 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
105 mLauncher.startApplicationUninstallActivity(appInfo.componentName);
106 } else {
107 // System applications cannot be installed. For now, show a toast explaining that.
108 // We may give them the option of disabling apps this way.
109 int messageId = R.string.uninstall_system_app_text;
110 Toast.makeText(mContext, messageId, Toast.LENGTH_SHORT).show();
111 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700112 }
113
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 if (item.container == -1) return;
115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700117 if (item instanceof LauncherAppWidgetInfo) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400118 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700120 } else if (source instanceof UserFolder) {
121 final UserFolder userFolder = (UserFolder) source;
122 final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder.getInfo();
123 // Item must be a ShortcutInfo otherwise it couldn't have been in the folder
124 // in the first place.
125 userFolderInfo.remove((ShortcutInfo)item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -0700127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 if (item instanceof UserFolderInfo) {
129 final UserFolderInfo userFolderInfo = (UserFolderInfo)item;
130 LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher, userFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400131 mLauncher.removeFolder(userFolderInfo);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700132 } else if (item instanceof LauncherAppWidgetInfo) {
133 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
134 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
135 if (appWidgetHost != null) {
136 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
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;
163 createAnimations();
164 final int[] location = mLocation;
165 getLocationOnScreen(location);
166 mRegion.set(location[0], location[1], location[0] + mRight - mLeft,
167 location[1] + mBottom - mTop);
Joe Onorato00acb122009-08-04 16:04:30 -0400168 mDragController.setDeleteRegion(mRegion);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 mTransition.resetTransition();
170 startAnimation(mInAnimation);
171 mHandle.startAnimation(mHandleOutAnimation);
172 setVisibility(VISIBLE);
173 }
174 }
175
176 public void onDragEnd() {
177 if (mTrashMode) {
178 mTrashMode = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400179 mDragController.setDeleteRegion(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 startAnimation(mOutAnimation);
181 mHandle.startAnimation(mHandleInAnimation);
182 setVisibility(GONE);
183 }
184 }
185
186 private void createAnimations() {
187 if (mInAnimation == null) {
188 mInAnimation = new FastAnimationSet();
189 final AnimationSet animationSet = mInAnimation;
190 animationSet.setInterpolator(new AccelerateInterpolator());
191 animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
192 if (mOrientation == ORIENTATION_HORIZONTAL) {
193 animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
194 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
195 Animation.RELATIVE_TO_SELF, 0.0f));
196 } else {
197 animationSet.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF,
198 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
199 Animation.ABSOLUTE, 0.0f));
200 }
201 animationSet.setDuration(ANIMATION_DURATION);
202 }
203 if (mHandleInAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400204 mHandleInAnimation = new AlphaAnimation(0.0f, 1.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 mHandleInAnimation.setDuration(ANIMATION_DURATION);
206 }
207 if (mOutAnimation == null) {
208 mOutAnimation = new FastAnimationSet();
209 final AnimationSet animationSet = mOutAnimation;
210 animationSet.setInterpolator(new AccelerateInterpolator());
211 animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
212 if (mOrientation == ORIENTATION_HORIZONTAL) {
213 animationSet.addAnimation(new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
214 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
215 Animation.RELATIVE_TO_SELF, 1.0f));
216 } else {
217 animationSet.addAnimation(new FastTranslateAnimation(Animation.RELATIVE_TO_SELF,
218 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f,
219 Animation.ABSOLUTE, 0.0f));
220 }
221 animationSet.setDuration(ANIMATION_DURATION);
222 }
223 if (mHandleOutAnimation == null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400224 mHandleOutAnimation = new AlphaAnimation(1.0f, 0.0f);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 mHandleOutAnimation.setFillAfter(true);
226 mHandleOutAnimation.setDuration(ANIMATION_DURATION);
227 }
228 }
229
230 void setLauncher(Launcher launcher) {
231 mLauncher = launcher;
232 }
233
Joe Onorato00acb122009-08-04 16:04:30 -0400234 void setDragController(DragController dragController) {
235 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 }
237
238 void setHandle(View view) {
239 mHandle = view;
240 }
241
242 private static class FastTranslateAnimation extends TranslateAnimation {
243 public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
244 int fromYType, float fromYValue, int toYType, float toYValue) {
245 super(fromXType, fromXValue, toXType, toXValue,
246 fromYType, fromYValue, toYType, toYValue);
247 }
248
249 @Override
250 public boolean willChangeTransformationMatrix() {
251 return true;
252 }
253
254 @Override
255 public boolean willChangeBounds() {
256 return false;
257 }
258 }
259
260 private static class FastAnimationSet extends AnimationSet {
261 FastAnimationSet() {
262 super(false);
263 }
264
265 @Override
266 public boolean willChangeTransformationMatrix() {
267 return true;
268 }
269
270 @Override
271 public boolean willChangeBounds() {
272 return false;
273 }
274 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700275
276 @Override
277 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
278 DragView dragView, Object dragInfo) {
279 return null;
280 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281}