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