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 | |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 19 | import android.app.WallpaperManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.ComponentName; |
| 23 | import android.content.res.TypedArray; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 25 | import android.graphics.Canvas; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 26 | import android.graphics.Rect; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 27 | import android.graphics.drawable.Drawable; |
Joe Onorato | 080d9b6 | 2009-11-02 12:01:11 -0500 | [diff] [blame] | 28 | import android.os.Parcelable; |
| 29 | import android.os.Parcel; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | import android.util.AttributeSet; |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 31 | import android.util.Log; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 32 | import android.view.MotionEvent; |
| 33 | import android.view.VelocityTracker; |
| 34 | import android.view.View; |
| 35 | import android.view.ViewConfiguration; |
| 36 | import android.view.ViewGroup; |
| 37 | import android.view.ViewParent; |
| 38 | import android.widget.Scroller; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 39 | import android.widget.TextView; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 40 | |
| 41 | import java.util.ArrayList; |
| 42 | |
| 43 | /** |
| 44 | * The workspace is a wide area with a wallpaper and a finite number of screens. Each |
| 45 | * screen contains a number of icons, folders or widgets the user can interact with. |
| 46 | * A workspace is meant to be used with a fixed width only. |
| 47 | */ |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 48 | public class Workspace extends ViewGroup implements DropTarget, DragSource, DragScroller { |
Romain Guy | e47f55c | 2009-11-11 19:21:22 -0800 | [diff] [blame] | 49 | @SuppressWarnings({"UnusedDeclaration"}) |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 50 | private static final String TAG = "Launcher.Workspace"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 51 | private static final int INVALID_SCREEN = -1; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 52 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 53 | /** |
| 54 | * The velocity at which a fling gesture will cause us to snap to the next screen |
| 55 | */ |
| 56 | private static final int SNAP_VELOCITY = 1000; |
| 57 | |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 58 | private final WallpaperManager mWallpaperManager; |
| 59 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 60 | private int mDefaultScreen; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 61 | |
| 62 | private boolean mFirstLayout = true; |
| 63 | |
| 64 | private int mCurrentScreen; |
| 65 | private int mNextScreen = INVALID_SCREEN; |
| 66 | private Scroller mScroller; |
| 67 | private VelocityTracker mVelocityTracker; |
| 68 | |
| 69 | /** |
| 70 | * CellInfo for the cell that is currently being dragged |
| 71 | */ |
| 72 | private CellLayout.CellInfo mDragInfo; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Target drop area calculated during last acceptDrop call. |
| 76 | */ |
| 77 | private int[] mTargetCell = null; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 78 | |
| 79 | private float mLastMotionX; |
| 80 | private float mLastMotionY; |
Mike Cleron | f8bbd34 | 2009-10-23 16:15:16 -0700 | [diff] [blame] | 81 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 82 | private final static int TOUCH_STATE_REST = 0; |
| 83 | private final static int TOUCH_STATE_SCROLLING = 1; |
| 84 | |
| 85 | private int mTouchState = TOUCH_STATE_REST; |
| 86 | |
| 87 | private OnLongClickListener mLongClickListener; |
| 88 | |
| 89 | private Launcher mLauncher; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 90 | private DragController mDragController; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Cache of vacant cells, used during drag events and invalidated as needed. |
| 94 | */ |
| 95 | private CellLayout.CellInfo mVacantCache = null; |
| 96 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 97 | private int[] mTempCell = new int[2]; |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 98 | private int[] mTempEstimate = new int[2]; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 99 | |
Joe Onorato | a9c28f6 | 2009-09-14 18:38:49 -0400 | [diff] [blame] | 100 | private boolean mAllowLongPress = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 101 | |
| 102 | private int mTouchSlop; |
Romain Guy | a206daa | 2009-07-06 11:51:18 -0700 | [diff] [blame] | 103 | private int mMaximumVelocity; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 104 | |
Romain Guy | 8a73c51 | 2009-11-09 19:19:59 -0800 | [diff] [blame] | 105 | private Drawable mPreviousIndicator; |
| 106 | private Drawable mNextIndicator; |
| 107 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 108 | /** |
| 109 | * Used to inflate the Workspace from XML. |
| 110 | * |
| 111 | * @param context The application's context. |
| 112 | * @param attrs The attribtues set containing the Workspace's customization values. |
| 113 | */ |
| 114 | public Workspace(Context context, AttributeSet attrs) { |
| 115 | this(context, attrs, 0); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Used to inflate the Workspace from XML. |
| 120 | * |
| 121 | * @param context The application's context. |
| 122 | * @param attrs The attribtues set containing the Workspace's customization values. |
| 123 | * @param defStyle Unused. |
| 124 | */ |
| 125 | public Workspace(Context context, AttributeSet attrs, int defStyle) { |
| 126 | super(context, attrs, defStyle); |
| 127 | |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 128 | mWallpaperManager = WallpaperManager.getInstance(context); |
| 129 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 130 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Workspace, defStyle, 0); |
| 131 | mDefaultScreen = a.getInt(R.styleable.Workspace_defaultScreen, 1); |
| 132 | a.recycle(); |
| 133 | |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 134 | setHapticFeedbackEnabled(false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 135 | initWorkspace(); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Initializes various states for this workspace. |
| 140 | */ |
| 141 | private void initWorkspace() { |
| 142 | mScroller = new Scroller(getContext()); |
| 143 | mCurrentScreen = mDefaultScreen; |
| 144 | Launcher.setScreen(mCurrentScreen); |
| 145 | |
Romain Guy | a206daa | 2009-07-06 11:51:18 -0700 | [diff] [blame] | 146 | final ViewConfiguration configuration = ViewConfiguration.get(getContext()); |
| 147 | mTouchSlop = configuration.getScaledTouchSlop(); |
| 148 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 149 | } |
| 150 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 151 | @Override |
| 152 | public void addView(View child, int index, LayoutParams params) { |
| 153 | if (!(child instanceof CellLayout)) { |
| 154 | throw new IllegalArgumentException("A Workspace can only have CellLayout children."); |
| 155 | } |
| 156 | super.addView(child, index, params); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void addView(View child) { |
| 161 | if (!(child instanceof CellLayout)) { |
| 162 | throw new IllegalArgumentException("A Workspace can only have CellLayout children."); |
| 163 | } |
| 164 | super.addView(child); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void addView(View child, int index) { |
| 169 | if (!(child instanceof CellLayout)) { |
| 170 | throw new IllegalArgumentException("A Workspace can only have CellLayout children."); |
| 171 | } |
| 172 | super.addView(child, index); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void addView(View child, int width, int height) { |
| 177 | if (!(child instanceof CellLayout)) { |
| 178 | throw new IllegalArgumentException("A Workspace can only have CellLayout children."); |
| 179 | } |
| 180 | super.addView(child, width, height); |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void addView(View child, LayoutParams params) { |
| 185 | if (!(child instanceof CellLayout)) { |
| 186 | throw new IllegalArgumentException("A Workspace can only have CellLayout children."); |
| 187 | } |
| 188 | super.addView(child, params); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * @return The open folder on the current screen, or null if there is none |
| 193 | */ |
| 194 | Folder getOpenFolder() { |
| 195 | CellLayout currentScreen = (CellLayout) getChildAt(mCurrentScreen); |
| 196 | int count = currentScreen.getChildCount(); |
| 197 | for (int i = 0; i < count; i++) { |
| 198 | View child = currentScreen.getChildAt(i); |
| 199 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 200 | if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) { |
| 201 | return (Folder) child; |
| 202 | } |
| 203 | } |
| 204 | return null; |
| 205 | } |
| 206 | |
| 207 | ArrayList<Folder> getOpenFolders() { |
| 208 | final int screens = getChildCount(); |
| 209 | ArrayList<Folder> folders = new ArrayList<Folder>(screens); |
| 210 | |
| 211 | for (int screen = 0; screen < screens; screen++) { |
| 212 | CellLayout currentScreen = (CellLayout) getChildAt(screen); |
| 213 | int count = currentScreen.getChildCount(); |
| 214 | for (int i = 0; i < count; i++) { |
| 215 | View child = currentScreen.getChildAt(i); |
| 216 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 217 | if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) { |
| 218 | folders.add((Folder) child); |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return folders; |
| 225 | } |
| 226 | |
| 227 | boolean isDefaultScreenShowing() { |
| 228 | return mCurrentScreen == mDefaultScreen; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Returns the index of the currently displayed screen. |
| 233 | * |
| 234 | * @return The index of the currently displayed screen. |
| 235 | */ |
| 236 | int getCurrentScreen() { |
| 237 | return mCurrentScreen; |
| 238 | } |
| 239 | |
| 240 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 241 | * Sets the current screen. |
| 242 | * |
| 243 | * @param currentScreen |
| 244 | */ |
| 245 | void setCurrentScreen(int currentScreen) { |
Romain Guy | 4c58c48 | 2009-05-12 17:35:41 -0700 | [diff] [blame] | 246 | clearVacantCache(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 247 | mCurrentScreen = Math.max(0, Math.min(currentScreen, getChildCount() - 1)); |
| 248 | scrollTo(mCurrentScreen * getWidth(), 0); |
| 249 | invalidate(); |
| 250 | } |
| 251 | |
| 252 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 253 | * Adds the specified child in the current screen. The position and dimension of |
| 254 | * the child are defined by x, y, spanX and spanY. |
| 255 | * |
| 256 | * @param child The child to add in one of the workspace's screens. |
| 257 | * @param x The X position of the child in the screen's grid. |
| 258 | * @param y The Y position of the child in the screen's grid. |
| 259 | * @param spanX The number of cells spanned horizontally by the child. |
| 260 | * @param spanY The number of cells spanned vertically by the child. |
| 261 | */ |
| 262 | void addInCurrentScreen(View child, int x, int y, int spanX, int spanY) { |
| 263 | addInScreen(child, mCurrentScreen, x, y, spanX, spanY, false); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Adds the specified child in the current screen. The position and dimension of |
| 268 | * the child are defined by x, y, spanX and spanY. |
| 269 | * |
| 270 | * @param child The child to add in one of the workspace's screens. |
| 271 | * @param x The X position of the child in the screen's grid. |
| 272 | * @param y The Y position of the child in the screen's grid. |
| 273 | * @param spanX The number of cells spanned horizontally by the child. |
| 274 | * @param spanY The number of cells spanned vertically by the child. |
| 275 | * @param insert When true, the child is inserted at the beginning of the children list. |
| 276 | */ |
| 277 | void addInCurrentScreen(View child, int x, int y, int spanX, int spanY, boolean insert) { |
| 278 | addInScreen(child, mCurrentScreen, x, y, spanX, spanY, insert); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Adds the specified child in the specified screen. The position and dimension of |
| 283 | * the child are defined by x, y, spanX and spanY. |
| 284 | * |
| 285 | * @param child The child to add in one of the workspace's screens. |
| 286 | * @param screen The screen in which to add the child. |
| 287 | * @param x The X position of the child in the screen's grid. |
| 288 | * @param y The Y position of the child in the screen's grid. |
| 289 | * @param spanX The number of cells spanned horizontally by the child. |
| 290 | * @param spanY The number of cells spanned vertically by the child. |
| 291 | */ |
| 292 | void addInScreen(View child, int screen, int x, int y, int spanX, int spanY) { |
| 293 | addInScreen(child, screen, x, y, spanX, spanY, false); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Adds the specified child in the specified screen. The position and dimension of |
| 298 | * the child are defined by x, y, spanX and spanY. |
| 299 | * |
| 300 | * @param child The child to add in one of the workspace's screens. |
| 301 | * @param screen The screen in which to add the child. |
| 302 | * @param x The X position of the child in the screen's grid. |
| 303 | * @param y The Y position of the child in the screen's grid. |
| 304 | * @param spanX The number of cells spanned horizontally by the child. |
| 305 | * @param spanY The number of cells spanned vertically by the child. |
| 306 | * @param insert When true, the child is inserted at the beginning of the children list. |
| 307 | */ |
| 308 | void addInScreen(View child, int screen, int x, int y, int spanX, int spanY, boolean insert) { |
| 309 | if (screen < 0 || screen >= getChildCount()) { |
| 310 | throw new IllegalStateException("The screen must be >= 0 and < " + getChildCount()); |
| 311 | } |
| 312 | |
Romain Guy | 4c58c48 | 2009-05-12 17:35:41 -0700 | [diff] [blame] | 313 | clearVacantCache(); |
| 314 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 315 | final CellLayout group = (CellLayout) getChildAt(screen); |
| 316 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 317 | if (lp == null) { |
| 318 | lp = new CellLayout.LayoutParams(x, y, spanX, spanY); |
| 319 | } else { |
| 320 | lp.cellX = x; |
| 321 | lp.cellY = y; |
| 322 | lp.cellHSpan = spanX; |
| 323 | lp.cellVSpan = spanY; |
| 324 | } |
| 325 | group.addView(child, insert ? 0 : -1, lp); |
| 326 | if (!(child instanceof Folder)) { |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 327 | child.setHapticFeedbackEnabled(false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 328 | child.setOnLongClickListener(mLongClickListener); |
| 329 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 330 | if (child instanceof DropTarget) { |
| 331 | mDragController.addDropTarget((DropTarget)child); |
| 332 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 333 | } |
| 334 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 335 | CellLayout.CellInfo findAllVacantCells(boolean[] occupied) { |
| 336 | CellLayout group = (CellLayout) getChildAt(mCurrentScreen); |
| 337 | if (group != null) { |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 338 | return group.findAllVacantCells(occupied, null); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 339 | } |
| 340 | return null; |
| 341 | } |
| 342 | |
Romain Guy | 4c58c48 | 2009-05-12 17:35:41 -0700 | [diff] [blame] | 343 | private void clearVacantCache() { |
| 344 | if (mVacantCache != null) { |
| 345 | mVacantCache.clearVacantCells(); |
| 346 | mVacantCache = null; |
| 347 | } |
| 348 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 349 | |
| 350 | /** |
| 351 | * Registers the specified listener on each screen contained in this workspace. |
| 352 | * |
| 353 | * @param l The listener used to respond to long clicks. |
| 354 | */ |
| 355 | @Override |
| 356 | public void setOnLongClickListener(OnLongClickListener l) { |
| 357 | mLongClickListener = l; |
| 358 | final int count = getChildCount(); |
| 359 | for (int i = 0; i < count; i++) { |
| 360 | getChildAt(i).setOnLongClickListener(l); |
| 361 | } |
| 362 | } |
| 363 | |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 364 | private void updateWallpaperOffset() { |
Romain Guy | 798300c | 2009-08-11 14:43:29 -0700 | [diff] [blame] | 365 | updateWallpaperOffset(getChildAt(getChildCount() - 1).getRight() - (mRight - mLeft)); |
| 366 | } |
| 367 | |
| 368 | private void updateWallpaperOffset(int scrollRange) { |
Marco Nelissen | c07c79b | 2009-11-09 16:08:03 -0800 | [diff] [blame] | 369 | mWallpaperManager.setWallpaperOffsetSteps(1.0f / (getChildCount() - 1), 0 ); |
Dianne Hackborn | 49cdb1b | 2009-08-13 16:50:18 -0700 | [diff] [blame] | 370 | mWallpaperManager.setWallpaperOffsets(getWindowToken(), mScrollX / (float) scrollRange, 0); |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 371 | } |
| 372 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 373 | @Override |
| 374 | public void computeScroll() { |
| 375 | if (mScroller.computeScrollOffset()) { |
| 376 | mScrollX = mScroller.getCurrX(); |
| 377 | mScrollY = mScroller.getCurrY(); |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 378 | updateWallpaperOffset(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 379 | postInvalidate(); |
| 380 | } else if (mNextScreen != INVALID_SCREEN) { |
| 381 | mCurrentScreen = Math.max(0, Math.min(mNextScreen, getChildCount() - 1)); |
Romain Guy | 8a73c51 | 2009-11-09 19:19:59 -0800 | [diff] [blame] | 382 | mPreviousIndicator.setLevel(mCurrentScreen); |
| 383 | mNextIndicator.setLevel(mCurrentScreen); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 384 | Launcher.setScreen(mCurrentScreen); |
| 385 | mNextScreen = INVALID_SCREEN; |
| 386 | clearChildrenCache(); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | @Override |
| 391 | protected void dispatchDraw(Canvas canvas) { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 392 | boolean restore = false; |
Joe Onorato | e77c08d | 2009-08-01 00:01:20 -0700 | [diff] [blame] | 393 | int restoreCount = 0; |
| 394 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 395 | // ViewGroup.dispatchDraw() supports many features we don't need: |
| 396 | // clip to padding, layout animation, animation listener, disappearing |
| 397 | // children, etc. The following implementation attempts to fast-track |
| 398 | // the drawing dispatch by drawing only what we know needs to be drawn. |
| 399 | |
Romain Guy | f8e6a80 | 2009-12-07 17:48:02 -0800 | [diff] [blame] | 400 | boolean fastDraw = mTouchState != TOUCH_STATE_SCROLLING && mNextScreen == INVALID_SCREEN; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 401 | // If we are not scrolling or flinging, draw only the current screen |
| 402 | if (fastDraw) { |
| 403 | drawChild(canvas, getChildAt(mCurrentScreen), getDrawingTime()); |
| 404 | } else { |
| 405 | final long drawingTime = getDrawingTime(); |
| 406 | // If we are flinging, draw only the current screen and the target screen |
| 407 | if (mNextScreen >= 0 && mNextScreen < getChildCount() && |
| 408 | Math.abs(mCurrentScreen - mNextScreen) == 1) { |
| 409 | drawChild(canvas, getChildAt(mCurrentScreen), drawingTime); |
| 410 | drawChild(canvas, getChildAt(mNextScreen), drawingTime); |
| 411 | } else { |
| 412 | // If we are scrolling, draw all of our children |
| 413 | final int count = getChildCount(); |
| 414 | for (int i = 0; i < count; i++) { |
| 415 | drawChild(canvas, getChildAt(i), drawingTime); |
| 416 | } |
| 417 | } |
| 418 | } |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 419 | |
| 420 | if (restore) { |
Joe Onorato | e77c08d | 2009-08-01 00:01:20 -0700 | [diff] [blame] | 421 | canvas.restoreToCount(restoreCount); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 422 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 425 | protected void onAttachedToWindow() { |
| 426 | super.onAttachedToWindow(); |
| 427 | mDragController.setWindowToken(getWindowToken()); |
| 428 | } |
| 429 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 430 | @Override |
| 431 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 432 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 433 | |
| 434 | final int width = MeasureSpec.getSize(widthMeasureSpec); |
| 435 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 436 | if (widthMode != MeasureSpec.EXACTLY) { |
| 437 | throw new IllegalStateException("Workspace can only be used in EXACTLY mode."); |
| 438 | } |
| 439 | |
| 440 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 441 | if (heightMode != MeasureSpec.EXACTLY) { |
| 442 | throw new IllegalStateException("Workspace can only be used in EXACTLY mode."); |
| 443 | } |
| 444 | |
| 445 | // The children are given the same width and height as the workspace |
| 446 | final int count = getChildCount(); |
| 447 | for (int i = 0; i < count; i++) { |
| 448 | getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec); |
| 449 | } |
| 450 | |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 451 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 452 | if (mFirstLayout) { |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 453 | setHorizontalScrollBarEnabled(false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 454 | scrollTo(mCurrentScreen * width, 0); |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 455 | setHorizontalScrollBarEnabled(true); |
Romain Guy | 798300c | 2009-08-11 14:43:29 -0700 | [diff] [blame] | 456 | updateWallpaperOffset(width * (getChildCount() - 1)); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 457 | mFirstLayout = false; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | @Override |
| 462 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 463 | int childLeft = 0; |
| 464 | |
| 465 | final int count = getChildCount(); |
| 466 | for (int i = 0; i < count; i++) { |
| 467 | final View child = getChildAt(i); |
| 468 | if (child.getVisibility() != View.GONE) { |
| 469 | final int childWidth = child.getMeasuredWidth(); |
| 470 | child.layout(childLeft, 0, childLeft + childWidth, child.getMeasuredHeight()); |
| 471 | childLeft += childWidth; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | @Override |
| 477 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
| 478 | int screen = indexOfChild(child); |
| 479 | if (screen != mCurrentScreen || !mScroller.isFinished()) { |
| 480 | if (!mLauncher.isWorkspaceLocked()) { |
| 481 | snapToScreen(screen); |
| 482 | } |
| 483 | return true; |
| 484 | } |
| 485 | return false; |
| 486 | } |
| 487 | |
| 488 | @Override |
| 489 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Joe Onorato | 6788621 | 2009-09-14 19:05:05 -0400 | [diff] [blame] | 490 | if (!mLauncher.isAllAppsVisible()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 491 | final Folder openFolder = getOpenFolder(); |
| 492 | if (openFolder != null) { |
| 493 | return openFolder.requestFocus(direction, previouslyFocusedRect); |
| 494 | } else { |
| 495 | int focusableScreen; |
| 496 | if (mNextScreen != INVALID_SCREEN) { |
| 497 | focusableScreen = mNextScreen; |
| 498 | } else { |
| 499 | focusableScreen = mCurrentScreen; |
| 500 | } |
| 501 | getChildAt(focusableScreen).requestFocus(direction, previouslyFocusedRect); |
| 502 | } |
| 503 | } |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | @Override |
| 508 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 509 | if (direction == View.FOCUS_LEFT) { |
| 510 | if (getCurrentScreen() > 0) { |
| 511 | snapToScreen(getCurrentScreen() - 1); |
| 512 | return true; |
| 513 | } |
| 514 | } else if (direction == View.FOCUS_RIGHT) { |
| 515 | if (getCurrentScreen() < getChildCount() - 1) { |
| 516 | snapToScreen(getCurrentScreen() + 1); |
| 517 | return true; |
| 518 | } |
| 519 | } |
| 520 | return super.dispatchUnhandledMove(focused, direction); |
| 521 | } |
| 522 | |
| 523 | @Override |
Romain Guy | c2e24c0 | 2009-06-01 16:11:41 -0700 | [diff] [blame] | 524 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Joe Onorato | 6788621 | 2009-09-14 19:05:05 -0400 | [diff] [blame] | 525 | if (!mLauncher.isAllAppsVisible()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 526 | final Folder openFolder = getOpenFolder(); |
| 527 | if (openFolder == null) { |
| 528 | getChildAt(mCurrentScreen).addFocusables(views, direction); |
| 529 | if (direction == View.FOCUS_LEFT) { |
| 530 | if (mCurrentScreen > 0) { |
| 531 | getChildAt(mCurrentScreen - 1).addFocusables(views, direction); |
| 532 | } |
| 533 | } else if (direction == View.FOCUS_RIGHT){ |
| 534 | if (mCurrentScreen < getChildCount() - 1) { |
| 535 | getChildAt(mCurrentScreen + 1).addFocusables(views, direction); |
| 536 | } |
| 537 | } |
| 538 | } else { |
| 539 | openFolder.addFocusables(views, direction); |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | @Override |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 545 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 546 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 547 | if (mLauncher.isWorkspaceLocked() || mLauncher.isAllAppsVisible()) { |
| 548 | return false; |
| 549 | } |
| 550 | } |
| 551 | return super.dispatchTouchEvent(ev); |
| 552 | } |
| 553 | |
| 554 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 555 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 556 | final boolean workspaceLocked = mLauncher.isWorkspaceLocked(); |
| 557 | final boolean allAppsVisible = mLauncher.isAllAppsVisible(); |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 558 | if (workspaceLocked || allAppsVisible) { |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 559 | return false; // We don't want the events. Let them fall through to the all apps view. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /* |
| 563 | * This method JUST determines whether we want to intercept the motion. |
| 564 | * If we return true, onTouchEvent will be called and we do the actual |
| 565 | * scrolling there. |
| 566 | */ |
| 567 | |
| 568 | /* |
| 569 | * Shortcut the most recurring case: the user is in the dragging |
| 570 | * state and he is moving his finger. We want to intercept this |
| 571 | * motion. |
| 572 | */ |
| 573 | final int action = ev.getAction(); |
| 574 | if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) { |
| 575 | return true; |
| 576 | } |
| 577 | |
| 578 | final float x = ev.getX(); |
| 579 | final float y = ev.getY(); |
| 580 | |
| 581 | switch (action) { |
| 582 | case MotionEvent.ACTION_MOVE: |
| 583 | /* |
| 584 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
| 585 | * whether the user has moved far enough from his original down touch. |
| 586 | */ |
| 587 | |
| 588 | /* |
| 589 | * Locally do absolute value. mLastMotionX is set to the y value |
| 590 | * of the down event. |
| 591 | */ |
| 592 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
| 593 | final int yDiff = (int) Math.abs(y - mLastMotionY); |
| 594 | |
| 595 | final int touchSlop = mTouchSlop; |
| 596 | boolean xMoved = xDiff > touchSlop; |
| 597 | boolean yMoved = yDiff > touchSlop; |
| 598 | |
| 599 | if (xMoved || yMoved) { |
| 600 | |
| 601 | if (xMoved) { |
| 602 | // Scroll if the user moved far enough along the X axis |
| 603 | mTouchState = TOUCH_STATE_SCROLLING; |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 604 | enableChildrenCache(mCurrentScreen - 1, mCurrentScreen + 1); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 605 | } |
| 606 | // Either way, cancel any pending longpress |
| 607 | if (mAllowLongPress) { |
| 608 | mAllowLongPress = false; |
| 609 | // Try canceling the long press. It could also have been scheduled |
| 610 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 611 | // everything |
| 612 | final View currentScreen = getChildAt(mCurrentScreen); |
| 613 | currentScreen.cancelLongPress(); |
| 614 | } |
| 615 | } |
| 616 | break; |
| 617 | |
| 618 | case MotionEvent.ACTION_DOWN: |
| 619 | // Remember location of down touch |
| 620 | mLastMotionX = x; |
| 621 | mLastMotionY = y; |
| 622 | mAllowLongPress = true; |
| 623 | |
| 624 | /* |
| 625 | * If being flinged and user touches the screen, initiate drag; |
| 626 | * otherwise don't. mScroller.isFinished should be false when |
| 627 | * being flinged. |
| 628 | */ |
| 629 | mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING; |
| 630 | break; |
| 631 | |
| 632 | case MotionEvent.ACTION_CANCEL: |
| 633 | case MotionEvent.ACTION_UP: |
Mike Cleron | f8bbd34 | 2009-10-23 16:15:16 -0700 | [diff] [blame] | 634 | |
| 635 | if (mTouchState != TOUCH_STATE_SCROLLING) { |
| 636 | |
| 637 | final CellLayout currentScreen = (CellLayout)getChildAt(mCurrentScreen); |
| 638 | if (!currentScreen.lastDownOnOccupiedCell()) { |
| 639 | // Send a tap to the wallpaper if the last down was on empty space |
| 640 | mWallpaperManager.sendWallpaperCommand(getWindowToken(), |
| 641 | "android.wallpaper.tap", (int) ev.getX(), (int) ev.getY(), 0, null); |
| 642 | } |
| 643 | } |
| 644 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 645 | // Release the drag |
| 646 | clearChildrenCache(); |
| 647 | mTouchState = TOUCH_STATE_REST; |
| 648 | mAllowLongPress = false; |
Mike Cleron | f8bbd34 | 2009-10-23 16:15:16 -0700 | [diff] [blame] | 649 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 650 | break; |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * The only time we want to intercept motion events is if we are in the |
| 655 | * drag mode. |
| 656 | */ |
| 657 | return mTouchState != TOUCH_STATE_REST; |
| 658 | } |
| 659 | |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 660 | /** |
| 661 | * If one of our descendant views decides that it could be focused now, only |
| 662 | * pass that along if it's on the current screen. |
| 663 | * |
| 664 | * This happens when live folders requery, and if they're off screen, they |
| 665 | * end up calling requestFocus, which pulls it on screen. |
| 666 | */ |
| 667 | @Override |
| 668 | public void focusableViewAvailable(View focused) { |
| 669 | View current = getChildAt(mCurrentScreen); |
| 670 | View v = focused; |
| 671 | while (true) { |
| 672 | if (v == current) { |
| 673 | super.focusableViewAvailable(focused); |
| 674 | return; |
| 675 | } |
| 676 | if (v == this) { |
| 677 | return; |
| 678 | } |
| 679 | ViewParent parent = v.getParent(); |
| 680 | if (parent instanceof View) { |
| 681 | v = (View)v.getParent(); |
| 682 | } else { |
| 683 | return; |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 688 | void enableChildrenCache(int fromScreen, int toScreen) { |
| 689 | if (fromScreen > toScreen) { |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 690 | fromScreen = toScreen; |
| 691 | toScreen = fromScreen; |
| 692 | } |
| 693 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 694 | final int count = getChildCount(); |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 695 | |
| 696 | fromScreen = Math.max(fromScreen, 0); |
| 697 | toScreen = Math.min(toScreen, count - 1); |
| 698 | |
| 699 | for (int i = fromScreen; i <= toScreen; i++) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 700 | final CellLayout layout = (CellLayout) getChildAt(i); |
| 701 | layout.setChildrenDrawnWithCacheEnabled(true); |
| 702 | layout.setChildrenDrawingCacheEnabled(true); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | void clearChildrenCache() { |
| 707 | final int count = getChildCount(); |
| 708 | for (int i = 0; i < count; i++) { |
| 709 | final CellLayout layout = (CellLayout) getChildAt(i); |
| 710 | layout.setChildrenDrawnWithCacheEnabled(false); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | @Override |
| 715 | public boolean onTouchEvent(MotionEvent ev) { |
Mike Cleron | f8bbd34 | 2009-10-23 16:15:16 -0700 | [diff] [blame] | 716 | |
Joe Onorato | 2bc6b7c | 2009-10-01 14:08:30 -0700 | [diff] [blame] | 717 | if (mLauncher.isWorkspaceLocked()) { |
| 718 | return false; // We don't want the events. Let them fall through to the all apps view. |
| 719 | } |
| 720 | if (mLauncher.isAllAppsVisible()) { |
| 721 | // Cancel any scrolling that is in progress. |
| 722 | if (!mScroller.isFinished()) { |
| 723 | mScroller.abortAnimation(); |
| 724 | } |
| 725 | snapToScreen(mCurrentScreen); |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 726 | return false; // We don't want the events. Let them fall through to the all apps view. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | if (mVelocityTracker == null) { |
| 730 | mVelocityTracker = VelocityTracker.obtain(); |
| 731 | } |
| 732 | mVelocityTracker.addMovement(ev); |
| 733 | |
| 734 | final int action = ev.getAction(); |
| 735 | final float x = ev.getX(); |
| 736 | |
| 737 | switch (action) { |
| 738 | case MotionEvent.ACTION_DOWN: |
| 739 | /* |
| 740 | * If being flinged and user touches, stop the fling. isFinished |
| 741 | * will be false if being flinged. |
| 742 | */ |
| 743 | if (!mScroller.isFinished()) { |
| 744 | mScroller.abortAnimation(); |
| 745 | } |
| 746 | |
| 747 | // Remember where the motion event started |
| 748 | mLastMotionX = x; |
| 749 | break; |
| 750 | case MotionEvent.ACTION_MOVE: |
| 751 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 752 | // Scroll to follow the motion event |
| 753 | final int deltaX = (int) (mLastMotionX - x); |
| 754 | mLastMotionX = x; |
| 755 | |
| 756 | if (deltaX < 0) { |
| 757 | if (mScrollX > 0) { |
| 758 | scrollBy(Math.max(-mScrollX, deltaX), 0); |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 759 | updateWallpaperOffset(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 760 | } |
| 761 | } else if (deltaX > 0) { |
| 762 | final int availableToScroll = getChildAt(getChildCount() - 1).getRight() - |
| 763 | mScrollX - getWidth(); |
| 764 | if (availableToScroll > 0) { |
| 765 | scrollBy(Math.min(availableToScroll, deltaX), 0); |
Dianne Hackborn | 8f57395 | 2009-08-10 23:21:09 -0700 | [diff] [blame] | 766 | updateWallpaperOffset(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 767 | } |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 768 | } else { |
| 769 | awakenScrollBars(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | break; |
| 773 | case MotionEvent.ACTION_UP: |
| 774 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 775 | final VelocityTracker velocityTracker = mVelocityTracker; |
Romain Guy | a206daa | 2009-07-06 11:51:18 -0700 | [diff] [blame] | 776 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 777 | int velocityX = (int) velocityTracker.getXVelocity(); |
| 778 | |
| 779 | if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) { |
| 780 | // Fling hard enough to move left |
| 781 | snapToScreen(mCurrentScreen - 1); |
| 782 | } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) { |
| 783 | // Fling hard enough to move right |
| 784 | snapToScreen(mCurrentScreen + 1); |
| 785 | } else { |
| 786 | snapToDestination(); |
| 787 | } |
| 788 | |
| 789 | if (mVelocityTracker != null) { |
| 790 | mVelocityTracker.recycle(); |
| 791 | mVelocityTracker = null; |
| 792 | } |
| 793 | } |
| 794 | mTouchState = TOUCH_STATE_REST; |
| 795 | break; |
| 796 | case MotionEvent.ACTION_CANCEL: |
| 797 | mTouchState = TOUCH_STATE_REST; |
| 798 | } |
| 799 | |
| 800 | return true; |
| 801 | } |
| 802 | |
| 803 | private void snapToDestination() { |
| 804 | final int screenWidth = getWidth(); |
| 805 | final int whichScreen = (mScrollX + (screenWidth / 2)) / screenWidth; |
| 806 | |
| 807 | snapToScreen(whichScreen); |
| 808 | } |
| 809 | |
| 810 | void snapToScreen(int whichScreen) { |
Joe Onorato | 14f122b | 2009-11-19 14:06:36 -0800 | [diff] [blame] | 811 | snapToScreen(whichScreen, true); |
| 812 | } |
| 813 | |
| 814 | void snapToScreen(int whichScreen, boolean animate) { |
Romain Guy | e47f55c | 2009-11-11 19:21:22 -0800 | [diff] [blame] | 815 | //if (!mScroller.isFinished()) return; |
Romain Guy | 82d94d9 | 2009-05-06 17:43:13 -0700 | [diff] [blame] | 816 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 817 | whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1)); |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 818 | |
| 819 | clearVacantCache(); |
| 820 | enableChildrenCache(mCurrentScreen, whichScreen); |
| 821 | |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 822 | final int screenDelta = Math.abs(whichScreen - mCurrentScreen); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 823 | |
| 824 | mNextScreen = whichScreen; |
Romain Guy | e47f55c | 2009-11-11 19:21:22 -0800 | [diff] [blame] | 825 | |
| 826 | mPreviousIndicator.setLevel(mNextScreen); |
| 827 | mNextIndicator.setLevel(mNextScreen); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 828 | |
| 829 | View focusedChild = getFocusedChild(); |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 830 | if (focusedChild != null && screenDelta != 0 && focusedChild == getChildAt(mCurrentScreen)) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 831 | focusedChild.clearFocus(); |
| 832 | } |
| 833 | |
| 834 | final int newX = whichScreen * getWidth(); |
| 835 | final int delta = newX - mScrollX; |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 836 | final int duration = screenDelta * 300; |
| 837 | awakenScrollBars(duration); |
Romain Guy | f8e6a80 | 2009-12-07 17:48:02 -0800 | [diff] [blame] | 838 | |
Joe Onorato | 14f122b | 2009-11-19 14:06:36 -0800 | [diff] [blame] | 839 | // 1ms is close to don't animate |
Romain Guy | f8e6a80 | 2009-12-07 17:48:02 -0800 | [diff] [blame] | 840 | if (!mScroller.isFinished()) mScroller.abortAnimation(); |
Joe Onorato | 14f122b | 2009-11-19 14:06:36 -0800 | [diff] [blame] | 841 | mScroller.startScroll(mScrollX, 0, delta, 0, animate ? duration : 1); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 842 | invalidate(); |
| 843 | } |
| 844 | |
| 845 | void startDrag(CellLayout.CellInfo cellInfo) { |
| 846 | View child = cellInfo.cell; |
| 847 | |
| 848 | // Make sure the drag was started by a long press as opposed to a long click. |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame^] | 849 | if (!child.isInTouchMode()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 850 | return; |
| 851 | } |
| 852 | |
| 853 | mDragInfo = cellInfo; |
| 854 | mDragInfo.screen = mCurrentScreen; |
| 855 | |
| 856 | CellLayout current = ((CellLayout) getChildAt(mCurrentScreen)); |
| 857 | |
| 858 | current.onDragChild(child); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 859 | mDragController.startDrag(child, this, child.getTag(), DragController.DRAG_ACTION_MOVE); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 860 | invalidate(); |
| 861 | } |
| 862 | |
| 863 | @Override |
| 864 | protected Parcelable onSaveInstanceState() { |
| 865 | final SavedState state = new SavedState(super.onSaveInstanceState()); |
| 866 | state.currentScreen = mCurrentScreen; |
| 867 | return state; |
| 868 | } |
| 869 | |
| 870 | @Override |
| 871 | protected void onRestoreInstanceState(Parcelable state) { |
| 872 | SavedState savedState = (SavedState) state; |
| 873 | super.onRestoreInstanceState(savedState.getSuperState()); |
| 874 | if (savedState.currentScreen != -1) { |
| 875 | mCurrentScreen = savedState.currentScreen; |
| 876 | Launcher.setScreen(mCurrentScreen); |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | void addApplicationShortcut(ApplicationInfo info, CellLayout.CellInfo cellInfo) { |
| 881 | addApplicationShortcut(info, cellInfo, false); |
| 882 | } |
| 883 | |
| 884 | void addApplicationShortcut(ApplicationInfo info, CellLayout.CellInfo cellInfo, |
| 885 | boolean insertAtFirst) { |
| 886 | final CellLayout layout = (CellLayout) getChildAt(cellInfo.screen); |
| 887 | final int[] result = new int[2]; |
| 888 | |
| 889 | layout.cellToPoint(cellInfo.cellX, cellInfo.cellY, result); |
| 890 | onDropExternal(result[0], result[1], info, layout, insertAtFirst); |
| 891 | } |
| 892 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 893 | public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 894 | DragView dragView, Object dragInfo) { |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 895 | final CellLayout cellLayout = getCurrentDropLayout(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 896 | if (source != this) { |
| 897 | onDropExternal(x - xOffset, y - yOffset, dragInfo, cellLayout); |
| 898 | } else { |
| 899 | // Move internally |
| 900 | if (mDragInfo != null) { |
| 901 | final View cell = mDragInfo.cell; |
Romain Guy | 52d9cb3 | 2009-09-11 17:29:51 -0700 | [diff] [blame] | 902 | int index = mScroller.isFinished() ? mCurrentScreen : mNextScreen; |
| 903 | if (index != mDragInfo.screen) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 904 | final CellLayout originalCellLayout = (CellLayout) getChildAt(mDragInfo.screen); |
| 905 | originalCellLayout.removeView(cell); |
| 906 | cellLayout.addView(cell); |
| 907 | } |
Romain Guy | 263e019 | 2009-05-11 11:50:46 -0700 | [diff] [blame] | 908 | mTargetCell = estimateDropCell(x - xOffset, y - yOffset, |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 909 | mDragInfo.spanX, mDragInfo.spanY, cell, cellLayout, mTargetCell); |
| 910 | cellLayout.onDropChild(cell, mTargetCell); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 911 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 912 | final ItemInfo info = (ItemInfo) cell.getTag(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 913 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams(); |
| 914 | LauncherModel.moveItemInDatabase(mLauncher, info, |
Romain Guy | 52d9cb3 | 2009-09-11 17:29:51 -0700 | [diff] [blame] | 915 | LauncherSettings.Favorites.CONTAINER_DESKTOP, index, lp.cellX, lp.cellY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | 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] | 921 | DragView dragView, Object dragInfo) { |
Romain Guy | 4c58c48 | 2009-05-12 17:35:41 -0700 | [diff] [blame] | 922 | clearVacantCache(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | 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] | 926 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | 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] | 930 | DragView dragView, Object dragInfo) { |
Romain Guy | 4c58c48 | 2009-05-12 17:35:41 -0700 | [diff] [blame] | 931 | clearVacantCache(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | private void onDropExternal(int x, int y, Object dragInfo, CellLayout cellLayout) { |
| 935 | onDropExternal(x, y, dragInfo, cellLayout, false); |
| 936 | } |
| 937 | |
| 938 | private void onDropExternal(int x, int y, Object dragInfo, CellLayout cellLayout, |
| 939 | boolean insertAtFirst) { |
| 940 | // Drag from somewhere else |
| 941 | ItemInfo info = (ItemInfo) dragInfo; |
| 942 | |
| 943 | View view; |
| 944 | |
| 945 | switch (info.itemType) { |
| 946 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 947 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
| 948 | if (info.container == NO_ID) { |
| 949 | // Came from all apps -- make a copy |
| 950 | info = new ApplicationInfo((ApplicationInfo) info); |
| 951 | } |
| 952 | view = mLauncher.createShortcut(R.layout.application, cellLayout, |
| 953 | (ApplicationInfo) info); |
| 954 | break; |
| 955 | case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER: |
| 956 | view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher, |
| 957 | (ViewGroup) getChildAt(mCurrentScreen), ((UserFolderInfo) info)); |
| 958 | break; |
| 959 | default: |
| 960 | throw new IllegalStateException("Unknown item type: " + info.itemType); |
| 961 | } |
| 962 | |
| 963 | cellLayout.addView(view, insertAtFirst ? 0 : -1); |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 964 | view.setHapticFeedbackEnabled(false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 965 | view.setOnLongClickListener(mLongClickListener); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 966 | if (view instanceof DropTarget) { |
Romain Guy | 207e40e | 2009-09-29 16:19:19 -0700 | [diff] [blame] | 967 | mDragController.addDropTarget((DropTarget) view); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 968 | } |
| 969 | |
Romain Guy | 263e019 | 2009-05-11 11:50:46 -0700 | [diff] [blame] | 970 | mTargetCell = estimateDropCell(x, y, 1, 1, view, cellLayout, mTargetCell); |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 971 | cellLayout.onDropChild(view, mTargetCell); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 972 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams(); |
| 973 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 974 | LauncherModel.addOrMoveItemInDatabase(mLauncher, info, |
| 975 | LauncherSettings.Favorites.CONTAINER_DESKTOP, mCurrentScreen, lp.cellX, lp.cellY); |
| 976 | } |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 977 | |
| 978 | /** |
| 979 | * Return the current {@link CellLayout}, correctly picking the destination |
| 980 | * screen while a scroll is in progress. |
| 981 | */ |
| 982 | private CellLayout getCurrentDropLayout() { |
| 983 | int index = mScroller.isFinished() ? mCurrentScreen : mNextScreen; |
| 984 | return (CellLayout) getChildAt(index); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 985 | } |
| 986 | |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 987 | /** |
| 988 | * {@inheritDoc} |
| 989 | */ |
| 990 | public boolean acceptDrop(DragSource source, int x, int y, |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 991 | int xOffset, int yOffset, DragView dragView, Object dragInfo) { |
Romain Guy | 4c58c48 | 2009-05-12 17:35:41 -0700 | [diff] [blame] | 992 | final CellLayout layout = getCurrentDropLayout(); |
| 993 | final CellLayout.CellInfo cellInfo = mDragInfo; |
| 994 | final int spanX = cellInfo == null ? 1 : cellInfo.spanX; |
| 995 | final int spanY = cellInfo == null ? 1 : cellInfo.spanY; |
| 996 | |
| 997 | if (mVacantCache == null) { |
| 998 | final View ignoreView = cellInfo == null ? null : cellInfo.cell; |
| 999 | mVacantCache = layout.findAllVacantCells(null, ignoreView); |
| 1000 | } |
| 1001 | |
| 1002 | return mVacantCache.findCellForSpan(mTempEstimate, spanX, spanY, false); |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | /** |
| 1006 | * {@inheritDoc} |
| 1007 | */ |
| 1008 | public Rect estimateDropLocation(DragSource source, int x, int y, |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1009 | int xOffset, int yOffset, DragView dragView, Object dragInfo, Rect recycle) { |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1010 | final CellLayout layout = getCurrentDropLayout(); |
| 1011 | |
| 1012 | final CellLayout.CellInfo cellInfo = mDragInfo; |
| 1013 | final int spanX = cellInfo == null ? 1 : cellInfo.spanX; |
| 1014 | final int spanY = cellInfo == null ? 1 : cellInfo.spanY; |
| 1015 | final View ignoreView = cellInfo == null ? null : cellInfo.cell; |
| 1016 | |
| 1017 | final Rect location = recycle != null ? recycle : new Rect(); |
| 1018 | |
| 1019 | // Find drop cell and convert into rectangle |
Romain Guy | 263e019 | 2009-05-11 11:50:46 -0700 | [diff] [blame] | 1020 | int[] dropCell = estimateDropCell(x - xOffset, y - yOffset, |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1021 | spanX, spanY, ignoreView, layout, mTempCell); |
| 1022 | |
| 1023 | if (dropCell == null) { |
| 1024 | return null; |
| 1025 | } |
| 1026 | |
| 1027 | layout.cellToPoint(dropCell[0], dropCell[1], mTempEstimate); |
| 1028 | location.left = mTempEstimate[0]; |
| 1029 | location.top = mTempEstimate[1]; |
| 1030 | |
| 1031 | layout.cellToPoint(dropCell[0] + spanX, dropCell[1] + spanY, mTempEstimate); |
| 1032 | location.right = mTempEstimate[0]; |
| 1033 | location.bottom = mTempEstimate[1]; |
| 1034 | |
| 1035 | return location; |
| 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * Calculate the nearest cell where the given object would be dropped. |
| 1040 | */ |
Romain Guy | 263e019 | 2009-05-11 11:50:46 -0700 | [diff] [blame] | 1041 | private int[] estimateDropCell(int pixelX, int pixelY, |
Jeff Sharkey | 7086428 | 2009-04-07 21:08:40 -0700 | [diff] [blame] | 1042 | int spanX, int spanY, View ignoreView, CellLayout layout, int[] recycle) { |
| 1043 | // Create vacant cell cache if none exists |
| 1044 | if (mVacantCache == null) { |
| 1045 | mVacantCache = layout.findAllVacantCells(null, ignoreView); |
| 1046 | } |
| 1047 | |
| 1048 | // Find the best target drop location |
| 1049 | return layout.findNearestVacantArea(pixelX, pixelY, |
| 1050 | spanX, spanY, mVacantCache, recycle); |
| 1051 | } |
| 1052 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1053 | void setLauncher(Launcher launcher) { |
| 1054 | mLauncher = launcher; |
| 1055 | } |
| 1056 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1057 | public void setDragController(DragController dragController) { |
| 1058 | mDragController = dragController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | public void onDropCompleted(View target, boolean success) { |
Romain Guy | 207e40e | 2009-09-29 16:19:19 -0700 | [diff] [blame] | 1062 | clearVacantCache(); |
| 1063 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1064 | if (success){ |
| 1065 | if (target != this && mDragInfo != null) { |
| 1066 | final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen); |
| 1067 | cellLayout.removeView(mDragInfo.cell); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1068 | if (mDragInfo.cell instanceof DropTarget) { |
| 1069 | mDragController.removeDropTarget((DropTarget)mDragInfo.cell); |
| 1070 | } |
Romain Guy | 207e40e | 2009-09-29 16:19:19 -0700 | [diff] [blame] | 1071 | //final Object tag = mDragInfo.cell.getTag(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1072 | } |
| 1073 | } else { |
| 1074 | if (mDragInfo != null) { |
| 1075 | final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen); |
| 1076 | cellLayout.onDropAborted(mDragInfo.cell); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | mDragInfo = null; |
| 1081 | } |
| 1082 | |
| 1083 | public void scrollLeft() { |
Romain Guy | 4c58c48 | 2009-05-12 17:35:41 -0700 | [diff] [blame] | 1084 | clearVacantCache(); |
Romain Guy | f8e6a80 | 2009-12-07 17:48:02 -0800 | [diff] [blame] | 1085 | if (mScroller.isFinished()) { |
| 1086 | if (mCurrentScreen > 0) snapToScreen(mCurrentScreen - 1); |
| 1087 | } else { |
| 1088 | if (mNextScreen > 0) snapToScreen(mNextScreen - 1); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | public void scrollRight() { |
Romain Guy | 4c58c48 | 2009-05-12 17:35:41 -0700 | [diff] [blame] | 1093 | clearVacantCache(); |
Romain Guy | f8e6a80 | 2009-12-07 17:48:02 -0800 | [diff] [blame] | 1094 | if (mScroller.isFinished()) { |
| 1095 | if (mCurrentScreen < getChildCount() -1) snapToScreen(mCurrentScreen + 1); |
| 1096 | } else { |
| 1097 | if (mNextScreen < getChildCount() -1) snapToScreen(mNextScreen + 1); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | public int getScreenForView(View v) { |
| 1102 | int result = -1; |
| 1103 | if (v != null) { |
| 1104 | ViewParent vp = v.getParent(); |
| 1105 | int count = getChildCount(); |
| 1106 | for (int i = 0; i < count; i++) { |
| 1107 | if (vp == getChildAt(i)) { |
| 1108 | return i; |
| 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | return result; |
| 1113 | } |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1114 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1115 | public Folder getFolderForTag(Object tag) { |
| 1116 | int screenCount = getChildCount(); |
| 1117 | for (int screen = 0; screen < screenCount; screen++) { |
| 1118 | CellLayout currentScreen = ((CellLayout) getChildAt(screen)); |
| 1119 | int count = currentScreen.getChildCount(); |
| 1120 | for (int i = 0; i < count; i++) { |
| 1121 | View child = currentScreen.getChildAt(i); |
| 1122 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 1123 | if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) { |
| 1124 | Folder f = (Folder) child; |
| 1125 | if (f.getInfo() == tag) { |
| 1126 | return f; |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | return null; |
| 1132 | } |
| 1133 | |
| 1134 | public View getViewForTag(Object tag) { |
| 1135 | int screenCount = getChildCount(); |
| 1136 | for (int screen = 0; screen < screenCount; screen++) { |
| 1137 | CellLayout currentScreen = ((CellLayout) getChildAt(screen)); |
| 1138 | int count = currentScreen.getChildCount(); |
| 1139 | for (int i = 0; i < count; i++) { |
| 1140 | View child = currentScreen.getChildAt(i); |
| 1141 | if (child.getTag() == tag) { |
| 1142 | return child; |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | return null; |
| 1147 | } |
| 1148 | |
| 1149 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1150 | * @return True is long presses are still allowed for the current touch |
| 1151 | */ |
| 1152 | public boolean allowLongPress() { |
| 1153 | return mAllowLongPress; |
| 1154 | } |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 1155 | |
| 1156 | /** |
| 1157 | * Set true to allow long-press events to be triggered, usually checked by |
| 1158 | * {@link Launcher} to accept or block dpad-initiated long-presses. |
| 1159 | */ |
| 1160 | public void setAllowLongPress(boolean allowLongPress) { |
| 1161 | mAllowLongPress = allowLongPress; |
| 1162 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1163 | |
| 1164 | void removeShortcutsForPackage(String packageName) { |
| 1165 | final ArrayList<View> childrenToRemove = new ArrayList<View>(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1166 | final int count = getChildCount(); |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 1167 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1168 | for (int i = 0; i < count; i++) { |
| 1169 | final CellLayout layout = (CellLayout) getChildAt(i); |
| 1170 | int childCount = layout.getChildCount(); |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 1171 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1172 | childrenToRemove.clear(); |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 1173 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1174 | for (int j = 0; j < childCount; j++) { |
| 1175 | final View view = layout.getChildAt(j); |
| 1176 | Object tag = view.getTag(); |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 1177 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1178 | if (tag instanceof ApplicationInfo) { |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 1179 | final ApplicationInfo info = (ApplicationInfo) tag; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1180 | // We need to check for ACTION_MAIN otherwise getComponent() might |
| 1181 | // return null for some shortcuts (for instance, for shortcuts to |
| 1182 | // web pages.) |
| 1183 | final Intent intent = info.intent; |
| 1184 | final ComponentName name = intent.getComponent(); |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 1185 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1186 | if (Intent.ACTION_MAIN.equals(intent.getAction()) && |
| 1187 | name != null && packageName.equals(name.getPackageName())) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1188 | LauncherModel.deleteItemFromDatabase(mLauncher, info); |
| 1189 | childrenToRemove.add(view); |
| 1190 | } |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 1191 | } else if (tag instanceof UserFolderInfo) { |
| 1192 | final UserFolderInfo info = (UserFolderInfo) tag; |
| 1193 | final ArrayList<ApplicationInfo> contents = info.contents; |
| 1194 | final ArrayList<ApplicationInfo> toRemove = new ArrayList<ApplicationInfo>(1); |
| 1195 | final int contentsCount = contents.size(); |
| 1196 | boolean removedFromFolder = false; |
| 1197 | |
| 1198 | for (int k = 0; k < contentsCount; k++) { |
| 1199 | final ApplicationInfo appInfo = contents.get(k); |
| 1200 | final Intent intent = appInfo.intent; |
| 1201 | final ComponentName name = intent.getComponent(); |
| 1202 | |
| 1203 | if (Intent.ACTION_MAIN.equals(intent.getAction()) && |
| 1204 | name != null && packageName.equals(name.getPackageName())) { |
| 1205 | toRemove.add(appInfo); |
| 1206 | LauncherModel.deleteItemFromDatabase(mLauncher, appInfo); |
| 1207 | removedFromFolder = true; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | contents.removeAll(toRemove); |
| 1212 | if (removedFromFolder) { |
| 1213 | final Folder folder = getOpenFolder(); |
| 1214 | if (folder != null) folder.notifyDataSetChanged(); |
| 1215 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1216 | } |
| 1217 | } |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 1218 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1219 | childCount = childrenToRemove.size(); |
| 1220 | for (int j = 0; j < childCount; j++) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1221 | View child = childrenToRemove.get(j); |
| 1222 | layout.removeViewInLayout(child); |
| 1223 | if (child instanceof DropTarget) { |
| 1224 | mDragController.removeDropTarget((DropTarget)child); |
| 1225 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1226 | } |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 1227 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1228 | if (childCount > 0) { |
| 1229 | layout.requestLayout(); |
| 1230 | layout.invalidate(); |
| 1231 | } |
| 1232 | } |
| 1233 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1234 | |
| 1235 | void updateShortcutsForPackage(String packageName) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1236 | final PackageManager pm = mLauncher.getPackageManager(); |
| 1237 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1238 | final int count = getChildCount(); |
| 1239 | for (int i = 0; i < count; i++) { |
| 1240 | final CellLayout layout = (CellLayout) getChildAt(i); |
| 1241 | int childCount = layout.getChildCount(); |
| 1242 | for (int j = 0; j < childCount; j++) { |
| 1243 | final View view = layout.getChildAt(j); |
| 1244 | Object tag = view.getTag(); |
| 1245 | if (tag instanceof ApplicationInfo) { |
| 1246 | ApplicationInfo info = (ApplicationInfo) tag; |
| 1247 | // We need to check for ACTION_MAIN otherwise getComponent() might |
| 1248 | // return null for some shortcuts (for instance, for shortcuts to |
| 1249 | // web pages.) |
| 1250 | final Intent intent = info.intent; |
| 1251 | final ComponentName name = intent.getComponent(); |
| 1252 | if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION && |
| 1253 | Intent.ACTION_MAIN.equals(intent.getAction()) && name != null && |
| 1254 | packageName.equals(name.getPackageName())) { |
| 1255 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1256 | final Drawable icon = AppInfoCache.getIconDrawable(pm, info); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1257 | if (icon != null && icon != info.icon) { |
| 1258 | info.icon.setCallback(null); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1259 | info.icon = Utilities.createIconThumbnail(icon, mContext); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1260 | info.filtered = true; |
| 1261 | ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(null, |
| 1262 | info.icon, null, null); |
| 1263 | } |
| 1264 | } |
| 1265 | } |
| 1266 | } |
| 1267 | } |
| 1268 | } |
| 1269 | |
Joe Onorato | 14f122b | 2009-11-19 14:06:36 -0800 | [diff] [blame] | 1270 | void moveToDefaultScreen(boolean animate) { |
| 1271 | snapToScreen(mDefaultScreen, animate); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1272 | getChildAt(mDefaultScreen).requestFocus(); |
| 1273 | } |
| 1274 | |
Romain Guy | 8a73c51 | 2009-11-09 19:19:59 -0800 | [diff] [blame] | 1275 | void setIndicators(Drawable previous, Drawable next) { |
| 1276 | mPreviousIndicator = previous; |
| 1277 | mNextIndicator = next; |
| 1278 | previous.setLevel(mCurrentScreen); |
| 1279 | next.setLevel(mCurrentScreen); |
| 1280 | } |
| 1281 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1282 | public static class SavedState extends BaseSavedState { |
| 1283 | int currentScreen = -1; |
| 1284 | |
| 1285 | SavedState(Parcelable superState) { |
| 1286 | super(superState); |
| 1287 | } |
| 1288 | |
| 1289 | private SavedState(Parcel in) { |
| 1290 | super(in); |
| 1291 | currentScreen = in.readInt(); |
| 1292 | } |
| 1293 | |
| 1294 | @Override |
| 1295 | public void writeToParcel(Parcel out, int flags) { |
| 1296 | super.writeToParcel(out, flags); |
| 1297 | out.writeInt(currentScreen); |
| 1298 | } |
| 1299 | |
| 1300 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 1301 | new Parcelable.Creator<SavedState>() { |
| 1302 | public SavedState createFromParcel(Parcel in) { |
| 1303 | return new SavedState(in); |
| 1304 | } |
| 1305 | |
| 1306 | public SavedState[] newArray(int size) { |
| 1307 | return new SavedState[size]; |
| 1308 | } |
| 1309 | }; |
| 1310 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1311 | } |