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