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