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 | |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 19 | import com.android.launcher.R; |
| 20 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.res.TypedArray; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 23 | import android.graphics.Paint; |
| 24 | import android.graphics.PorterDuff; |
| 25 | import android.graphics.PorterDuffColorFilter; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 26 | import android.graphics.RectF; |
| 27 | import android.graphics.drawable.TransitionDrawable; |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 28 | import android.util.AttributeSet; |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 29 | import android.view.View; |
| 30 | import android.view.animation.AccelerateInterpolator; |
| 31 | import android.view.animation.AlphaAnimation; |
| 32 | import android.view.animation.Animation; |
| 33 | import android.view.animation.AnimationSet; |
| 34 | import android.view.animation.TranslateAnimation; |
| 35 | import android.widget.ImageView; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 36 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 37 | public class DeleteZone extends ImageView implements DropTarget, DragController.DragListener { |
| 38 | private static final int ORIENTATION_HORIZONTAL = 1; |
| 39 | private static final int TRANSITION_DURATION = 250; |
| 40 | private static final int ANIMATION_DURATION = 200; |
| 41 | |
| 42 | private final int[] mLocation = new int[2]; |
| 43 | |
| 44 | private Launcher mLauncher; |
| 45 | private boolean mTrashMode; |
| 46 | |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 47 | /** |
| 48 | * If true, this View responsible for managing its own visibility, and that of its handle. |
| 49 | * This is generally the case, but it will be set to false when this is part of the |
| 50 | * Contextual Action Bar. |
| 51 | */ |
| 52 | private boolean mManageVisibility = true; |
| 53 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 54 | private AnimationSet mInAnimation; |
| 55 | private AnimationSet mOutAnimation; |
| 56 | private Animation mHandleInAnimation; |
| 57 | private Animation mHandleOutAnimation; |
| 58 | |
| 59 | private int mOrientation; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 60 | private DragController mDragController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 61 | |
| 62 | private final RectF mRegion = new RectF(); |
| 63 | private TransitionDrawable mTransition; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 64 | private final Paint mTrashPaint = new Paint(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 65 | |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 66 | /** The View that this view will replace. */ |
| 67 | private View mHandle = null; |
| 68 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 69 | public DeleteZone(Context context, AttributeSet attrs) { |
| 70 | this(context, attrs, 0); |
| 71 | } |
| 72 | |
| 73 | public DeleteZone(Context context, AttributeSet attrs, int defStyle) { |
| 74 | super(context, attrs, defStyle); |
| 75 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 76 | final int srcColor = context.getResources().getColor(R.color.delete_color_filter); |
| 77 | mTrashPaint.setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.SRC_ATOP)); |
| 78 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 79 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0); |
| 80 | mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL); |
| 81 | a.recycle(); |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 82 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | protected void onFinishInflate() { |
| 87 | super.onFinishInflate(); |
Joe Onorato | a9c28f6 | 2009-09-14 18:38:49 -0400 | [diff] [blame] | 88 | mTransition = (TransitionDrawable) getDrawable(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | 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] | 92 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 96 | public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 97 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 98 | final ItemInfo item = (ItemInfo) dragInfo; |
| 99 | |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 100 | // On x-large screens, you can uninstall an app by dragging from all apps |
| 101 | if (item instanceof ApplicationInfo && LauncherApplication.isScreenXLarge()) { |
Patrick Dubroy | 5539af7 | 2010-09-07 15:22:01 -0700 | [diff] [blame] | 102 | mLauncher.startApplicationUninstallActivity((ApplicationInfo) item); |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 103 | } |
| 104 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 105 | if (item.container == -1) return; |
| 106 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 107 | if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 108 | if (item instanceof LauncherAppWidgetInfo) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 109 | mLauncher.removeAppWidget((LauncherAppWidgetInfo) item); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 110 | } |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 111 | } else if (source instanceof UserFolder) { |
| 112 | final UserFolder userFolder = (UserFolder) source; |
| 113 | final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder.getInfo(); |
| 114 | // Item must be a ShortcutInfo otherwise it couldn't have been in the folder |
| 115 | // in the first place. |
| 116 | userFolderInfo.remove((ShortcutInfo)item); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 117 | } |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 118 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 119 | if (item instanceof UserFolderInfo) { |
| 120 | final UserFolderInfo userFolderInfo = (UserFolderInfo)item; |
| 121 | LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher, userFolderInfo); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 122 | mLauncher.removeFolder(userFolderInfo); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 123 | } else if (item instanceof LauncherAppWidgetInfo) { |
| 124 | final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item; |
| 125 | final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost(); |
| 126 | if (appWidgetHost != null) { |
Brad Fitzpatrick | 73013bf | 2010-09-14 12:15:32 -0700 | [diff] [blame] | 127 | final int appWidgetId = launcherAppWidgetInfo.appWidgetId; |
| 128 | // Deleting an app widget ID is a void call but writes to disk before returning |
| 129 | // to the caller... |
| 130 | new Thread("deleteAppWidgetId") { |
| 131 | public void run() { |
| 132 | appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId); |
| 133 | } |
| 134 | }.start(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 135 | } |
| 136 | } |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 137 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 138 | LauncherModel.deleteItemFromDatabase(mLauncher, item); |
| 139 | } |
| 140 | |
| 141 | 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] | 142 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 143 | mTransition.reverseTransition(TRANSITION_DURATION); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 144 | dragView.setPaint(mTrashPaint); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | 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] | 148 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | 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] | 152 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 153 | mTransition.reverseTransition(TRANSITION_DURATION); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 154 | dragView.setPaint(null); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 157 | public void onDragStart(DragSource source, Object info, int dragAction) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 158 | final ItemInfo item = (ItemInfo) info; |
| 159 | if (item != null) { |
| 160 | mTrashMode = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 161 | final int[] location = mLocation; |
| 162 | getLocationOnScreen(location); |
| 163 | mRegion.set(location[0], location[1], location[0] + mRight - mLeft, |
| 164 | location[1] + mBottom - mTop); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 165 | mDragController.setDeleteRegion(mRegion); |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 166 | |
| 167 | // Make sure the icon is set to the default drawable, not the hover drawable |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 168 | mTransition.resetTransition(); |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 169 | |
| 170 | if (mManageVisibility) { |
| 171 | createAnimations(); |
| 172 | startAnimation(mInAnimation); |
| 173 | mHandle.startAnimation(mHandleOutAnimation); |
| 174 | setVisibility(VISIBLE); |
| 175 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | public void onDragEnd() { |
| 180 | if (mTrashMode) { |
| 181 | mTrashMode = false; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 182 | mDragController.setDeleteRegion(null); |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 183 | |
| 184 | if (mOutAnimation != null) startAnimation(mOutAnimation); |
| 185 | if (mHandleInAnimation != null) mHandle.startAnimation(mHandleInAnimation); |
| 186 | |
| 187 | if (mManageVisibility) { |
| 188 | setVisibility(GONE); |
| 189 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame^] | 193 | public boolean isDropEnabled() { |
| 194 | return true; |
| 195 | } |
| 196 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 197 | private void createAnimations() { |
| 198 | if (mInAnimation == null) { |
| 199 | mInAnimation = new FastAnimationSet(); |
| 200 | final AnimationSet animationSet = mInAnimation; |
| 201 | animationSet.setInterpolator(new AccelerateInterpolator()); |
| 202 | animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f)); |
| 203 | if (mOrientation == ORIENTATION_HORIZONTAL) { |
| 204 | animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f, |
| 205 | Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, |
| 206 | Animation.RELATIVE_TO_SELF, 0.0f)); |
| 207 | } else { |
| 208 | animationSet.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF, |
| 209 | 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f, |
| 210 | Animation.ABSOLUTE, 0.0f)); |
| 211 | } |
| 212 | animationSet.setDuration(ANIMATION_DURATION); |
| 213 | } |
| 214 | if (mHandleInAnimation == null) { |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 215 | mHandleInAnimation = new AlphaAnimation(0.0f, 1.0f); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 216 | mHandleInAnimation.setDuration(ANIMATION_DURATION); |
| 217 | } |
| 218 | if (mOutAnimation == null) { |
| 219 | mOutAnimation = new FastAnimationSet(); |
| 220 | final AnimationSet animationSet = mOutAnimation; |
| 221 | animationSet.setInterpolator(new AccelerateInterpolator()); |
| 222 | animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f)); |
| 223 | if (mOrientation == ORIENTATION_HORIZONTAL) { |
| 224 | animationSet.addAnimation(new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f, |
| 225 | Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, |
| 226 | Animation.RELATIVE_TO_SELF, 1.0f)); |
| 227 | } else { |
| 228 | animationSet.addAnimation(new FastTranslateAnimation(Animation.RELATIVE_TO_SELF, |
| 229 | 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f, |
| 230 | Animation.ABSOLUTE, 0.0f)); |
| 231 | } |
| 232 | animationSet.setDuration(ANIMATION_DURATION); |
| 233 | } |
| 234 | if (mHandleOutAnimation == null) { |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 235 | mHandleOutAnimation = new AlphaAnimation(1.0f, 0.0f); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 236 | mHandleOutAnimation.setFillAfter(true); |
| 237 | mHandleOutAnimation.setDuration(ANIMATION_DURATION); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | void setLauncher(Launcher launcher) { |
| 242 | mLauncher = launcher; |
| 243 | } |
| 244 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 245 | void setDragController(DragController dragController) { |
| 246 | mDragController = dragController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void setHandle(View view) { |
| 250 | mHandle = view; |
| 251 | } |
| 252 | |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 253 | void setManageVisibility(boolean value) { |
| 254 | mManageVisibility = value; |
| 255 | } |
| 256 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 257 | private static class FastTranslateAnimation extends TranslateAnimation { |
| 258 | public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, |
| 259 | int fromYType, float fromYValue, int toYType, float toYValue) { |
| 260 | super(fromXType, fromXValue, toXType, toXValue, |
| 261 | fromYType, fromYValue, toYType, toYValue); |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public boolean willChangeTransformationMatrix() { |
| 266 | return true; |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public boolean willChangeBounds() { |
| 271 | return false; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | private static class FastAnimationSet extends AnimationSet { |
| 276 | FastAnimationSet() { |
| 277 | super(false); |
| 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public boolean willChangeTransformationMatrix() { |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public boolean willChangeBounds() { |
| 287 | return false; |
| 288 | } |
| 289 | } |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 290 | |
| 291 | @Override |
| 292 | public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset, |
| 293 | DragView dragView, Object dragInfo) { |
| 294 | return null; |
| 295 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 296 | } |