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