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