blob: 43fb1a69acc2fbb15f5dc435403ef930d50034f3 [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
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -070097 if (item instanceof LauncherAppWidgetInfo) {
Joe Onorato9c1289c2009-08-17 11:03:03 -040098 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 }
100 } else {
101 if (source instanceof UserFolder) {
102 final UserFolder userFolder = (UserFolder) source;
103 final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder.getInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400104 // item must be an ApplicationInfo otherwise it couldn't have been in the folder
105 // in the first place.
106 userFolderInfo.remove((ApplicationInfo)item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 }
108 }
109 if (item instanceof UserFolderInfo) {
110 final UserFolderInfo userFolderInfo = (UserFolderInfo)item;
111 LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher, userFolderInfo);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400112 mLauncher.removeFolder(userFolderInfo);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700113 } else if (item instanceof LauncherAppWidgetInfo) {
114 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
115 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
116 if (appWidgetHost != null) {
117 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 }
119 }
120 LauncherModel.deleteItemFromDatabase(mLauncher, item);
121 }
122
123 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400124 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 mTransition.reverseTransition(TRANSITION_DURATION);
Joe Onorato00acb122009-08-04 16:04:30 -0400126 dragView.setPaint(mTrashPaint);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 }
128
129 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400130 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 }
132
133 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400134 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 mTransition.reverseTransition(TRANSITION_DURATION);
Joe Onorato00acb122009-08-04 16:04:30 -0400136 dragView.setPaint(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 }
138
139 public void onDragStart(View v, DragSource source, Object info, int dragAction) {
140 final ItemInfo item = (ItemInfo) info;
141 if (item != null) {
142 mTrashMode = true;
143 createAnimations();
144 final int[] location = mLocation;
145 getLocationOnScreen(location);
146 mRegion.set(location[0], location[1], location[0] + mRight - mLeft,
147 location[1] + mBottom - mTop);
Joe Onorato00acb122009-08-04 16:04:30 -0400148 mDragController.setDeleteRegion(mRegion);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 mTransition.resetTransition();
150 startAnimation(mInAnimation);
151 mHandle.startAnimation(mHandleOutAnimation);
152 setVisibility(VISIBLE);
153 }
154 }
155
156 public void onDragEnd() {
157 if (mTrashMode) {
158 mTrashMode = false;
Joe Onorato00acb122009-08-04 16:04:30 -0400159 mDragController.setDeleteRegion(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 startAnimation(mOutAnimation);
161 mHandle.startAnimation(mHandleInAnimation);
162 setVisibility(GONE);
163 }
164 }
165
166 private void createAnimations() {
167 if (mInAnimation == null) {
168 mInAnimation = new FastAnimationSet();
169 final AnimationSet animationSet = mInAnimation;
170 animationSet.setInterpolator(new AccelerateInterpolator());
171 animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
172 if (mOrientation == ORIENTATION_HORIZONTAL) {
173 animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
174 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
175 Animation.RELATIVE_TO_SELF, 0.0f));
176 } else {
177 animationSet.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF,
178 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
179 Animation.ABSOLUTE, 0.0f));
180 }
181 animationSet.setDuration(ANIMATION_DURATION);
182 }
183 if (mHandleInAnimation == null) {
184 if (mOrientation == ORIENTATION_HORIZONTAL) {
185 mHandleInAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
186 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
187 Animation.RELATIVE_TO_SELF, 0.0f);
188 } else {
189 mHandleInAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
190 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
191 Animation.ABSOLUTE, 0.0f);
192 }
193 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) {
212 if (mOrientation == ORIENTATION_HORIZONTAL) {
213 mHandleOutAnimation = 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 mHandleOutAnimation = 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 mHandleOutAnimation.setFillAfter(true);
222 mHandleOutAnimation.setDuration(ANIMATION_DURATION);
223 }
224 }
225
226 void setLauncher(Launcher launcher) {
227 mLauncher = launcher;
228 }
229
Joe Onorato00acb122009-08-04 16:04:30 -0400230 void setDragController(DragController dragController) {
231 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 }
233
234 void setHandle(View view) {
235 mHandle = view;
236 }
237
238 private static class FastTranslateAnimation extends TranslateAnimation {
239 public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
240 int fromYType, float fromYValue, int toYType, float toYValue) {
241 super(fromXType, fromXValue, toXType, toXValue,
242 fromYType, fromYValue, toYType, toYValue);
243 }
244
245 @Override
246 public boolean willChangeTransformationMatrix() {
247 return true;
248 }
249
250 @Override
251 public boolean willChangeBounds() {
252 return false;
253 }
254 }
255
256 private static class FastAnimationSet extends AnimationSet {
257 FastAnimationSet() {
258 super(false);
259 }
260
261 @Override
262 public boolean willChangeTransformationMatrix() {
263 return true;
264 }
265
266 @Override
267 public boolean willChangeBounds() {
268 return false;
269 }
270 }
271}