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