2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.launcher;
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;
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;
37 private final int[] mLocation = new int[2];
39 private Launcher mLauncher;
40 private boolean mTrashMode;
42 private AnimationSet mInAnimation;
43 private AnimationSet mOutAnimation;
44 private Animation mHandleInAnimation;
45 private Animation mHandleOutAnimation;
47 private int mOrientation;
48 private DragLayer mDragLayer;
50 private final RectF mRegion = new RectF();
51 private TransitionDrawable mTransition;
54 public DeleteZone(Context context) {
58 public DeleteZone(Context context, AttributeSet attrs) {
59 this(context, attrs, 0);
62 public DeleteZone(Context context, AttributeSet attrs, int defStyle) {
63 super(context, attrs, defStyle);
65 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0);
66 mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL);
71 protected void onFinishInflate() {
72 super.onFinishInflate();
73 mTransition = (TransitionDrawable) getBackground();
76 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
81 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo) {
82 final ItemInfo item = (ItemInfo) dragInfo;
84 if (item.container == -1) return;
86 final LauncherModel model = Launcher.getModel();
87 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
88 model.removeDesktopItem(item);
90 if (source instanceof UserFolder) {
91 final UserFolder userFolder = (UserFolder) source;
92 final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder.getInfo();
93 model.removeUserFolderItem(userFolderInfo, item);
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);
107 LauncherModel.deleteItemFromDatabase(mLauncher, item);
110 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
112 mTransition.reverseTransition(TRANSITION_DURATION);
115 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
119 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
121 mTransition.reverseTransition(TRANSITION_DURATION);
124 public void onDragStart(View v, DragSource source, Object info, int dragAction) {
125 final ItemInfo item = (ItemInfo) info;
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);
141 public void onDragEnd() {
144 mDragLayer.setDeleteRegion(null);
145 startAnimation(mOutAnimation);
146 mHandle.startAnimation(mHandleInAnimation);
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));
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));
166 animationSet.setDuration(ANIMATION_DURATION);
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);
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);
178 mHandleInAnimation.setDuration(ANIMATION_DURATION);
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));
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));
194 animationSet.setDuration(ANIMATION_DURATION);
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);
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);
206 mHandleOutAnimation.setFillAfter(true);
207 mHandleOutAnimation.setDuration(ANIMATION_DURATION);
211 void setLauncher(Launcher launcher) {
212 mLauncher = launcher;
215 void setDragController(DragLayer dragLayer) {
216 mDragLayer = dragLayer;
219 void setHandle(View view) {
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);
231 public boolean willChangeTransformationMatrix() {
236 public boolean willChangeBounds() {
241 private static class FastAnimationSet extends AnimationSet {
247 public boolean willChangeTransformationMatrix() {
252 public boolean willChangeBounds() {