The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame^] | 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 | if (item instanceof LauncherGadgetInfo) { |
| 89 | model.removeDesktopGadget((LauncherGadgetInfo) item); |
| 90 | } else { |
| 91 | model.removeDesktopItem(item); |
| 92 | } |
| 93 | } else { |
| 94 | if (source instanceof UserFolder) { |
| 95 | final UserFolder userFolder = (UserFolder) source; |
| 96 | final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder.getInfo(); |
| 97 | model.removeUserFolderItem(userFolderInfo, item); |
| 98 | } |
| 99 | } |
| 100 | if (item instanceof UserFolderInfo) { |
| 101 | final UserFolderInfo userFolderInfo = (UserFolderInfo)item; |
| 102 | LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher, userFolderInfo); |
| 103 | model.removeUserFolder(userFolderInfo); |
| 104 | } else if (item instanceof LauncherGadgetInfo) { |
| 105 | final LauncherGadgetInfo launcherGadgetInfo = (LauncherGadgetInfo)item; |
| 106 | final LauncherGadgetHost gadgetHost = mLauncher.getGadgetHost(); |
| 107 | if (gadgetHost != null) { |
| 108 | gadgetHost.deleteGadgetId(launcherGadgetInfo.gadgetId); |
| 109 | } |
| 110 | } |
| 111 | LauncherModel.deleteItemFromDatabase(mLauncher, item); |
| 112 | } |
| 113 | |
| 114 | public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, |
| 115 | Object dragInfo) { |
| 116 | mTransition.reverseTransition(TRANSITION_DURATION); |
| 117 | } |
| 118 | |
| 119 | public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, |
| 120 | Object dragInfo) { |
| 121 | } |
| 122 | |
| 123 | public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, |
| 124 | Object dragInfo) { |
| 125 | mTransition.reverseTransition(TRANSITION_DURATION); |
| 126 | } |
| 127 | |
| 128 | public void onDragStart(View v, DragSource source, Object info, int dragAction) { |
| 129 | final ItemInfo item = (ItemInfo) info; |
| 130 | if (item != null) { |
| 131 | mTrashMode = true; |
| 132 | createAnimations(); |
| 133 | final int[] location = mLocation; |
| 134 | getLocationOnScreen(location); |
| 135 | mRegion.set(location[0], location[1], location[0] + mRight - mLeft, |
| 136 | location[1] + mBottom - mTop); |
| 137 | mDragLayer.setDeleteRegion(mRegion); |
| 138 | mTransition.resetTransition(); |
| 139 | startAnimation(mInAnimation); |
| 140 | mHandle.startAnimation(mHandleOutAnimation); |
| 141 | setVisibility(VISIBLE); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | public void onDragEnd() { |
| 146 | if (mTrashMode) { |
| 147 | mTrashMode = false; |
| 148 | mDragLayer.setDeleteRegion(null); |
| 149 | startAnimation(mOutAnimation); |
| 150 | mHandle.startAnimation(mHandleInAnimation); |
| 151 | setVisibility(GONE); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private void createAnimations() { |
| 156 | if (mInAnimation == null) { |
| 157 | mInAnimation = new FastAnimationSet(); |
| 158 | final AnimationSet animationSet = mInAnimation; |
| 159 | animationSet.setInterpolator(new AccelerateInterpolator()); |
| 160 | animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f)); |
| 161 | if (mOrientation == ORIENTATION_HORIZONTAL) { |
| 162 | animationSet.addAnimation(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 | animationSet.addAnimation(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 | animationSet.setDuration(ANIMATION_DURATION); |
| 171 | } |
| 172 | if (mHandleInAnimation == null) { |
| 173 | if (mOrientation == ORIENTATION_HORIZONTAL) { |
| 174 | mHandleInAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0.0f, |
| 175 | Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, |
| 176 | Animation.RELATIVE_TO_SELF, 0.0f); |
| 177 | } else { |
| 178 | mHandleInAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, |
| 179 | 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f, |
| 180 | Animation.ABSOLUTE, 0.0f); |
| 181 | } |
| 182 | mHandleInAnimation.setDuration(ANIMATION_DURATION); |
| 183 | } |
| 184 | if (mOutAnimation == null) { |
| 185 | mOutAnimation = new FastAnimationSet(); |
| 186 | final AnimationSet animationSet = mOutAnimation; |
| 187 | animationSet.setInterpolator(new AccelerateInterpolator()); |
| 188 | animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f)); |
| 189 | if (mOrientation == ORIENTATION_HORIZONTAL) { |
| 190 | animationSet.addAnimation(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 | animationSet.addAnimation(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 | animationSet.setDuration(ANIMATION_DURATION); |
| 199 | } |
| 200 | if (mHandleOutAnimation == null) { |
| 201 | if (mOrientation == ORIENTATION_HORIZONTAL) { |
| 202 | mHandleOutAnimation = new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f, |
| 203 | Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, |
| 204 | Animation.RELATIVE_TO_SELF, 1.0f); |
| 205 | } else { |
| 206 | mHandleOutAnimation = new FastTranslateAnimation(Animation.RELATIVE_TO_SELF, |
| 207 | 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f, |
| 208 | Animation.ABSOLUTE, 0.0f); |
| 209 | } |
| 210 | mHandleOutAnimation.setFillAfter(true); |
| 211 | mHandleOutAnimation.setDuration(ANIMATION_DURATION); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void setLauncher(Launcher launcher) { |
| 216 | mLauncher = launcher; |
| 217 | } |
| 218 | |
| 219 | void setDragController(DragLayer dragLayer) { |
| 220 | mDragLayer = dragLayer; |
| 221 | } |
| 222 | |
| 223 | void setHandle(View view) { |
| 224 | mHandle = view; |
| 225 | } |
| 226 | |
| 227 | private static class FastTranslateAnimation extends TranslateAnimation { |
| 228 | public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, |
| 229 | int fromYType, float fromYValue, int toYType, float toYValue) { |
| 230 | super(fromXType, fromXValue, toXType, toXValue, |
| 231 | fromYType, fromYValue, toYType, toYValue); |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public boolean willChangeTransformationMatrix() { |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public boolean willChangeBounds() { |
| 241 | return false; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | private static class FastAnimationSet extends AnimationSet { |
| 246 | FastAnimationSet() { |
| 247 | super(false); |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public boolean willChangeTransformationMatrix() { |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public boolean willChangeBounds() { |
| 257 | return false; |
| 258 | } |
| 259 | } |
| 260 | } |