Workspace.java revision 4d279d94ade1c0d455404312b3c9cfde0078c547
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.animation.Animator; 20import android.animation.AnimatorListenerAdapter; 21import android.animation.AnimatorSet; 22import android.animation.ObjectAnimator; 23import android.animation.TimeInterpolator; 24import android.animation.ValueAnimator; 25import android.animation.Animator.AnimatorListener; 26import android.animation.ValueAnimator.AnimatorUpdateListener; 27import android.app.AlertDialog; 28import android.app.WallpaperManager; 29import android.appwidget.AppWidgetManager; 30import android.appwidget.AppWidgetProviderInfo; 31import android.content.ClipData; 32import android.content.ClipDescription; 33import android.content.ComponentName; 34import android.content.Context; 35import android.content.Intent; 36import android.content.pm.PackageManager; 37import android.content.res.Resources; 38import android.content.res.TypedArray; 39import android.graphics.Bitmap; 40import android.graphics.Camera; 41import android.graphics.Canvas; 42import android.graphics.Color; 43import android.graphics.Matrix; 44import android.graphics.Paint; 45import android.graphics.Rect; 46import android.graphics.RectF; 47import android.graphics.Region.Op; 48import android.graphics.drawable.Drawable; 49import android.os.IBinder; 50import android.os.Parcelable; 51import android.util.AttributeSet; 52import android.util.DisplayMetrics; 53import android.util.Log; 54import android.util.Pair; 55import android.view.Display; 56import android.view.DragEvent; 57import android.view.MotionEvent; 58import android.view.View; 59import android.view.ViewGroup; 60import android.view.animation.DecelerateInterpolator; 61import android.widget.TextView; 62import android.widget.Toast; 63 64import com.android.launcher.R; 65import com.android.launcher2.FolderIcon.FolderRingAnimator; 66import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData; 67 68import java.util.ArrayList; 69import java.util.HashSet; 70import java.util.List; 71 72/** 73 * The workspace is a wide area with a wallpaper and a finite number of pages. 74 * Each page contains a number of icons, folders or widgets the user can 75 * interact with. A workspace is meant to be used with a fixed width only. 76 */ 77public class Workspace extends SmoothPagedView 78 implements DropTarget, DragSource, DragScroller, View.OnTouchListener, 79 DragController.DragListener { 80 @SuppressWarnings({"UnusedDeclaration"}) 81 private static final String TAG = "Launcher.Workspace"; 82 83 // Y rotation to apply to the workspace screens 84 private static final float WORKSPACE_ROTATION = 12.5f; 85 86 // These are extra scale factors to apply to the mini home screens 87 // so as to achieve the desired transform 88 private static final float EXTRA_SCALE_FACTOR_0 = 0.972f; 89 private static final float EXTRA_SCALE_FACTOR_1 = 1.0f; 90 private static final float EXTRA_SCALE_FACTOR_2 = 1.10f; 91 92 private static final int CHILDREN_OUTLINE_FADE_OUT_DELAY = 0; 93 private static final int CHILDREN_OUTLINE_FADE_OUT_DURATION = 375; 94 private static final int CHILDREN_OUTLINE_FADE_IN_DURATION = 100; 95 96 private static final int BACKGROUND_FADE_OUT_DURATION = 350; 97 private static final int BACKGROUND_FADE_IN_DURATION = 350; 98 99 // These animators are used to fade the children's outlines 100 private ObjectAnimator mChildrenOutlineFadeInAnimation; 101 private ObjectAnimator mChildrenOutlineFadeOutAnimation; 102 private float mChildrenOutlineAlpha = 0; 103 104 // These properties refer to the background protection gradient used for AllApps and Customize 105 private ValueAnimator mBackgroundFadeInAnimation; 106 private ValueAnimator mBackgroundFadeOutAnimation; 107 private Drawable mBackground; 108 boolean mDrawBackground = true; 109 private float mBackgroundAlpha = 0; 110 private float mOverScrollMaxBackgroundAlpha = 0.0f; 111 private int mOverScrollPageIndex = -1; 112 113 private final WallpaperManager mWallpaperManager; 114 private IBinder mWindowToken; 115 116 private int mDefaultPage; 117 118 /** 119 * CellInfo for the cell that is currently being dragged 120 */ 121 private CellLayout.CellInfo mDragInfo; 122 123 /** 124 * Target drop area calculated during last acceptDrop call. 125 */ 126 private int[] mTargetCell = new int[2]; 127 128 /** 129 * The CellLayout that is currently being dragged over 130 */ 131 private CellLayout mDragTargetLayout = null; 132 133 private Launcher mLauncher; 134 private IconCache mIconCache; 135 private DragController mDragController; 136 137 // These are temporary variables to prevent having to allocate a new object just to 138 // return an (x, y) value from helper functions. Do NOT use them to maintain other state. 139 private int[] mTempCell = new int[2]; 140 private int[] mTempEstimate = new int[2]; 141 private float[] mDragViewVisualCenter = new float[2]; 142 private float[] mTempDragCoordinates = new float[2]; 143 private float[] mTempTouchCoordinates = new float[2]; 144 private float[] mTempCellLayoutCenterCoordinates = new float[2]; 145 private float[] mTempDragBottomRightCoordinates = new float[2]; 146 private Matrix mTempInverseMatrix = new Matrix(); 147 148 private SpringLoadedDragController mSpringLoadedDragController; 149 private float mSpringLoadedShrinkFactor; 150 151 private static final int DEFAULT_CELL_COUNT_X = 4; 152 private static final int DEFAULT_CELL_COUNT_Y = 4; 153 154 // State variable that indicates whether the pages are small (ie when you're 155 // in all apps or customize mode) 156 157 enum State { NORMAL, SPRING_LOADED, SMALL }; 158 private State mState; 159 private boolean mIsSwitchingState = false; 160 161 private boolean mSwitchStateAfterFirstLayout = false; 162 private State mStateAfterFirstLayout; 163 164 private AnimatorSet mAnimator; 165 private AnimatorListener mShrinkAnimationListener; 166 private AnimatorListener mUnshrinkAnimationListener; 167 168 boolean mAnimatingViewIntoPlace = false; 169 boolean mIsDragOccuring = false; 170 boolean mChildrenLayersEnabled = true; 171 172 /** Is the user is dragging an item near the edge of a page? */ 173 private boolean mInScrollArea = false; 174 175 private final HolographicOutlineHelper mOutlineHelper = new HolographicOutlineHelper(); 176 private Bitmap mDragOutline = null; 177 private final Rect mTempRect = new Rect(); 178 private final int[] mTempXY = new int[2]; 179 180 // Paint used to draw external drop outline 181 private final Paint mExternalDragOutlinePaint = new Paint(); 182 183 // Camera and Matrix used to determine the final position of a neighboring CellLayout 184 private final Matrix mMatrix = new Matrix(); 185 private final Camera mCamera = new Camera(); 186 private final float mTempFloat2[] = new float[2]; 187 188 enum WallpaperVerticalOffset { TOP, MIDDLE, BOTTOM }; 189 int mWallpaperWidth; 190 int mWallpaperHeight; 191 WallpaperOffsetInterpolator mWallpaperOffset; 192 boolean mUpdateWallpaperOffsetImmediately = false; 193 boolean mSyncWallpaperOffsetWithScroll = true; 194 private Runnable mDelayedResizeRunnable; 195 196 // Variables relating to the creation of user folders by hovering shortcuts over shortcuts 197 private static final int FOLDER_CREATION_TIMEOUT = 250; 198 private final Alarm mFolderCreationAlarm = new Alarm(); 199 private FolderRingAnimator mDragFolderRingAnimator = null; 200 private View mLastDragOverView = null; 201 private boolean mCreateUserFolderOnDrop = false; 202 203 // Variables relating to touch disambiguation (scrolling workspace vs. scrolling a widget) 204 private float mXDown; 205 private float mYDown; 206 final static float START_DAMPING_TOUCH_SLOP_ANGLE = (float) Math.PI / 6; 207 final static float MAX_SWIPE_ANGLE = (float) Math.PI / 3; 208 final static float TOUCH_SLOP_DAMPING_FACTOR = 4; 209 210 /** 211 * Used to inflate the Workspace from XML. 212 * 213 * @param context The application's context. 214 * @param attrs The attributes set containing the Workspace's customization values. 215 */ 216 public Workspace(Context context, AttributeSet attrs) { 217 this(context, attrs, 0); 218 } 219 220 /** 221 * Used to inflate the Workspace from XML. 222 * 223 * @param context The application's context. 224 * @param attrs The attributes set containing the Workspace's customization values. 225 * @param defStyle Unused. 226 */ 227 public Workspace(Context context, AttributeSet attrs, int defStyle) { 228 super(context, attrs, defStyle); 229 mContentIsRefreshable = false; 230 231 // With workspace, data is available straight from the get-go 232 setDataIsReady(); 233 234 if (!LauncherApplication.isScreenLarge()) { 235 mFadeInAdjacentScreens = false; 236 } 237 238 mWallpaperManager = WallpaperManager.getInstance(context); 239 240 int cellCountX = DEFAULT_CELL_COUNT_X; 241 int cellCountY = DEFAULT_CELL_COUNT_Y; 242 243 TypedArray a = context.obtainStyledAttributes(attrs, 244 R.styleable.Workspace, defStyle, 0); 245 246 final Resources res = context.getResources(); 247 if (LauncherApplication.isScreenLarge()) { 248 // Determine number of rows/columns dynamically 249 // TODO: This code currently fails on tablets with an aspect ratio < 1.3. 250 // Around that ratio we should make cells the same size in portrait and 251 // landscape 252 TypedArray actionBarSizeTypedArray = 253 context.obtainStyledAttributes(new int[] { android.R.attr.actionBarSize }); 254 final float actionBarHeight = actionBarSizeTypedArray.getDimension(0, 0f); 255 final float systemBarHeight = res.getDimension(R.dimen.status_bar_height); 256 final float smallestScreenDim = res.getConfiguration().smallestScreenWidthDp; 257 258 cellCountX = 1; 259 while (CellLayout.widthInPortrait(res, cellCountX + 1) <= smallestScreenDim) { 260 cellCountX++; 261 } 262 263 cellCountY = 1; 264 while (actionBarHeight + CellLayout.heightInLandscape(res, cellCountY + 1) 265 <= smallestScreenDim - systemBarHeight) { 266 cellCountY++; 267 } 268 } 269 270 mSpringLoadedShrinkFactor = 271 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f; 272 273 // if the value is manually specified, use that instead 274 cellCountX = a.getInt(R.styleable.Workspace_cellCountX, cellCountX); 275 cellCountY = a.getInt(R.styleable.Workspace_cellCountY, cellCountY); 276 mDefaultPage = a.getInt(R.styleable.Workspace_defaultScreen, 1); 277 a.recycle(); 278 279 LauncherModel.updateWorkspaceLayoutCells(cellCountX, cellCountY); 280 setHapticFeedbackEnabled(false); 281 282 initWorkspace(); 283 284 // Disable multitouch across the workspace/all apps/customize tray 285 setMotionEventSplittingEnabled(true); 286 } 287 288 public void onDragStart(DragSource source, Object info, int dragAction) { 289 mIsDragOccuring = true; 290 updateChildrenLayersEnabled(); 291 mLauncher.lockScreenOrientation(); 292 } 293 294 public void onDragEnd() { 295 mIsDragOccuring = false; 296 updateChildrenLayersEnabled(); 297 mLauncher.unlockScreenOrientation(); 298 } 299 300 /** 301 * Initializes various states for this workspace. 302 */ 303 protected void initWorkspace() { 304 Context context = getContext(); 305 mCurrentPage = mDefaultPage; 306 Launcher.setScreen(mCurrentPage); 307 LauncherApplication app = (LauncherApplication)context.getApplicationContext(); 308 mIconCache = app.getIconCache(); 309 mExternalDragOutlinePaint.setAntiAlias(true); 310 setWillNotDraw(false); 311 312 try { 313 final Resources res = getResources(); 314 mBackground = res.getDrawable(R.drawable.apps_customize_bg); 315 } catch (Resources.NotFoundException e) { 316 // In this case, we will skip drawing background protection 317 } 318 319 mUnshrinkAnimationListener = new AnimatorListenerAdapter() { 320 @Override 321 public void onAnimationStart(Animator animation) { 322 mIsSwitchingState = true; 323 } 324 325 @Override 326 public void onAnimationEnd(Animator animation) { 327 mIsSwitchingState = false; 328 mSyncWallpaperOffsetWithScroll = true; 329 mWallpaperOffset.setOverrideHorizontalCatchupConstant(false); 330 mAnimator = null; 331 updateChildrenLayersEnabled(); 332 } 333 }; 334 mShrinkAnimationListener = new AnimatorListenerAdapter() { 335 @Override 336 public void onAnimationStart(Animator animation) { 337 mIsSwitchingState = true; 338 } 339 @Override 340 public void onAnimationEnd(Animator animation) { 341 mIsSwitchingState = false; 342 mWallpaperOffset.setOverrideHorizontalCatchupConstant(false); 343 mAnimator = null; 344 } 345 }; 346 mSnapVelocity = 600; 347 mWallpaperOffset = new WallpaperOffsetInterpolator(); 348 } 349 350 @Override 351 protected int getScrollMode() { 352 return SmoothPagedView.X_LARGE_MODE; 353 } 354 355 private void onAddView(View child) { 356 if (!(child instanceof CellLayout)) { 357 throw new IllegalArgumentException("A Workspace can only have CellLayout children."); 358 } 359 CellLayout cl = ((CellLayout) child); 360 cl.setOnInterceptTouchListener(this); 361 cl.setClickable(true); 362 cl.enableHardwareLayers(); 363 } 364 365 @Override 366 public void addView(View child, int index, LayoutParams params) { 367 onAddView(child); 368 super.addView(child, index, params); 369 } 370 371 @Override 372 public void addView(View child) { 373 onAddView(child); 374 super.addView(child); 375 } 376 377 @Override 378 public void addView(View child, int index) { 379 onAddView(child); 380 super.addView(child, index); 381 } 382 383 @Override 384 public void addView(View child, int width, int height) { 385 onAddView(child); 386 super.addView(child, width, height); 387 } 388 389 @Override 390 public void addView(View child, LayoutParams params) { 391 onAddView(child); 392 super.addView(child, params); 393 } 394 395 /** 396 * @return The open folder on the current screen, or null if there is none 397 */ 398 Folder getOpenFolder() { 399 DragLayer dragLayer = mLauncher.getDragLayer(); 400 int count = dragLayer.getChildCount(); 401 for (int i = 0; i < count; i++) { 402 View child = dragLayer.getChildAt(i); 403 if (child instanceof Folder) { 404 Folder folder = (Folder) child; 405 if (folder.getInfo().opened) 406 return folder; 407 } 408 } 409 return null; 410 } 411 412 boolean isTouchActive() { 413 return mTouchState != TOUCH_STATE_REST; 414 } 415 416 /** 417 * Adds the specified child in the specified screen. The position and dimension of 418 * the child are defined by x, y, spanX and spanY. 419 * 420 * @param child The child to add in one of the workspace's screens. 421 * @param screen The screen in which to add the child. 422 * @param x The X position of the child in the screen's grid. 423 * @param y The Y position of the child in the screen's grid. 424 * @param spanX The number of cells spanned horizontally by the child. 425 * @param spanY The number of cells spanned vertically by the child. 426 */ 427 void addInScreen(View child, long container, int screen, int x, int y, int spanX, int spanY) { 428 addInScreen(child, container, screen, x, y, spanX, spanY, false); 429 } 430 431 /** 432 * Adds the specified child in the specified screen. The position and dimension of 433 * the child are defined by x, y, spanX and spanY. 434 * 435 * @param child The child to add in one of the workspace's screens. 436 * @param screen The screen in which to add the child. 437 * @param x The X position of the child in the screen's grid. 438 * @param y The Y position of the child in the screen's grid. 439 * @param spanX The number of cells spanned horizontally by the child. 440 * @param spanY The number of cells spanned vertically by the child. 441 * @param insert When true, the child is inserted at the beginning of the children list. 442 */ 443 void addInScreen(View child, long container, int screen, int x, int y, int spanX, int spanY, 444 boolean insert) { 445 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { 446 if (screen < 0 || screen >= getChildCount()) { 447 Log.e(TAG, "The screen must be >= 0 and < " + getChildCount() 448 + " (was " + screen + "); skipping child"); 449 return; 450 } 451 } 452 453 final CellLayout layout; 454 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { 455 layout = mLauncher.getHotseat().getLayout(); 456 child.setOnKeyListener(null); 457 458 if (screen < 0) { 459 screen = mLauncher.getHotseat().getOrderInHotseat(x, y); 460 } else { 461 // Note: We do this to ensure that the hotseat is always laid out in the orientation 462 // of the hotseat in order regardless of which orientation they were added 463 x = mLauncher.getHotseat().getCellXFromOrder(screen); 464 y = mLauncher.getHotseat().getCellYFromOrder(screen); 465 } 466 } else { 467 layout = (CellLayout) getChildAt(screen); 468 child.setOnKeyListener(new BubbleTextViewKeyEventListener()); 469 } 470 471 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); 472 if (lp == null) { 473 lp = new CellLayout.LayoutParams(x, y, spanX, spanY); 474 } else { 475 lp.cellX = x; 476 lp.cellY = y; 477 lp.cellHSpan = spanX; 478 lp.cellVSpan = spanY; 479 } 480 481 if (spanX < 0 && spanY < 0) { 482 lp.isLockedToGrid = false; 483 } 484 485 // Get the canonical child id to uniquely represent this view in this screen 486 int childId = LauncherModel.getCellLayoutChildId(container, screen, x, y, spanX, spanY); 487 boolean markCellsAsOccupied = !(child instanceof Folder); 488 if (!layout.addViewToCellLayout(child, insert ? 0 : -1, childId, lp, markCellsAsOccupied)) { 489 // TODO: This branch occurs when the workspace is adding views 490 // outside of the defined grid 491 // maybe we should be deleting these items from the LauncherModel? 492 Log.w(TAG, "Failed to add to item at (" + lp.cellX + "," + lp.cellY + ") to CellLayout"); 493 } 494 495 if (!(child instanceof Folder)) { 496 child.setHapticFeedbackEnabled(false); 497 child.setOnLongClickListener(mLongClickListener); 498 } 499 if (child instanceof DropTarget) { 500 mDragController.addDropTarget((DropTarget) child); 501 } 502 } 503 504 /** 505 * Check if the point (x, y) hits a given page. 506 */ 507 private boolean hitsPage(int index, float x, float y) { 508 final View page = getChildAt(index); 509 if (page != null) { 510 float[] localXY = { x, y }; 511 mapPointFromSelfToChild(page, localXY); 512 return (localXY[0] >= 0 && localXY[0] < page.getWidth() 513 && localXY[1] >= 0 && localXY[1] < page.getHeight()); 514 } 515 return false; 516 } 517 518 @Override 519 protected boolean hitsPreviousPage(float x, float y) { 520 // mNextPage is set to INVALID_PAGE whenever we are stationary. 521 // Calculating "next page" this way ensures that you scroll to whatever page you tap on 522 final int current = (mNextPage == INVALID_PAGE) ? mCurrentPage : mNextPage; 523 return hitsPage(current - 1, x, y); 524 } 525 526 @Override 527 protected boolean hitsNextPage(float x, float y) { 528 // mNextPage is set to INVALID_PAGE whenever we are stationary. 529 // Calculating "next page" this way ensures that you scroll to whatever page you tap on 530 final int current = (mNextPage == INVALID_PAGE) ? mCurrentPage : mNextPage; 531 return hitsPage(current + 1, x, y); 532 } 533 534 /** 535 * Called directly from a CellLayout (not by the framework), after we've been added as a 536 * listener via setOnInterceptTouchEventListener(). This allows us to tell the CellLayout 537 * that it should intercept touch events, which is not something that is normally supported. 538 */ 539 @Override 540 public boolean onTouch(View v, MotionEvent event) { 541 return (isSmall() || mIsSwitchingState); 542 } 543 544 public boolean isSwitchingState() { 545 return mIsSwitchingState; 546 } 547 548 protected void onWindowVisibilityChanged (int visibility) { 549 mLauncher.onWindowVisibilityChanged(visibility); 550 } 551 552 @Override 553 public boolean dispatchUnhandledMove(View focused, int direction) { 554 if (isSmall() || mIsSwitchingState) { 555 // when the home screens are shrunken, shouldn't allow side-scrolling 556 return false; 557 } 558 return super.dispatchUnhandledMove(focused, direction); 559 } 560 561 @Override 562 public boolean onInterceptTouchEvent(MotionEvent ev) { 563 if (ev.getAction() == MotionEvent.ACTION_DOWN) { 564 mXDown = ev.getX(); 565 mYDown = ev.getY(); 566 } 567 568 return super.onInterceptTouchEvent(ev); 569 } 570 571 @Override 572 protected void determineScrollingStart(MotionEvent ev) { 573 if (!isSmall() && !mIsSwitchingState) { 574 float deltaX = Math.abs(ev.getX() - mXDown); 575 float deltaY = Math.abs(ev.getY() - mYDown); 576 577 if (Float.compare(deltaX, 0f) == 0) return; 578 579 float slope = deltaY / deltaX; 580 float theta = (float) Math.atan(slope); 581 582 if (deltaX > mTouchSlop || deltaY > mTouchSlop) { 583 cancelCurrentPageLongPress(); 584 } 585 586 if (theta > MAX_SWIPE_ANGLE) { 587 // Above MAX_SWIPE_ANGLE, we don't want to ever start scrolling the workspace 588 return; 589 } else if (theta > START_DAMPING_TOUCH_SLOP_ANGLE) { 590 // Above START_DAMPING_TOUCH_SLOP_ANGLE and below MAX_SWIPE_ANGLE, we want to 591 // increase the touch slop to make it harder to begin scrolling the workspace. This 592 // results in vertically scrolling widgets to more easily. The higher the angle, the 593 // more we increase touch slop. 594 theta -= START_DAMPING_TOUCH_SLOP_ANGLE; 595 float extraRatio = (float) 596 Math.sqrt((theta / (MAX_SWIPE_ANGLE - START_DAMPING_TOUCH_SLOP_ANGLE))); 597 super.determineScrollingStart(ev, 1 + TOUCH_SLOP_DAMPING_FACTOR * extraRatio); 598 } else { 599 // Below START_DAMPING_TOUCH_SLOP_ANGLE, we don't do anything special 600 super.determineScrollingStart(ev); 601 } 602 } 603 } 604 605 @Override 606 protected boolean isScrollingIndicatorEnabled() { 607 return mState != State.SPRING_LOADED; 608 } 609 610 protected void onPageBeginMoving() { 611 super.onPageBeginMoving(); 612 613 if (isHardwareAccelerated()) { 614 updateChildrenLayersEnabled(); 615 } else { 616 if (mNextPage != INVALID_PAGE) { 617 // we're snapping to a particular screen 618 enableChildrenCache(mCurrentPage, mNextPage); 619 } else { 620 // this is when user is actively dragging a particular screen, they might 621 // swipe it either left or right (but we won't advance by more than one screen) 622 enableChildrenCache(mCurrentPage - 1, mCurrentPage + 1); 623 } 624 } 625 626 // Only show page outlines as we pan if we are on large screen 627 if (LauncherApplication.isScreenLarge()) { 628 showOutlines(); 629 } 630 } 631 632 protected void onPageEndMoving() { 633 super.onPageEndMoving(); 634 635 if (isHardwareAccelerated()) { 636 updateChildrenLayersEnabled(); 637 } else { 638 clearChildrenCache(); 639 } 640 641 // Hide the outlines, as long as we're not dragging 642 if (!mDragController.dragging()) { 643 // Only hide page outlines as we pan if we are on large screen 644 if (LauncherApplication.isScreenLarge()) { 645 hideOutlines(); 646 } 647 } 648 mOverScrollMaxBackgroundAlpha = 0.0f; 649 mOverScrollPageIndex = -1; 650 651 if (mDelayedResizeRunnable != null) { 652 mDelayedResizeRunnable.run(); 653 mDelayedResizeRunnable = null; 654 } 655 } 656 657 @Override 658 protected void notifyPageSwitchListener() { 659 super.notifyPageSwitchListener(); 660 Launcher.setScreen(mCurrentPage); 661 }; 662 663 // As a ratio of screen height, the total distance we want the parallax effect to span 664 // vertically 665 private float wallpaperTravelToScreenHeightRatio(int width, int height) { 666 return 1.1f; 667 } 668 669 // As a ratio of screen height, the total distance we want the parallax effect to span 670 // horizontally 671 private float wallpaperTravelToScreenWidthRatio(int width, int height) { 672 float aspectRatio = width / (float) height; 673 674 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width 675 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width 676 // We will use these two data points to extrapolate how much the wallpaper parallax effect 677 // to span (ie travel) at any aspect ratio: 678 679 final float ASPECT_RATIO_LANDSCAPE = 16/10f; 680 final float ASPECT_RATIO_PORTRAIT = 10/16f; 681 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f; 682 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f; 683 684 // To find out the desired width at different aspect ratios, we use the following two 685 // formulas, where the coefficient on x is the aspect ratio (width/height): 686 // (16/10)x + y = 1.5 687 // (10/16)x + y = 1.2 688 // We solve for x and y and end up with a final formula: 689 final float x = 690 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) / 691 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT); 692 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT; 693 return x * aspectRatio + y; 694 } 695 696 // The range of scroll values for Workspace 697 private int getScrollRange() { 698 return getChildOffset(getChildCount() - 1) - getChildOffset(0); 699 } 700 701 protected void setWallpaperDimension() { 702 Display display = mLauncher.getWindowManager().getDefaultDisplay(); 703 DisplayMetrics displayMetrics = new DisplayMetrics(); 704 display.getRealMetrics(displayMetrics); 705 final int maxDim = Math.max(displayMetrics.widthPixels, displayMetrics.heightPixels); 706 final int minDim = Math.min(displayMetrics.widthPixels, displayMetrics.heightPixels); 707 708 // We need to ensure that there is enough extra space in the wallpaper for the intended 709 // parallax effects 710 mWallpaperWidth = (int) (maxDim * wallpaperTravelToScreenWidthRatio(maxDim, minDim)); 711 mWallpaperHeight = (int)(maxDim * wallpaperTravelToScreenHeightRatio(maxDim, minDim)); 712 new Thread("setWallpaperDimension") { 713 public void run() { 714 mWallpaperManager.suggestDesiredDimensions(mWallpaperWidth, mWallpaperHeight); 715 } 716 }.start(); 717 } 718 719 public void setVerticalWallpaperOffset(float offset) { 720 mWallpaperOffset.setFinalY(offset); 721 } 722 public float getVerticalWallpaperOffset() { 723 return mWallpaperOffset.getCurrY(); 724 } 725 public void setHorizontalWallpaperOffset(float offset) { 726 mWallpaperOffset.setFinalX(offset); 727 } 728 public float getHorizontalWallpaperOffset() { 729 return mWallpaperOffset.getCurrX(); 730 } 731 732 private float wallpaperOffsetForCurrentScroll() { 733 Display display = mLauncher.getWindowManager().getDefaultDisplay(); 734 final boolean isStaticWallpaper = (mWallpaperManager.getWallpaperInfo() == null); 735 // The wallpaper travel width is how far, from left to right, the wallpaper will move 736 // at this orientation (for example, in portrait mode we don't move all the way to the 737 // edges of the wallpaper, or otherwise the parallax effect would be too strong) 738 int wallpaperTravelWidth = (int) (display.getWidth() * 739 wallpaperTravelToScreenWidthRatio(display.getWidth(), display.getHeight())); 740 if (!isStaticWallpaper) { 741 wallpaperTravelWidth = mWallpaperWidth; 742 } 743 744 // Set wallpaper offset steps (1 / (number of screens - 1)) 745 // We have 3 vertical offset states (centered, and then top/bottom aligned 746 // for all apps/customize) 747 mWallpaperManager.setWallpaperOffsetSteps(1.0f / (getChildCount() - 1), 1.0f / (3 - 1)); 748 749 int scrollRange = getScrollRange(); 750 float scrollProgressOffset = 0; 751 752 // Account for overscroll: you only see the absolute edge of the wallpaper if 753 // you overscroll as far as you can in landscape mode. Only do this for static wallpapers 754 // because live wallpapers (and probably 3rd party wallpaper providers) rely on the offset 755 // being even intervals from 0 to 1 (eg [0, 0.25, 0.5, 0.75, 1]) 756 if (isStaticWallpaper) { 757 int overscrollOffset = (int) (maxOverScroll() * display.getWidth()); 758 scrollProgressOffset += overscrollOffset / (float) getScrollRange(); 759 scrollRange += 2 * overscrollOffset; 760 } 761 762 float scrollProgress = 763 mScrollX / (float) scrollRange + scrollProgressOffset; 764 float offsetInDips = wallpaperTravelWidth * scrollProgress + 765 (mWallpaperWidth - wallpaperTravelWidth) / 2; // center it 766 float offset = offsetInDips / (float) mWallpaperWidth; 767 return offset; 768 } 769 private void syncWallpaperOffsetWithScroll() { 770 final boolean enableWallpaperEffects = isHardwareAccelerated(); 771 if (enableWallpaperEffects) { 772 mWallpaperOffset.setFinalX(wallpaperOffsetForCurrentScroll()); 773 } 774 } 775 776 public void updateWallpaperOffsetImmediately() { 777 mUpdateWallpaperOffsetImmediately = true; 778 } 779 780 private void updateWallpaperOffsets() { 781 boolean updateNow = false; 782 boolean keepUpdating = true; 783 if (mUpdateWallpaperOffsetImmediately) { 784 updateNow = true; 785 keepUpdating = false; 786 mWallpaperOffset.jumpToFinal(); 787 mUpdateWallpaperOffsetImmediately = false; 788 } else { 789 updateNow = keepUpdating = mWallpaperOffset.computeScrollOffset(); 790 } 791 if (updateNow) { 792 if (mWindowToken != null) { 793 mWallpaperManager.setWallpaperOffsets(mWindowToken, 794 mWallpaperOffset.getCurrX(), mWallpaperOffset.getCurrY()); 795 } 796 } 797 if (keepUpdating) { 798 fastInvalidate(); 799 } 800 } 801 802 class WallpaperOffsetInterpolator { 803 float mFinalHorizontalWallpaperOffset = 0.0f; 804 float mFinalVerticalWallpaperOffset = 0.5f; 805 float mHorizontalWallpaperOffset = 0.0f; 806 float mVerticalWallpaperOffset = 0.5f; 807 long mLastWallpaperOffsetUpdateTime; 808 boolean mIsMovingFast; 809 boolean mOverrideHorizontalCatchupConstant; 810 float mHorizontalCatchupConstant = 0.35f; 811 float mVerticalCatchupConstant = 0.35f; 812 813 public WallpaperOffsetInterpolator() { 814 } 815 816 public void setOverrideHorizontalCatchupConstant(boolean override) { 817 mOverrideHorizontalCatchupConstant = override; 818 } 819 820 public void setHorizontalCatchupConstant(float f) { 821 mHorizontalCatchupConstant = f; 822 } 823 824 public void setVerticalCatchupConstant(float f) { 825 mVerticalCatchupConstant = f; 826 } 827 828 public boolean computeScrollOffset() { 829 if (Float.compare(mHorizontalWallpaperOffset, mFinalHorizontalWallpaperOffset) == 0 && 830 Float.compare(mVerticalWallpaperOffset, mFinalVerticalWallpaperOffset) == 0) { 831 mIsMovingFast = false; 832 return false; 833 } 834 Display display = mLauncher.getWindowManager().getDefaultDisplay(); 835 boolean isLandscape = display.getWidth() > display.getHeight(); 836 837 long currentTime = System.currentTimeMillis(); 838 long timeSinceLastUpdate = currentTime - mLastWallpaperOffsetUpdateTime; 839 timeSinceLastUpdate = Math.min((long) (1000/30f), timeSinceLastUpdate); 840 timeSinceLastUpdate = Math.max(1L, timeSinceLastUpdate); 841 842 float xdiff = Math.abs(mFinalHorizontalWallpaperOffset - mHorizontalWallpaperOffset); 843 if (!mIsMovingFast && xdiff > 0.07) { 844 mIsMovingFast = true; 845 } 846 847 float fractionToCatchUpIn1MsHorizontal; 848 if (mOverrideHorizontalCatchupConstant) { 849 fractionToCatchUpIn1MsHorizontal = mHorizontalCatchupConstant; 850 } else if (mIsMovingFast) { 851 fractionToCatchUpIn1MsHorizontal = isLandscape ? 0.5f : 0.75f; 852 } else { 853 // slow 854 fractionToCatchUpIn1MsHorizontal = isLandscape ? 0.27f : 0.5f; 855 } 856 float fractionToCatchUpIn1MsVertical = mVerticalCatchupConstant; 857 858 fractionToCatchUpIn1MsHorizontal /= 33f; 859 fractionToCatchUpIn1MsVertical /= 33f; 860 861 final float UPDATE_THRESHOLD = 0.00001f; 862 float hOffsetDelta = mFinalHorizontalWallpaperOffset - mHorizontalWallpaperOffset; 863 float vOffsetDelta = mFinalVerticalWallpaperOffset - mVerticalWallpaperOffset; 864 boolean jumpToFinalValue = Math.abs(hOffsetDelta) < UPDATE_THRESHOLD && 865 Math.abs(vOffsetDelta) < UPDATE_THRESHOLD; 866 if (jumpToFinalValue) { 867 mHorizontalWallpaperOffset = mFinalHorizontalWallpaperOffset; 868 mVerticalWallpaperOffset = mFinalVerticalWallpaperOffset; 869 } else { 870 float percentToCatchUpVertical = 871 Math.min(1.0f, timeSinceLastUpdate * fractionToCatchUpIn1MsVertical); 872 float percentToCatchUpHorizontal = 873 Math.min(1.0f, timeSinceLastUpdate * fractionToCatchUpIn1MsHorizontal); 874 mHorizontalWallpaperOffset += percentToCatchUpHorizontal * hOffsetDelta; 875 mVerticalWallpaperOffset += percentToCatchUpVertical * vOffsetDelta; 876 } 877 878 mLastWallpaperOffsetUpdateTime = System.currentTimeMillis(); 879 return true; 880 } 881 882 public float getCurrX() { 883 return mHorizontalWallpaperOffset; 884 } 885 886 public float getFinalX() { 887 return mFinalHorizontalWallpaperOffset; 888 } 889 890 public float getCurrY() { 891 return mVerticalWallpaperOffset; 892 } 893 894 public float getFinalY() { 895 return mFinalVerticalWallpaperOffset; 896 } 897 898 public void setFinalX(float x) { 899 mFinalHorizontalWallpaperOffset = Math.max(0f, Math.min(x, 1.0f)); 900 } 901 902 public void setFinalY(float y) { 903 mFinalVerticalWallpaperOffset = Math.max(0f, Math.min(y, 1.0f)); 904 } 905 906 public void jumpToFinal() { 907 mHorizontalWallpaperOffset = mFinalHorizontalWallpaperOffset; 908 mVerticalWallpaperOffset = mFinalVerticalWallpaperOffset; 909 } 910 } 911 912 @Override 913 public void computeScroll() { 914 super.computeScroll(); 915 if (mSyncWallpaperOffsetWithScroll) { 916 syncWallpaperOffsetWithScroll(); 917 } 918 } 919 920 void showOutlines() { 921 if (!isSmall() && !mIsSwitchingState) { 922 if (mChildrenOutlineFadeOutAnimation != null) mChildrenOutlineFadeOutAnimation.cancel(); 923 if (mChildrenOutlineFadeInAnimation != null) mChildrenOutlineFadeInAnimation.cancel(); 924 mChildrenOutlineFadeInAnimation = ObjectAnimator.ofFloat(this, "childrenOutlineAlpha", 1.0f); 925 mChildrenOutlineFadeInAnimation.setDuration(CHILDREN_OUTLINE_FADE_IN_DURATION); 926 mChildrenOutlineFadeInAnimation.start(); 927 } 928 } 929 930 void hideOutlines() { 931 if (!isSmall() && !mIsSwitchingState) { 932 if (mChildrenOutlineFadeInAnimation != null) mChildrenOutlineFadeInAnimation.cancel(); 933 if (mChildrenOutlineFadeOutAnimation != null) mChildrenOutlineFadeOutAnimation.cancel(); 934 mChildrenOutlineFadeOutAnimation = ObjectAnimator.ofFloat(this, "childrenOutlineAlpha", 0.0f); 935 mChildrenOutlineFadeOutAnimation.setDuration(CHILDREN_OUTLINE_FADE_OUT_DURATION); 936 mChildrenOutlineFadeOutAnimation.setStartDelay(CHILDREN_OUTLINE_FADE_OUT_DELAY); 937 mChildrenOutlineFadeOutAnimation.start(); 938 } 939 } 940 941 public void showOutlinesTemporarily() { 942 if (!mIsPageMoving && !isTouchActive()) { 943 snapToPage(mCurrentPage); 944 } 945 } 946 947 public void setChildrenOutlineAlpha(float alpha) { 948 mChildrenOutlineAlpha = alpha; 949 for (int i = 0; i < getChildCount(); i++) { 950 CellLayout cl = (CellLayout) getChildAt(i); 951 cl.setBackgroundAlpha(alpha); 952 } 953 } 954 955 public float getChildrenOutlineAlpha() { 956 return mChildrenOutlineAlpha; 957 } 958 959 void disableBackground() { 960 mDrawBackground = false; 961 } 962 void enableBackground() { 963 mDrawBackground = true; 964 } 965 966 private void showBackgroundGradientForAllApps() { 967 showBackgroundGradient(); 968 } 969 970 private void showBackgroundGradient() { 971 if (mBackground == null) return; 972 if (mBackgroundFadeOutAnimation != null) mBackgroundFadeOutAnimation.cancel(); 973 if (mBackgroundFadeInAnimation != null) mBackgroundFadeInAnimation.cancel(); 974 mBackgroundFadeInAnimation = ValueAnimator.ofFloat(getBackgroundAlpha(), 1f); 975 mBackgroundFadeInAnimation.addUpdateListener(new AnimatorUpdateListener() { 976 public void onAnimationUpdate(ValueAnimator animation) { 977 setBackgroundAlpha(((Float) animation.getAnimatedValue()).floatValue()); 978 } 979 }); 980 mBackgroundFadeInAnimation.setInterpolator(new DecelerateInterpolator(1.5f)); 981 mBackgroundFadeInAnimation.setDuration(BACKGROUND_FADE_IN_DURATION); 982 mBackgroundFadeInAnimation.start(); 983 } 984 985 private void hideBackgroundGradient(float finalAlpha) { 986 if (mBackground == null) return; 987 if (mBackgroundFadeInAnimation != null) mBackgroundFadeInAnimation.cancel(); 988 if (mBackgroundFadeOutAnimation != null) mBackgroundFadeOutAnimation.cancel(); 989 mBackgroundFadeOutAnimation = ValueAnimator.ofFloat(getBackgroundAlpha(), finalAlpha); 990 mBackgroundFadeOutAnimation.addUpdateListener(new AnimatorUpdateListener() { 991 public void onAnimationUpdate(ValueAnimator animation) { 992 setBackgroundAlpha(((Float) animation.getAnimatedValue()).floatValue()); 993 } 994 }); 995 mBackgroundFadeOutAnimation.setInterpolator(new DecelerateInterpolator(1.5f)); 996 mBackgroundFadeOutAnimation.setDuration(BACKGROUND_FADE_OUT_DURATION); 997 mBackgroundFadeOutAnimation.start(); 998 } 999 1000 public void setBackgroundAlpha(float alpha) { 1001 if (alpha != mBackgroundAlpha) { 1002 mBackgroundAlpha = alpha; 1003 invalidate(); 1004 } 1005 } 1006 1007 public float getBackgroundAlpha() { 1008 return mBackgroundAlpha; 1009 } 1010 1011 /** 1012 * Due to 3D transformations, if two CellLayouts are theoretically touching each other, 1013 * on the xy plane, when one is rotated along the y-axis, the gap between them is perceived 1014 * as being larger. This method computes what offset the rotated view should be translated 1015 * in order to minimize this perceived gap. 1016 * @param degrees Angle of the view 1017 * @param width Width of the view 1018 * @param height Height of the view 1019 * @return Offset to be used in a View.setTranslationX() call 1020 */ 1021 private float getOffsetXForRotation(float degrees, int width, int height) { 1022 mMatrix.reset(); 1023 mCamera.save(); 1024 mCamera.rotateY(Math.abs(degrees)); 1025 mCamera.getMatrix(mMatrix); 1026 mCamera.restore(); 1027 1028 mMatrix.preTranslate(-width * 0.5f, -height * 0.5f); 1029 mMatrix.postTranslate(width * 0.5f, height * 0.5f); 1030 mTempFloat2[0] = width; 1031 mTempFloat2[1] = height; 1032 mMatrix.mapPoints(mTempFloat2); 1033 return (width - mTempFloat2[0]) * (degrees > 0.0f ? 1.0f : -1.0f); 1034 } 1035 1036 float backgroundAlphaInterpolator(float r) { 1037 float pivotA = 0.1f; 1038 float pivotB = 0.4f; 1039 if (r < pivotA) { 1040 return 0; 1041 } else if (r > pivotB) { 1042 return 1.0f; 1043 } else { 1044 return (r - pivotA)/(pivotB - pivotA); 1045 } 1046 } 1047 1048 float overScrollBackgroundAlphaInterpolator(float r) { 1049 float threshold = 0.08f; 1050 1051 if (r > mOverScrollMaxBackgroundAlpha) { 1052 mOverScrollMaxBackgroundAlpha = r; 1053 } else if (r < mOverScrollMaxBackgroundAlpha) { 1054 r = mOverScrollMaxBackgroundAlpha; 1055 } 1056 1057 return Math.min(r / threshold, 1.0f); 1058 } 1059 1060 @Override 1061 protected void screenScrolled(int screenCenter) { 1062 super.screenScrolled(screenCenter); 1063 1064 // If the screen is not xlarge, then don't rotate the CellLayouts 1065 // NOTE: If we don't update the side pages alpha, then we should not hide the side pages. 1066 // see unshrink(). 1067 if (!LauncherApplication.isScreenLarge()) return; 1068 1069 final int halfScreenSize = getMeasuredWidth() / 2; 1070 1071 for (int i = 0; i < getChildCount(); i++) { 1072 CellLayout cl = (CellLayout) getChildAt(i); 1073 if (cl != null) { 1074 int totalDistance = getScaledMeasuredWidth(cl) + mPageSpacing; 1075 int delta = screenCenter - (getChildOffset(i) - 1076 getRelativeChildOffset(i) + halfScreenSize); 1077 1078 float scrollProgress = delta / (totalDistance * 1.0f); 1079 scrollProgress = Math.min(scrollProgress, 1.0f); 1080 scrollProgress = Math.max(scrollProgress, -1.0f); 1081 1082 // If the current page (i) is being overscrolled, we use a different 1083 // set of rules for setting the background alpha multiplier. 1084 if ((mScrollX < 0 && i == 0) || (mScrollX > mMaxScrollX && 1085 i == getChildCount() -1 )) { 1086 cl.setBackgroundAlphaMultiplier( 1087 overScrollBackgroundAlphaInterpolator(Math.abs(scrollProgress))); 1088 mOverScrollPageIndex = i; 1089 } else if (mOverScrollPageIndex != i) { 1090 cl.setBackgroundAlphaMultiplier( 1091 backgroundAlphaInterpolator(Math.abs(scrollProgress))); 1092 } 1093 1094 float rotation = WORKSPACE_ROTATION * scrollProgress; 1095 float translationX = getOffsetXForRotation(rotation, cl.getWidth(), cl.getHeight()); 1096 cl.setTranslationX(translationX); 1097 1098 cl.setRotationY(rotation); 1099 } 1100 } 1101 } 1102 1103 protected void onAttachedToWindow() { 1104 super.onAttachedToWindow(); 1105 mWindowToken = getWindowToken(); 1106 computeScroll(); 1107 mDragController.setWindowToken(mWindowToken); 1108 } 1109 1110 protected void onDetachedFromWindow() { 1111 mWindowToken = null; 1112 } 1113 1114 @Override 1115 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 1116 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) { 1117 mUpdateWallpaperOffsetImmediately = true; 1118 } 1119 super.onLayout(changed, left, top, right, bottom); 1120 1121 // if shrinkToBottom() is called on initialization, it has to be deferred 1122 // until after the first call to onLayout so that it has the correct width 1123 if (mSwitchStateAfterFirstLayout) { 1124 mSwitchStateAfterFirstLayout = false; 1125 // shrink can trigger a synchronous onLayout call, so we 1126 // post this to avoid a stack overflow / tangled onLayout calls 1127 post(new Runnable() { 1128 public void run() { 1129 shrink(mStateAfterFirstLayout, false); 1130 } 1131 }); 1132 } 1133 } 1134 1135 @Override 1136 protected void onDraw(Canvas canvas) { 1137 updateWallpaperOffsets(); 1138 1139 // Draw the background gradient if necessary 1140 if (mBackground != null && mBackgroundAlpha > 0.0f && mDrawBackground) { 1141 int alpha = (int) (mBackgroundAlpha * 255); 1142 mBackground.setAlpha(alpha); 1143 mBackground.setBounds(mScrollX, 0, mScrollX + getMeasuredWidth(), 1144 getMeasuredHeight()); 1145 mBackground.draw(canvas); 1146 } 1147 1148 super.onDraw(canvas); 1149 } 1150 1151 @Override 1152 protected void dispatchDraw(Canvas canvas) { 1153 super.dispatchDraw(canvas); 1154 1155 if (mInScrollArea && !LauncherApplication.isScreenLarge()) { 1156 final int width = getWidth(); 1157 final int height = getHeight(); 1158 final int pageHeight = getChildAt(0).getHeight(); 1159 1160 // This determines the height of the glowing edge: 90% of the page height 1161 final int padding = (int) ((height - pageHeight) * 0.5f + pageHeight * 0.1f); 1162 1163 final CellLayout leftPage = (CellLayout) getChildAt(mCurrentPage - 1); 1164 final CellLayout rightPage = (CellLayout) getChildAt(mCurrentPage + 1); 1165 1166 if (leftPage != null && leftPage.getIsDragOverlapping()) { 1167 final Drawable d = getResources().getDrawable(R.drawable.page_hover_left_holo); 1168 d.setBounds(mScrollX, padding, mScrollX + d.getIntrinsicWidth(), height - padding); 1169 d.draw(canvas); 1170 } else if (rightPage != null && rightPage.getIsDragOverlapping()) { 1171 final Drawable d = getResources().getDrawable(R.drawable.page_hover_right_holo); 1172 d.setBounds(mScrollX + width - d.getIntrinsicWidth(), padding, mScrollX + width, height - padding); 1173 d.draw(canvas); 1174 } 1175 } 1176 } 1177 1178 @Override 1179 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { 1180 if (!mLauncher.isAllAppsVisible()) { 1181 final Folder openFolder = getOpenFolder(); 1182 if (openFolder != null) { 1183 return openFolder.requestFocus(direction, previouslyFocusedRect); 1184 } else { 1185 return super.onRequestFocusInDescendants(direction, previouslyFocusedRect); 1186 } 1187 } 1188 return false; 1189 } 1190 1191 @Override 1192 public int getDescendantFocusability() { 1193 if (isSmall()) { 1194 return ViewGroup.FOCUS_BLOCK_DESCENDANTS; 1195 } 1196 return super.getDescendantFocusability(); 1197 } 1198 1199 @Override 1200 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { 1201 if (!mLauncher.isAllAppsVisible()) { 1202 final Folder openFolder = getOpenFolder(); 1203 if (openFolder != null) { 1204 openFolder.addFocusables(views, direction); 1205 } else { 1206 super.addFocusables(views, direction, focusableMode); 1207 } 1208 } 1209 } 1210 1211 public boolean isSmall() { 1212 return mState == State.SMALL || mState == State.SPRING_LOADED; 1213 } 1214 1215 void enableChildrenCache(int fromPage, int toPage) { 1216 if (fromPage > toPage) { 1217 final int temp = fromPage; 1218 fromPage = toPage; 1219 toPage = temp; 1220 } 1221 1222 final int screenCount = getChildCount(); 1223 1224 fromPage = Math.max(fromPage, 0); 1225 toPage = Math.min(toPage, screenCount - 1); 1226 1227 for (int i = fromPage; i <= toPage; i++) { 1228 final CellLayout layout = (CellLayout) getChildAt(i); 1229 layout.setChildrenDrawnWithCacheEnabled(true); 1230 layout.setChildrenDrawingCacheEnabled(true); 1231 } 1232 } 1233 1234 void clearChildrenCache() { 1235 final int screenCount = getChildCount(); 1236 for (int i = 0; i < screenCount; i++) { 1237 final CellLayout layout = (CellLayout) getChildAt(i); 1238 layout.setChildrenDrawnWithCacheEnabled(false); 1239 } 1240 } 1241 1242 private boolean childLayersEnabled() { 1243 boolean isSmallOrSpringloaded = 1244 isSmall() || mIsSwitchingState || mState == State.SPRING_LOADED; 1245 return isSmallOrSpringloaded || isPageMoving() || mIsDragOccuring; 1246 } 1247 1248 private void updateChildrenLayersEnabled() { 1249 boolean small = 1250 isSmall() || mIsSwitchingState || mState == State.SPRING_LOADED; 1251 boolean dragging = mAnimatingViewIntoPlace || mIsDragOccuring; 1252 boolean enableChildrenLayers = small || dragging || isPageMoving(); 1253 1254 if (enableChildrenLayers != mChildrenLayersEnabled) { 1255 mChildrenLayersEnabled = enableChildrenLayers; 1256 for (int i = 0; i < getPageCount(); i++) { 1257 ((ViewGroup)getChildAt(i)).setChildrenLayersEnabled(enableChildrenLayers); 1258 } 1259 } 1260 } 1261 1262 @Override 1263 protected void onWallpaperTap(MotionEvent ev) { 1264 final int[] position = mTempCell; 1265 getLocationOnScreen(position); 1266 1267 int pointerIndex = ev.getActionIndex(); 1268 position[0] += (int) ev.getX(pointerIndex); 1269 position[1] += (int) ev.getY(pointerIndex); 1270 1271 mWallpaperManager.sendWallpaperCommand(getWindowToken(), 1272 ev.getAction() == MotionEvent.ACTION_UP 1273 ? WallpaperManager.COMMAND_TAP : WallpaperManager.COMMAND_SECONDARY_TAP, 1274 position[0], position[1], 0, null); 1275 } 1276 1277 private float getYScaleForScreen(int screen) { 1278 int x = Math.abs(screen - 2); 1279 1280 // TODO: This should be generalized for use with arbitrary rotation angles. 1281 switch(x) { 1282 case 0: return EXTRA_SCALE_FACTOR_0; 1283 case 1: return EXTRA_SCALE_FACTOR_1; 1284 case 2: return EXTRA_SCALE_FACTOR_2; 1285 } 1286 return 1.0f; 1287 } 1288 1289 public void shrink(State shrinkState) { 1290 shrink(shrinkState, true); 1291 } 1292 1293 // we use this to shrink the workspace for the all apps view and the customize view 1294 public void shrink(State shrinkState, boolean animated) { 1295 if (mFirstLayout) { 1296 // (mFirstLayout == "first layout has not happened yet") 1297 // if we get a call to shrink() as part of our initialization (for example, if 1298 // Launcher is started in All Apps mode) then we need to wait for a layout call 1299 // to get our width so we can layout the mini-screen views correctly 1300 mSwitchStateAfterFirstLayout = true; 1301 mStateAfterFirstLayout = shrinkState; 1302 return; 1303 } 1304 1305 // Stop any scrolling, move to the current page right away 1306 setCurrentPage((mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage); 1307 1308 CellLayout currentPage = (CellLayout) getChildAt(mCurrentPage); 1309 if (currentPage == null) { 1310 Log.w(TAG, "currentPage is NULL! mCurrentPage " + mCurrentPage 1311 + " mNextPage " + mNextPage); 1312 return; 1313 } 1314 if (currentPage.getBackgroundAlphaMultiplier() < 1.0f) { 1315 currentPage.setBackgroundAlpha(0.0f); 1316 } 1317 currentPage.setBackgroundAlphaMultiplier(1.0f); 1318 1319 mState = shrinkState; 1320 updateChildrenLayersEnabled(); 1321 1322 // we intercept and reject all touch events when we're small, so be sure to reset the state 1323 mTouchState = TOUCH_STATE_REST; 1324 mActivePointerId = INVALID_POINTER; 1325 1326 final Resources res = getResources(); 1327 final int screenWidth = getWidth(); 1328 final int screenHeight = getHeight(); 1329 1330 // How much the workspace shrinks when we enter all apps or customization mode 1331 final float shrinkFactor = res.getInteger(R.integer.config_workspaceShrinkPercent) / 100.0f; 1332 1333 // Making the assumption that all pages have the same width as the 0th 1334 final int pageWidth = getChildAt(0).getMeasuredWidth(); 1335 final int pageHeight = getChildAt(0).getMeasuredHeight(); 1336 1337 final int scaledPageWidth = (int) (shrinkFactor * pageWidth); 1338 final int scaledPageHeight = (int) (shrinkFactor * pageHeight); 1339 final float extraScaledSpacing = res.getDimension(R.dimen.smallScreenExtraSpacing); 1340 1341 final int screenCount = getChildCount(); 1342 float totalWidth = screenCount * scaledPageWidth + (screenCount - 1) * extraScaledSpacing; 1343 1344 boolean isPortrait = getMeasuredHeight() > getMeasuredWidth(); 1345 float y = (isPortrait ? 1346 getResources().getDimension(R.dimen.allAppsSmallScreenVerticalMarginPortrait) : 1347 getResources().getDimension(R.dimen.allAppsSmallScreenVerticalMarginLandscape)); 1348 float finalAlpha = 1.0f; 1349 float extraShrinkFactor = 1.0f; 1350 1351 // We shrink and disappear to nothing 1352 y = screenHeight - y - scaledPageHeight; 1353 finalAlpha = 0.0f; 1354 1355 int duration = res.getInteger(R.integer.config_appsCustomizeWorkspaceShrinkTime); 1356 1357 // We animate all the screens to the centered position in workspace 1358 // At the same time, the screens become greyed/dimmed 1359 1360 // newX is initialized to the left-most position of the centered screens 1361 float x = mScroller.getFinalX() + screenWidth / 2 - totalWidth / 2; 1362 1363 // We are going to scale about the center of the view, so we need to adjust the positions 1364 // of the views accordingly 1365 x -= (pageWidth - scaledPageWidth) / 2.0f; 1366 y -= (pageHeight - scaledPageHeight) / 2.0f; 1367 1368 if (mAnimator != null) { 1369 mAnimator.cancel(); 1370 } 1371 1372 mAnimator = new AnimatorSet(); 1373 1374 final int childCount = getChildCount(); 1375 final float[] oldXs = new float[childCount]; 1376 final float[] oldYs = new float[childCount]; 1377 final float[] oldScaleXs = new float[childCount]; 1378 final float[] oldScaleYs = new float[childCount]; 1379 final float[] oldBackgroundAlphas = new float[childCount]; 1380 final float[] oldAlphas = new float[childCount]; 1381 final float[] oldRotationYs = new float[childCount]; 1382 final float[] newXs = new float[childCount]; 1383 final float[] newYs = new float[childCount]; 1384 final float[] newScaleXs = new float[childCount]; 1385 final float[] newScaleYs = new float[childCount]; 1386 final float[] newBackgroundAlphas = new float[childCount]; 1387 final float[] newAlphas = new float[childCount]; 1388 final float[] newRotationYs = new float[childCount]; 1389 1390 for (int i = 0; i < screenCount; i++) { 1391 final CellLayout cl = (CellLayout) getChildAt(i); 1392 1393 float rotation = (-i + 2) * WORKSPACE_ROTATION; 1394 float rotationScaleX = (float) (1.0f / Math.cos(Math.PI * rotation / 180.0f)); 1395 float rotationScaleY = getYScaleForScreen(i); 1396 1397 oldAlphas[i] = cl.getAlpha(); 1398 newAlphas[i] = finalAlpha; 1399 if (animated && (oldAlphas[i] != 0f || newAlphas[i] != 0f)) { 1400 // if the CellLayout will be visible during the animation, force building its 1401 // hardware layer immediately so we don't see a blip later in the animation 1402 cl.buildChildrenLayer(); 1403 } 1404 if (animated) { 1405 oldXs[i] = cl.getX(); 1406 oldYs[i] = cl.getY(); 1407 oldScaleXs[i] = cl.getScaleX(); 1408 oldScaleYs[i] = cl.getScaleY(); 1409 oldBackgroundAlphas[i] = cl.getBackgroundAlpha(); 1410 oldRotationYs[i] = cl.getRotationY(); 1411 newXs[i] = x; 1412 newYs[i] = y; 1413 newScaleXs[i] = shrinkFactor * rotationScaleX * extraShrinkFactor; 1414 newScaleYs[i] = shrinkFactor * rotationScaleY * extraShrinkFactor; 1415 newBackgroundAlphas[i] = finalAlpha; 1416 newRotationYs[i] = rotation; 1417 } else { 1418 cl.setX((int)x); 1419 cl.setY((int)y); 1420 cl.setScaleX(shrinkFactor * rotationScaleX * extraShrinkFactor); 1421 cl.setScaleY(shrinkFactor * rotationScaleY * extraShrinkFactor); 1422 cl.setBackgroundAlpha(finalAlpha); 1423 cl.setAlpha(finalAlpha); 1424 cl.setRotationY(rotation); 1425 mShrinkAnimationListener.onAnimationEnd(null); 1426 } 1427 // increment newX for the next screen 1428 x += scaledPageWidth + extraScaledSpacing; 1429 } 1430 1431 float wallpaperOffset = 0.5f; 1432 Display display = mLauncher.getWindowManager().getDefaultDisplay(); 1433 int wallpaperTravelHeight = (int) (display.getHeight() * 1434 wallpaperTravelToScreenHeightRatio(display.getWidth(), display.getHeight())); 1435 float offsetFromCenter = (wallpaperTravelHeight / (float) mWallpaperHeight) / 2f; 1436 boolean isLandscape = display.getWidth() > display.getHeight(); 1437 1438 // on phones, don't scroll the wallpaper horizontally or vertically when switching 1439 // to/from all apps 1440 final boolean enableWallpaperEffects = 1441 isHardwareAccelerated() && LauncherApplication.isScreenLarge(); 1442 if (enableWallpaperEffects) { 1443 switch (shrinkState) { 1444 // animating in 1445 case SPRING_LOADED: 1446 wallpaperOffset = 0.5f; 1447 mWallpaperOffset.setVerticalCatchupConstant(isLandscape ? 0.34f : 0.32f); 1448 break; 1449 case SMALL: 1450 // allapps 1451 wallpaperOffset = 0.5f - offsetFromCenter; 1452 mWallpaperOffset.setVerticalCatchupConstant(isLandscape ? 0.34f : 0.32f); 1453 break; 1454 } 1455 } 1456 1457 setLayoutScale(1.0f); 1458 if (animated) { 1459 if (enableWallpaperEffects) { 1460 mWallpaperOffset.setHorizontalCatchupConstant(0.46f); 1461 mWallpaperOffset.setOverrideHorizontalCatchupConstant(true); 1462 } 1463 1464 mSyncWallpaperOffsetWithScroll = false; 1465 1466 ValueAnimator animWithInterpolator = 1467 ValueAnimator.ofFloat(0f, 1f).setDuration(duration); 1468 animWithInterpolator.setInterpolator(mZoomOutInterpolator); 1469 1470 final float oldHorizontalWallpaperOffset = getHorizontalWallpaperOffset(); 1471 final float oldVerticalWallpaperOffset = getVerticalWallpaperOffset(); 1472 final float newHorizontalWallpaperOffset = 0.5f; 1473 final float newVerticalWallpaperOffset = wallpaperOffset; 1474 animWithInterpolator.addUpdateListener(new LauncherAnimatorUpdateListener() { 1475 public void onAnimationUpdate(float a, float b) { 1476 if (b == 0f) { 1477 // an optimization, and required for correct behavior. 1478 return; 1479 } 1480 invalidate(); 1481 if (enableWallpaperEffects) { 1482 setHorizontalWallpaperOffset( 1483 a * oldHorizontalWallpaperOffset + b * newHorizontalWallpaperOffset); 1484 setVerticalWallpaperOffset( 1485 a * oldVerticalWallpaperOffset + b * newVerticalWallpaperOffset); 1486 } 1487 for (int i = 0; i < screenCount; i++) { 1488 final CellLayout cl = (CellLayout) getChildAt(i); 1489 cl.fastInvalidate(); 1490 cl.setFastX(a * oldXs[i] + b * newXs[i]); 1491 cl.setFastY(a * oldYs[i] + b * newYs[i]); 1492 cl.setFastScaleX(a * oldScaleXs[i] + b * newScaleXs[i]); 1493 cl.setFastScaleY(a * oldScaleYs[i] + b * newScaleYs[i]); 1494 cl.setFastBackgroundAlpha( 1495 a * oldBackgroundAlphas[i] + b * newBackgroundAlphas[i]); 1496 cl.setFastAlpha(a * oldAlphas[i] + b * newAlphas[i]); 1497 cl.setFastRotationY(a * oldRotationYs[i] + b * newRotationYs[i]); 1498 } 1499 } 1500 }); 1501 mAnimator.playTogether(animWithInterpolator); 1502 mAnimator.addListener(mShrinkAnimationListener); 1503 mAnimator.start(); 1504 } else if (enableWallpaperEffects) { 1505 setVerticalWallpaperOffset(wallpaperOffset); 1506 setHorizontalWallpaperOffset(0.5f); 1507 updateWallpaperOffsetImmediately(); 1508 } 1509 setChildrenDrawnWithCacheEnabled(true); 1510 1511 showBackgroundGradientForAllApps(); 1512 } 1513 1514 /* 1515 * This interpolator emulates the rate at which the perceived scale of an object changes 1516 * as its distance from a camera increases. When this interpolator is applied to a scale 1517 * animation on a view, it evokes the sense that the object is shrinking due to moving away 1518 * from the camera. 1519 */ 1520 static class ZInterpolator implements TimeInterpolator { 1521 private float focalLength; 1522 1523 public ZInterpolator(float foc) { 1524 focalLength = foc; 1525 } 1526 1527 public float getInterpolation(float input) { 1528 return (1.0f - focalLength / (focalLength + input)) / 1529 (1.0f - focalLength / (focalLength + 1.0f)); 1530 } 1531 } 1532 1533 /* 1534 * The exact reverse of ZInterpolator. 1535 */ 1536 static class InverseZInterpolator implements TimeInterpolator { 1537 private ZInterpolator zInterpolator; 1538 public InverseZInterpolator(float foc) { 1539 zInterpolator = new ZInterpolator(foc); 1540 } 1541 public float getInterpolation(float input) { 1542 return 1 - zInterpolator.getInterpolation(1 - input); 1543 } 1544 } 1545 1546 /* 1547 * ZInterpolator compounded with an ease-out. 1548 */ 1549 static class ZoomOutInterpolator implements TimeInterpolator { 1550 private final ZInterpolator zInterpolator = new ZInterpolator(0.2f); 1551 private final DecelerateInterpolator decelerate = new DecelerateInterpolator(1.8f); 1552 1553 public float getInterpolation(float input) { 1554 return decelerate.getInterpolation(zInterpolator.getInterpolation(input)); 1555 } 1556 } 1557 1558 /* 1559 * InvereZInterpolator compounded with an ease-out. 1560 */ 1561 static class ZoomInInterpolator implements TimeInterpolator { 1562 private final InverseZInterpolator inverseZInterpolator = new InverseZInterpolator(0.35f); 1563 private final DecelerateInterpolator decelerate = new DecelerateInterpolator(3.0f); 1564 1565 public float getInterpolation(float input) { 1566 return decelerate.getInterpolation(inverseZInterpolator.getInterpolation(input)); 1567 } 1568 } 1569 1570 private final ZoomOutInterpolator mZoomOutInterpolator = new ZoomOutInterpolator(); 1571 private final ZoomInInterpolator mZoomInInterpolator = new ZoomInInterpolator(); 1572 1573 /* 1574 * 1575 * We call these methods (onDragStartedWithItemSpans/onDragStartedWithSize) whenever we 1576 * start a drag in Launcher, regardless of whether the drag has ever entered the Workspace 1577 * 1578 * These methods mark the appropriate pages as accepting drops (which alters their visual 1579 * appearance). 1580 * 1581 */ 1582 public void onDragStartedWithItem(View v) { 1583 final Canvas canvas = new Canvas(); 1584 1585 // We need to add extra padding to the bitmap to make room for the glow effect 1586 final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS; 1587 1588 // The outline is used to visualize where the item will land if dropped 1589 mDragOutline = createDragOutline(v, canvas, bitmapPadding); 1590 } 1591 1592 public void onDragStartedWithItemSpans(int spanX, int spanY, Bitmap b) { 1593 final Canvas canvas = new Canvas(); 1594 1595 // We need to add extra padding to the bitmap to make room for the glow effect 1596 final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS; 1597 1598 CellLayout cl = (CellLayout) getChildAt(0); 1599 1600 int[] size = cl.cellSpansToSize(spanX, spanY); 1601 1602 // The outline is used to visualize where the item will land if dropped 1603 mDragOutline = createDragOutline(b, canvas, bitmapPadding, size[0], size[1]); 1604 } 1605 1606 // we call this method whenever a drag and drop in Launcher finishes, even if Workspace was 1607 // never dragged over 1608 public void onDragStopped(boolean success) { 1609 // In the success case, DragController has already called onDragExit() 1610 if (!success) { 1611 doDragExit(null); 1612 } 1613 } 1614 1615 // We call this when we trigger an unshrink by clicking on the CellLayout cl 1616 public void unshrink(CellLayout clThatWasClicked) { 1617 unshrink(clThatWasClicked, false); 1618 } 1619 1620 public void unshrink(CellLayout clThatWasClicked, boolean springLoaded) { 1621 int newCurrentPage = indexOfChild(clThatWasClicked); 1622 if (isSmall()) { 1623 if (springLoaded) { 1624 setLayoutScale(mSpringLoadedShrinkFactor); 1625 } 1626 scrollToNewPageWithoutMovingPages(newCurrentPage); 1627 unshrink(true, springLoaded); 1628 } 1629 } 1630 1631 1632 public void enterSpringLoadedDragMode(CellLayout clThatWasClicked) { 1633 unshrink(clThatWasClicked, true); 1634 } 1635 1636 public void exitSpringLoadedDragMode(State shrinkState) { 1637 shrink(shrinkState); 1638 } 1639 1640 public void exitWidgetResizeMode() { 1641 DragLayer dragLayer = mLauncher.getDragLayer(); 1642 dragLayer.clearAllResizeFrames(); 1643 } 1644 1645 void unshrink(boolean animated) { 1646 unshrink(animated, false); 1647 } 1648 1649 void unshrink(boolean animated, boolean springLoaded) { 1650 if (isSmall()) { 1651 float finalScaleFactor = 1.0f; 1652 float finalBackgroundAlpha = 0.0f; 1653 if (springLoaded) { 1654 finalScaleFactor = mSpringLoadedShrinkFactor; 1655 finalBackgroundAlpha = 1.0f; 1656 mState = State.SPRING_LOADED; 1657 } else { 1658 mState = State.NORMAL; 1659 } 1660 if (mAnimator != null) { 1661 mAnimator.cancel(); 1662 } 1663 1664 mAnimator = new AnimatorSet(); 1665 final int screenCount = getChildCount(); 1666 1667 final int duration = getResources().getInteger(R.integer.config_workspaceUnshrinkTime); 1668 1669 final float[] oldTranslationXs = new float[getChildCount()]; 1670 final float[] oldTranslationYs = new float[getChildCount()]; 1671 final float[] oldScaleXs = new float[getChildCount()]; 1672 final float[] oldScaleYs = new float[getChildCount()]; 1673 final float[] oldBackgroundAlphas = new float[getChildCount()]; 1674 final float[] oldBackgroundAlphaMultipliers = new float[getChildCount()]; 1675 final float[] oldAlphas = new float[getChildCount()]; 1676 final float[] oldRotationYs = new float[getChildCount()]; 1677 final float[] newTranslationXs = new float[getChildCount()]; 1678 final float[] newTranslationYs = new float[getChildCount()]; 1679 final float[] newScaleXs = new float[getChildCount()]; 1680 final float[] newScaleYs = new float[getChildCount()]; 1681 final float[] newBackgroundAlphas = new float[getChildCount()]; 1682 final float[] newBackgroundAlphaMultipliers = new float[getChildCount()]; 1683 final float[] newAlphas = new float[getChildCount()]; 1684 final float[] newRotationYs = new float[getChildCount()]; 1685 1686 for (int i = 0; i < screenCount; i++) { 1687 final CellLayout cl = (CellLayout)getChildAt(i); 1688 float finalAlphaValue = 0f; 1689 float rotation = 0f; 1690 if (LauncherApplication.isScreenLarge()) { 1691 finalAlphaValue = (i == mCurrentPage) ? 1.0f : 0.0f; 1692 1693 if (i < mCurrentPage) { 1694 rotation = WORKSPACE_ROTATION; 1695 } else if (i > mCurrentPage) { 1696 rotation = -WORKSPACE_ROTATION; 1697 } 1698 } else { 1699 // Don't hide the side panes on the phone if we don't also update the side pages 1700 // alpha. See screenScrolled(). 1701 finalAlphaValue = 1f; 1702 } 1703 float finalAlphaMultiplierValue = 1f; 1704 1705 float translation = 0f; 1706 1707 // If the screen is not xlarge, then don't rotate the CellLayouts 1708 // NOTE: If we don't update the side pages alpha, then we should not hide the side 1709 // pages. see unshrink(). 1710 if (LauncherApplication.isScreenLarge()) { 1711 translation = getOffsetXForRotation(rotation, cl.getWidth(), cl.getHeight()); 1712 } 1713 1714 oldAlphas[i] = cl.getAlpha(); 1715 newAlphas[i] = finalAlphaValue; 1716 if (animated) { 1717 oldTranslationXs[i] = cl.getTranslationX(); 1718 oldTranslationYs[i] = cl.getTranslationY(); 1719 oldScaleXs[i] = cl.getScaleX(); 1720 oldScaleYs[i] = cl.getScaleY(); 1721 oldBackgroundAlphas[i] = cl.getBackgroundAlpha(); 1722 oldBackgroundAlphaMultipliers[i] = cl.getBackgroundAlphaMultiplier(); 1723 oldRotationYs[i] = cl.getRotationY(); 1724 1725 newTranslationXs[i] = translation; 1726 newTranslationYs[i] = 0f; 1727 newScaleXs[i] = finalScaleFactor; 1728 newScaleYs[i] = finalScaleFactor; 1729 newBackgroundAlphas[i] = finalBackgroundAlpha; 1730 newBackgroundAlphaMultipliers[i] = finalAlphaMultiplierValue; 1731 newRotationYs[i] = rotation; 1732 } else { 1733 cl.setTranslationX(translation); 1734 cl.setTranslationY(0.0f); 1735 cl.setScaleX(finalScaleFactor); 1736 cl.setScaleY(finalScaleFactor); 1737 cl.setBackgroundAlpha(0.0f); 1738 cl.setBackgroundAlphaMultiplier(finalAlphaMultiplierValue); 1739 cl.setAlpha(finalAlphaValue); 1740 cl.setRotationY(rotation); 1741 mUnshrinkAnimationListener.onAnimationEnd(null); 1742 } 1743 } 1744 Display display = mLauncher.getWindowManager().getDefaultDisplay(); 1745 boolean isLandscape = display.getWidth() > display.getHeight(); 1746 // on phones, don't scroll the wallpaper horizontally or vertically when switching 1747 // to/from all apps 1748 final boolean enableWallpaperEffects = 1749 isHardwareAccelerated() && LauncherApplication.isScreenLarge(); 1750 if (enableWallpaperEffects) { 1751 switch (mState) { 1752 // animating out 1753 case SPRING_LOADED: 1754 if (animated) { 1755 mWallpaperOffset.setHorizontalCatchupConstant(isLandscape ? 0.49f : 0.46f); 1756 mWallpaperOffset.setVerticalCatchupConstant(isLandscape ? 0.49f : 0.46f); 1757 mWallpaperOffset.setOverrideHorizontalCatchupConstant(true); 1758 } 1759 break; 1760 case SMALL: 1761 // all apps 1762 if (animated) { 1763 mWallpaperOffset.setHorizontalCatchupConstant(isLandscape ? 0.65f : 0.65f); 1764 mWallpaperOffset.setVerticalCatchupConstant(isLandscape ? 0.65f : 0.65f); 1765 mWallpaperOffset.setOverrideHorizontalCatchupConstant(true); 1766 } 1767 break; 1768 } 1769 } 1770 if (animated) { 1771 ValueAnimator animWithInterpolator = 1772 ValueAnimator.ofFloat(0f, 1f).setDuration(duration); 1773 animWithInterpolator.setInterpolator(mZoomInInterpolator); 1774 1775 final float oldHorizontalWallpaperOffset = enableWallpaperEffects ? 1776 getHorizontalWallpaperOffset() : 0; 1777 final float oldVerticalWallpaperOffset = enableWallpaperEffects ? 1778 getVerticalWallpaperOffset() : 0; 1779 final float newHorizontalWallpaperOffset = enableWallpaperEffects ? 1780 wallpaperOffsetForCurrentScroll() : 0; 1781 final float newVerticalWallpaperOffset = enableWallpaperEffects ? 0.5f : 0; 1782 animWithInterpolator.addUpdateListener(new LauncherAnimatorUpdateListener() { 1783 public void onAnimationUpdate(float a, float b) { 1784 if (b == 0f) { 1785 // an optimization, but not required 1786 return; 1787 } 1788 invalidate(); 1789 if (enableWallpaperEffects) { 1790 setHorizontalWallpaperOffset(a * oldHorizontalWallpaperOffset 1791 + b * newHorizontalWallpaperOffset); 1792 setVerticalWallpaperOffset(a * oldVerticalWallpaperOffset 1793 + b * newVerticalWallpaperOffset); 1794 } 1795 for (int i = 0; i < screenCount; i++) { 1796 final CellLayout cl = (CellLayout) getChildAt(i); 1797 cl.fastInvalidate(); 1798 cl.setFastTranslationX( 1799 a * oldTranslationXs[i] + b * newTranslationXs[i]); 1800 cl.setFastTranslationY( 1801 a * oldTranslationYs[i] + b * newTranslationYs[i]); 1802 cl.setFastScaleX(a * oldScaleXs[i] + b * newScaleXs[i]); 1803 cl.setFastScaleY(a * oldScaleYs[i] + b * newScaleYs[i]); 1804 cl.setFastBackgroundAlpha( 1805 a * oldBackgroundAlphas[i] + b * newBackgroundAlphas[i]); 1806 cl.setBackgroundAlphaMultiplier(a * oldBackgroundAlphaMultipliers[i] + 1807 b * newBackgroundAlphaMultipliers[i]); 1808 cl.setFastAlpha(a * oldAlphas[i] + b * newAlphas[i]); 1809 } 1810 } 1811 }); 1812 1813 ValueAnimator rotationAnim = 1814 ValueAnimator.ofFloat(0f, 1f).setDuration(duration); 1815 rotationAnim.setInterpolator(new DecelerateInterpolator(2.0f)); 1816 rotationAnim.addUpdateListener(new LauncherAnimatorUpdateListener() { 1817 public void onAnimationUpdate(float a, float b) { 1818 // don't invalidate workspace because we did it above 1819 if (b == 0f) { 1820 // an optimization, but not required 1821 return; 1822 } 1823 for (int i = 0; i < screenCount; i++) { 1824 final CellLayout cl = (CellLayout) getChildAt(i); 1825 cl.setFastRotationY(a * oldRotationYs[i] + b * newRotationYs[i]); 1826 } 1827 } 1828 }); 1829 1830 mAnimator.playTogether(animWithInterpolator, rotationAnim); 1831 // If we call this when we're not animated, onAnimationEnd is never called on 1832 // the listener; make sure we only use the listener when we're actually animating 1833 mAnimator.addListener(mUnshrinkAnimationListener); 1834 mAnimator.start(); 1835 } else { 1836 if (enableWallpaperEffects) { 1837 setHorizontalWallpaperOffset(wallpaperOffsetForCurrentScroll()); 1838 setVerticalWallpaperOffset(0.5f); 1839 updateWallpaperOffsetImmediately(); 1840 } 1841 } 1842 } 1843 1844 hideBackgroundGradient(springLoaded ? getResources().getInteger( 1845 R.integer.config_appsCustomizeSpringLoadedBgAlpha) / 100f : 0f); 1846 } 1847 1848 /** 1849 * Draw the View v into the given Canvas. 1850 * 1851 * @param v the view to draw 1852 * @param destCanvas the canvas to draw on 1853 * @param padding the horizontal and vertical padding to use when drawing 1854 */ 1855 private void drawDragView(View v, Canvas destCanvas, int padding, boolean pruneToDrawable) { 1856 final Rect clipRect = mTempRect; 1857 v.getDrawingRect(clipRect); 1858 1859 destCanvas.save(); 1860 if (v instanceof TextView && pruneToDrawable) { 1861 Drawable d = ((TextView) v).getCompoundDrawables()[1]; 1862 clipRect.set(0, 0, d.getIntrinsicWidth() + padding, d.getIntrinsicHeight() + padding); 1863 destCanvas.translate(padding / 2, padding / 2); 1864 d.draw(destCanvas); 1865 } else { 1866 if (v instanceof FolderIcon) { 1867 clipRect.bottom = getResources().getDimensionPixelSize(R.dimen.folder_preview_size); 1868 } else if (v instanceof BubbleTextView) { 1869 final BubbleTextView tv = (BubbleTextView) v; 1870 clipRect.bottom = tv.getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + 1871 tv.getLayout().getLineTop(0); 1872 } else if (v instanceof TextView) { 1873 final TextView tv = (TextView) v; 1874 clipRect.bottom = tv.getExtendedPaddingTop() - tv.getCompoundDrawablePadding() + 1875 tv.getLayout().getLineTop(0); 1876 } 1877 destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2); 1878 destCanvas.clipRect(clipRect, Op.REPLACE); 1879 v.draw(destCanvas); 1880 } 1881 destCanvas.restore(); 1882 } 1883 1884 /** 1885 * Returns a new bitmap to show when the given View is being dragged around. 1886 * Responsibility for the bitmap is transferred to the caller. 1887 */ 1888 public Bitmap createDragBitmap(View v, Canvas canvas, int padding) { 1889 final int outlineColor = getResources().getColor(R.color.drag_outline_color); 1890 Bitmap b; 1891 1892 if (v instanceof TextView) { 1893 Drawable d = ((TextView) v).getCompoundDrawables()[1]; 1894 b = Bitmap.createBitmap(d.getIntrinsicWidth() + padding, 1895 d.getIntrinsicHeight() + padding, Bitmap.Config.ARGB_8888); 1896 } else { 1897 b = Bitmap.createBitmap( 1898 v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888); 1899 } 1900 1901 canvas.setBitmap(b); 1902 drawDragView(v, canvas, padding, true); 1903 mOutlineHelper.applyOuterBlur(b, canvas, outlineColor); 1904 1905 return b; 1906 } 1907 1908 /** 1909 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location. 1910 * Responsibility for the bitmap is transferred to the caller. 1911 */ 1912 private Bitmap createDragOutline(View v, Canvas canvas, int padding) { 1913 final int outlineColor = getResources().getColor(R.color.drag_outline_color); 1914 final Bitmap b = Bitmap.createBitmap( 1915 v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888); 1916 1917 canvas.setBitmap(b); 1918 drawDragView(v, canvas, padding, false); 1919 mOutlineHelper.applyMediumExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor); 1920 return b; 1921 } 1922 1923 /** 1924 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location. 1925 * Responsibility for the bitmap is transferred to the caller. 1926 */ 1927 private Bitmap createDragOutline(Bitmap orig, Canvas canvas, int padding, int w, int h) { 1928 final int outlineColor = getResources().getColor(R.color.drag_outline_color); 1929 final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 1930 canvas.setBitmap(b); 1931 1932 Rect src = new Rect(0, 0, orig.getWidth(), orig.getHeight()); 1933 float scaleFactor = Math.min((w - padding) / (float) orig.getWidth(), 1934 (h - padding) / (float) orig.getHeight()); 1935 int scaledWidth = (int) (scaleFactor * orig.getWidth()); 1936 int scaledHeight = (int) (scaleFactor * orig.getHeight()); 1937 Rect dst = new Rect(0, 0, scaledWidth, scaledHeight); 1938 1939 // center the image 1940 dst.offset((w - scaledWidth) / 2, (h - scaledHeight) / 2); 1941 1942 Paint p = new Paint(); 1943 p.setFilterBitmap(true); 1944 canvas.drawBitmap(orig, src, dst, p); 1945 mOutlineHelper.applyMediumExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor); 1946 1947 return b; 1948 } 1949 1950 /** 1951 * Creates a drag outline to represent a drop (that we don't have the actual information for 1952 * yet). May be changed in the future to alter the drop outline slightly depending on the 1953 * clip description mime data. 1954 */ 1955 private Bitmap createExternalDragOutline(Canvas canvas, int padding) { 1956 Resources r = getResources(); 1957 final int outlineColor = r.getColor(R.color.drag_outline_color); 1958 final int iconWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width); 1959 final int iconHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height); 1960 final int rectRadius = r.getDimensionPixelSize(R.dimen.external_drop_icon_rect_radius); 1961 final int inset = (int) (Math.min(iconWidth, iconHeight) * 0.2f); 1962 final Bitmap b = Bitmap.createBitmap( 1963 iconWidth + padding, iconHeight + padding, Bitmap.Config.ARGB_8888); 1964 1965 canvas.setBitmap(b); 1966 canvas.drawRoundRect(new RectF(inset, inset, iconWidth - inset, iconHeight - inset), 1967 rectRadius, rectRadius, mExternalDragOutlinePaint); 1968 mOutlineHelper.applyMediumExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor); 1969 return b; 1970 } 1971 1972 void startDrag(CellLayout.CellInfo cellInfo) { 1973 View child = cellInfo.cell; 1974 1975 // Make sure the drag was started by a long press as opposed to a long click. 1976 if (!child.isInTouchMode()) { 1977 return; 1978 } 1979 1980 mDragInfo = cellInfo; 1981 child.setVisibility(GONE); 1982 1983 child.clearFocus(); 1984 child.setPressed(false); 1985 1986 final Canvas canvas = new Canvas(); 1987 1988 // We need to add extra padding to the bitmap to make room for the glow effect 1989 final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS; 1990 1991 // The outline is used to visualize where the item will land if dropped 1992 mDragOutline = createDragOutline(child, canvas, bitmapPadding); 1993 beginDragShared(child, this); 1994 } 1995 1996 public void beginDragShared(View child, DragSource source) { 1997 // We need to add extra padding to the bitmap to make room for the glow effect 1998 final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS; 1999 2000 // The drag bitmap follows the touch point around on the screen 2001 final Bitmap b = createDragBitmap(child, new Canvas(), bitmapPadding); 2002 2003 final int bmpWidth = b.getWidth(); 2004 final int bmpHeight = b.getHeight(); 2005 2006 mLauncher.getDragLayer().getLocationInDragLayer(child, mTempXY); 2007 final int dragLayerX = (int) mTempXY[0] + (child.getWidth() - bmpWidth) / 2; 2008 int dragLayerY = mTempXY[1] - bitmapPadding / 2; 2009 2010 Rect dragRect = null; 2011 if (child instanceof BubbleTextView) { 2012 int iconSize = getResources().getDimensionPixelSize(R.dimen.app_icon_size); 2013 int top = child.getPaddingTop(); 2014 int left = (bmpWidth - iconSize) / 2; 2015 int right = left + iconSize; 2016 int bottom = top + iconSize; 2017 dragLayerY += top; 2018 dragRect = new Rect(left, top, right, bottom); 2019 } else if (child instanceof FolderIcon) { 2020 int previewSize = getResources().getDimensionPixelSize(R.dimen.folder_preview_size); 2021 dragRect = new Rect(0, 0, child.getWidth(), previewSize); 2022 } 2023 2024 mDragController.startDrag(b, dragLayerX, dragLayerY, source, child.getTag(), 2025 DragController.DRAG_ACTION_MOVE, dragRect); 2026 b.recycle(); 2027 } 2028 2029 void addApplicationShortcut(ShortcutInfo info, CellLayout target, long container, int screen, 2030 int cellX, int cellY, boolean insertAtFirst, int intersectX, int intersectY) { 2031 View view = mLauncher.createShortcut(R.layout.application, target, (ShortcutInfo) info); 2032 2033 final int[] cellXY = new int[2]; 2034 target.findCellForSpanThatIntersects(cellXY, 1, 1, intersectX, intersectY); 2035 addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1, insertAtFirst); 2036 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screen, cellXY[0], 2037 cellXY[1]); 2038 } 2039 2040 /** 2041 * {@inheritDoc} 2042 */ 2043 public boolean acceptDrop(DragObject d) { 2044 // If it's an external drop (e.g. from All Apps), check if it should be accepted 2045 if (d.dragSource != this) { 2046 // Don't accept the drop if we're not over a screen at time of drop 2047 if (mDragTargetLayout == null) { 2048 return false; 2049 } 2050 2051 mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, 2052 d.dragView, mDragViewVisualCenter); 2053 2054 int spanX = 1; 2055 int spanY = 1; 2056 View ignoreView = null; 2057 if (mDragInfo != null) { 2058 final CellLayout.CellInfo dragCellInfo = mDragInfo; 2059 spanX = dragCellInfo.spanX; 2060 spanY = dragCellInfo.spanY; 2061 ignoreView = dragCellInfo.cell; 2062 } else { 2063 final ItemInfo dragInfo = (ItemInfo) d.dragInfo; 2064 spanX = dragInfo.spanX; 2065 spanY = dragInfo.spanY; 2066 } 2067 2068 mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], 2069 (int) mDragViewVisualCenter[1], spanX, spanY, mDragTargetLayout, mTargetCell); 2070 if (willCreateUserFolder((ItemInfo) d.dragInfo, mDragTargetLayout, mTargetCell, true)) { 2071 return true; 2072 } 2073 if (willAddToExistingUserFolder((ItemInfo) d.dragInfo, mDragTargetLayout, 2074 mTargetCell)) { 2075 return true; 2076 } 2077 2078 2079 // Don't accept the drop if there's no room for the item 2080 if (!mDragTargetLayout.findCellForSpanIgnoring(null, spanX, spanY, ignoreView)) { 2081 mLauncher.showOutOfSpaceMessage(); 2082 return false; 2083 } 2084 } 2085 return true; 2086 } 2087 2088 boolean willCreateUserFolder(ItemInfo info, CellLayout target, int[] targetCell, 2089 boolean considerTimeout) { 2090 View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); 2091 2092 boolean hasntMoved = false; 2093 if (mDragInfo != null) { 2094 CellLayout cellParent = getParentCellLayoutForView(mDragInfo.cell); 2095 hasntMoved = (mDragInfo.cellX == targetCell[0] && 2096 mDragInfo.cellY == targetCell[1]) && (cellParent == target); 2097 } 2098 2099 if (dropOverView == null || hasntMoved || (considerTimeout && !mCreateUserFolderOnDrop)) { 2100 return false; 2101 } 2102 2103 boolean aboveShortcut = (dropOverView.getTag() instanceof ShortcutInfo); 2104 boolean willBecomeShortcut = 2105 (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || 2106 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT); 2107 2108 return (aboveShortcut && willBecomeShortcut); 2109 } 2110 2111 boolean willAddToExistingUserFolder(Object dragInfo, CellLayout target, int[] targetCell) { 2112 View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); 2113 if (dropOverView instanceof FolderIcon) { 2114 FolderIcon fi = (FolderIcon) dropOverView; 2115 if (fi.acceptDrop(dragInfo)) { 2116 return true; 2117 } 2118 } 2119 return false; 2120 } 2121 2122 boolean createUserFolderIfNecessary(View newView, long container, CellLayout target, 2123 int[] targetCell, boolean external, DragView dragView, Runnable postAnimationRunnable) { 2124 View v = target.getChildAt(targetCell[0], targetCell[1]); 2125 boolean hasntMoved = false; 2126 if (mDragInfo != null) { 2127 CellLayout cellParent = getParentCellLayoutForView(mDragInfo.cell); 2128 hasntMoved = (mDragInfo.cellX == targetCell[0] && 2129 mDragInfo.cellY == targetCell[1]) && (cellParent == target); 2130 } 2131 2132 if (v == null || hasntMoved || !mCreateUserFolderOnDrop) return false; 2133 mCreateUserFolderOnDrop = false; 2134 final int screen = (targetCell == null) ? mDragInfo.screen : indexOfChild(target); 2135 2136 boolean aboveShortcut = (v.getTag() instanceof ShortcutInfo); 2137 boolean willBecomeShortcut = (newView.getTag() instanceof ShortcutInfo); 2138 2139 if (aboveShortcut && willBecomeShortcut) { 2140 ShortcutInfo sourceInfo = (ShortcutInfo) newView.getTag(); 2141 ShortcutInfo destInfo = (ShortcutInfo) v.getTag(); 2142 // if the drag started here, we need to remove it from the workspace 2143 if (!external) { 2144 getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell); 2145 } 2146 2147 Rect folderLocation = new Rect(); 2148 float scale = mLauncher.getDragLayer().getDescendantRectRelativeToSelf(v, folderLocation); 2149 target.removeView(v); 2150 2151 FolderIcon fi = 2152 mLauncher.addFolder(target, container, screen, targetCell[0], targetCell[1]); 2153 destInfo.cellX = -1; 2154 destInfo.cellY = -1; 2155 sourceInfo.cellX = -1; 2156 sourceInfo.cellY = -1; 2157 2158 fi.performCreateAnimation(destInfo, v, sourceInfo, dragView, folderLocation, scale, 2159 postAnimationRunnable); 2160 return true; 2161 } 2162 return false; 2163 } 2164 2165 boolean addToExistingFolderIfNecessary(View newView, CellLayout target, int[] targetCell, 2166 DragObject d, boolean external) { 2167 View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); 2168 if (dropOverView instanceof FolderIcon) { 2169 FolderIcon fi = (FolderIcon) dropOverView; 2170 if (fi.acceptDrop(d.dragInfo)) { 2171 fi.onDrop(d); 2172 2173 // if the drag started here, we need to remove it from the workspace 2174 if (!external) { 2175 getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell); 2176 } 2177 return true; 2178 } 2179 } 2180 return false; 2181 } 2182 2183 public void onDrop(DragObject d) { 2184 mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, 2185 mDragViewVisualCenter); 2186 2187 // We want the point to be mapped to the dragTarget. 2188 if (mDragTargetLayout != null) { 2189 if (mLauncher.isHotseatLayout(mDragTargetLayout)) { 2190 mapPointFromSelfToSibling(mLauncher.getHotseat(), mDragViewVisualCenter); 2191 } else { 2192 mapPointFromSelfToChild(mDragTargetLayout, mDragViewVisualCenter, null); 2193 } 2194 } 2195 2196 // When you are in customization mode and drag to a particular screen, make that the 2197 // new current/default screen, so any subsequent taps add items to that screen 2198 if (!mLauncher.isAllAppsVisible()) { 2199 int dragTargetIndex = indexOfChild(mDragTargetLayout); 2200 if (mCurrentPage != dragTargetIndex && (isSmall() || mIsSwitchingState)) { 2201 scrollToNewPageWithoutMovingPages(dragTargetIndex); 2202 } 2203 } 2204 CellLayout dropTargetLayout = mDragTargetLayout; 2205 2206 if (d.dragSource != this) { 2207 final int[] touchXY = new int[] { (int) mDragViewVisualCenter[0], 2208 (int) mDragViewVisualCenter[1] }; 2209 onDropExternal(touchXY, d.dragInfo, dropTargetLayout, false, d); 2210 } else if (mDragInfo != null) { 2211 final View cell = mDragInfo.cell; 2212 2213 boolean continueDrop = true; 2214 if (mLauncher.isHotseatLayout(mDragTargetLayout) && d.dragInfo instanceof ItemInfo) { 2215 ItemInfo info = (ItemInfo) d.dragInfo; 2216 if (info.spanX > 1 || info.spanY > 1) { 2217 continueDrop = false; 2218 Toast.makeText(getContext(), R.string.invalid_hotseat_item, 2219 Toast.LENGTH_SHORT).show(); 2220 } 2221 } 2222 2223 if (continueDrop && dropTargetLayout != null) { 2224 // Move internally 2225 long container = mLauncher.isHotseatLayout(dropTargetLayout) ? 2226 LauncherSettings.Favorites.CONTAINER_HOTSEAT : 2227 LauncherSettings.Favorites.CONTAINER_DESKTOP; 2228 int screen = (mTargetCell[0] < 0) ? 2229 mDragInfo.screen : indexOfChild(dropTargetLayout); 2230 int spanX = mDragInfo != null ? mDragInfo.spanX : 1; 2231 int spanY = mDragInfo != null ? mDragInfo.spanY : 1; 2232 // First we find the cell nearest to point at which the item is 2233 // dropped, without any consideration to whether there is an item there. 2234 mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], (int) 2235 mDragViewVisualCenter[1], spanX, spanY, dropTargetLayout, mTargetCell); 2236 // If the item being dropped is a shortcut and the nearest drop 2237 // cell also contains a shortcut, then create a folder with the two shortcuts. 2238 boolean dropInscrollArea = mCurrentPage != screen && screen > -1; 2239 2240 if (!dropInscrollArea && createUserFolderIfNecessary(cell, container, 2241 dropTargetLayout, mTargetCell, false, d.dragView, null)) { 2242 return; 2243 } 2244 2245 if (addToExistingFolderIfNecessary(cell, dropTargetLayout, mTargetCell, d, false)) { 2246 return; 2247 } 2248 2249 // Aside from the special case where we're dropping a shortcut onto a shortcut, 2250 // we need to find the nearest cell location that is vacant 2251 mTargetCell = findNearestVacantArea((int) mDragViewVisualCenter[0], 2252 (int) mDragViewVisualCenter[1], mDragInfo.spanX, mDragInfo.spanY, cell, 2253 dropTargetLayout, mTargetCell); 2254 2255 if (dropInscrollArea && mState != State.SPRING_LOADED) { 2256 snapToPage(screen); 2257 } 2258 2259 2260 if (mTargetCell[0] >= 0 && mTargetCell[1] >= 0) { 2261 if (screen != mDragInfo.screen) { 2262 // Reparent the view 2263 getParentCellLayoutForView(cell).removeView(cell); 2264 addInScreen(cell, container, screen, mTargetCell[0], mTargetCell[1], 2265 mDragInfo.spanX, mDragInfo.spanY); 2266 } 2267 2268 // update the item's position after drop 2269 final ItemInfo info = (ItemInfo) cell.getTag(); 2270 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams(); 2271 dropTargetLayout.onMove(cell, mTargetCell[0], mTargetCell[1]); 2272 lp.cellX = mTargetCell[0]; 2273 lp.cellY = mTargetCell[1]; 2274 cell.setId(LauncherModel.getCellLayoutChildId(container, mDragInfo.screen, 2275 mTargetCell[0], mTargetCell[1], mDragInfo.spanX, mDragInfo.spanY)); 2276 2277 if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT && 2278 cell instanceof LauncherAppWidgetHostView) { 2279 final CellLayout cellLayout = dropTargetLayout; 2280 // We post this call so that the widget has a chance to be placed 2281 // in its final location 2282 2283 final LauncherAppWidgetHostView hostView = (LauncherAppWidgetHostView) cell; 2284 AppWidgetProviderInfo pinfo = hostView.getAppWidgetInfo(); 2285 if (pinfo.resizeMode != AppWidgetProviderInfo.RESIZE_NONE) { 2286 final Runnable resizeRunnable = new Runnable() { 2287 public void run() { 2288 DragLayer dragLayer = mLauncher.getDragLayer(); 2289 dragLayer.addResizeFrame(info, hostView, cellLayout); 2290 } 2291 }; 2292 post(new Runnable() { 2293 public void run() { 2294 if (!isPageMoving()) { 2295 resizeRunnable.run(); 2296 } else { 2297 mDelayedResizeRunnable = resizeRunnable; 2298 } 2299 } 2300 }); 2301 } 2302 } 2303 2304 LauncherModel.moveItemInDatabase(mLauncher, info, container, screen, lp.cellX, 2305 lp.cellY); 2306 } 2307 } 2308 2309 final CellLayout parent = (CellLayout) cell.getParent().getParent(); 2310 2311 // Prepare it to be animated into its new position 2312 // This must be called after the view has been re-parented 2313 final Runnable disableHardwareLayersRunnable = new Runnable() { 2314 @Override 2315 public void run() { 2316 mAnimatingViewIntoPlace = false; 2317 updateChildrenLayersEnabled(); 2318 } 2319 }; 2320 mAnimatingViewIntoPlace = true; 2321 if (d.dragView.hasDrawn()) { 2322 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, cell, 2323 disableHardwareLayersRunnable); 2324 } else { 2325 cell.setVisibility(VISIBLE); 2326 } 2327 parent.onDropChild(cell); 2328 } 2329 } 2330 2331 public void getViewLocationRelativeToSelf(View v, int[] location) { 2332 getLocationInWindow(location); 2333 int x = location[0]; 2334 int y = location[1]; 2335 2336 v.getLocationInWindow(location); 2337 int vX = location[0]; 2338 int vY = location[1]; 2339 2340 location[0] = vX - x; 2341 location[1] = vY - y; 2342 } 2343 2344 public void onDragEnter(DragObject d) { 2345 if (mDragTargetLayout != null) { 2346 mDragTargetLayout.setIsDragOverlapping(false); 2347 mDragTargetLayout.onDragExit(); 2348 } 2349 mDragTargetLayout = getCurrentDropLayout(); 2350 mDragTargetLayout.setIsDragOverlapping(true); 2351 mDragTargetLayout.onDragEnter(); 2352 2353 // Because we don't have space in the Phone UI (the CellLayouts run to the edge) we 2354 // don't need to show the outlines 2355 if (LauncherApplication.isScreenLarge()) { 2356 showOutlines(); 2357 } 2358 } 2359 2360 private void doDragExit(DragObject d) { 2361 // Clean up folders 2362 cleanupFolderCreation(d); 2363 2364 // Reset the scroll area and previous drag target 2365 onResetScrollArea(); 2366 2367 if (mDragTargetLayout != null) { 2368 mDragTargetLayout.setIsDragOverlapping(false); 2369 mDragTargetLayout.onDragExit(); 2370 } 2371 mLastDragOverView = null; 2372 2373 if (!mIsPageMoving) { 2374 hideOutlines(); 2375 } 2376 } 2377 2378 public void onDragExit(DragObject d) { 2379 doDragExit(d); 2380 } 2381 2382 public DropTarget getDropTargetDelegate(DragObject d) { 2383 return null; 2384 } 2385 2386 /** 2387 * Tests to see if the drop will be accepted by Launcher, and if so, includes additional data 2388 * in the returned structure related to the widgets that match the drop (or a null list if it is 2389 * a shortcut drop). If the drop is not accepted then a null structure is returned. 2390 */ 2391 private Pair<Integer, List<WidgetMimeTypeHandlerData>> validateDrag(DragEvent event) { 2392 final LauncherModel model = mLauncher.getModel(); 2393 final ClipDescription desc = event.getClipDescription(); 2394 final int mimeTypeCount = desc.getMimeTypeCount(); 2395 for (int i = 0; i < mimeTypeCount; ++i) { 2396 final String mimeType = desc.getMimeType(i); 2397 if (mimeType.equals(InstallShortcutReceiver.SHORTCUT_MIMETYPE)) { 2398 return new Pair<Integer, List<WidgetMimeTypeHandlerData>>(i, null); 2399 } else { 2400 final List<WidgetMimeTypeHandlerData> widgets = 2401 model.resolveWidgetsForMimeType(mContext, mimeType); 2402 if (widgets.size() > 0) { 2403 return new Pair<Integer, List<WidgetMimeTypeHandlerData>>(i, widgets); 2404 } 2405 } 2406 } 2407 return null; 2408 } 2409 2410 /** 2411 * Global drag and drop handler 2412 */ 2413 @Override 2414 public boolean onDragEvent(DragEvent event) { 2415 final ClipDescription desc = event.getClipDescription(); 2416 final CellLayout layout = (CellLayout) getChildAt(mCurrentPage); 2417 final int[] pos = new int[2]; 2418 layout.getLocationOnScreen(pos); 2419 // We need to offset the drag coordinates to layout coordinate space 2420 final int x = (int) event.getX() - pos[0]; 2421 final int y = (int) event.getY() - pos[1]; 2422 2423 switch (event.getAction()) { 2424 case DragEvent.ACTION_DRAG_STARTED: { 2425 // Validate this drag 2426 Pair<Integer, List<WidgetMimeTypeHandlerData>> test = validateDrag(event); 2427 if (test != null) { 2428 boolean isShortcut = (test.second == null); 2429 if (isShortcut) { 2430 // Check if we have enough space on this screen to add a new shortcut 2431 if (!layout.findCellForSpan(pos, 1, 1)) { 2432 mLauncher.showOutOfSpaceMessage(); 2433 return false; 2434 } 2435 } 2436 } else { 2437 // Show error message if we couldn't accept any of the items 2438 Toast.makeText(mContext, mContext.getString(R.string.external_drop_widget_error), 2439 Toast.LENGTH_SHORT).show(); 2440 return false; 2441 } 2442 2443 // Create the drag outline 2444 // We need to add extra padding to the bitmap to make room for the glow effect 2445 final Canvas canvas = new Canvas(); 2446 final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS; 2447 mDragOutline = createExternalDragOutline(canvas, bitmapPadding); 2448 2449 // Show the current page outlines to indicate that we can accept this drop 2450 showOutlines(); 2451 layout.setIsDragOccuring(true); 2452 layout.onDragEnter(); 2453 layout.visualizeDropLocation(null, mDragOutline, x, y, 1, 1); 2454 2455 return true; 2456 } 2457 case DragEvent.ACTION_DRAG_LOCATION: 2458 // Visualize the drop location 2459 layout.visualizeDropLocation(null, mDragOutline, x, y, 1, 1); 2460 return true; 2461 case DragEvent.ACTION_DROP: { 2462 // Try and add any shortcuts 2463 final LauncherModel model = mLauncher.getModel(); 2464 final ClipData data = event.getClipData(); 2465 2466 // We assume that the mime types are ordered in descending importance of 2467 // representation. So we enumerate the list of mime types and alert the 2468 // user if any widgets can handle the drop. Only the most preferred 2469 // representation will be handled. 2470 pos[0] = x; 2471 pos[1] = y; 2472 Pair<Integer, List<WidgetMimeTypeHandlerData>> test = validateDrag(event); 2473 if (test != null) { 2474 final int index = test.first; 2475 final List<WidgetMimeTypeHandlerData> widgets = test.second; 2476 final boolean isShortcut = (widgets == null); 2477 final String mimeType = desc.getMimeType(index); 2478 if (isShortcut) { 2479 final Intent intent = data.getItemAt(index).getIntent(); 2480 Object info = model.infoFromShortcutIntent(mContext, intent, data.getIcon()); 2481 onDropExternal(new int[] { x, y }, info, layout, false); 2482 } else { 2483 if (widgets.size() == 1) { 2484 // If there is only one item, then go ahead and add and configure 2485 // that widget 2486 final AppWidgetProviderInfo widgetInfo = widgets.get(0).widgetInfo; 2487 final PendingAddWidgetInfo createInfo = 2488 new PendingAddWidgetInfo(widgetInfo, mimeType, data); 2489 mLauncher.addAppWidgetFromDrop(createInfo, 2490 LauncherSettings.Favorites.CONTAINER_DESKTOP, mCurrentPage, null, pos); 2491 } else { 2492 // Show the widget picker dialog if there is more than one widget 2493 // that can handle this data type 2494 final InstallWidgetReceiver.WidgetListAdapter adapter = 2495 new InstallWidgetReceiver.WidgetListAdapter(mLauncher, mimeType, 2496 data, widgets, layout, mCurrentPage, pos); 2497 final AlertDialog.Builder builder = 2498 new AlertDialog.Builder(mContext); 2499 builder.setAdapter(adapter, adapter); 2500 builder.setCancelable(true); 2501 builder.setTitle(mContext.getString( 2502 R.string.external_drop_widget_pick_title)); 2503 builder.setIcon(R.drawable.ic_no_applications); 2504 builder.show(); 2505 } 2506 } 2507 } 2508 return true; 2509 } 2510 case DragEvent.ACTION_DRAG_ENDED: 2511 // Hide the page outlines after the drop 2512 layout.setIsDragOccuring(false); 2513 layout.onDragExit(); 2514 hideOutlines(); 2515 return true; 2516 } 2517 return super.onDragEvent(event); 2518 } 2519 2520 /* 2521 * 2522 * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's 2523 * coordinate space. The argument xy is modified with the return result. 2524 * 2525 */ 2526 void mapPointFromSelfToChild(View v, float[] xy) { 2527 mapPointFromSelfToChild(v, xy, null); 2528 } 2529 2530 /* 2531 * 2532 * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's 2533 * coordinate space. The argument xy is modified with the return result. 2534 * 2535 * if cachedInverseMatrix is not null, this method will just use that matrix instead of 2536 * computing it itself; we use this to avoid redundant matrix inversions in 2537 * findMatchingPageForDragOver 2538 * 2539 */ 2540 void mapPointFromSelfToChild(View v, float[] xy, Matrix cachedInverseMatrix) { 2541 if (cachedInverseMatrix == null) { 2542 v.getMatrix().invert(mTempInverseMatrix); 2543 cachedInverseMatrix = mTempInverseMatrix; 2544 } 2545 xy[0] = xy[0] + mScrollX - v.getLeft(); 2546 xy[1] = xy[1] + mScrollY - v.getTop(); 2547 cachedInverseMatrix.mapPoints(xy); 2548 } 2549 2550 /* 2551 * Maps a point from the Workspace's coordinate system to another sibling view's. (Workspace 2552 * covers the full screen) 2553 */ 2554 void mapPointFromSelfToSibling(View v, float[] xy) { 2555 xy[0] = xy[0] - v.getLeft(); 2556 xy[1] = xy[1] - v.getTop(); 2557 } 2558 2559 /* 2560 * 2561 * Convert the 2D coordinate xy from this CellLayout's coordinate space to 2562 * the parent View's coordinate space. The argument xy is modified with the return result. 2563 * 2564 */ 2565 void mapPointFromChildToSelf(View v, float[] xy) { 2566 v.getMatrix().mapPoints(xy); 2567 xy[0] -= (mScrollX - v.getLeft()); 2568 xy[1] -= (mScrollY - v.getTop()); 2569 } 2570 2571 static private float squaredDistance(float[] point1, float[] point2) { 2572 float distanceX = point1[0] - point2[0]; 2573 float distanceY = point2[1] - point2[1]; 2574 return distanceX * distanceX + distanceY * distanceY; 2575 } 2576 2577 /* 2578 * 2579 * Returns true if the passed CellLayout cl overlaps with dragView 2580 * 2581 */ 2582 boolean overlaps(CellLayout cl, DragView dragView, 2583 int dragViewX, int dragViewY, Matrix cachedInverseMatrix) { 2584 // Transform the coordinates of the item being dragged to the CellLayout's coordinates 2585 final float[] draggedItemTopLeft = mTempDragCoordinates; 2586 draggedItemTopLeft[0] = dragViewX; 2587 draggedItemTopLeft[1] = dragViewY; 2588 final float[] draggedItemBottomRight = mTempDragBottomRightCoordinates; 2589 draggedItemBottomRight[0] = draggedItemTopLeft[0] + dragView.getDragRegionWidth(); 2590 draggedItemBottomRight[1] = draggedItemTopLeft[1] + dragView.getDragRegionHeight(); 2591 2592 // Transform the dragged item's top left coordinates 2593 // to the CellLayout's local coordinates 2594 mapPointFromSelfToChild(cl, draggedItemTopLeft, cachedInverseMatrix); 2595 float overlapRegionLeft = Math.max(0f, draggedItemTopLeft[0]); 2596 float overlapRegionTop = Math.max(0f, draggedItemTopLeft[1]); 2597 2598 if (overlapRegionLeft <= cl.getWidth() && overlapRegionTop >= 0) { 2599 // Transform the dragged item's bottom right coordinates 2600 // to the CellLayout's local coordinates 2601 mapPointFromSelfToChild(cl, draggedItemBottomRight, cachedInverseMatrix); 2602 float overlapRegionRight = Math.min(cl.getWidth(), draggedItemBottomRight[0]); 2603 float overlapRegionBottom = Math.min(cl.getHeight(), draggedItemBottomRight[1]); 2604 2605 if (overlapRegionRight >= 0 && overlapRegionBottom <= cl.getHeight()) { 2606 float overlap = (overlapRegionRight - overlapRegionLeft) * 2607 (overlapRegionBottom - overlapRegionTop); 2608 if (overlap > 0) { 2609 return true; 2610 } 2611 } 2612 } 2613 return false; 2614 } 2615 2616 /* 2617 * 2618 * This method returns the CellLayout that is currently being dragged to. In order to drag 2619 * to a CellLayout, either the touch point must be directly over the CellLayout, or as a second 2620 * strategy, we see if the dragView is overlapping any CellLayout and choose the closest one 2621 * 2622 * Return null if no CellLayout is currently being dragged over 2623 * 2624 */ 2625 private CellLayout findMatchingPageForDragOver( 2626 DragView dragView, float originX, float originY, boolean exact) { 2627 // We loop through all the screens (ie CellLayouts) and see which ones overlap 2628 // with the item being dragged and then choose the one that's closest to the touch point 2629 final int screenCount = getChildCount(); 2630 CellLayout bestMatchingScreen = null; 2631 float smallestDistSoFar = Float.MAX_VALUE; 2632 2633 for (int i = 0; i < screenCount; i++) { 2634 CellLayout cl = (CellLayout) getChildAt(i); 2635 2636 final float[] touchXy = {originX, originY}; 2637 // Transform the touch coordinates to the CellLayout's local coordinates 2638 // If the touch point is within the bounds of the cell layout, we can return immediately 2639 cl.getMatrix().invert(mTempInverseMatrix); 2640 mapPointFromSelfToChild(cl, touchXy, mTempInverseMatrix); 2641 2642 if (touchXy[0] >= 0 && touchXy[0] <= cl.getWidth() && 2643 touchXy[1] >= 0 && touchXy[1] <= cl.getHeight()) { 2644 return cl; 2645 } 2646 2647 if (!exact && overlaps(cl, dragView, (int) originX, (int) originY, mTempInverseMatrix)) { 2648 // Get the center of the cell layout in screen coordinates 2649 final float[] cellLayoutCenter = mTempCellLayoutCenterCoordinates; 2650 cellLayoutCenter[0] = cl.getWidth()/2; 2651 cellLayoutCenter[1] = cl.getHeight()/2; 2652 mapPointFromChildToSelf(cl, cellLayoutCenter); 2653 2654 touchXy[0] = originX; 2655 touchXy[1] = originY; 2656 2657 // Calculate the distance between the center of the CellLayout 2658 // and the touch point 2659 float dist = squaredDistance(touchXy, cellLayoutCenter); 2660 2661 if (dist < smallestDistSoFar) { 2662 smallestDistSoFar = dist; 2663 bestMatchingScreen = cl; 2664 } 2665 } 2666 } 2667 return bestMatchingScreen; 2668 } 2669 2670 // This is used to compute the visual center of the dragView. This point is then 2671 // used to visualize drop locations and determine where to drop an item. The idea is that 2672 // the visual center represents the user's interpretation of where the item is, and hence 2673 // is the appropriate point to use when determining drop location. 2674 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset, 2675 DragView dragView, float[] recycle) { 2676 float res[]; 2677 if (recycle == null) { 2678 res = new float[2]; 2679 } else { 2680 res = recycle; 2681 } 2682 2683 // First off, the drag view has been shifted in a way that is not represented in the 2684 // x and y values or the x/yOffsets. Here we account for that shift. 2685 x += getResources().getDimensionPixelSize(R.dimen.dragViewOffsetX); 2686 y += getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY); 2687 2688 // These represent the visual top and left of drag view if a dragRect was provided. 2689 // If a dragRect was not provided, then they correspond to the actual view left and 2690 // top, as the dragRect is in that case taken to be the entire dragView. 2691 // R.dimen.dragViewOffsetY. 2692 int left = x - xOffset; 2693 int top = y - yOffset; 2694 2695 // In order to find the visual center, we shift by half the dragRect 2696 res[0] = left + dragView.getDragRegion().width() / 2; 2697 res[1] = top + dragView.getDragRegion().height() / 2; 2698 2699 return res; 2700 } 2701 2702 public void onDragOver(DragObject d) { 2703 // Skip drag over events while we are dragging over side pages 2704 if (mInScrollArea) return; 2705 if (mIsSwitchingState) return; 2706 2707 CellLayout layout = null; 2708 ItemInfo item = (ItemInfo) d.dragInfo; 2709 2710 // Ensure that we have proper spans for the item that we are dropping 2711 if (item.spanX < 0 || item.spanY < 0) throw new RuntimeException("Improper spans found"); 2712 2713 mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, 2714 d.dragView, mDragViewVisualCenter); 2715 2716 // Identify whether we have dragged over a side page 2717 if (isSmall()) { 2718 int left = d.x - d.xOffset; 2719 int top = d.y - d.yOffset; 2720 layout = findMatchingPageForDragOver(d.dragView, mDragViewVisualCenter[0], 2721 mDragViewVisualCenter[1], true); 2722 if (layout != mDragTargetLayout) { 2723 // Cancel all intermediate folder states 2724 cleanupFolderCreation(d); 2725 2726 if (mDragTargetLayout != null) { 2727 mDragTargetLayout.setIsDragOverlapping(false); 2728 mDragTargetLayout.onDragExit(); 2729 } 2730 mDragTargetLayout = layout; 2731 if (mDragTargetLayout != null) { 2732 mDragTargetLayout.setIsDragOverlapping(true); 2733 mDragTargetLayout.onDragEnter(); 2734 } else { 2735 mLastDragOverView = null; 2736 } 2737 2738 boolean isInSpringLoadedMode = (mState == State.SPRING_LOADED); 2739 if (isInSpringLoadedMode) { 2740 mSpringLoadedDragController.setAlarm(mDragTargetLayout); 2741 } 2742 } 2743 } else { 2744 // Test to see if we are over the hotseat otherwise just use the current page 2745 Rect r = new Rect(); 2746 if (mLauncher.getHotseat() != null) { 2747 mLauncher.getHotseat().getHitRect(r); 2748 if (r.contains(d.x, d.y)) { 2749 layout = mLauncher.getHotseat().getLayout(); 2750 } 2751 } 2752 if (layout == null) { 2753 layout = getCurrentDropLayout(); 2754 } 2755 if (layout != mDragTargetLayout) { 2756 if (mDragTargetLayout != null) { 2757 mDragTargetLayout.setIsDragOverlapping(false); 2758 mDragTargetLayout.onDragExit(); 2759 } 2760 mDragTargetLayout = layout; 2761 mDragTargetLayout.setIsDragOverlapping(true); 2762 mDragTargetLayout.onDragEnter(); 2763 } 2764 } 2765 2766 // Handle the drag over 2767 if (mDragTargetLayout != null) { 2768 final View child = (mDragInfo == null) ? null : mDragInfo.cell; 2769 2770 // We want the point to be mapped to the dragTarget. 2771 if (mLauncher.isHotseatLayout(mDragTargetLayout)) { 2772 mapPointFromSelfToSibling(mLauncher.getHotseat(), mDragViewVisualCenter); 2773 } else { 2774 mapPointFromSelfToChild(mDragTargetLayout, mDragViewVisualCenter, null); 2775 } 2776 ItemInfo info = (ItemInfo) d.dragInfo; 2777 2778 mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], 2779 (int) mDragViewVisualCenter[1], 1, 1, mDragTargetLayout, mTargetCell); 2780 final View dragOverView = mDragTargetLayout.getChildAt(mTargetCell[0], 2781 mTargetCell[1]); 2782 2783 boolean userFolderPending = willCreateUserFolder(info, mDragTargetLayout, 2784 mTargetCell, false); 2785 boolean isOverFolder = dragOverView instanceof FolderIcon; 2786 if (dragOverView != mLastDragOverView) { 2787 cancelFolderCreation(); 2788 if (mLastDragOverView != null && mLastDragOverView instanceof FolderIcon) { 2789 ((FolderIcon) mLastDragOverView).onDragExit(d.dragInfo); 2790 } 2791 } 2792 2793 if (userFolderPending && dragOverView != mLastDragOverView) { 2794 mFolderCreationAlarm.setOnAlarmListener(new 2795 FolderCreationAlarmListener(mDragTargetLayout, mTargetCell[0], mTargetCell[1])); 2796 mFolderCreationAlarm.setAlarm(FOLDER_CREATION_TIMEOUT); 2797 } 2798 2799 if (dragOverView != mLastDragOverView && isOverFolder) { 2800 ((FolderIcon) dragOverView).onDragEnter(d.dragInfo); 2801 if (mDragTargetLayout != null) { 2802 mDragTargetLayout.clearDragOutlines(); 2803 } 2804 } 2805 mLastDragOverView = dragOverView; 2806 2807 if (!mCreateUserFolderOnDrop && !isOverFolder) { 2808 mDragTargetLayout.visualizeDropLocation(child, mDragOutline, 2809 (int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], 2810 item.spanX, item.spanY); 2811 } 2812 } 2813 } 2814 2815 private void cleanupFolderCreation(DragObject d) { 2816 if (mDragFolderRingAnimator != null && mCreateUserFolderOnDrop) { 2817 mDragFolderRingAnimator.animateToNaturalState(); 2818 } 2819 if (mLastDragOverView != null && mLastDragOverView instanceof FolderIcon) { 2820 if (d != null) { 2821 ((FolderIcon) mLastDragOverView).onDragExit(d.dragInfo); 2822 } 2823 } 2824 mFolderCreationAlarm.cancelAlarm(); 2825 } 2826 2827 private void cancelFolderCreation() { 2828 if (mDragFolderRingAnimator != null && mCreateUserFolderOnDrop) { 2829 mDragFolderRingAnimator.animateToNaturalState(); 2830 } 2831 mCreateUserFolderOnDrop = false; 2832 mFolderCreationAlarm.cancelAlarm(); 2833 } 2834 2835 class FolderCreationAlarmListener implements OnAlarmListener { 2836 CellLayout layout; 2837 int cellX; 2838 int cellY; 2839 2840 public FolderCreationAlarmListener(CellLayout layout, int cellX, int cellY) { 2841 this.layout = layout; 2842 this.cellX = cellX; 2843 this.cellY = cellY; 2844 } 2845 2846 public void onAlarm(Alarm alarm) { 2847 if (mDragFolderRingAnimator == null) { 2848 mDragFolderRingAnimator = new FolderRingAnimator(mLauncher, null); 2849 } 2850 mDragFolderRingAnimator.setCell(cellX, cellY); 2851 mDragFolderRingAnimator.setCellLayout(layout); 2852 mDragFolderRingAnimator.animateToAcceptState(); 2853 layout.showFolderAccept(mDragFolderRingAnimator); 2854 layout.clearDragOutlines(); 2855 mCreateUserFolderOnDrop = true; 2856 } 2857 } 2858 2859 @Override 2860 public void getHitRect(Rect outRect) { 2861 // We want the workspace to have the whole area of the display (it will find the correct 2862 // cell layout to drop to in the existing drag/drop logic. 2863 final Display d = mLauncher.getWindowManager().getDefaultDisplay(); 2864 outRect.set(0, 0, d.getWidth(), d.getHeight()); 2865 } 2866 2867 /** 2868 * Add the item specified by dragInfo to the given layout. 2869 * @return true if successful 2870 */ 2871 public boolean addExternalItemToScreen(ItemInfo dragInfo, CellLayout layout) { 2872 if (layout.findCellForSpan(mTempEstimate, dragInfo.spanX, dragInfo.spanY)) { 2873 onDropExternal(dragInfo.dropPos, (ItemInfo) dragInfo, (CellLayout) layout, false); 2874 return true; 2875 } 2876 mLauncher.showOutOfSpaceMessage(); 2877 return false; 2878 } 2879 2880 private void onDropExternal(int[] touchXY, Object dragInfo, 2881 CellLayout cellLayout, boolean insertAtFirst) { 2882 onDropExternal(touchXY, dragInfo, cellLayout, insertAtFirst, null); 2883 } 2884 2885 /** 2886 * Drop an item that didn't originate on one of the workspace screens. 2887 * It may have come from Launcher (e.g. from all apps or customize), or it may have 2888 * come from another app altogether. 2889 * 2890 * NOTE: This can also be called when we are outside of a drag event, when we want 2891 * to add an item to one of the workspace screens. 2892 */ 2893 private void onDropExternal(final int[] touchXY, final Object dragInfo, 2894 final CellLayout cellLayout, boolean insertAtFirst, DragObject d) { 2895 final Runnable exitSpringLoadedRunnable = new Runnable() { 2896 @Override 2897 public void run() { 2898 mLauncher.exitSpringLoadedDragModeDelayed(false); 2899 } 2900 }; 2901 2902 ItemInfo info = (ItemInfo) dragInfo; 2903 int spanX = info.spanX; 2904 int spanY = info.spanY; 2905 if (mDragInfo != null) { 2906 spanX = mDragInfo.spanX; 2907 spanY = mDragInfo.spanY; 2908 } 2909 2910 final long container = mLauncher.isHotseatLayout(cellLayout) ? 2911 LauncherSettings.Favorites.CONTAINER_HOTSEAT : 2912 LauncherSettings.Favorites.CONTAINER_DESKTOP; 2913 final int screen = indexOfChild(cellLayout); 2914 if (!mLauncher.isHotseatLayout(cellLayout) && screen != mCurrentPage 2915 && mState != State.SPRING_LOADED) { 2916 snapToPage(screen); 2917 } 2918 2919 if (info instanceof PendingAddItemInfo) { 2920 final PendingAddItemInfo pendingInfo = (PendingAddItemInfo) dragInfo; 2921 2922 mTargetCell = findNearestVacantArea(touchXY[0], touchXY[1], spanX, spanY, null, 2923 cellLayout, mTargetCell); 2924 Runnable onAnimationCompleteRunnable = new Runnable() { 2925 @Override 2926 public void run() { 2927 // When dragging and dropping from customization tray, we deal with creating 2928 // widgets/shortcuts/folders in a slightly different way 2929 switch (pendingInfo.itemType) { 2930 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: 2931 mLauncher.addAppWidgetFromDrop((PendingAddWidgetInfo) pendingInfo, 2932 container, screen, mTargetCell, null); 2933 break; 2934 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: 2935 mLauncher.processShortcutFromDrop(pendingInfo.componentName, 2936 container, screen, mTargetCell, null); 2937 break; 2938 default: 2939 throw new IllegalStateException("Unknown item type: " + 2940 pendingInfo.itemType); 2941 } 2942 cellLayout.onDragExit(); 2943 } 2944 }; 2945 2946 // Now we animate the dragView, (ie. the widget or shortcut preview) into its final 2947 // location and size on the home screen. 2948 int loc[] = new int[2]; 2949 cellLayout.cellToPoint(mTargetCell[0], mTargetCell[1], loc); 2950 2951 RectF r = new RectF(); 2952 cellLayout.cellToRect(mTargetCell[0], mTargetCell[1], spanX, spanY, r); 2953 float cellLayoutScale = 2954 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(cellLayout, loc); 2955 2956 float dragViewScale = r.width() / d.dragView.getMeasuredWidth(); 2957 // The animation will scale the dragView about its center, so we need to center about 2958 // the final location. 2959 loc[0] -= (d.dragView.getMeasuredWidth() - cellLayoutScale * r.width()) / 2; 2960 loc[1] -= (d.dragView.getMeasuredHeight() - cellLayoutScale * r.height()) / 2; 2961 2962 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, loc, 2963 dragViewScale * cellLayoutScale, onAnimationCompleteRunnable); 2964 } else { 2965 // This is for other drag/drop cases, like dragging from All Apps 2966 View view = null; 2967 2968 switch (info.itemType) { 2969 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: 2970 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: 2971 if (info.container == NO_ID && info instanceof ApplicationInfo) { 2972 // Came from all apps -- make a copy 2973 info = new ShortcutInfo((ApplicationInfo) info); 2974 } 2975 view = mLauncher.createShortcut(R.layout.application, cellLayout, 2976 (ShortcutInfo) info); 2977 break; 2978 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: 2979 view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher, cellLayout, 2980 (FolderInfo) info, mIconCache); 2981 break; 2982 default: 2983 throw new IllegalStateException("Unknown item type: " + info.itemType); 2984 } 2985 2986 // First we find the cell nearest to point at which the item is 2987 // dropped, without any consideration to whether there is an item there. 2988 if (touchXY != null) { 2989 mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY, 2990 cellLayout, mTargetCell); 2991 d.postAnimationRunnable = exitSpringLoadedRunnable; 2992 if (createUserFolderIfNecessary(view, container, cellLayout, mTargetCell, true, 2993 d.dragView, d.postAnimationRunnable)) { 2994 return; 2995 } 2996 if (addToExistingFolderIfNecessary(view, cellLayout, mTargetCell, d, true)) { 2997 return; 2998 } 2999 } 3000 3001 if (touchXY != null) { 3002 // when dragging and dropping, just find the closest free spot 3003 mTargetCell = findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, null, 3004 cellLayout, mTargetCell); 3005 } else { 3006 cellLayout.findCellForSpan(mTargetCell, 1, 1); 3007 } 3008 addInScreen(view, container, screen, mTargetCell[0], mTargetCell[1], info.spanX, 3009 info.spanY, insertAtFirst); 3010 cellLayout.onDropChild(view); 3011 cellLayout.animateDrop(); 3012 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams(); 3013 cellLayout.getChildrenLayout().measureChild(view); 3014 3015 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screen, 3016 lp.cellX, lp.cellY); 3017 3018 if (d.dragView != null) { 3019 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, view, 3020 exitSpringLoadedRunnable); 3021 } 3022 } 3023 } 3024 3025 /** 3026 * Return the current {@link CellLayout}, correctly picking the destination 3027 * screen while a scroll is in progress. 3028 */ 3029 public CellLayout getCurrentDropLayout() { 3030 return (CellLayout) getChildAt(mNextPage == INVALID_PAGE ? mCurrentPage : mNextPage); 3031 } 3032 3033 /** 3034 * Return the current CellInfo describing our current drag; this method exists 3035 * so that Launcher can sync this object with the correct info when the activity is created/ 3036 * destroyed 3037 * 3038 */ 3039 public CellLayout.CellInfo getDragInfo() { 3040 return mDragInfo; 3041 } 3042 3043 /** 3044 * Calculate the nearest cell where the given object would be dropped. 3045 * 3046 * pixelX and pixelY should be in the coordinate system of layout 3047 */ 3048 private int[] findNearestVacantArea(int pixelX, int pixelY, 3049 int spanX, int spanY, View ignoreView, CellLayout layout, int[] recycle) { 3050 return layout.findNearestVacantArea( 3051 pixelX, pixelY, spanX, spanY, ignoreView, recycle); 3052 } 3053 3054 /** 3055 * Calculate the nearest cell where the given object would be dropped. 3056 * 3057 * pixelX and pixelY should be in the coordinate system of layout 3058 */ 3059 private int[] findNearestArea(int pixelX, int pixelY, 3060 int spanX, int spanY, CellLayout layout, int[] recycle) { 3061 return layout.findNearestArea( 3062 pixelX, pixelY, spanX, spanY, recycle); 3063 } 3064 3065 void setup(Launcher launcher, DragController dragController) { 3066 mLauncher = launcher; 3067 mSpringLoadedDragController = new SpringLoadedDragController(mLauncher); 3068 mDragController = dragController; 3069 3070 3071 // hardware layers on children are enabled on startup, but should be disabled until 3072 // needed 3073 updateChildrenLayersEnabled(); 3074 setWallpaperDimension(); 3075 } 3076 3077 /** 3078 * Called at the end of a drag which originated on the workspace. 3079 */ 3080 public void onDropCompleted(View target, DragObject d, boolean success) { 3081 if (success) { 3082 if (target != this) { 3083 if (mDragInfo != null) { 3084 getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell); 3085 if (mDragInfo.cell instanceof DropTarget) { 3086 mDragController.removeDropTarget((DropTarget) mDragInfo.cell); 3087 } 3088 } 3089 } 3090 } else if (mDragInfo != null) { 3091 // NOTE: When 'success' is true, onDragExit is called by the DragController before 3092 // calling onDropCompleted(). We call it ourselves here, but maybe this should be 3093 // moved into DragController.cancelDrag(). 3094 doDragExit(null); 3095 CellLayout cellLayout; 3096 if (mLauncher.isHotseatLayout(target)) { 3097 cellLayout = mLauncher.getHotseat().getLayout(); 3098 } else { 3099 cellLayout = (CellLayout) getChildAt(mDragInfo.screen); 3100 } 3101 cellLayout.onDropChild(mDragInfo.cell); 3102 } 3103 mDragOutline = null; 3104 mDragInfo = null; 3105 } 3106 3107 public boolean isDropEnabled() { 3108 return true; 3109 } 3110 3111 @Override 3112 protected void onRestoreInstanceState(Parcelable state) { 3113 super.onRestoreInstanceState(state); 3114 Launcher.setScreen(mCurrentPage); 3115 } 3116 3117 @Override 3118 public void scrollLeft() { 3119 if (!isSmall() && !mIsSwitchingState) { 3120 super.scrollLeft(); 3121 } 3122 Folder openFolder = getOpenFolder(); 3123 if (openFolder != null) { 3124 openFolder.completeDragExit(); 3125 } 3126 } 3127 3128 @Override 3129 public void scrollRight() { 3130 if (!isSmall() && !mIsSwitchingState) { 3131 super.scrollRight(); 3132 } 3133 Folder openFolder = getOpenFolder(); 3134 if (openFolder != null) { 3135 openFolder.completeDragExit(); 3136 } 3137 } 3138 3139 @Override 3140 public void onEnterScrollArea(int direction) { 3141 if (!isSmall() && !mIsSwitchingState) { 3142 mInScrollArea = true; 3143 3144 final int page = mCurrentPage + (direction == DragController.SCROLL_LEFT ? -1 : 1); 3145 final CellLayout layout = (CellLayout) getChildAt(page); 3146 cancelFolderCreation(); 3147 3148 if (layout != null) { 3149 // Exit the current layout and mark the overlapping layout 3150 if (mDragTargetLayout != null) { 3151 mDragTargetLayout.setIsDragOverlapping(false); 3152 mDragTargetLayout.onDragExit(); 3153 } 3154 mDragTargetLayout = layout; 3155 mDragTargetLayout.setIsDragOverlapping(true); 3156 3157 // Workspace is responsible for drawing the edge glow on adjacent pages, 3158 // so we need to redraw the workspace when this may have changed. 3159 invalidate(); 3160 } 3161 } 3162 } 3163 3164 @Override 3165 public void onExitScrollArea() { 3166 if (mInScrollArea) { 3167 if (mDragTargetLayout != null) { 3168 // Unmark the overlapping layout and re-enter the current layout 3169 mDragTargetLayout.setIsDragOverlapping(false); 3170 mDragTargetLayout = getCurrentDropLayout(); 3171 mDragTargetLayout.onDragEnter(); 3172 3173 // Workspace is responsible for drawing the edge glow on adjacent pages, 3174 // so we need to redraw the workspace when this may have changed. 3175 invalidate(); 3176 } 3177 mInScrollArea = false; 3178 } 3179 } 3180 3181 private void onResetScrollArea() { 3182 if (mDragTargetLayout != null) { 3183 // Unmark the overlapping layout 3184 mDragTargetLayout.setIsDragOverlapping(false); 3185 3186 // Workspace is responsible for drawing the edge glow on adjacent pages, 3187 // so we need to redraw the workspace when this may have changed. 3188 invalidate(); 3189 } 3190 mInScrollArea = false; 3191 } 3192 3193 /** 3194 * Returns a specific CellLayout 3195 */ 3196 CellLayout getParentCellLayoutForView(View v) { 3197 ArrayList<CellLayout> layouts = getWorkspaceAndHotseatCellLayouts(); 3198 for (CellLayout layout : layouts) { 3199 if (layout.getChildrenLayout().indexOfChild(v) > -1) { 3200 return layout; 3201 } 3202 } 3203 return null; 3204 } 3205 3206 /** 3207 * Returns a list of all the CellLayouts in the workspace. 3208 */ 3209 ArrayList<CellLayout> getWorkspaceAndHotseatCellLayouts() { 3210 ArrayList<CellLayout> layouts = new ArrayList<CellLayout>(); 3211 int screenCount = getChildCount(); 3212 for (int screen = 0; screen < screenCount; screen++) { 3213 layouts.add(((CellLayout) getChildAt(screen))); 3214 } 3215 if (mLauncher.getHotseat() != null) { 3216 layouts.add(mLauncher.getHotseat().getLayout()); 3217 } 3218 return layouts; 3219 } 3220 3221 /** 3222 * We should only use this to search for specific children. Do not use this method to modify 3223 * CellLayoutChildren directly. 3224 */ 3225 ArrayList<CellLayoutChildren> getWorkspaceAndHotseatCellLayoutChildren() { 3226 ArrayList<CellLayoutChildren> childrenLayouts = new ArrayList<CellLayoutChildren>(); 3227 int screenCount = getChildCount(); 3228 for (int screen = 0; screen < screenCount; screen++) { 3229 childrenLayouts.add(((CellLayout) getChildAt(screen)).getChildrenLayout()); 3230 } 3231 if (mLauncher.getHotseat() != null) { 3232 childrenLayouts.add(mLauncher.getHotseat().getLayout().getChildrenLayout()); 3233 } 3234 return childrenLayouts; 3235 } 3236 3237 public Folder getFolderForTag(Object tag) { 3238 ArrayList<CellLayoutChildren> childrenLayouts = getWorkspaceAndHotseatCellLayoutChildren(); 3239 for (CellLayoutChildren layout: childrenLayouts) { 3240 int count = layout.getChildCount(); 3241 for (int i = 0; i < count; i++) { 3242 View child = layout.getChildAt(i); 3243 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); 3244 if (child instanceof Folder) { 3245 Folder f = (Folder) child; 3246 if (f.getInfo() == tag && f.getInfo().opened) { 3247 return f; 3248 } 3249 } 3250 } 3251 } 3252 return null; 3253 } 3254 3255 public View getViewForTag(Object tag) { 3256 ArrayList<CellLayoutChildren> childrenLayouts = getWorkspaceAndHotseatCellLayoutChildren(); 3257 for (CellLayoutChildren layout: childrenLayouts) { 3258 int count = layout.getChildCount(); 3259 for (int i = 0; i < count; i++) { 3260 View child = layout.getChildAt(i); 3261 if (child.getTag() == tag) { 3262 return child; 3263 } 3264 } 3265 } 3266 return null; 3267 } 3268 3269 void clearDropTargets() { 3270 ArrayList<CellLayoutChildren> childrenLayouts = getWorkspaceAndHotseatCellLayoutChildren(); 3271 for (CellLayoutChildren layout: childrenLayouts) { 3272 int childCount = layout.getChildCount(); 3273 for (int j = 0; j < childCount; j++) { 3274 View v = layout.getChildAt(j); 3275 if (v instanceof DropTarget) { 3276 mDragController.removeDropTarget((DropTarget) v); 3277 } 3278 } 3279 } 3280 } 3281 3282 void removeItems(final ArrayList<ApplicationInfo> apps) { 3283 final int screenCount = getChildCount(); 3284 final AppWidgetManager widgets = AppWidgetManager.getInstance(getContext()); 3285 3286 final HashSet<String> packageNames = new HashSet<String>(); 3287 final int appCount = apps.size(); 3288 for (int i = 0; i < appCount; i++) { 3289 packageNames.add(apps.get(i).componentName.getPackageName()); 3290 } 3291 3292 ArrayList<CellLayout> cellLayouts = getWorkspaceAndHotseatCellLayouts(); 3293 for (final CellLayout layoutParent: cellLayouts) { 3294 final ViewGroup layout = layoutParent.getChildrenLayout(); 3295 3296 // Avoid ANRs by treating each screen separately 3297 post(new Runnable() { 3298 public void run() { 3299 final ArrayList<View> childrenToRemove = new ArrayList<View>(); 3300 childrenToRemove.clear(); 3301 3302 int childCount = layout.getChildCount(); 3303 for (int j = 0; j < childCount; j++) { 3304 final View view = layout.getChildAt(j); 3305 Object tag = view.getTag(); 3306 3307 if (tag instanceof ShortcutInfo) { 3308 final ShortcutInfo info = (ShortcutInfo) tag; 3309 final Intent intent = info.intent; 3310 final ComponentName name = intent.getComponent(); 3311 3312 if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) { 3313 for (String packageName: packageNames) { 3314 if (packageName.equals(name.getPackageName())) { 3315 LauncherModel.deleteItemFromDatabase(mLauncher, info); 3316 childrenToRemove.add(view); 3317 } 3318 } 3319 } 3320 } else if (tag instanceof FolderInfo) { 3321 final FolderInfo info = (FolderInfo) tag; 3322 final ArrayList<ShortcutInfo> contents = info.contents; 3323 final int contentsCount = contents.size(); 3324 final ArrayList<ShortcutInfo> appsToRemoveFromFolder = 3325 new ArrayList<ShortcutInfo>(); 3326 3327 for (int k = 0; k < contentsCount; k++) { 3328 final ShortcutInfo appInfo = contents.get(k); 3329 final Intent intent = appInfo.intent; 3330 final ComponentName name = intent.getComponent(); 3331 3332 if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) { 3333 for (String packageName: packageNames) { 3334 if (packageName.equals(name.getPackageName())) { 3335 appsToRemoveFromFolder.add(appInfo); 3336 } 3337 } 3338 } 3339 } 3340 for (ShortcutInfo item: appsToRemoveFromFolder) { 3341 info.remove(item); 3342 LauncherModel.deleteItemFromDatabase(mLauncher, item); 3343 } 3344 } else if (tag instanceof LauncherAppWidgetInfo) { 3345 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) tag; 3346 final AppWidgetProviderInfo provider = 3347 widgets.getAppWidgetInfo(info.appWidgetId); 3348 if (provider != null) { 3349 for (String packageName: packageNames) { 3350 if (packageName.equals(provider.provider.getPackageName())) { 3351 LauncherModel.deleteItemFromDatabase(mLauncher, info); 3352 childrenToRemove.add(view); 3353 } 3354 } 3355 } 3356 } 3357 } 3358 3359 childCount = childrenToRemove.size(); 3360 for (int j = 0; j < childCount; j++) { 3361 View child = childrenToRemove.get(j); 3362 // Note: We can not remove the view directly from CellLayoutChildren as this 3363 // does not re-mark the spaces as unoccupied. 3364 layoutParent.removeViewInLayout(child); 3365 if (child instanceof DropTarget) { 3366 mDragController.removeDropTarget((DropTarget)child); 3367 } 3368 } 3369 3370 if (childCount > 0) { 3371 layout.requestLayout(); 3372 layout.invalidate(); 3373 } 3374 } 3375 }); 3376 } 3377 } 3378 3379 void updateShortcuts(ArrayList<ApplicationInfo> apps) { 3380 ArrayList<CellLayoutChildren> childrenLayouts = getWorkspaceAndHotseatCellLayoutChildren(); 3381 for (CellLayoutChildren layout: childrenLayouts) { 3382 int childCount = layout.getChildCount(); 3383 for (int j = 0; j < childCount; j++) { 3384 final View view = layout.getChildAt(j); 3385 Object tag = view.getTag(); 3386 if (tag instanceof ShortcutInfo) { 3387 ShortcutInfo info = (ShortcutInfo)tag; 3388 // We need to check for ACTION_MAIN otherwise getComponent() might 3389 // return null for some shortcuts (for instance, for shortcuts to 3390 // web pages.) 3391 final Intent intent = info.intent; 3392 final ComponentName name = intent.getComponent(); 3393 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION && 3394 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) { 3395 final int appCount = apps.size(); 3396 for (int k = 0; k < appCount; k++) { 3397 ApplicationInfo app = apps.get(k); 3398 if (app.componentName.equals(name)) { 3399 info.setIcon(mIconCache.getIcon(info.intent)); 3400 ((TextView)view).setCompoundDrawablesWithIntrinsicBounds(null, 3401 new FastBitmapDrawable(info.getIcon(mIconCache)), 3402 null, null); 3403 } 3404 } 3405 } 3406 } 3407 } 3408 } 3409 } 3410 3411 void moveToDefaultScreen(boolean animate) { 3412 if (isSmall() || mIsSwitchingState) { 3413 mLauncher.showWorkspace(animate, (CellLayout) getChildAt(mDefaultPage)); 3414 } else if (animate) { 3415 snapToPage(mDefaultPage); 3416 } else { 3417 setCurrentPage(mDefaultPage); 3418 } 3419 getChildAt(mDefaultPage).requestFocus(); 3420 } 3421 3422 @Override 3423 public void syncPages() { 3424 } 3425 3426 @Override 3427 public void syncPageItems(int page) { 3428 } 3429 3430 @Override 3431 protected String getCurrentPageDescription() { 3432 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; 3433 return String.format(mContext.getString(R.string.workspace_scroll_format), 3434 page + 1, getChildCount()); 3435 } 3436 3437 public void getLocationInDragLayer(int[] loc) { 3438 mLauncher.getDragLayer().getLocationInDragLayer(this, loc); 3439 } 3440 3441 /** 3442 * Return true because we want the scrolling indicator to stretch to fit the space. 3443 */ 3444 protected boolean hasElasticScrollIndicator() { 3445 return true; 3446 } 3447} 3448