OSDN Git Service

6f67884db2ae9b8ea97cf62ef5b39883a6834087
[android-x86/packages-apps-Launcher.git] / src / com / android / launcher / DeleteZone.java
1 /*
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
17 package com.android.launcher;
18
19 import android.widget.ImageView;
20 import android.content.Context;
21 import android.content.res.TypedArray;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.view.animation.TranslateAnimation;
25 import android.view.animation.Animation;
26 import android.view.animation.AnimationSet;
27 import android.view.animation.AccelerateInterpolator;
28 import android.view.animation.AlphaAnimation;
29 import android.graphics.RectF;
30 import android.graphics.drawable.TransitionDrawable;
31
32 public 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
37     private final int[] mLocation = new int[2];
38     
39     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) {
78         return true;
79     }
80
81     public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo) {
82         final ItemInfo item = (ItemInfo) dragInfo;
83
84         if (item.container == -1) return;
85
86         final LauncherModel model = Launcher.getModel();
87         if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
88             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         } else if (item instanceof LauncherGadgetInfo) {
101             final LauncherGadgetInfo launcherGadgetInfo = (LauncherGadgetInfo)item;
102             final LauncherGadgetHost gadgetHost = mLauncher.getGadgetHost();
103             if (gadgetHost != null) {
104                 gadgetHost.deleteGadgetId(launcherGadgetInfo.gadgetId);
105             }
106         }
107         LauncherModel.deleteItemFromDatabase(mLauncher, item);
108     }
109
110     public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
111             Object dragInfo) {
112         mTransition.reverseTransition(TRANSITION_DURATION);
113     }
114
115     public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
116             Object dragInfo) {
117     }
118
119     public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
120             Object dragInfo) {
121         mTransition.reverseTransition(TRANSITION_DURATION);
122     }
123
124     public void onDragStart(View v, DragSource source, Object info, int dragAction) {
125         final ItemInfo item = (ItemInfo) info;
126         if (item != null) {
127             mTrashMode = true;
128             createAnimations();
129             final int[] location = mLocation;
130             getLocationOnScreen(location);
131             mRegion.set(location[0], location[1], location[0] + mRight - mLeft,
132                     location[1] + mBottom - mTop);
133             mDragLayer.setDeleteRegion(mRegion);
134             mTransition.resetTransition();
135             startAnimation(mInAnimation);
136             mHandle.startAnimation(mHandleOutAnimation);
137             setVisibility(VISIBLE);
138         }
139     }
140
141     public void onDragEnd() {
142         if (mTrashMode) {
143             mTrashMode = false;
144             mDragLayer.setDeleteRegion(null);
145             startAnimation(mOutAnimation);
146             mHandle.startAnimation(mHandleInAnimation);
147             setVisibility(GONE);
148         }
149     }
150
151     private void createAnimations() {
152         if (mInAnimation == null) {
153             mInAnimation = new FastAnimationSet();
154             final AnimationSet animationSet = mInAnimation;
155             animationSet.setInterpolator(new AccelerateInterpolator());
156             animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
157             if (mOrientation == ORIENTATION_HORIZONTAL) {
158                 animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
159                         Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
160                         Animation.RELATIVE_TO_SELF, 0.0f));
161             } else {
162                 animationSet.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF,
163                         1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
164                         Animation.ABSOLUTE, 0.0f));
165             }
166             animationSet.setDuration(ANIMATION_DURATION);
167         }
168         if (mHandleInAnimation == null) {
169             if (mOrientation == ORIENTATION_HORIZONTAL) {
170                 mHandleInAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
171                         Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
172                         Animation.RELATIVE_TO_SELF, 0.0f);
173             } else {
174                 mHandleInAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
175                         1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f,
176                         Animation.ABSOLUTE, 0.0f);
177             }
178             mHandleInAnimation.setDuration(ANIMATION_DURATION);
179         }
180         if (mOutAnimation == null) {
181             mOutAnimation = new FastAnimationSet();
182             final AnimationSet animationSet = mOutAnimation;
183             animationSet.setInterpolator(new AccelerateInterpolator());
184             animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
185             if (mOrientation == ORIENTATION_HORIZONTAL) {
186                 animationSet.addAnimation(new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
187                         Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
188                         Animation.RELATIVE_TO_SELF, 1.0f));
189             } else {
190                 animationSet.addAnimation(new FastTranslateAnimation(Animation.RELATIVE_TO_SELF,
191                         0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f,
192                         Animation.ABSOLUTE, 0.0f));
193             }
194             animationSet.setDuration(ANIMATION_DURATION);
195         }
196         if (mHandleOutAnimation == null) {
197             if (mOrientation == ORIENTATION_HORIZONTAL) {
198                 mHandleOutAnimation = new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
199                         Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
200                         Animation.RELATIVE_TO_SELF, 1.0f);
201             } else {
202                 mHandleOutAnimation = new FastTranslateAnimation(Animation.RELATIVE_TO_SELF,
203                         0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f,
204                         Animation.ABSOLUTE, 0.0f);
205             }
206             mHandleOutAnimation.setFillAfter(true);
207             mHandleOutAnimation.setDuration(ANIMATION_DURATION);
208         }
209     }
210
211     void setLauncher(Launcher launcher) {
212         mLauncher = launcher;
213     }
214
215     void setDragController(DragLayer dragLayer) {
216         mDragLayer = dragLayer;
217     }
218
219     void setHandle(View view) {
220         mHandle = view;
221     }
222
223     private static class FastTranslateAnimation extends TranslateAnimation {
224         public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
225                 int fromYType, float fromYValue, int toYType, float toYValue) {
226             super(fromXType, fromXValue, toXType, toXValue,
227                     fromYType, fromYValue, toYType, toYValue);
228         }
229
230         @Override
231         public boolean willChangeTransformationMatrix() {
232             return true;
233         }
234
235         @Override
236         public boolean willChangeBounds() {
237             return false;
238         }
239     }
240
241     private static class FastAnimationSet extends AnimationSet {
242         FastAnimationSet() {
243             super(false);
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 }