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