blob: 8a2545fd5021144a792de58d36b6f931c871229b [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
19import android.widget.ImageView;
20import android.content.Context;
21import android.content.res.TypedArray;
Joe Onorato00acb122009-08-04 16:04:30 -040022import android.graphics.Paint;
23import android.graphics.PorterDuff;
24import android.graphics.PorterDuffColorFilter;
Jeff Sharkey70864282009-04-07 21:08:40 -070025import android.graphics.Rect;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.util.AttributeSet;
27import android.view.View;
28import android.view.animation.TranslateAnimation;
29import android.view.animation.Animation;
30import android.view.animation.AnimationSet;
31import android.view.animation.AccelerateInterpolator;
32import android.view.animation.AlphaAnimation;
33import android.graphics.RectF;
34import android.graphics.drawable.TransitionDrawable;
35
36public class DeleteZone extends ImageView implements DropTarget, DragController.DragListener {
37 private static final int ORIENTATION_HORIZONTAL = 1;
38 private static final int TRANSITION_DURATION = 250;
39 private static final int ANIMATION_DURATION = 200;
40
41 private final int[] mLocation = new int[2];
42
43 private Launcher mLauncher;
44 private boolean mTrashMode;
45
46 private AnimationSet mInAnimation;
47 private AnimationSet mOutAnimation;
48 private Animation mHandleInAnimation;
49 private Animation mHandleOutAnimation;
50
51 private int mOrientation;
Joe Onorato00acb122009-08-04 16:04:30 -040052 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
54 private final RectF mRegion = new RectF();
55 private TransitionDrawable mTransition;
56 private View mHandle;
Joe Onorato00acb122009-08-04 16:04:30 -040057 private final Paint mTrashPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058
59 public DeleteZone(Context context, AttributeSet attrs) {
60 this(context, attrs, 0);
61 }
62
63 public DeleteZone(Context context, AttributeSet attrs, int defStyle) {
64 super(context, attrs, defStyle);
65
Joe Onorato00acb122009-08-04 16:04:30 -040066 final int srcColor = context.getResources().getColor(R.color.delete_color_filter);
67 mTrashPaint.setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.SRC_ATOP));
68
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0);
70 mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL);
71 a.recycle();
72 }
73
74 @Override
75 protected void onFinishInflate() {
76 super.onFinishInflate();
77 mTransition = (TransitionDrawable) getBackground();
78 }
79
80 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040081 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082 return true;
83 }
Jeff Sharkey70864282009-04-07 21:08:40 -070084
Joe Onorato00acb122009-08-04 16:04:30 -040085 public Rect estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset,
86 DragView dragView, Object dragInfo, Rect recycle) {
Jeff Sharkey70864282009-04-07 21:08:40 -070087 return null;
88 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089
Joe Onorato00acb122009-08-04 16:04:30 -040090 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
91 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 final ItemInfo item = (ItemInfo) dragInfo;
93
94 if (item.container == -1) return;
95
96 final LauncherModel model = Launcher.getModel();
97 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -070098 if (item instanceof LauncherAppWidgetInfo) {
99 model.removeDesktopAppWidget((LauncherAppWidgetInfo) item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 } else {
101 model.removeDesktopItem(item);
102 }
103 } else {
104 if (source instanceof UserFolder) {
105 final UserFolder userFolder = (UserFolder) source;
106 final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder.getInfo();
107 model.removeUserFolderItem(userFolderInfo, item);
108 }
109 }
110 if (item instanceof UserFolderInfo) {
111 final UserFolderInfo userFolderInfo = (UserFolderInfo)item;
112 LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher, userFolderInfo);
113 model.removeUserFolder(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) {
118 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 }
120 }
121 LauncherModel.deleteItemFromDatabase(mLauncher, item);
122 }
123
124 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400125 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 mTransition.reverseTransition(TRANSITION_DURATION);
Joe Onorato00acb122009-08-04 16:04:30 -0400127 dragView.setPaint(mTrashPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 }
129
130 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400131 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 }
133
134 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400135 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 mTransition.reverseTransition(TRANSITION_DURATION);
Joe Onorato00acb122009-08-04 16:04:30 -0400137 dragView.setPaint(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 }
139
140 public void onDragStart(View v, DragSource source, Object info, int dragAction) {
141 final ItemInfo item = (ItemInfo) info;
142 if (item != null) {
143 mTrashMode = true;
144 createAnimations();
145 final int[] location = mLocation;
146 getLocationOnScreen(location);
147 mRegion.set(location[0], location[1], location[0] + mRight - mLeft,
148 location[1] + mBottom - mTop);
Joe Onorato00acb122009-08-04 16:04:30 -0400149 mDragController.setDeleteRegion(mRegion);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 mTransition.resetTransition();
151 startAnimation(mInAnimation);
152 mHandle.startAnimation(mHandleOutAnimation);
153 setVisibility(VISIBLE);
154 }
155 }
156
157 public void onDragEnd() {
158 if (mTrashMode) {
159 mTrashMode = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400160 mDragController.setDeleteRegion(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 startAnimation(mOutAnimation);
162 mHandle.startAnimation(mHandleInAnimation);
163 setVisibility(GONE);
164 }
165 }
166
167 private void createAnimations() {
168 if (mInAnimation == null) {
169 mInAnimation = new FastAnimationSet();
170 final AnimationSet animationSet = mInAnimation;
171 animationSet.setInterpolator(new AccelerateInterpolator());
172 animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
173 if (mOrientation == ORIENTATION_HORIZONTAL) {
174 animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
175 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
176 Animation.RELATIVE_TO_SELF, 0.0f));
177 } else {
178 animationSet.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF,
179 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
180 Animation.ABSOLUTE, 0.0f));
181 }
182 animationSet.setDuration(ANIMATION_DURATION);
183 }
184 if (mHandleInAnimation == null) {
185 if (mOrientation == ORIENTATION_HORIZONTAL) {
186 mHandleInAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
187 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
188 Animation.RELATIVE_TO_SELF, 0.0f);
189 } else {
190 mHandleInAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
191 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
192 Animation.ABSOLUTE, 0.0f);
193 }
194 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) {
213 if (mOrientation == ORIENTATION_HORIZONTAL) {
214 mHandleOutAnimation = new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
215 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
216 Animation.RELATIVE_TO_SELF, 1.0f);
217 } else {
218 mHandleOutAnimation = new FastTranslateAnimation(Animation.RELATIVE_TO_SELF,
219 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f,
220 Animation.ABSOLUTE, 0.0f);
221 }
222 mHandleOutAnimation.setFillAfter(true);
223 mHandleOutAnimation.setDuration(ANIMATION_DURATION);
224 }
225 }
226
227 void setLauncher(Launcher launcher) {
228 mLauncher = launcher;
229 }
230
Joe Onorato00acb122009-08-04 16:04:30 -0400231 void setDragController(DragController dragController) {
232 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 }
234
235 void setHandle(View view) {
236 mHandle = view;
237 }
238
239 private static class FastTranslateAnimation extends TranslateAnimation {
240 public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
241 int fromYType, float fromYValue, int toYType, float toYValue) {
242 super(fromXType, fromXValue, toXType, toXValue,
243 fromYType, fromYValue, toYType, toYValue);
244 }
245
246 @Override
247 public boolean willChangeTransformationMatrix() {
248 return true;
249 }
250
251 @Override
252 public boolean willChangeBounds() {
253 return false;
254 }
255 }
256
257 private static class FastAnimationSet extends AnimationSet {
258 FastAnimationSet() {
259 super(false);
260 }
261
262 @Override
263 public boolean willChangeTransformationMatrix() {
264 return true;
265 }
266
267 @Override
268 public boolean willChangeBounds() {
269 return false;
270 }
271 }
272}