blob: ab2b891360d42331f68072bfeb2777d5f1cdd0eb [file] [log] [blame]
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001/*
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
17package com.android.launcher;
18
19import android.widget.ImageView;
20import android.content.Context;
21import android.content.res.TypedArray;
22import android.util.AttributeSet;
23import com.android.internal.provider.Settings;
24import android.view.View;
25import android.view.animation.TranslateAnimation;
26import android.view.animation.Animation;
27import android.view.animation.AnimationSet;
28import android.view.animation.AccelerateInterpolator;
29import android.view.animation.AlphaAnimation;
30import android.graphics.RectF;
31import android.graphics.drawable.TransitionDrawable;
32
33public class DeleteZone extends ImageView implements DropTarget, DragController.DragListener {
34 private static final int ORIENTATION_HORIZONTAL = 1;
35 private static final int TRANSITION_DURATION = 250;
36 private static final int ANIMATION_DURATION = 200;
37
38 private Launcher mLauncher;
39 private boolean mTrashMode;
40
41 private AnimationSet mInAnimation;
42 private AnimationSet mOutAnimation;
43 private Animation mHandleInAnimation;
44 private Animation mHandleOutAnimation;
45
46 private int mOrientation;
47 private DragLayer mDragLayer;
48
49 private final RectF mRegion = new RectF();
50 private TransitionDrawable mTransition;
51 private View mHandle;
52
53 public DeleteZone(Context context) {
54 super(context);
55 }
56
57 public DeleteZone(Context context, AttributeSet attrs) {
58 this(context, attrs, 0);
59 }
60
61 public DeleteZone(Context context, AttributeSet attrs, int defStyle) {
62 super(context, attrs, defStyle);
63
64 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0);
65 mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL);
66 a.recycle();
67 }
68
69 @Override
70 protected void onFinishInflate() {
71 super.onFinishInflate();
72 mTransition = (TransitionDrawable) getBackground();
73 }
74
75 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
76 Object dragInfo) {
77 final ItemInfo item = (ItemInfo) dragInfo;
78 // Accept anything except items in the all apps folder
79 return item.container != ItemInfo.NO_ID;
80 }
81
82 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo) {
83 final ItemInfo item = (ItemInfo) dragInfo;
84 final LauncherModel model = Launcher.getModel();
85 if (item.container == Settings.Favorites.CONTAINER_DESKTOP) {
86 model.removeDesktopItem(item);
87 } else {
88 if (source instanceof UserFolder) {
89 final UserFolder userFolder = (UserFolder) source;
90 final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder.getInfo();
91 model.removeUserFolderItem(userFolderInfo, item);
92 }
93 }
94 if (item instanceof UserFolderInfo) {
95 final UserFolderInfo userFolderInfo = (UserFolderInfo)item;
96 LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher, userFolderInfo);
97 model.removeUserFolder(userFolderInfo);
98 }
99 LauncherModel.deleteItemFromDatabase(mLauncher, item);
100 }
101
102 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
103 Object dragInfo) {
104 mTransition.reverseTransition(TRANSITION_DURATION);
105 }
106
107 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
108 Object dragInfo) {
109 }
110
111 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
112 Object dragInfo) {
113 mTransition.reverseTransition(TRANSITION_DURATION);
114 }
115
116 public void onDragStart(View v, DragSource source, Object info, int dragAction) {
117 final ItemInfo item = (ItemInfo) info;
118 if (item != null && item.container != ItemInfo.NO_ID) {
119 mTrashMode = true;
120 createAnimations();
121 final int[] location = mLocation;
122 getLocationOnScreen(location);
123 mRegion.set(location[0], location[1], location[0] + mRight - mLeft,
124 location[1] + mBottom - mTop);
125 mDragLayer.setDeleteRegion(mRegion);
126 mTransition.resetTransition();
127 startAnimation(mInAnimation);
128 mHandle.startAnimation(mHandleOutAnimation);
129 setVisibility(VISIBLE);
130 }
131 }
132
133 public void onDragEnd() {
134 if (mTrashMode) {
135 mTrashMode = false;
136 mDragLayer.setDeleteRegion(null);
137 startAnimation(mOutAnimation);
138 mHandle.startAnimation(mHandleInAnimation);
139 setVisibility(GONE);
140 }
141 }
142
143 private void createAnimations() {
144 if (mInAnimation == null) {
145 mInAnimation = new FastAnimationSet();
146 final AnimationSet animationSet = mInAnimation;
147 animationSet.setInterpolator(new AccelerateInterpolator());
148 animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
149 if (mOrientation == ORIENTATION_HORIZONTAL) {
150 animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
151 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
152 Animation.RELATIVE_TO_SELF, 0.0f));
153 } else {
154 animationSet.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF,
155 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
156 Animation.ABSOLUTE, 0.0f));
157 }
158 animationSet.setDuration(ANIMATION_DURATION);
159 }
160 if (mHandleInAnimation == null) {
161 if (mOrientation == ORIENTATION_HORIZONTAL) {
162 mHandleInAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
163 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
164 Animation.RELATIVE_TO_SELF, 0.0f);
165 } else {
166 mHandleInAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
167 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
168 Animation.ABSOLUTE, 0.0f);
169 }
170 mHandleInAnimation.setDuration(ANIMATION_DURATION);
171 }
172 if (mOutAnimation == null) {
173 mOutAnimation = new FastAnimationSet();
174 final AnimationSet animationSet = mOutAnimation;
175 animationSet.setInterpolator(new AccelerateInterpolator());
176 animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
177 if (mOrientation == ORIENTATION_HORIZONTAL) {
178 animationSet.addAnimation(new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
179 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
180 Animation.RELATIVE_TO_SELF, 1.0f));
181 } else {
182 animationSet.addAnimation(new FastTranslateAnimation(Animation.RELATIVE_TO_SELF,
183 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f,
184 Animation.ABSOLUTE, 0.0f));
185 }
186 animationSet.setDuration(ANIMATION_DURATION);
187 }
188 if (mHandleOutAnimation == null) {
189 if (mOrientation == ORIENTATION_HORIZONTAL) {
190 mHandleOutAnimation = new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
191 Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
192 Animation.RELATIVE_TO_SELF, 1.0f);
193 } else {
194 mHandleOutAnimation = new FastTranslateAnimation(Animation.RELATIVE_TO_SELF,
195 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f,
196 Animation.ABSOLUTE, 0.0f);
197 }
198 mHandleOutAnimation.setFillAfter(true);
199 mHandleOutAnimation.setDuration(ANIMATION_DURATION);
200 }
201 }
202
203 void setLauncher(Launcher launcher) {
204 mLauncher = launcher;
205 }
206
207 void setDragController(DragLayer dragLayer) {
208 mDragLayer = dragLayer;
209 }
210
211 void setHandle(View view) {
212 mHandle = view;
213 }
214
215 private static class FastTranslateAnimation extends TranslateAnimation {
216 public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
217 int fromYType, float fromYValue, int toYType, float toYValue) {
218 super(fromXType, fromXValue, toXType, toXValue,
219 fromYType, fromYValue, toYType, toYValue);
220 }
221
222 @Override
223 public boolean willChangeTransformationMatrix() {
224 return true;
225 }
226
227 @Override
228 public boolean willChangeBounds() {
229 return false;
230 }
231 }
232
233 private static class FastAnimationSet extends AnimationSet {
234 FastAnimationSet() {
235 super(false);
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}