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