Workspace.java revision a30b51ceb29e179caf7f441763ffeb6bfd336998
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.launcher3; 18 19import android.animation.Animator; 20import android.animation.AnimatorListenerAdapter; 21import android.animation.AnimatorSet; 22import android.animation.LayoutTransition; 23import android.animation.ObjectAnimator; 24import android.animation.PropertyValuesHolder; 25import android.animation.ValueAnimator; 26import android.animation.ValueAnimator.AnimatorUpdateListener; 27import android.annotation.SuppressLint; 28import android.annotation.TargetApi; 29import android.app.WallpaperManager; 30import android.appwidget.AppWidgetHostView; 31import android.appwidget.AppWidgetProviderInfo; 32import android.content.ComponentName; 33import android.content.Context; 34import android.content.res.Resources; 35import android.graphics.Bitmap; 36import android.graphics.Canvas; 37import android.graphics.Matrix; 38import android.graphics.Paint; 39import android.graphics.Point; 40import android.graphics.PointF; 41import android.graphics.Rect; 42import android.graphics.Region.Op; 43import android.graphics.drawable.Drawable; 44import android.os.Build; 45import android.os.Handler; 46import android.os.IBinder; 47import android.os.Parcelable; 48import android.util.AttributeSet; 49import android.util.Log; 50import android.util.Property; 51import android.util.SparseArray; 52import android.view.MotionEvent; 53import android.view.View; 54import android.view.ViewDebug; 55import android.view.ViewGroup; 56import android.view.ViewGroup.LayoutParams; 57import android.view.accessibility.AccessibilityManager; 58import android.view.animation.DecelerateInterpolator; 59import android.view.animation.Interpolator; 60import android.widget.Space; 61import android.widget.TextView; 62 63import com.android.launcher3.Launcher.CustomContentCallbacks; 64import com.android.launcher3.Launcher.LauncherOverlay; 65import com.android.launcher3.UninstallDropTarget.DropTargetSource; 66import com.android.launcher3.accessibility.LauncherAccessibilityDelegate.AccessibilityDragSource; 67import com.android.launcher3.accessibility.OverviewAccessibilityDelegate; 68import com.android.launcher3.accessibility.OverviewScreenAccessibilityDelegate; 69import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper; 70import com.android.launcher3.compat.AppWidgetManagerCompat; 71import com.android.launcher3.compat.UserHandleCompat; 72import com.android.launcher3.config.FeatureFlags; 73import com.android.launcher3.config.ProviderConfig; 74import com.android.launcher3.dragndrop.DragController; 75import com.android.launcher3.dragndrop.DragLayer; 76import com.android.launcher3.dragndrop.DragScroller; 77import com.android.launcher3.dragndrop.DragView; 78import com.android.launcher3.dragndrop.SpringLoadedDragController; 79import com.android.launcher3.folder.Folder; 80import com.android.launcher3.folder.FolderIcon; 81import com.android.launcher3.logging.UserEventDispatcher; 82import com.android.launcher3.shortcuts.DeepShortcutManager; 83import com.android.launcher3.shortcuts.ShortcutsContainerListener; 84import com.android.launcher3.userevent.nano.LauncherLogProto; 85import com.android.launcher3.userevent.nano.LauncherLogProto.Target; 86import com.android.launcher3.util.LongArrayMap; 87import com.android.launcher3.util.MultiStateAlphaController; 88import com.android.launcher3.util.Thunk; 89import com.android.launcher3.util.WallpaperOffsetInterpolator; 90import com.android.launcher3.widget.PendingAddShortcutInfo; 91import com.android.launcher3.widget.PendingAddWidgetInfo; 92 93import java.util.ArrayList; 94import java.util.HashMap; 95import java.util.HashSet; 96import java.util.concurrent.atomic.AtomicInteger; 97 98/** 99 * The workspace is a wide area with a wallpaper and a finite number of pages. 100 * Each page contains a number of icons, folders or widgets the user can 101 * interact with. A workspace is meant to be used with a fixed width only. 102 */ 103public class Workspace extends PagedView 104 implements DropTarget, DragSource, DragScroller, View.OnTouchListener, 105 DragController.DragListener, LauncherTransitionable, ViewGroup.OnHierarchyChangeListener, 106 Insettable, DropTargetSource, AccessibilityDragSource, UserEventDispatcher.LaunchSourceProvider { 107 private static final String TAG = "Launcher.Workspace"; 108 109 private static boolean ENFORCE_DRAG_EVENT_ORDER = false; 110 111 private static final int SNAP_OFF_EMPTY_SCREEN_DURATION = 400; 112 private static final int FADE_EMPTY_SCREEN_DURATION = 150; 113 114 private static final int ADJACENT_SCREEN_DROP_DURATION = 300; 115 116 private static final boolean MAP_NO_RECURSE = false; 117 private static final boolean MAP_RECURSE = true; 118 119 // The screen id used for the empty screen always present to the right. 120 public static final long EXTRA_EMPTY_SCREEN_ID = -201; 121 // The is the first screen. It is always present, even if its empty. 122 public static final long FIRST_SCREEN_ID = 0; 123 124 private final static long CUSTOM_CONTENT_SCREEN_ID = -301; 125 126 private static final long CUSTOM_CONTENT_GESTURE_DELAY = 200; 127 private long mTouchDownTime = -1; 128 private long mCustomContentShowTime = -1; 129 130 private LayoutTransition mLayoutTransition; 131 @Thunk final WallpaperManager mWallpaperManager; 132 133 private ShortcutAndWidgetContainer mDragSourceInternal; 134 135 @Thunk LongArrayMap<CellLayout> mWorkspaceScreens = new LongArrayMap<>(); 136 @Thunk ArrayList<Long> mScreenOrder = new ArrayList<Long>(); 137 138 @Thunk Runnable mRemoveEmptyScreenRunnable; 139 @Thunk boolean mDeferRemoveExtraEmptyScreen = false; 140 @Thunk boolean mAddNewPageOnDrag = true; 141 142 /** 143 * CellInfo for the cell that is currently being dragged 144 */ 145 private CellLayout.CellInfo mDragInfo; 146 147 /** 148 * Target drop area calculated during last acceptDrop call. 149 */ 150 @Thunk int[] mTargetCell = new int[2]; 151 private int mDragOverX = -1; 152 private int mDragOverY = -1; 153 154 CustomContentCallbacks mCustomContentCallbacks; 155 boolean mCustomContentShowing; 156 private float mLastCustomContentScrollProgress = -1f; 157 private String mCustomContentDescription = ""; 158 159 /** 160 * The CellLayout that is currently being dragged over 161 */ 162 @Thunk CellLayout mDragTargetLayout = null; 163 /** 164 * The CellLayout that we will show as highlighted 165 */ 166 private CellLayout mDragOverlappingLayout = null; 167 168 /** 169 * The CellLayout which will be dropped to 170 */ 171 private CellLayout mDropToLayout = null; 172 173 @Thunk Launcher mLauncher; 174 @Thunk IconCache mIconCache; 175 @Thunk DragController mDragController; 176 177 // These are temporary variables to prevent having to allocate a new object just to 178 // return an (x, y) value from helper functions. Do NOT use them to maintain other state. 179 private static final Rect sTempRect = new Rect(); 180 private final int[] mTempXY = new int[2]; 181 @Thunk float[] mDragViewVisualCenter = new float[2]; 182 private float[] mTempCellLayoutCenterCoordinates = new float[2]; 183 private int[] mTempVisiblePagesRange = new int[2]; 184 private Matrix mTempMatrix = new Matrix(); 185 186 private SpringLoadedDragController mSpringLoadedDragController; 187 private float mOverviewModeShrinkFactor; 188 189 // State variable that indicates whether the pages are small (ie when you're 190 // in all apps or customize mode) 191 192 public enum State { 193 NORMAL (false, false), 194 NORMAL_HIDDEN (false, false), 195 SPRING_LOADED (false, true), 196 OVERVIEW (true, true), 197 OVERVIEW_HIDDEN (true, false); 198 199 public final boolean shouldUpdateWidget; 200 public final boolean hasMultipleVisiblePages; 201 202 State(boolean shouldUpdateWidget, boolean hasMultipleVisiblePages) { 203 this.shouldUpdateWidget = shouldUpdateWidget; 204 this.hasMultipleVisiblePages = hasMultipleVisiblePages; 205 } 206 } 207 208 // Direction used for moving the workspace and hotseat UI 209 public enum Direction { 210 X (TRANSLATION_X), 211 Y (TRANSLATION_Y); 212 213 private final Property<View, Float> viewProperty; 214 215 Direction(Property<View, Float> viewProperty) { 216 this.viewProperty = viewProperty; 217 } 218 } 219 220 private static final int HOTSEAT_STATE_ALPHA_INDEX = 2; 221 222 /** 223 * These values correspond to {@link Direction#X} & {@link Direction#Y} 224 */ 225 private float[] mPageAlpha = new float[] {1, 1}; 226 /** 227 * Hotseat alpha can be changed when moving horizontally, vertically, changing states. 228 * The values correspond to {@link Direction#X}, {@link Direction#Y} & 229 * {@link #HOTSEAT_STATE_ALPHA_INDEX} respectively. 230 */ 231 private float[] mHotseatAlpha = new float[] {1, 1, 1}; 232 233 public static final int QSB_ALPHA_INDEX_STATE_CHANGE = 0; 234 public static final int QSB_ALPHA_INDEX_Y_TRANSLATION = 1; 235 public static final int QSB_ALPHA_INDEX_PAGE_SCROLL = 2; 236 237 MultiStateAlphaController mQsbAlphaController; 238 239 @ViewDebug.ExportedProperty(category = "launcher") 240 private State mState = State.NORMAL; 241 private boolean mIsSwitchingState = false; 242 243 boolean mAnimatingViewIntoPlace = false; 244 boolean mChildrenLayersEnabled = true; 245 246 private boolean mStripScreensOnPageStopMoving = false; 247 248 /** Is the user is dragging an item near the edge of a page? */ 249 private boolean mInScrollArea = false; 250 251 private HolographicOutlineHelper mOutlineHelper; 252 @Thunk Bitmap mDragOutline = null; 253 public static final int DRAG_BITMAP_PADDING = 2; 254 private boolean mWorkspaceFadeInAdjacentScreens; 255 256 final WallpaperOffsetInterpolator mWallpaperOffset; 257 258 @Thunk Runnable mDelayedResizeRunnable; 259 private Runnable mDelayedSnapToPageRunnable; 260 261 // Variables relating to the creation of user folders by hovering shortcuts over shortcuts 262 private static final int FOLDER_CREATION_TIMEOUT = 0; 263 public static final int REORDER_TIMEOUT = 350; 264 private final Alarm mFolderCreationAlarm = new Alarm(); 265 private final Alarm mReorderAlarm = new Alarm(); 266 private FolderIcon.PreviewBackground mFolderCreateBg; 267 private FolderIcon mDragOverFolderIcon = null; 268 private boolean mCreateUserFolderOnDrop = false; 269 private boolean mAddToExistingFolderOnDrop = false; 270 private float mMaxDistanceForFolderCreation; 271 272 private final Canvas mCanvas = new Canvas(); 273 274 // Variables relating to touch disambiguation (scrolling workspace vs. scrolling a widget) 275 private float mXDown; 276 private float mYDown; 277 final static float START_DAMPING_TOUCH_SLOP_ANGLE = (float) Math.PI / 6; 278 final static float MAX_SWIPE_ANGLE = (float) Math.PI / 3; 279 final static float TOUCH_SLOP_DAMPING_FACTOR = 4; 280 281 // Relating to the animation of items being dropped externally 282 public static final int ANIMATE_INTO_POSITION_AND_DISAPPEAR = 0; 283 public static final int ANIMATE_INTO_POSITION_AND_REMAIN = 1; 284 public static final int ANIMATE_INTO_POSITION_AND_RESIZE = 2; 285 public static final int COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION = 3; 286 public static final int CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION = 4; 287 288 // Related to dragging, folder creation and reordering 289 private static final int DRAG_MODE_NONE = 0; 290 private static final int DRAG_MODE_CREATE_FOLDER = 1; 291 private static final int DRAG_MODE_ADD_TO_FOLDER = 2; 292 private static final int DRAG_MODE_REORDER = 3; 293 private int mDragMode = DRAG_MODE_NONE; 294 @Thunk int mLastReorderX = -1; 295 @Thunk int mLastReorderY = -1; 296 297 private SparseArray<Parcelable> mSavedStates; 298 private final ArrayList<Integer> mRestoredPages = new ArrayList<Integer>(); 299 300 private float mCurrentScale; 301 private float mTransitionProgress; 302 303 @Thunk Runnable mDeferredAction; 304 private boolean mDeferDropAfterUninstall; 305 private boolean mUninstallSuccessful; 306 307 // State related to Launcher Overlay 308 LauncherOverlay mLauncherOverlay; 309 boolean mScrollInteractionBegan; 310 boolean mStartedSendingScrollEvents; 311 float mLastOverlaySroll = 0; 312 // Total over scrollX in the overlay direction. 313 private int mUnboundedScrollX; 314 private boolean mForceDrawAdjacentPages = false; 315 // Total over scrollX in the overlay direction. 316 private float mOverlayTranslation; 317 private int mFirstPageScrollX; 318 319 // Handles workspace state transitions 320 private WorkspaceStateTransitionAnimation mStateTransitionAnimation; 321 322 private AccessibilityDelegate mPagesAccessibilityDelegate; 323 private OnStateChangeListener mOnStateChangeListener; 324 325 /** 326 * Used to inflate the Workspace from XML. 327 * 328 * @param context The application's context. 329 * @param attrs The attributes set containing the Workspace's customization values. 330 */ 331 public Workspace(Context context, AttributeSet attrs) { 332 this(context, attrs, 0); 333 } 334 335 /** 336 * Used to inflate the Workspace from XML. 337 * 338 * @param context The application's context. 339 * @param attrs The attributes set containing the Workspace's customization values. 340 * @param defStyle Unused. 341 */ 342 public Workspace(Context context, AttributeSet attrs, int defStyle) { 343 super(context, attrs, defStyle); 344 345 mOutlineHelper = HolographicOutlineHelper.obtain(context); 346 347 mLauncher = (Launcher) context; 348 mStateTransitionAnimation = new WorkspaceStateTransitionAnimation(mLauncher, this); 349 final Resources res = getResources(); 350 DeviceProfile grid = mLauncher.getDeviceProfile(); 351 mWorkspaceFadeInAdjacentScreens = grid.shouldFadeAdjacentWorkspaceScreens(); 352 mWallpaperManager = WallpaperManager.getInstance(context); 353 354 mWallpaperOffset = new WallpaperOffsetInterpolator(this); 355 mOverviewModeShrinkFactor = 356 res.getInteger(R.integer.config_workspaceOverviewShrinkPercentage) / 100f; 357 358 setOnHierarchyChangeListener(this); 359 setHapticFeedbackEnabled(false); 360 361 initWorkspace(); 362 363 // Disable multitouch across the workspace/all apps/customize tray 364 setMotionEventSplittingEnabled(true); 365 } 366 367 @Override 368 public void setInsets(Rect insets) { 369 int extraLeftPadding = insets.left - mInsets.left; 370 mInsets.set(insets); 371 if (extraLeftPadding != 0) { 372 /** 373 * Initial layout assumes that the insets is on the right, 374 * {@link DeviceProfile#getWorkspacePadding()}. Compensate for the difference. 375 */ 376 setPadding(getPaddingLeft() + extraLeftPadding, getPaddingTop(), 377 getPaddingRight() - extraLeftPadding, getPaddingBottom()); 378 } 379 380 CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID); 381 if (customScreen != null) { 382 View customContent = customScreen.getShortcutsAndWidgets().getChildAt(0); 383 if (customContent instanceof Insettable) { 384 ((Insettable) customContent).setInsets(mInsets); 385 } 386 } 387 } 388 389 public void setOnStateChangeListener(OnStateChangeListener listener) { 390 mOnStateChangeListener = listener; 391 } 392 393 // estimate the size of a widget with spans hSpan, vSpan. return MAX_VALUE for each 394 // dimension if unsuccessful 395 public int[] estimateItemSize(ItemInfo itemInfo, boolean springLoaded) { 396 float shrinkFactor = mLauncher.getDeviceProfile().workspaceSpringLoadShrinkFactor; 397 int[] size = new int[2]; 398 if (getChildCount() > 0) { 399 // Use the first non-custom page to estimate the child position 400 CellLayout cl = (CellLayout) getChildAt(numCustomPages()); 401 Rect r = estimateItemPosition(cl, 0, 0, itemInfo.spanX, itemInfo.spanY); 402 size[0] = r.width(); 403 size[1] = r.height(); 404 if (springLoaded) { 405 size[0] *= shrinkFactor; 406 size[1] *= shrinkFactor; 407 } 408 return size; 409 } else { 410 size[0] = Integer.MAX_VALUE; 411 size[1] = Integer.MAX_VALUE; 412 return size; 413 } 414 } 415 416 public Rect estimateItemPosition(CellLayout cl, int hCell, int vCell, int hSpan, int vSpan) { 417 Rect r = new Rect(); 418 cl.cellToRect(hCell, vCell, hSpan, vSpan, r); 419 return r; 420 } 421 422 @Override 423 public void onDragStart(final DragSource source, ItemInfo info, int dragAction) { 424 if (ENFORCE_DRAG_EVENT_ORDER) { 425 enfoceDragParity("onDragStart", 0, 0); 426 } 427 428 updateChildrenLayersEnabled(false); 429 mLauncher.lockScreenOrientation(); 430 mLauncher.onInteractionBegin(); 431 // Prevent any Un/InstallShortcutReceivers from updating the db while we are dragging 432 InstallShortcutReceiver.enableInstallQueue(); 433 434 if (mAddNewPageOnDrag) { 435 mDeferRemoveExtraEmptyScreen = false; 436 addExtraEmptyScreenOnDrag(); 437 } 438 } 439 440 public void setAddNewPageOnDrag(boolean addPage) { 441 mAddNewPageOnDrag = addPage; 442 } 443 444 public void deferRemoveExtraEmptyScreen() { 445 mDeferRemoveExtraEmptyScreen = true; 446 } 447 448 @Override 449 public void onDragEnd() { 450 if (ENFORCE_DRAG_EVENT_ORDER) { 451 enfoceDragParity("onDragEnd", 0, 0); 452 } 453 454 if (!mDeferRemoveExtraEmptyScreen) { 455 removeExtraEmptyScreen(true, mDragSourceInternal != null); 456 } 457 458 updateChildrenLayersEnabled(false); 459 mLauncher.unlockScreenOrientation(false); 460 461 // Re-enable any Un/InstallShortcutReceiver and now process any queued items 462 InstallShortcutReceiver.disableAndFlushInstallQueue(getContext()); 463 464 mDragSourceInternal = null; 465 mLauncher.onInteractionEnd(); 466 } 467 468 /** 469 * Initializes various states for this workspace. 470 */ 471 protected void initWorkspace() { 472 mCurrentPage = getDefaultPage(); 473 LauncherAppState app = LauncherAppState.getInstance(); 474 DeviceProfile grid = mLauncher.getDeviceProfile(); 475 mIconCache = app.getIconCache(); 476 setWillNotDraw(false); 477 setClipChildren(false); 478 setClipToPadding(false); 479 setChildrenDrawnWithCacheEnabled(true); 480 481 setMinScale(mOverviewModeShrinkFactor); 482 setupLayoutTransition(); 483 484 mMaxDistanceForFolderCreation = (0.55f * grid.iconSizePx); 485 486 // Set the wallpaper dimensions when Launcher starts up 487 setWallpaperDimension(); 488 489 setEdgeGlowColor(getResources().getColor(R.color.workspace_edge_effect_color)); 490 } 491 492 @Override 493 public void initParentViews(View parent) { 494 super.initParentViews(parent); 495 mPageIndicator.setAccessibilityDelegate(new OverviewAccessibilityDelegate()); 496 mQsbAlphaController = new MultiStateAlphaController(mLauncher.getQsbContainer(), 3); 497 } 498 499 private int getDefaultPage() { 500 return numCustomPages(); 501 } 502 503 private void setupLayoutTransition() { 504 // We want to show layout transitions when pages are deleted, to close the gap. 505 mLayoutTransition = new LayoutTransition(); 506 mLayoutTransition.enableTransitionType(LayoutTransition.DISAPPEARING); 507 mLayoutTransition.enableTransitionType(LayoutTransition.CHANGE_DISAPPEARING); 508 mLayoutTransition.disableTransitionType(LayoutTransition.APPEARING); 509 mLayoutTransition.disableTransitionType(LayoutTransition.CHANGE_APPEARING); 510 setLayoutTransition(mLayoutTransition); 511 } 512 513 void enableLayoutTransitions() { 514 setLayoutTransition(mLayoutTransition); 515 } 516 void disableLayoutTransitions() { 517 setLayoutTransition(null); 518 } 519 520 @Override 521 public void onChildViewAdded(View parent, View child) { 522 if (!(child instanceof CellLayout)) { 523 throw new IllegalArgumentException("A Workspace can only have CellLayout children."); 524 } 525 CellLayout cl = ((CellLayout) child); 526 cl.setOnInterceptTouchListener(this); 527 cl.setClickable(true); 528 cl.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO); 529 super.onChildViewAdded(parent, child); 530 } 531 532 protected boolean shouldDrawChild(View child) { 533 final CellLayout cl = (CellLayout) child; 534 return super.shouldDrawChild(child) && 535 (mIsSwitchingState || 536 cl.getShortcutsAndWidgets().getAlpha() > 0 || 537 cl.getBackgroundAlpha() > 0); 538 } 539 540 /** 541 * @return The open folder on the current screen, or null if there is none 542 */ 543 public Folder getOpenFolder() { 544 DragLayer dragLayer = mLauncher.getDragLayer(); 545 int count = dragLayer.getChildCount(); 546 for (int i = 0; i < count; i++) { 547 View child = dragLayer.getChildAt(i); 548 if (child instanceof Folder) { 549 Folder folder = (Folder) child; 550 if (folder.getInfo().opened) 551 return folder; 552 } 553 } 554 return null; 555 } 556 557 boolean isTouchActive() { 558 return mTouchState != TOUCH_STATE_REST; 559 } 560 561 private int getEmbeddedQsbId() { 562 return mLauncher.getDeviceProfile().isVerticalBarLayout() 563 ? R.id.qsb_container : R.id.workspace_blocked_row; 564 } 565 566 /** 567 * Initializes and binds the first page 568 * @param qsb an exisitng qsb to recycle or null. 569 */ 570 public void bindAndInitFirstWorkspaceScreen(View qsb) { 571 if (!FeatureFlags.QSB_ON_FIRST_SCREEN) { 572 return; 573 } 574 // Add the first page 575 CellLayout firstPage = insertNewWorkspaceScreen(Workspace.FIRST_SCREEN_ID, 0); 576 577 // Always add a QSB on the first screen. 578 if (qsb == null) { 579 // In transposed layout, we add the QSB in the Grid. As workspace does not touch the 580 // edges, we do not need a full width QSB. 581 qsb = mLauncher.getLayoutInflater().inflate( 582 mLauncher.getDeviceProfile().isVerticalBarLayout() 583 ? R.layout.qsb_container : R.layout.qsb_blocker_view, 584 firstPage, false); 585 } 586 587 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(0, 0, firstPage.getCountX(), 1); 588 lp.canReorder = false; 589 if (!firstPage.addViewToCellLayout(qsb, 0, getEmbeddedQsbId(), lp, true)) { 590 Log.e(TAG, "Failed to add to item at (0, 0) to CellLayout"); 591 } 592 } 593 594 @Override 595 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 596 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 597 598 // Update the QSB to match the cell height. This is treating the QSB essentially as a child 599 // of workspace despite that it's not a true child. 600 // Note that it relies on the strict ordering of measuring the workspace before the QSB 601 // at the dragLayer level. 602 if (getChildCount() > 0) { 603 CellLayout firstPage = (CellLayout) getChildAt(0); 604 int cellHeight = firstPage.getCellHeight(); 605 606 View qsbContainer = mLauncher.getQsbContainer(); 607 ViewGroup.LayoutParams lp = qsbContainer.getLayoutParams(); 608 if (cellHeight > 0 && lp.height != cellHeight) { 609 lp.height = cellHeight; 610 } 611 qsbContainer.setLayoutParams(lp); 612 } 613 } 614 615 public void removeAllWorkspaceScreens() { 616 // Disable all layout transitions before removing all pages to ensure that we don't get the 617 // transition animations competing with us changing the scroll when we add pages or the 618 // custom content screen 619 disableLayoutTransitions(); 620 621 // Since we increment the current page when we call addCustomContentPage via bindScreens 622 // (and other places), we need to adjust the current page back when we clear the pages 623 if (hasCustomContent()) { 624 removeCustomContentPage(); 625 } 626 627 // Recycle the QSB widget 628 View qsb = findViewById(getEmbeddedQsbId()); 629 if (qsb != null) { 630 ((ViewGroup) qsb.getParent()).removeView(qsb); 631 } 632 633 // Remove the pages and clear the screen models 634 removeAllViews(); 635 mScreenOrder.clear(); 636 mWorkspaceScreens.clear(); 637 638 // Ensure that the first page is always present 639 bindAndInitFirstWorkspaceScreen(qsb); 640 641 // Re-enable the layout transitions 642 enableLayoutTransitions(); 643 } 644 645 public void insertNewWorkspaceScreenBeforeEmptyScreen(long screenId) { 646 // Find the index to insert this view into. If the empty screen exists, then 647 // insert it before that. 648 int insertIndex = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID); 649 if (insertIndex < 0) { 650 insertIndex = mScreenOrder.size(); 651 } 652 insertNewWorkspaceScreen(screenId, insertIndex); 653 } 654 655 public void insertNewWorkspaceScreen(long screenId) { 656 insertNewWorkspaceScreen(screenId, getChildCount()); 657 } 658 659 public CellLayout insertNewWorkspaceScreen(long screenId, int insertIndex) { 660 if (mWorkspaceScreens.containsKey(screenId)) { 661 throw new RuntimeException("Screen id " + screenId + " already exists!"); 662 } 663 664 // Inflate the cell layout, but do not add it automatically so that we can get the newly 665 // created CellLayout. 666 CellLayout newScreen = (CellLayout) mLauncher.getLayoutInflater().inflate( 667 R.layout.workspace_screen, this, false /* attachToRoot */); 668 669 newScreen.setOnLongClickListener(mLongClickListener); 670 newScreen.setOnClickListener(mLauncher); 671 newScreen.setSoundEffectsEnabled(false); 672 mWorkspaceScreens.put(screenId, newScreen); 673 mScreenOrder.add(insertIndex, screenId); 674 addView(newScreen, insertIndex); 675 676 if (mLauncher.getAccessibilityDelegate().isInAccessibleDrag()) { 677 newScreen.enableAccessibleDrag(true, CellLayout.WORKSPACE_ACCESSIBILITY_DRAG); 678 } 679 680 return newScreen; 681 } 682 683 public void createCustomContentContainer() { 684 CellLayout customScreen = (CellLayout) 685 mLauncher.getLayoutInflater().inflate(R.layout.workspace_screen, this, false); 686 customScreen.disableDragTarget(); 687 customScreen.disableJailContent(); 688 689 mWorkspaceScreens.put(CUSTOM_CONTENT_SCREEN_ID, customScreen); 690 mScreenOrder.add(0, CUSTOM_CONTENT_SCREEN_ID); 691 692 // We want no padding on the custom content 693 customScreen.setPadding(0, 0, 0, 0); 694 695 addFullScreenPage(customScreen); 696 697 // Update the custom content hint 698 if (mRestorePage != INVALID_RESTORE_PAGE) { 699 mRestorePage = mRestorePage + 1; 700 } else { 701 setCurrentPage(getCurrentPage() + 1); 702 } 703 } 704 705 public void removeCustomContentPage() { 706 CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID); 707 if (customScreen == null) { 708 throw new RuntimeException("Expected custom content screen to exist"); 709 } 710 711 mWorkspaceScreens.remove(CUSTOM_CONTENT_SCREEN_ID); 712 mScreenOrder.remove(CUSTOM_CONTENT_SCREEN_ID); 713 removeView(customScreen); 714 715 if (mCustomContentCallbacks != null) { 716 mCustomContentCallbacks.onScrollProgressChanged(0); 717 mCustomContentCallbacks.onHide(); 718 } 719 720 mCustomContentCallbacks = null; 721 722 // Update the custom content hint 723 if (mRestorePage != INVALID_RESTORE_PAGE) { 724 mRestorePage = mRestorePage - 1; 725 } else { 726 setCurrentPage(getCurrentPage() - 1); 727 } 728 } 729 730 public void addToCustomContentPage(View customContent, CustomContentCallbacks callbacks, 731 String description) { 732 if (getPageIndexForScreenId(CUSTOM_CONTENT_SCREEN_ID) < 0) { 733 throw new RuntimeException("Expected custom content screen to exist"); 734 } 735 736 // Add the custom content to the full screen custom page 737 CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID); 738 int spanX = customScreen.getCountX(); 739 int spanY = customScreen.getCountY(); 740 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(0, 0, spanX, spanY); 741 lp.canReorder = false; 742 lp.isFullscreen = true; 743 if (customContent instanceof Insettable) { 744 ((Insettable)customContent).setInsets(mInsets); 745 } 746 747 // Verify that the child is removed from any existing parent. 748 if (customContent.getParent() instanceof ViewGroup) { 749 ViewGroup parent = (ViewGroup) customContent.getParent(); 750 parent.removeView(customContent); 751 } 752 customScreen.removeAllViews(); 753 customContent.setFocusable(true); 754 customContent.setOnKeyListener(new FullscreenKeyEventListener()); 755 customContent.setOnFocusChangeListener(mLauncher.mFocusHandler 756 .getHideIndicatorOnFocusListener()); 757 customScreen.addViewToCellLayout(customContent, 0, 0, lp, true); 758 mCustomContentDescription = description; 759 760 mCustomContentCallbacks = callbacks; 761 } 762 763 public void addExtraEmptyScreenOnDrag() { 764 boolean lastChildOnScreen = false; 765 boolean childOnFinalScreen = false; 766 767 // Cancel any pending removal of empty screen 768 mRemoveEmptyScreenRunnable = null; 769 770 if (mDragSourceInternal != null) { 771 if (mDragSourceInternal.getChildCount() == 1) { 772 lastChildOnScreen = true; 773 } 774 CellLayout cl = (CellLayout) mDragSourceInternal.getParent(); 775 if (indexOfChild(cl) == getChildCount() - 1) { 776 childOnFinalScreen = true; 777 } 778 } 779 780 // If this is the last item on the final screen 781 if (lastChildOnScreen && childOnFinalScreen) { 782 return; 783 } 784 if (!mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID)) { 785 insertNewWorkspaceScreen(EXTRA_EMPTY_SCREEN_ID); 786 } 787 } 788 789 public boolean addExtraEmptyScreen() { 790 if (!mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID)) { 791 insertNewWorkspaceScreen(EXTRA_EMPTY_SCREEN_ID); 792 return true; 793 } 794 return false; 795 } 796 797 private void convertFinalScreenToEmptyScreenIfNecessary() { 798 if (mLauncher.isWorkspaceLoading()) { 799 // Invalid and dangerous operation if workspace is loading 800 return; 801 } 802 803 if (hasExtraEmptyScreen() || mScreenOrder.size() == 0) return; 804 long finalScreenId = mScreenOrder.get(mScreenOrder.size() - 1); 805 806 if (finalScreenId == CUSTOM_CONTENT_SCREEN_ID) return; 807 CellLayout finalScreen = mWorkspaceScreens.get(finalScreenId); 808 809 // If the final screen is empty, convert it to the extra empty screen 810 if (finalScreen.getShortcutsAndWidgets().getChildCount() == 0 && 811 !finalScreen.isDropPending()) { 812 mWorkspaceScreens.remove(finalScreenId); 813 mScreenOrder.remove(finalScreenId); 814 815 // if this is the last non-custom content screen, convert it to the empty screen 816 mWorkspaceScreens.put(EXTRA_EMPTY_SCREEN_ID, finalScreen); 817 mScreenOrder.add(EXTRA_EMPTY_SCREEN_ID); 818 819 // Update the model if we have changed any screens 820 mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder); 821 } 822 } 823 824 public void removeExtraEmptyScreen(final boolean animate, boolean stripEmptyScreens) { 825 removeExtraEmptyScreenDelayed(animate, null, 0, stripEmptyScreens); 826 } 827 828 public void removeExtraEmptyScreenDelayed(final boolean animate, final Runnable onComplete, 829 final int delay, final boolean stripEmptyScreens) { 830 if (mLauncher.isWorkspaceLoading()) { 831 // Don't strip empty screens if the workspace is still loading 832 return; 833 } 834 835 if (delay > 0) { 836 postDelayed(new Runnable() { 837 @Override 838 public void run() { 839 removeExtraEmptyScreenDelayed(animate, onComplete, 0, stripEmptyScreens); 840 } 841 }, delay); 842 return; 843 } 844 845 convertFinalScreenToEmptyScreenIfNecessary(); 846 if (hasExtraEmptyScreen()) { 847 int emptyIndex = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID); 848 if (getNextPage() == emptyIndex) { 849 snapToPage(getNextPage() - 1, SNAP_OFF_EMPTY_SCREEN_DURATION); 850 fadeAndRemoveEmptyScreen(SNAP_OFF_EMPTY_SCREEN_DURATION, FADE_EMPTY_SCREEN_DURATION, 851 onComplete, stripEmptyScreens); 852 } else { 853 snapToPage(getNextPage(), 0); 854 fadeAndRemoveEmptyScreen(0, FADE_EMPTY_SCREEN_DURATION, 855 onComplete, stripEmptyScreens); 856 } 857 return; 858 } else if (stripEmptyScreens) { 859 // If we're not going to strip the empty screens after removing 860 // the extra empty screen, do it right away. 861 stripEmptyScreens(); 862 } 863 864 if (onComplete != null) { 865 onComplete.run(); 866 } 867 } 868 869 private void fadeAndRemoveEmptyScreen(int delay, int duration, final Runnable onComplete, 870 final boolean stripEmptyScreens) { 871 // XXX: Do we need to update LM workspace screens below? 872 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f); 873 PropertyValuesHolder bgAlpha = PropertyValuesHolder.ofFloat("backgroundAlpha", 0f); 874 875 final CellLayout cl = mWorkspaceScreens.get(EXTRA_EMPTY_SCREEN_ID); 876 877 mRemoveEmptyScreenRunnable = new Runnable() { 878 @Override 879 public void run() { 880 if (hasExtraEmptyScreen()) { 881 mWorkspaceScreens.remove(EXTRA_EMPTY_SCREEN_ID); 882 mScreenOrder.remove(EXTRA_EMPTY_SCREEN_ID); 883 removeView(cl); 884 if (stripEmptyScreens) { 885 stripEmptyScreens(); 886 } 887 // Update the page indicator to reflect the removed page. 888 showPageIndicatorAtCurrentScroll(); 889 } 890 } 891 }; 892 893 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(cl, alpha, bgAlpha); 894 oa.setDuration(duration); 895 oa.setStartDelay(delay); 896 oa.addListener(new AnimatorListenerAdapter() { 897 @Override 898 public void onAnimationEnd(Animator animation) { 899 if (mRemoveEmptyScreenRunnable != null) { 900 mRemoveEmptyScreenRunnable.run(); 901 } 902 if (onComplete != null) { 903 onComplete.run(); 904 } 905 } 906 }); 907 oa.start(); 908 } 909 910 public boolean hasExtraEmptyScreen() { 911 int nScreens = getChildCount(); 912 nScreens = nScreens - numCustomPages(); 913 return mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID) && nScreens > 1; 914 } 915 916 public long commitExtraEmptyScreen() { 917 if (mLauncher.isWorkspaceLoading()) { 918 // Invalid and dangerous operation if workspace is loading 919 return -1; 920 } 921 922 int index = getPageIndexForScreenId(EXTRA_EMPTY_SCREEN_ID); 923 CellLayout cl = mWorkspaceScreens.get(EXTRA_EMPTY_SCREEN_ID); 924 mWorkspaceScreens.remove(EXTRA_EMPTY_SCREEN_ID); 925 mScreenOrder.remove(EXTRA_EMPTY_SCREEN_ID); 926 927 long newId = LauncherSettings.Settings.call(getContext().getContentResolver(), 928 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID) 929 .getLong(LauncherSettings.Settings.EXTRA_VALUE); 930 mWorkspaceScreens.put(newId, cl); 931 mScreenOrder.add(newId); 932 933 // Update the model for the new screen 934 mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder); 935 936 return newId; 937 } 938 939 public CellLayout getScreenWithId(long screenId) { 940 return mWorkspaceScreens.get(screenId); 941 } 942 943 public long getIdForScreen(CellLayout layout) { 944 int index = mWorkspaceScreens.indexOfValue(layout); 945 if (index != -1) { 946 return mWorkspaceScreens.keyAt(index); 947 } 948 return -1; 949 } 950 951 public int getPageIndexForScreenId(long screenId) { 952 return indexOfChild(mWorkspaceScreens.get(screenId)); 953 } 954 955 public long getScreenIdForPageIndex(int index) { 956 if (0 <= index && index < mScreenOrder.size()) { 957 return mScreenOrder.get(index); 958 } 959 return -1; 960 } 961 962 public ArrayList<Long> getScreenOrder() { 963 return mScreenOrder; 964 } 965 966 public void stripEmptyScreens() { 967 if (mLauncher.isWorkspaceLoading()) { 968 // Don't strip empty screens if the workspace is still loading. 969 // This is dangerous and can result in data loss. 970 return; 971 } 972 973 if (isPageMoving()) { 974 mStripScreensOnPageStopMoving = true; 975 return; 976 } 977 978 int currentPage = getNextPage(); 979 ArrayList<Long> removeScreens = new ArrayList<Long>(); 980 int total = mWorkspaceScreens.size(); 981 for (int i = 0; i < total; i++) { 982 long id = mWorkspaceScreens.keyAt(i); 983 CellLayout cl = mWorkspaceScreens.valueAt(i); 984 // FIRST_SCREEN_ID can never be removed. 985 if ((!FeatureFlags.QSB_ON_FIRST_SCREEN || id > FIRST_SCREEN_ID) 986 && cl.getShortcutsAndWidgets().getChildCount() == 0) { 987 removeScreens.add(id); 988 } 989 } 990 991 boolean isInAccessibleDrag = mLauncher.getAccessibilityDelegate().isInAccessibleDrag(); 992 993 // We enforce at least one page to add new items to. In the case that we remove the last 994 // such screen, we convert the last screen to the empty screen 995 int minScreens = 1 + numCustomPages(); 996 997 int pageShift = 0; 998 for (Long id: removeScreens) { 999 CellLayout cl = mWorkspaceScreens.get(id); 1000 mWorkspaceScreens.remove(id); 1001 mScreenOrder.remove(id); 1002 1003 if (getChildCount() > minScreens) { 1004 if (indexOfChild(cl) < currentPage) { 1005 pageShift++; 1006 } 1007 1008 if (isInAccessibleDrag) { 1009 cl.enableAccessibleDrag(false, CellLayout.WORKSPACE_ACCESSIBILITY_DRAG); 1010 } 1011 1012 removeView(cl); 1013 } else { 1014 // if this is the last non-custom content screen, convert it to the empty screen 1015 mRemoveEmptyScreenRunnable = null; 1016 mWorkspaceScreens.put(EXTRA_EMPTY_SCREEN_ID, cl); 1017 mScreenOrder.add(EXTRA_EMPTY_SCREEN_ID); 1018 } 1019 } 1020 1021 if (!removeScreens.isEmpty()) { 1022 // Update the model if we have changed any screens 1023 mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder); 1024 } 1025 1026 if (pageShift >= 0) { 1027 setCurrentPage(currentPage - pageShift); 1028 } 1029 } 1030 1031 // See implementation for parameter definition. 1032 void addInScreen(View child, long container, long screenId, 1033 int x, int y, int spanX, int spanY) { 1034 addInScreen(child, container, screenId, x, y, spanX, spanY, false, false); 1035 } 1036 1037 // At bind time, we use the rank (screenId) to compute x and y for hotseat items. 1038 // See implementation for parameter definition. 1039 public void addInScreenFromBind(View child, long container, long screenId, int x, int y, 1040 int spanX, int spanY) { 1041 addInScreen(child, container, screenId, x, y, spanX, spanY, false, true); 1042 } 1043 1044 // See implementation for parameter definition. 1045 void addInScreen(View child, long container, long screenId, int x, int y, int spanX, int spanY, 1046 boolean insert) { 1047 addInScreen(child, container, screenId, x, y, spanX, spanY, insert, false); 1048 } 1049 1050 /** 1051 * Adds the specified child in the specified screen. The position and dimension of 1052 * the child are defined by x, y, spanX and spanY. 1053 * 1054 * @param child The child to add in one of the workspace's screens. 1055 * @param screenId The screen in which to add the child. 1056 * @param x The X position of the child in the screen's grid. 1057 * @param y The Y position of the child in the screen's grid. 1058 * @param spanX The number of cells spanned horizontally by the child. 1059 * @param spanY The number of cells spanned vertically by the child. 1060 * @param insert When true, the child is inserted at the beginning of the children list. 1061 * @param computeXYFromRank When true, we use the rank (stored in screenId) to compute 1062 * the x and y position in which to place hotseat items. Otherwise 1063 * we use the x and y position to compute the rank. 1064 */ 1065 void addInScreen(View child, long container, long screenId, int x, int y, int spanX, int spanY, 1066 boolean insert, boolean computeXYFromRank) { 1067 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { 1068 if (getScreenWithId(screenId) == null) { 1069 Log.e(TAG, "Skipping child, screenId " + screenId + " not found"); 1070 // DEBUGGING - Print out the stack trace to see where we are adding from 1071 new Throwable().printStackTrace(); 1072 return; 1073 } 1074 } 1075 if (screenId == EXTRA_EMPTY_SCREEN_ID) { 1076 // This should never happen 1077 throw new RuntimeException("Screen id should not be EXTRA_EMPTY_SCREEN_ID"); 1078 } 1079 1080 final CellLayout layout; 1081 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { 1082 layout = mLauncher.getHotseat().getLayout(); 1083 child.setOnKeyListener(new HotseatIconKeyEventListener()); 1084 1085 // Hide folder title in the hotseat 1086 if (child instanceof FolderIcon) { 1087 ((FolderIcon) child).setTextVisible(false); 1088 } 1089 1090 if (computeXYFromRank) { 1091 x = mLauncher.getHotseat().getCellXFromOrder((int) screenId); 1092 y = mLauncher.getHotseat().getCellYFromOrder((int) screenId); 1093 } else { 1094 screenId = mLauncher.getHotseat().getOrderInHotseat(x, y); 1095 } 1096 } else { 1097 // Show folder title if not in the hotseat 1098 if (child instanceof FolderIcon) { 1099 ((FolderIcon) child).setTextVisible(true); 1100 } 1101 layout = getScreenWithId(screenId); 1102 child.setOnKeyListener(new IconKeyEventListener()); 1103 } 1104 1105 ViewGroup.LayoutParams genericLp = child.getLayoutParams(); 1106 CellLayout.LayoutParams lp; 1107 if (genericLp == null || !(genericLp instanceof CellLayout.LayoutParams)) { 1108 lp = new CellLayout.LayoutParams(x, y, spanX, spanY); 1109 } else { 1110 lp = (CellLayout.LayoutParams) genericLp; 1111 lp.cellX = x; 1112 lp.cellY = y; 1113 lp.cellHSpan = spanX; 1114 lp.cellVSpan = spanY; 1115 } 1116 1117 if (spanX < 0 && spanY < 0) { 1118 lp.isLockedToGrid = false; 1119 } 1120 1121 // Get the canonical child id to uniquely represent this view in this screen 1122 ItemInfo info = (ItemInfo) child.getTag(); 1123 int childId = mLauncher.getViewIdForItem(info); 1124 1125 boolean markCellsAsOccupied = !(child instanceof Folder); 1126 if (!layout.addViewToCellLayout(child, insert ? 0 : -1, childId, lp, markCellsAsOccupied)) { 1127 // TODO: This branch occurs when the workspace is adding views 1128 // outside of the defined grid 1129 // maybe we should be deleting these items from the LauncherModel? 1130 Log.e(TAG, "Failed to add to item at (" + lp.cellX + "," + lp.cellY + ") to CellLayout"); 1131 } 1132 1133 if (!(child instanceof Folder)) { 1134 child.setHapticFeedbackEnabled(false); 1135 child.setOnLongClickListener(mLongClickListener); 1136 if (child instanceof BubbleTextView && DeepShortcutManager.supportsShortcuts(info)) { 1137 // TODO: only add this listener if the item has shortcuts associated with it. 1138 child.setOnTouchListener(new ShortcutsContainerListener((BubbleTextView) child)); 1139 } 1140 } 1141 if (child instanceof DropTarget) { 1142 mDragController.addDropTarget((DropTarget) child); 1143 } 1144 } 1145 1146 /** 1147 * Called directly from a CellLayout (not by the framework), after we've been added as a 1148 * listener via setOnInterceptTouchEventListener(). This allows us to tell the CellLayout 1149 * that it should intercept touch events, which is not something that is normally supported. 1150 */ 1151 @SuppressLint("ClickableViewAccessibility") 1152 @Override 1153 public boolean onTouch(View v, MotionEvent event) { 1154 return (workspaceInModalState() || !isFinishedSwitchingState()) 1155 || (!workspaceInModalState() && indexOfChild(v) != mCurrentPage); 1156 } 1157 1158 public boolean isSwitchingState() { 1159 return mIsSwitchingState; 1160 } 1161 1162 /** This differs from isSwitchingState in that we take into account how far the transition 1163 * has completed. */ 1164 public boolean isFinishedSwitchingState() { 1165 return !mIsSwitchingState || (mTransitionProgress > 0.5f); 1166 } 1167 1168 protected void onWindowVisibilityChanged (int visibility) { 1169 mLauncher.onWindowVisibilityChanged(visibility); 1170 } 1171 1172 @Override 1173 public boolean dispatchUnhandledMove(View focused, int direction) { 1174 if (workspaceInModalState() || !isFinishedSwitchingState()) { 1175 // when the home screens are shrunken, shouldn't allow side-scrolling 1176 return false; 1177 } 1178 return super.dispatchUnhandledMove(focused, direction); 1179 } 1180 1181 @Override 1182 public boolean onInterceptTouchEvent(MotionEvent ev) { 1183 switch (ev.getAction() & MotionEvent.ACTION_MASK) { 1184 case MotionEvent.ACTION_DOWN: 1185 mXDown = ev.getX(); 1186 mYDown = ev.getY(); 1187 mTouchDownTime = System.currentTimeMillis(); 1188 break; 1189 case MotionEvent.ACTION_POINTER_UP: 1190 case MotionEvent.ACTION_UP: 1191 if (mTouchState == TOUCH_STATE_REST) { 1192 final CellLayout currentPage = (CellLayout) getChildAt(mCurrentPage); 1193 if (currentPage != null) { 1194 onWallpaperTap(ev); 1195 } 1196 } 1197 } 1198 return super.onInterceptTouchEvent(ev); 1199 } 1200 1201 @Override 1202 public boolean onGenericMotionEvent(MotionEvent event) { 1203 // Ignore pointer scroll events if the custom content doesn't allow scrolling. 1204 if ((getScreenIdForPageIndex(getCurrentPage()) == CUSTOM_CONTENT_SCREEN_ID) 1205 && (mCustomContentCallbacks != null) 1206 && !mCustomContentCallbacks.isScrollingAllowed()) { 1207 return false; 1208 } 1209 return super.onGenericMotionEvent(event); 1210 } 1211 1212 protected void reinflateWidgetsIfNecessary() { 1213 final int clCount = getChildCount(); 1214 for (int i = 0; i < clCount; i++) { 1215 CellLayout cl = (CellLayout) getChildAt(i); 1216 ShortcutAndWidgetContainer swc = cl.getShortcutsAndWidgets(); 1217 final int itemCount = swc.getChildCount(); 1218 for (int j = 0; j < itemCount; j++) { 1219 View v = swc.getChildAt(j); 1220 1221 if (v instanceof LauncherAppWidgetHostView 1222 && v.getTag() instanceof LauncherAppWidgetInfo) { 1223 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag(); 1224 LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) v; 1225 if (lahv.isReinflateRequired()) { 1226 // Remove and rebind the current widget (which was inflated in the wrong 1227 // orientation), but don't delete it from the database 1228 mLauncher.removeItem(lahv, info, false /* deleteFromDb */); 1229 mLauncher.bindAppWidget(info); 1230 } 1231 } 1232 } 1233 } 1234 } 1235 1236 @Override 1237 protected void determineScrollingStart(MotionEvent ev) { 1238 if (!isFinishedSwitchingState()) return; 1239 1240 float deltaX = ev.getX() - mXDown; 1241 float absDeltaX = Math.abs(deltaX); 1242 float absDeltaY = Math.abs(ev.getY() - mYDown); 1243 1244 if (Float.compare(absDeltaX, 0f) == 0) return; 1245 1246 float slope = absDeltaY / absDeltaX; 1247 float theta = (float) Math.atan(slope); 1248 1249 if (absDeltaX > mTouchSlop || absDeltaY > mTouchSlop) { 1250 cancelCurrentPageLongPress(); 1251 } 1252 1253 boolean passRightSwipesToCustomContent = 1254 (mTouchDownTime - mCustomContentShowTime) > CUSTOM_CONTENT_GESTURE_DELAY; 1255 1256 boolean swipeInIgnoreDirection = mIsRtl ? deltaX < 0 : deltaX > 0; 1257 boolean onCustomContentScreen = 1258 getScreenIdForPageIndex(getCurrentPage()) == CUSTOM_CONTENT_SCREEN_ID; 1259 if (swipeInIgnoreDirection && onCustomContentScreen && passRightSwipesToCustomContent) { 1260 // Pass swipes to the right to the custom content page. 1261 return; 1262 } 1263 1264 if (onCustomContentScreen && (mCustomContentCallbacks != null) 1265 && !mCustomContentCallbacks.isScrollingAllowed()) { 1266 // Don't allow workspace scrolling if the current custom content screen doesn't allow 1267 // scrolling. 1268 return; 1269 } 1270 1271 if (theta > MAX_SWIPE_ANGLE) { 1272 // Above MAX_SWIPE_ANGLE, we don't want to ever start scrolling the workspace 1273 return; 1274 } else if (theta > START_DAMPING_TOUCH_SLOP_ANGLE) { 1275 // Above START_DAMPING_TOUCH_SLOP_ANGLE and below MAX_SWIPE_ANGLE, we want to 1276 // increase the touch slop to make it harder to begin scrolling the workspace. This 1277 // results in vertically scrolling widgets to more easily. The higher the angle, the 1278 // more we increase touch slop. 1279 theta -= START_DAMPING_TOUCH_SLOP_ANGLE; 1280 float extraRatio = (float) 1281 Math.sqrt((theta / (MAX_SWIPE_ANGLE - START_DAMPING_TOUCH_SLOP_ANGLE))); 1282 super.determineScrollingStart(ev, 1 + TOUCH_SLOP_DAMPING_FACTOR * extraRatio); 1283 } else { 1284 // Below START_DAMPING_TOUCH_SLOP_ANGLE, we don't do anything special 1285 super.determineScrollingStart(ev); 1286 } 1287 } 1288 1289 protected void onPageBeginMoving() { 1290 super.onPageBeginMoving(); 1291 1292 if (isHardwareAccelerated()) { 1293 updateChildrenLayersEnabled(false); 1294 } else { 1295 if (mNextPage != INVALID_PAGE) { 1296 // we're snapping to a particular screen 1297 enableChildrenCache(mCurrentPage, mNextPage); 1298 } else { 1299 // this is when user is actively dragging a particular screen, they might 1300 // swipe it either left or right (but we won't advance by more than one screen) 1301 enableChildrenCache(mCurrentPage - 1, mCurrentPage + 1); 1302 } 1303 } 1304 } 1305 1306 protected void onPageEndMoving() { 1307 super.onPageEndMoving(); 1308 1309 if (isHardwareAccelerated()) { 1310 updateChildrenLayersEnabled(false); 1311 } else { 1312 clearChildrenCache(); 1313 } 1314 1315 if (mDragController.isDragging()) { 1316 if (workspaceInModalState()) { 1317 // If we are in springloaded mode, then force an event to check if the current touch 1318 // is under a new page (to scroll to) 1319 mDragController.forceTouchMove(); 1320 } 1321 } 1322 1323 if (mDelayedResizeRunnable != null && !mIsSwitchingState) { 1324 mDelayedResizeRunnable.run(); 1325 mDelayedResizeRunnable = null; 1326 } 1327 1328 if (mDelayedSnapToPageRunnable != null) { 1329 mDelayedSnapToPageRunnable.run(); 1330 mDelayedSnapToPageRunnable = null; 1331 } 1332 if (mStripScreensOnPageStopMoving) { 1333 stripEmptyScreens(); 1334 mStripScreensOnPageStopMoving = false; 1335 } 1336 } 1337 1338 protected void onScrollInteractionBegin() { 1339 super.onScrollInteractionEnd(); 1340 mScrollInteractionBegan = true; 1341 } 1342 1343 protected void onScrollInteractionEnd() { 1344 super.onScrollInteractionEnd(); 1345 mScrollInteractionBegan = false; 1346 if (mStartedSendingScrollEvents) { 1347 mStartedSendingScrollEvents = false; 1348 mLauncherOverlay.onScrollInteractionEnd(); 1349 } 1350 } 1351 1352 public void setLauncherOverlay(LauncherOverlay overlay) { 1353 mLauncherOverlay = overlay; 1354 // A new overlay has been set. Reset event tracking 1355 mStartedSendingScrollEvents = false; 1356 onOverlayScrollChanged(0); 1357 } 1358 1359 @Override 1360 protected int getUnboundedScrollX() { 1361 if (isScrollingOverlay()) { 1362 return mUnboundedScrollX; 1363 } 1364 1365 return super.getUnboundedScrollX(); 1366 } 1367 1368 private boolean isScrollingOverlay() { 1369 return mLauncherOverlay != null && 1370 ((mIsRtl && mUnboundedScrollX > mMaxScrollX) || (!mIsRtl && mUnboundedScrollX < 0)); 1371 } 1372 1373 @Override 1374 protected void snapToDestination() { 1375 // If we're overscrolling the overlay, we make sure to immediately reset the PagedView 1376 // to it's baseline position instead of letting the overscroll settle. The overlay handles 1377 // it's own settling, and every gesture to the overlay should be self-contained and start 1378 // from 0, so we zero it out here. 1379 if (isScrollingOverlay()) { 1380 int finalScroll = mIsRtl ? mMaxScrollX : 0; 1381 1382 // We reset mWasInOverscroll so that PagedView doesn't zero out the overscroll 1383 // interaction when we call scrollTo. 1384 mWasInOverscroll = false; 1385 scrollTo(finalScroll, getScrollY()); 1386 } else { 1387 super.snapToDestination(); 1388 } 1389 } 1390 1391 @Override 1392 public void scrollTo(int x, int y) { 1393 mUnboundedScrollX = x; 1394 super.scrollTo(x, y); 1395 } 1396 1397 private void onWorkspaceOverallScrollChanged() { 1398 mLauncher.getQsbContainer().setTranslationX( 1399 mOverlayTranslation + mFirstPageScrollX - getScrollX()); 1400 } 1401 1402 @Override 1403 protected void onScrollChanged(int l, int t, int oldl, int oldt) { 1404 super.onScrollChanged(l, t, oldl, oldt); 1405 onWorkspaceOverallScrollChanged(); 1406 1407 // Update the page indicator progress. 1408 boolean isTransitioning = mIsSwitchingState 1409 || (getLayoutTransition() != null && getLayoutTransition().isRunning()); 1410 if (!isTransitioning) { 1411 showPageIndicatorAtCurrentScroll(); 1412 } 1413 } 1414 1415 private void showPageIndicatorAtCurrentScroll() { 1416 if (mPageIndicator != null) { 1417 mPageIndicator.setScroll(getScrollX(), computeMaxScrollX()); 1418 } 1419 } 1420 1421 @Override 1422 protected void overScroll(float amount) { 1423 boolean shouldOverScroll = (amount <= 0 && (!hasCustomContent() || mIsRtl)) || 1424 (amount >= 0 && (!hasCustomContent() || !mIsRtl)); 1425 1426 boolean shouldScrollOverlay = mLauncherOverlay != null && 1427 ((amount <= 0 && !mIsRtl) || (amount >= 0 && mIsRtl)); 1428 1429 boolean shouldZeroOverlay = mLauncherOverlay != null && mLastOverlaySroll != 0 && 1430 ((amount >= 0 && !mIsRtl) || (amount <= 0 && mIsRtl)); 1431 1432 if (shouldScrollOverlay) { 1433 if (!mStartedSendingScrollEvents && mScrollInteractionBegan) { 1434 mStartedSendingScrollEvents = true; 1435 mLauncherOverlay.onScrollInteractionBegin(); 1436 } 1437 1438 mLastOverlaySroll = Math.abs(amount / getViewportWidth()); 1439 mLauncherOverlay.onScrollChange(mLastOverlaySroll, mIsRtl); 1440 } else if (shouldOverScroll) { 1441 dampedOverScroll(amount); 1442 } 1443 1444 if (shouldZeroOverlay) { 1445 mLauncherOverlay.onScrollChange(0, mIsRtl); 1446 } 1447 } 1448 1449 private final Interpolator mAlphaInterpolator = new DecelerateInterpolator(3f); 1450 1451 /** 1452 * The overlay scroll is being controlled locally, just update our overlay effect 1453 */ 1454 public void onOverlayScrollChanged(float scroll) { 1455 float offset = 0f; 1456 float slip = 0f; 1457 1458 scroll = Math.max(scroll - offset, 0); 1459 scroll = Math.min(1, scroll / (1 - offset)); 1460 1461 float alpha = 1 - mAlphaInterpolator.getInterpolation(scroll); 1462 float transX = mLauncher.getDragLayer().getMeasuredWidth() * scroll; 1463 transX *= 1 - slip; 1464 1465 if (mIsRtl) { 1466 transX = -transX; 1467 } 1468 mOverlayTranslation = transX; 1469 1470 // TODO(adamcohen): figure out a final effect here. We may need to recommend 1471 // different effects based on device performance. On at least one relatively high-end 1472 // device I've tried, translating the launcher causes things to get quite laggy. 1473 setWorkspaceTranslationAndAlpha(Direction.X, transX, alpha); 1474 setHotseatTranslationAndAlpha(Direction.X, transX, alpha); 1475 onWorkspaceOverallScrollChanged(); 1476 } 1477 1478 /** 1479 * Moves the workspace UI in the Y direction. 1480 * @param translation the amount of shift. 1481 * @param alpha the alpha for the workspace page 1482 */ 1483 public void setWorkspaceYTranslationAndAlpha(float translation, float alpha) { 1484 setWorkspaceTranslationAndAlpha(Direction.Y, translation, alpha); 1485 1486 mLauncher.getQsbContainer().setTranslationY(translation); 1487 mQsbAlphaController.setAlphaAtIndex(alpha, QSB_ALPHA_INDEX_Y_TRANSLATION); 1488 } 1489 1490 /** 1491 * Moves the workspace UI in the provided direction. 1492 * @param direction the direction to move the workspace 1493 * @param translation the amount of shift. 1494 * @param alpha the alpha for the workspace page 1495 */ 1496 private void setWorkspaceTranslationAndAlpha(Direction direction, float translation, float alpha) { 1497 Property<View, Float> property = direction.viewProperty; 1498 mPageAlpha[direction.ordinal()] = alpha; 1499 float finalAlpha = mPageAlpha[0] * mPageAlpha[1]; 1500 1501 View currentChild = getChildAt(getCurrentPage()); 1502 if (currentChild != null) { 1503 property.set(currentChild, translation); 1504 currentChild.setAlpha(finalAlpha); 1505 } 1506 1507 // When the animation finishes, reset all pages, just in case we missed a page. 1508 if (Float.compare(translation, 0) == 0) { 1509 for (int i = getChildCount() - 1; i >= 0; i--) { 1510 View child = getChildAt(i); 1511 property.set(child, translation); 1512 child.setAlpha(finalAlpha); 1513 } 1514 } 1515 } 1516 1517 /** 1518 * Moves the Hotseat UI in the provided direction. 1519 * @param direction the direction to move the workspace 1520 * @param translation the amound of shift. 1521 * @param alpha the alpha for the hotseat page 1522 */ 1523 public void setHotseatTranslationAndAlpha(Direction direction, float translation, float alpha) { 1524 Property<View, Float> property = direction.viewProperty; 1525 property.set(mPageIndicator, translation); 1526 property.set(mLauncher.getHotseat(), translation); 1527 setHotseatAlphaAtIndex(alpha, direction.ordinal()); 1528 } 1529 1530 private void setHotseatAlphaAtIndex(float alpha, int index) { 1531 mHotseatAlpha[index] = alpha; 1532 float finalAlpha = mHotseatAlpha[0] * mHotseatAlpha[1] * mHotseatAlpha[2]; 1533 1534 mLauncher.getHotseat().setAlpha(finalAlpha); 1535 } 1536 1537 public ValueAnimator createHotseatAlphaAnimator(float finalValue) { 1538 if (Float.compare(finalValue, mHotseatAlpha[HOTSEAT_STATE_ALPHA_INDEX]) == 0) { 1539 // Return a dummy animator to avoid null checks. 1540 return ValueAnimator.ofFloat(0, 0); 1541 } else { 1542 ValueAnimator animator = ValueAnimator 1543 .ofFloat(mHotseatAlpha[HOTSEAT_STATE_ALPHA_INDEX], finalValue); 1544 animator.addUpdateListener(new AnimatorUpdateListener() { 1545 @Override 1546 public void onAnimationUpdate(ValueAnimator valueAnimator) { 1547 float value = (Float) valueAnimator.getAnimatedValue(); 1548 setHotseatAlphaAtIndex(value, HOTSEAT_STATE_ALPHA_INDEX); 1549 } 1550 }); 1551 1552 AccessibilityManager am = (AccessibilityManager) 1553 mLauncher.getSystemService(Context.ACCESSIBILITY_SERVICE); 1554 final boolean accessibilityEnabled = am.isEnabled(); 1555 animator.addUpdateListener( 1556 new AlphaUpdateListener(mLauncher.getHotseat(), accessibilityEnabled)); 1557 animator.addUpdateListener( 1558 new AlphaUpdateListener(mPageIndicator, accessibilityEnabled)); 1559 return animator; 1560 } 1561 } 1562 1563 @Override 1564 protected Matrix getPageShiftMatrix() { 1565 if (Float.compare(mOverlayTranslation, 0) != 0) { 1566 // The pages are translated by mOverlayTranslation. incorporate that in the 1567 // visible page calculation by shifting everything back by that same amount. 1568 mTempMatrix.set(getMatrix()); 1569 mTempMatrix.postTranslate(-mOverlayTranslation, 0); 1570 return mTempMatrix; 1571 } 1572 return super.getPageShiftMatrix(); 1573 } 1574 1575 @Override 1576 protected void getEdgeVerticalPostion(int[] pos) { 1577 View child = getChildAt(getPageCount() - 1); 1578 pos[0] = child.getTop(); 1579 pos[1] = child.getBottom(); 1580 } 1581 1582 @Override 1583 protected void notifyPageSwitchListener() { 1584 super.notifyPageSwitchListener(); 1585 1586 if (hasCustomContent() && getNextPage() == 0 && !mCustomContentShowing) { 1587 mCustomContentShowing = true; 1588 if (mCustomContentCallbacks != null) { 1589 mCustomContentCallbacks.onShow(false); 1590 mCustomContentShowTime = System.currentTimeMillis(); 1591 } 1592 } else if (hasCustomContent() && getNextPage() != 0 && mCustomContentShowing) { 1593 mCustomContentShowing = false; 1594 if (mCustomContentCallbacks != null) { 1595 mCustomContentCallbacks.onHide(); 1596 } 1597 } 1598 } 1599 1600 protected CustomContentCallbacks getCustomContentCallbacks() { 1601 return mCustomContentCallbacks; 1602 } 1603 1604 protected void setWallpaperDimension() { 1605 Utilities.THREAD_POOL_EXECUTOR.execute(new Runnable() { 1606 @Override 1607 public void run() { 1608 final Point size = LauncherAppState.getInstance() 1609 .getInvariantDeviceProfile().defaultWallpaperSize; 1610 if (size.x != mWallpaperManager.getDesiredMinimumWidth() 1611 || size.y != mWallpaperManager.getDesiredMinimumHeight()) { 1612 mWallpaperManager.suggestDesiredDimensions(size.x, size.y); 1613 } 1614 } 1615 }); 1616 } 1617 1618 protected void snapToPage(int whichPage, Runnable r) { 1619 snapToPage(whichPage, SLOW_PAGE_SNAP_ANIMATION_DURATION, r); 1620 } 1621 1622 protected void snapToPage(int whichPage, int duration, Runnable r) { 1623 if (mDelayedSnapToPageRunnable != null) { 1624 mDelayedSnapToPageRunnable.run(); 1625 } 1626 mDelayedSnapToPageRunnable = r; 1627 snapToPage(whichPage, duration); 1628 } 1629 1630 public void snapToScreenId(long screenId) { 1631 snapToScreenId(screenId, null); 1632 } 1633 1634 protected void snapToScreenId(long screenId, Runnable r) { 1635 snapToPage(getPageIndexForScreenId(screenId), r); 1636 } 1637 1638 @Override 1639 public void computeScroll() { 1640 super.computeScroll(); 1641 mWallpaperOffset.syncWithScroll(); 1642 } 1643 1644 public void computeScrollWithoutInvalidation() { 1645 computeScrollHelper(false); 1646 } 1647 1648 @Override 1649 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { 1650 if (!isSwitchingState()) { 1651 super.determineScrollingStart(ev, touchSlopScale); 1652 } 1653 } 1654 1655 @Override 1656 public void announceForAccessibility(CharSequence text) { 1657 // Don't announce if apps is on top of us. 1658 if (!mLauncher.isAppsViewVisible()) { 1659 super.announceForAccessibility(text); 1660 } 1661 } 1662 1663 public void showOutlinesTemporarily() { 1664 if (!mIsPageMoving && !isTouchActive()) { 1665 snapToPage(mCurrentPage); 1666 } 1667 } 1668 1669 private void updatePageAlphaValues(int screenCenter) { 1670 if (mWorkspaceFadeInAdjacentScreens && 1671 !workspaceInModalState() && 1672 !mIsSwitchingState) { 1673 for (int i = numCustomPages(); i < getChildCount(); i++) { 1674 CellLayout child = (CellLayout) getChildAt(i); 1675 if (child != null) { 1676 float scrollProgress = getScrollProgress(screenCenter, child, i); 1677 float alpha = 1 - Math.abs(scrollProgress); 1678 child.getShortcutsAndWidgets().setAlpha(alpha); 1679 1680 if (isQsbContainerPage(i)) { 1681 mQsbAlphaController.setAlphaAtIndex(alpha, QSB_ALPHA_INDEX_PAGE_SCROLL); 1682 } 1683 } 1684 } 1685 } 1686 } 1687 1688 @TargetApi(Build.VERSION_CODES.LOLLIPOP) 1689 @Override 1690 public void enableAccessibleDrag(boolean enable) { 1691 for (int i = 0; i < getChildCount(); i++) { 1692 CellLayout child = (CellLayout) getChildAt(i); 1693 child.enableAccessibleDrag(enable, CellLayout.WORKSPACE_ACCESSIBILITY_DRAG); 1694 } 1695 1696 if (enable) { 1697 // We need to allow our individual children to become click handlers in this case 1698 setOnClickListener(null); 1699 } else { 1700 // Reset our click listener 1701 setOnClickListener(mLauncher); 1702 } 1703 mLauncher.getDropTargetBar().enableAccessibleDrag(enable); 1704 mLauncher.getHotseat().getLayout() 1705 .enableAccessibleDrag(enable, CellLayout.WORKSPACE_ACCESSIBILITY_DRAG); 1706 } 1707 1708 public boolean hasCustomContent() { 1709 return (mScreenOrder.size() > 0 && mScreenOrder.get(0) == CUSTOM_CONTENT_SCREEN_ID); 1710 } 1711 1712 public int numCustomPages() { 1713 return hasCustomContent() ? 1 : 0; 1714 } 1715 1716 public boolean isOnOrMovingToCustomContent() { 1717 return hasCustomContent() && getNextPage() == 0 && mRestorePage == INVALID_RESTORE_PAGE; 1718 } 1719 1720 private void updateStateForCustomContent(int screenCenter) { 1721 float translationX = 0; 1722 float progress = 0; 1723 if (hasCustomContent()) { 1724 int index = mScreenOrder.indexOf(CUSTOM_CONTENT_SCREEN_ID); 1725 1726 int scrollDelta = getScrollX() - getScrollForPage(index) - 1727 getLayoutTransitionOffsetForPage(index); 1728 float scrollRange = getScrollForPage(index + 1) - getScrollForPage(index); 1729 translationX = scrollRange - scrollDelta; 1730 progress = (scrollRange - scrollDelta) / scrollRange; 1731 1732 if (mIsRtl) { 1733 translationX = Math.min(0, translationX); 1734 } else { 1735 translationX = Math.max(0, translationX); 1736 } 1737 progress = Math.max(0, progress); 1738 } 1739 1740 if (Float.compare(progress, mLastCustomContentScrollProgress) == 0) return; 1741 1742 CellLayout cc = mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID); 1743 if (progress > 0 && cc.getVisibility() != VISIBLE && !workspaceInModalState()) { 1744 cc.setVisibility(VISIBLE); 1745 } 1746 1747 mLastCustomContentScrollProgress = progress; 1748 1749 // We should only update the drag layer background alpha if we are not in all apps or the 1750 // widgets tray 1751 if (mState == State.NORMAL) { 1752 mLauncher.getDragLayer().setBackgroundAlpha(progress == 1 ? 0 : progress * 0.8f); 1753 } 1754 1755 if (mLauncher.getHotseat() != null) { 1756 mLauncher.getHotseat().setTranslationX(translationX); 1757 } 1758 1759 if (mPageIndicator != null) { 1760 mPageIndicator.setTranslationX(translationX); 1761 } 1762 1763 if (mCustomContentCallbacks != null) { 1764 mCustomContentCallbacks.onScrollProgressChanged(progress); 1765 } 1766 } 1767 1768 @Override 1769 protected void screenScrolled(int screenCenter) { 1770 updatePageAlphaValues(screenCenter); 1771 updateStateForCustomContent(screenCenter); 1772 enableHwLayersOnVisiblePages(); 1773 } 1774 1775 protected void onAttachedToWindow() { 1776 super.onAttachedToWindow(); 1777 IBinder windowToken = getWindowToken(); 1778 mWallpaperOffset.setWindowToken(windowToken); 1779 computeScroll(); 1780 mDragController.setWindowToken(windowToken); 1781 } 1782 1783 protected void onDetachedFromWindow() { 1784 super.onDetachedFromWindow(); 1785 mWallpaperOffset.setWindowToken(null); 1786 } 1787 1788 protected void onResume() { 1789 // Update wallpaper dimensions if they were changed since last onResume 1790 // (we also always set the wallpaper dimensions in the constructor) 1791 if (LauncherAppState.getInstance().hasWallpaperChangedSinceLastCheck()) { 1792 setWallpaperDimension(); 1793 } 1794 mWallpaperOffset.onResume(); 1795 } 1796 1797 @Override 1798 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 1799 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) { 1800 mWallpaperOffset.syncWithScroll(); 1801 mWallpaperOffset.jumpToFinal(); 1802 } 1803 super.onLayout(changed, left, top, right, bottom); 1804 mFirstPageScrollX = getScrollForPage(0); 1805 onWorkspaceOverallScrollChanged(); 1806 } 1807 1808 @Override 1809 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { 1810 if (!mLauncher.isAppsViewVisible()) { 1811 final Folder openFolder = getOpenFolder(); 1812 if (openFolder != null) { 1813 return openFolder.requestFocus(direction, previouslyFocusedRect); 1814 } else { 1815 return super.onRequestFocusInDescendants(direction, previouslyFocusedRect); 1816 } 1817 } 1818 return false; 1819 } 1820 1821 @Override 1822 public int getDescendantFocusability() { 1823 if (workspaceInModalState()) { 1824 return ViewGroup.FOCUS_BLOCK_DESCENDANTS; 1825 } 1826 return super.getDescendantFocusability(); 1827 } 1828 1829 @Override 1830 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { 1831 if (!mLauncher.isAppsViewVisible()) { 1832 final Folder openFolder = getOpenFolder(); 1833 if (openFolder != null) { 1834 openFolder.addFocusables(views, direction); 1835 } else { 1836 super.addFocusables(views, direction, focusableMode); 1837 } 1838 } 1839 } 1840 1841 public boolean workspaceInModalState() { 1842 return mState != State.NORMAL; 1843 } 1844 1845 void enableChildrenCache(int fromPage, int toPage) { 1846 if (fromPage > toPage) { 1847 final int temp = fromPage; 1848 fromPage = toPage; 1849 toPage = temp; 1850 } 1851 1852 final int screenCount = getChildCount(); 1853 1854 fromPage = Math.max(fromPage, 0); 1855 toPage = Math.min(toPage, screenCount - 1); 1856 1857 for (int i = fromPage; i <= toPage; i++) { 1858 final CellLayout layout = (CellLayout) getChildAt(i); 1859 layout.setChildrenDrawnWithCacheEnabled(true); 1860 layout.setChildrenDrawingCacheEnabled(true); 1861 } 1862 } 1863 1864 void clearChildrenCache() { 1865 final int screenCount = getChildCount(); 1866 for (int i = 0; i < screenCount; i++) { 1867 final CellLayout layout = (CellLayout) getChildAt(i); 1868 layout.setChildrenDrawnWithCacheEnabled(false); 1869 // In software mode, we don't want the items to continue to be drawn into bitmaps 1870 if (!isHardwareAccelerated()) { 1871 layout.setChildrenDrawingCacheEnabled(false); 1872 } 1873 } 1874 } 1875 1876 @Thunk void updateChildrenLayersEnabled(boolean force) { 1877 boolean small = mState == State.OVERVIEW || mIsSwitchingState; 1878 boolean enableChildrenLayers = force || small || mAnimatingViewIntoPlace || isPageMoving(); 1879 1880 if (enableChildrenLayers != mChildrenLayersEnabled) { 1881 mChildrenLayersEnabled = enableChildrenLayers; 1882 if (mChildrenLayersEnabled) { 1883 enableHwLayersOnVisiblePages(); 1884 } else { 1885 for (int i = 0; i < getPageCount(); i++) { 1886 final CellLayout cl = (CellLayout) getChildAt(i); 1887 cl.enableHardwareLayer(false); 1888 } 1889 } 1890 } 1891 } 1892 1893 private void enableHwLayersOnVisiblePages() { 1894 if (mChildrenLayersEnabled) { 1895 final int screenCount = getChildCount(); 1896 getVisiblePages(mTempVisiblePagesRange); 1897 int leftScreen = mTempVisiblePagesRange[0]; 1898 int rightScreen = mTempVisiblePagesRange[1]; 1899 if (leftScreen == rightScreen) { 1900 // make sure we're caching at least two pages always 1901 if (rightScreen < screenCount - 1) { 1902 rightScreen++; 1903 } else if (leftScreen > 0) { 1904 leftScreen--; 1905 } 1906 } 1907 1908 final CellLayout customScreen = mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID); 1909 for (int i = 0; i < screenCount; i++) { 1910 final CellLayout layout = (CellLayout) getPageAt(i); 1911 1912 // enable layers between left and right screen inclusive, except for the 1913 // customScreen, which may animate its content during transitions. 1914 boolean enableLayer = layout != customScreen && 1915 leftScreen <= i && i <= rightScreen && shouldDrawChild(layout); 1916 layout.enableHardwareLayer(enableLayer); 1917 } 1918 } 1919 } 1920 1921 public void buildPageHardwareLayers() { 1922 // force layers to be enabled just for the call to buildLayer 1923 updateChildrenLayersEnabled(true); 1924 if (getWindowToken() != null) { 1925 final int childCount = getChildCount(); 1926 for (int i = 0; i < childCount; i++) { 1927 CellLayout cl = (CellLayout) getChildAt(i); 1928 cl.buildHardwareLayer(); 1929 } 1930 } 1931 updateChildrenLayersEnabled(false); 1932 } 1933 1934 @Override 1935 protected void getVisiblePages(int[] range) { 1936 super.getVisiblePages(range); 1937 if (mForceDrawAdjacentPages) { 1938 // In overview mode, make sure that the two side pages are visible. 1939 range[0] = Utilities.boundToRange(getCurrentPage() - 1, numCustomPages(), range[1]); 1940 range[1] = Utilities.boundToRange(getCurrentPage() + 1, range[0], getPageCount() - 1); 1941 } 1942 } 1943 1944 protected void onWallpaperTap(MotionEvent ev) { 1945 final int[] position = mTempXY; 1946 getLocationOnScreen(position); 1947 1948 int pointerIndex = ev.getActionIndex(); 1949 position[0] += (int) ev.getX(pointerIndex); 1950 position[1] += (int) ev.getY(pointerIndex); 1951 1952 mWallpaperManager.sendWallpaperCommand(getWindowToken(), 1953 ev.getAction() == MotionEvent.ACTION_UP 1954 ? WallpaperManager.COMMAND_TAP : WallpaperManager.COMMAND_SECONDARY_TAP, 1955 position[0], position[1], 0, null); 1956 } 1957 1958 /* 1959 * 1960 * We call these methods (onDragStartedWithItemSpans/onDragStartedWithSize) whenever we 1961 * start a drag in Launcher, regardless of whether the drag has ever entered the Workspace 1962 * 1963 * These methods mark the appropriate pages as accepting drops (which alters their visual 1964 * appearance). 1965 * 1966 */ 1967 private static Rect getDrawableBounds(Drawable d) { 1968 Rect bounds = new Rect(); 1969 d.copyBounds(bounds); 1970 if (bounds.width() == 0 || bounds.height() == 0) { 1971 bounds.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 1972 } else { 1973 bounds.offsetTo(0, 0); 1974 } 1975 if (d instanceof PreloadIconDrawable) { 1976 int inset = -((PreloadIconDrawable) d).getOutset(); 1977 bounds.inset(inset, inset); 1978 } 1979 return bounds; 1980 } 1981 1982 public void onExternalDragStartedWithItem(View v) { 1983 // Compose a drag bitmap with the view scaled to the icon size 1984 DeviceProfile grid = mLauncher.getDeviceProfile(); 1985 int iconSize = grid.iconSizePx; 1986 int bmpWidth = v.getMeasuredWidth(); 1987 int bmpHeight = v.getMeasuredHeight(); 1988 1989 // If this is a text view, use its drawable instead 1990 if (v instanceof TextView) { 1991 Drawable d = getTextViewIcon((TextView) v); 1992 Rect bounds = getDrawableBounds(d); 1993 bmpWidth = bounds.width(); 1994 bmpHeight = bounds.height(); 1995 } 1996 1997 // Compose the bitmap to create the icon from 1998 Bitmap b = Bitmap.createBitmap(bmpWidth, bmpHeight, 1999 Bitmap.Config.ARGB_8888); 2000 mCanvas.setBitmap(b); 2001 drawDragView(v, mCanvas, 0); 2002 mCanvas.setBitmap(null); 2003 2004 // The outline is used to visualize where the item will land if dropped 2005 mDragOutline = createDragOutline(b, DRAG_BITMAP_PADDING, iconSize, iconSize, true); 2006 } 2007 2008 public void onDragStartedWithItem(PendingAddItemInfo info, Bitmap b, boolean clipAlpha) { 2009 // Find a page that has enough space to place this widget (after rearranging/resizing). 2010 // Start at the current page and search right (on LTR) until finding a page with enough 2011 // space. Since an empty screen is the furthest right, a page must be found. 2012 int currentPageInOverview = getPageNearestToCenterOfScreen(); 2013 for (int pageIndex = currentPageInOverview; pageIndex < getPageCount(); pageIndex++) { 2014 CellLayout page = (CellLayout) getPageAt(pageIndex); 2015 if (page.hasReorderSolution(info)) { 2016 setCurrentPage(pageIndex); 2017 break; 2018 } 2019 } 2020 2021 int[] size = estimateItemSize(info, false); 2022 2023 // The outline is used to visualize where the item will land if dropped 2024 mDragOutline = createDragOutline(b, DRAG_BITMAP_PADDING, size[0], size[1], clipAlpha); 2025 } 2026 2027 public void exitWidgetResizeMode() { 2028 DragLayer dragLayer = mLauncher.getDragLayer(); 2029 dragLayer.clearAllResizeFrames(); 2030 } 2031 2032 @Override 2033 protected void getFreeScrollPageRange(int[] range) { 2034 getOverviewModePages(range); 2035 } 2036 2037 private void getOverviewModePages(int[] range) { 2038 int start = numCustomPages(); 2039 int end = getChildCount() - 1; 2040 2041 range[0] = Math.max(0, Math.min(start, getChildCount() - 1)); 2042 range[1] = Math.max(0, end); 2043 } 2044 2045 public void onStartReordering() { 2046 super.onStartReordering(); 2047 // Reordering handles its own animations, disable the automatic ones. 2048 disableLayoutTransitions(); 2049 } 2050 2051 public void onEndReordering() { 2052 super.onEndReordering(); 2053 2054 if (mLauncher.isWorkspaceLoading()) { 2055 // Invalid and dangerous operation if workspace is loading 2056 return; 2057 } 2058 2059 mScreenOrder.clear(); 2060 int count = getChildCount(); 2061 for (int i = 0; i < count; i++) { 2062 CellLayout cl = ((CellLayout) getChildAt(i)); 2063 mScreenOrder.add(getIdForScreen(cl)); 2064 } 2065 2066 mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder); 2067 2068 // Re-enable auto layout transitions for page deletion. 2069 enableLayoutTransitions(); 2070 } 2071 2072 public boolean isInOverviewMode() { 2073 return mState == State.OVERVIEW; 2074 } 2075 2076 public void snapToPageFromOverView(int whichPage) { 2077 mStateTransitionAnimation.snapToPageFromOverView(whichPage); 2078 } 2079 2080 int getOverviewModeTranslationY() { 2081 DeviceProfile grid = mLauncher.getDeviceProfile(); 2082 int overviewButtonBarHeight = grid.getOverviewModeButtonBarHeight(); 2083 2084 int scaledHeight = (int) (mOverviewModeShrinkFactor * getNormalChildHeight()); 2085 Rect workspacePadding = grid.getWorkspacePadding(sTempRect); 2086 int workspaceTop = mInsets.top + workspacePadding.top; 2087 int workspaceBottom = getViewportHeight() - mInsets.bottom - workspacePadding.bottom; 2088 int overviewTop = mInsets.top; 2089 int overviewBottom = getViewportHeight() - mInsets.bottom - overviewButtonBarHeight; 2090 int workspaceOffsetTopEdge = workspaceTop + ((workspaceBottom - workspaceTop) - scaledHeight) / 2; 2091 int overviewOffsetTopEdge = overviewTop + (overviewBottom - overviewTop - scaledHeight) / 2; 2092 return -workspaceOffsetTopEdge + overviewOffsetTopEdge; 2093 } 2094 2095 float getSpringLoadedTranslationY() { 2096 DeviceProfile grid = mLauncher.getDeviceProfile(); 2097 if (grid.isVerticalBarLayout() || getChildCount() == 0) { 2098 return 0; 2099 } 2100 2101 float scaledHeight = grid.workspaceSpringLoadShrinkFactor * getNormalChildHeight(); 2102 float shrunkTop = mInsets.top + grid.dropTargetBarSizePx; 2103 float shrunkBottom = getViewportHeight() - mInsets.bottom 2104 - grid.getWorkspacePadding(sTempRect).bottom 2105 - grid.workspaceSpringLoadedBottomSpace; 2106 float totalShrunkSpace = shrunkBottom - shrunkTop; 2107 2108 float desiredCellTop = shrunkTop + (totalShrunkSpace - scaledHeight) / 2; 2109 2110 float halfHeight = getHeight() / 2; 2111 float myCenter = getTop() + halfHeight; 2112 float cellTopFromCenter = halfHeight - getChildAt(0).getTop(); 2113 float actualCellTop = myCenter - cellTopFromCenter * grid.workspaceSpringLoadShrinkFactor; 2114 return (desiredCellTop - actualCellTop) / grid.workspaceSpringLoadShrinkFactor; 2115 } 2116 2117 float getOverviewModeShrinkFactor() { 2118 return mOverviewModeShrinkFactor; 2119 } 2120 2121 /** 2122 * Sets the current workspace {@link State}, returning an animation transitioning the workspace 2123 * to that new state. 2124 */ 2125 public Animator setStateWithAnimation(State toState, boolean animated, 2126 HashMap<View, Integer> layerViews) { 2127 // Create the animation to the new state 2128 AnimatorSet workspaceAnim = mStateTransitionAnimation.getAnimationToState(mState, 2129 toState, animated, layerViews); 2130 2131 boolean shouldNotifyWidgetChange = !mState.shouldUpdateWidget 2132 && toState.shouldUpdateWidget; 2133 // Update the current state 2134 mState = toState; 2135 updateAccessibilityFlags(); 2136 2137 if (shouldNotifyWidgetChange) { 2138 mLauncher.notifyWidgetProvidersChanged(); 2139 } 2140 2141 if (mOnStateChangeListener != null) { 2142 mOnStateChangeListener.prepareStateChange(toState, animated ? workspaceAnim : null); 2143 } 2144 2145 return workspaceAnim; 2146 } 2147 2148 public State getState() { 2149 return mState; 2150 } 2151 2152 public void updateAccessibilityFlags() { 2153 // TODO: Update the accessibility flags appropriately when dragging. 2154 if (!mLauncher.getAccessibilityDelegate().isInAccessibleDrag()) { 2155 if (Utilities.ATLEAST_LOLLIPOP) { 2156 int total = getPageCount(); 2157 for (int i = numCustomPages(); i < total; i++) { 2158 updateAccessibilityFlags((CellLayout) getPageAt(i), i); 2159 } 2160 setImportantForAccessibility((mState == State.NORMAL || mState == State.OVERVIEW) 2161 ? IMPORTANT_FOR_ACCESSIBILITY_AUTO 2162 : IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); 2163 } else { 2164 int accessible = mState == State.NORMAL ? 2165 IMPORTANT_FOR_ACCESSIBILITY_AUTO : 2166 IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS; 2167 setImportantForAccessibility(accessible); 2168 } 2169 } 2170 } 2171 2172 private void updateAccessibilityFlags(CellLayout page, int pageNo) { 2173 if (mState == State.OVERVIEW) { 2174 page.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES); 2175 page.getShortcutsAndWidgets().setImportantForAccessibility( 2176 IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); 2177 page.setContentDescription(getPageDescription(pageNo)); 2178 2179 // No custom action for the first page. 2180 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || pageNo > 0) { 2181 if (mPagesAccessibilityDelegate == null) { 2182 mPagesAccessibilityDelegate = new OverviewScreenAccessibilityDelegate(this); 2183 } 2184 page.setAccessibilityDelegate(mPagesAccessibilityDelegate); 2185 } 2186 } else { 2187 int accessible = mState == State.NORMAL ? 2188 IMPORTANT_FOR_ACCESSIBILITY_AUTO : 2189 IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS; 2190 page.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO); 2191 page.getShortcutsAndWidgets().setImportantForAccessibility(accessible); 2192 page.setContentDescription(null); 2193 page.setAccessibilityDelegate(null); 2194 } 2195 } 2196 2197 @Override 2198 public void onLauncherTransitionPrepare(Launcher l, boolean animated, 2199 boolean multiplePagesVisible) { 2200 mIsSwitchingState = true; 2201 mTransitionProgress = 0; 2202 2203 if (multiplePagesVisible) { 2204 mForceDrawAdjacentPages = true; 2205 } 2206 invalidate(); // This will call dispatchDraw(), which calls getVisiblePages(). 2207 2208 updateChildrenLayersEnabled(false); 2209 hideCustomContentIfNecessary(); 2210 } 2211 2212 @Override 2213 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) { 2214 if (mPageIndicator != null) { 2215 boolean isNewStateSpringLoaded = mState == State.SPRING_LOADED; 2216 mPageIndicator.setShouldAutoHide(!isNewStateSpringLoaded); 2217 if (isNewStateSpringLoaded) { 2218 // Show the page indicator at the same time as the rest of the transition. 2219 showPageIndicatorAtCurrentScroll(); 2220 } 2221 } 2222 } 2223 2224 @Override 2225 public void onLauncherTransitionStep(Launcher l, float t) { 2226 mTransitionProgress = t; 2227 } 2228 2229 @Override 2230 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) { 2231 mIsSwitchingState = false; 2232 updateChildrenLayersEnabled(false); 2233 showCustomContentIfNecessary(); 2234 mForceDrawAdjacentPages = false; 2235 if (mState == State.SPRING_LOADED) { 2236 showPageIndicatorAtCurrentScroll(); 2237 } 2238 } 2239 2240 void updateCustomContentVisibility() { 2241 int visibility = mState == Workspace.State.NORMAL ? VISIBLE : INVISIBLE; 2242 setCustomContentVisibility(visibility); 2243 } 2244 2245 void setCustomContentVisibility(int visibility) { 2246 if (hasCustomContent()) { 2247 mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(visibility); 2248 } 2249 } 2250 2251 void showCustomContentIfNecessary() { 2252 boolean show = mState == Workspace.State.NORMAL; 2253 if (show && hasCustomContent()) { 2254 mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(VISIBLE); 2255 } 2256 } 2257 2258 void hideCustomContentIfNecessary() { 2259 boolean hide = mState != Workspace.State.NORMAL; 2260 if (hide && hasCustomContent()) { 2261 disableLayoutTransitions(); 2262 mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(INVISIBLE); 2263 enableLayoutTransitions(); 2264 } 2265 } 2266 2267 /** 2268 * Returns the drawable for the given text view. 2269 */ 2270 public static Drawable getTextViewIcon(TextView tv) { 2271 final Drawable[] drawables = tv.getCompoundDrawables(); 2272 for (int i = 0; i < drawables.length; i++) { 2273 if (drawables[i] != null) { 2274 return drawables[i]; 2275 } 2276 } 2277 return null; 2278 } 2279 2280 /** 2281 * Draw the View v into the given Canvas. 2282 * 2283 * @param v the view to draw 2284 * @param destCanvas the canvas to draw on 2285 * @param padding the horizontal and vertical padding to use when drawing 2286 */ 2287 private static void drawDragView(View v, Canvas destCanvas, int padding) { 2288 destCanvas.save(); 2289 if (v instanceof TextView) { 2290 Drawable d = getTextViewIcon((TextView) v); 2291 Rect bounds = getDrawableBounds(d); 2292 destCanvas.translate(padding / 2 - bounds.left, padding / 2 - bounds.top); 2293 d.draw(destCanvas); 2294 } else { 2295 final Rect clipRect = sTempRect; 2296 v.getDrawingRect(clipRect); 2297 2298 boolean textVisible = false; 2299 if (v instanceof FolderIcon) { 2300 // For FolderIcons the text can bleed into the icon area, and so we need to 2301 // hide the text completely (which can't be achieved by clipping). 2302 if (((FolderIcon) v).getTextVisible()) { 2303 ((FolderIcon) v).setTextVisible(false); 2304 textVisible = true; 2305 } 2306 } 2307 destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2); 2308 destCanvas.clipRect(clipRect, Op.REPLACE); 2309 v.draw(destCanvas); 2310 2311 // Restore text visibility of FolderIcon if necessary 2312 if (textVisible) { 2313 ((FolderIcon) v).setTextVisible(true); 2314 } 2315 } 2316 destCanvas.restore(); 2317 } 2318 2319 /** 2320 * Returns a new bitmap to show when the given View is being dragged around. 2321 * Responsibility for the bitmap is transferred to the caller. 2322 * @param expectedPadding padding to add to the drag view. If a different padding was used 2323 * its value will be changed 2324 */ 2325 public Bitmap createDragBitmap(View v, AtomicInteger expectedPadding) { 2326 Bitmap b; 2327 2328 int padding = expectedPadding.get(); 2329 if (v instanceof TextView) { 2330 Drawable d = getTextViewIcon((TextView) v); 2331 Rect bounds = getDrawableBounds(d); 2332 b = Bitmap.createBitmap(bounds.width() + padding, 2333 bounds.height() + padding, Bitmap.Config.ARGB_8888); 2334 expectedPadding.set(padding - bounds.left - bounds.top); 2335 } else { 2336 b = Bitmap.createBitmap( 2337 v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888); 2338 } 2339 2340 mCanvas.setBitmap(b); 2341 drawDragView(v, mCanvas, padding); 2342 mCanvas.setBitmap(null); 2343 2344 return b; 2345 } 2346 2347 /** 2348 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location. 2349 * Responsibility for the bitmap is transferred to the caller. 2350 */ 2351 private Bitmap createDragOutline(View v, int padding) { 2352 final int outlineColor = getResources().getColor(R.color.outline_color); 2353 final Bitmap b = Bitmap.createBitmap( 2354 v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888); 2355 2356 mCanvas.setBitmap(b); 2357 drawDragView(v, mCanvas, padding); 2358 mOutlineHelper.applyExpensiveOutlineWithBlur(b, mCanvas, outlineColor, outlineColor); 2359 mCanvas.setBitmap(null); 2360 return b; 2361 } 2362 2363 /** 2364 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location. 2365 * Responsibility for the bitmap is transferred to the caller. 2366 */ 2367 private Bitmap createDragOutline(Bitmap orig, int padding, int w, int h, 2368 boolean clipAlpha) { 2369 final int outlineColor = getResources().getColor(R.color.outline_color); 2370 final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 2371 mCanvas.setBitmap(b); 2372 2373 Rect src = new Rect(0, 0, orig.getWidth(), orig.getHeight()); 2374 float scaleFactor = Math.min((w - padding) / (float) orig.getWidth(), 2375 (h - padding) / (float) orig.getHeight()); 2376 int scaledWidth = (int) (scaleFactor * orig.getWidth()); 2377 int scaledHeight = (int) (scaleFactor * orig.getHeight()); 2378 Rect dst = new Rect(0, 0, scaledWidth, scaledHeight); 2379 2380 // center the image 2381 dst.offset((w - scaledWidth) / 2, (h - scaledHeight) / 2); 2382 2383 mCanvas.drawBitmap(orig, src, dst, null); 2384 mOutlineHelper.applyExpensiveOutlineWithBlur(b, mCanvas, outlineColor, outlineColor, 2385 clipAlpha); 2386 mCanvas.setBitmap(null); 2387 2388 return b; 2389 } 2390 2391 public void startDrag(CellLayout.CellInfo cellInfo) { 2392 startDrag(cellInfo, false); 2393 } 2394 2395 @Override 2396 public void startDrag(CellLayout.CellInfo cellInfo, boolean accessible) { 2397 View child = cellInfo.cell; 2398 2399 // Make sure the drag was started by a long press as opposed to a long click. 2400 if (!child.isInTouchMode()) { 2401 return; 2402 } 2403 2404 mDragInfo = cellInfo; 2405 child.setVisibility(INVISIBLE); 2406 CellLayout layout = (CellLayout) child.getParent().getParent(); 2407 layout.prepareChildForDrag(child); 2408 2409 beginDragShared(child, this, accessible); 2410 } 2411 2412 public void beginDragShared(View child, DragSource source, boolean accessible) { 2413 beginDragShared(child, new Point(), source, accessible); 2414 } 2415 2416 public void beginDragShared(View child, Point relativeTouchPos, DragSource source, 2417 boolean accessible) { 2418 child.clearFocus(); 2419 child.setPressed(false); 2420 2421 // The outline is used to visualize where the item will land if dropped 2422 mDragOutline = createDragOutline(child, DRAG_BITMAP_PADDING); 2423 2424 mLauncher.onDragStarted(child); 2425 // The drag bitmap follows the touch point around on the screen 2426 AtomicInteger padding = new AtomicInteger(DRAG_BITMAP_PADDING); 2427 final Bitmap b = createDragBitmap(child, padding); 2428 2429 final int bmpWidth = b.getWidth(); 2430 final int bmpHeight = b.getHeight(); 2431 2432 float scale = mLauncher.getDragLayer().getLocationInDragLayer(child, mTempXY); 2433 int dragLayerX = Math.round(mTempXY[0] - (bmpWidth - scale * child.getWidth()) / 2); 2434 int dragLayerY = Math.round(mTempXY[1] - (bmpHeight - scale * bmpHeight) / 2 2435 - padding.get() / 2); 2436 2437 DeviceProfile grid = mLauncher.getDeviceProfile(); 2438 Point dragVisualizeOffset = null; 2439 Rect dragRect = null; 2440 if (child instanceof BubbleTextView) { 2441 BubbleTextView icon = (BubbleTextView) child; 2442 int iconSize = grid.iconSizePx; 2443 int top = child.getPaddingTop(); 2444 int left = (bmpWidth - iconSize) / 2; 2445 int right = left + iconSize; 2446 int bottom = top + iconSize; 2447 if (icon.isLayoutHorizontal()) { 2448 // If the layout is horizontal, then if we are just picking up the icon, then just 2449 // use the child position since the icon is top-left aligned. Otherwise, offset 2450 // the drag layer position horizontally so that the icon is under the current 2451 // touch position. 2452 if (icon.getIcon().getBounds().contains(relativeTouchPos.x, relativeTouchPos.y)) { 2453 dragLayerX = Math.round(mTempXY[0]); 2454 } else { 2455 dragLayerX = Math.round(mTempXY[0] + relativeTouchPos.x - (bmpWidth / 2)); 2456 } 2457 } 2458 dragLayerY += top; 2459 // Note: The drag region is used to calculate drag layer offsets, but the 2460 // dragVisualizeOffset in addition to the dragRect (the size) to position the outline. 2461 dragVisualizeOffset = new Point(-padding.get() / 2, padding.get() / 2); 2462 dragRect = new Rect(left, top, right, bottom); 2463 } else if (child instanceof FolderIcon) { 2464 int previewSize = grid.folderIconSizePx; 2465 dragVisualizeOffset = new Point(-padding.get() / 2, 2466 padding.get() / 2 - child.getPaddingTop()); 2467 dragRect = new Rect(0, child.getPaddingTop(), child.getWidth(), previewSize); 2468 } 2469 2470 // Clear the pressed state if necessary 2471 if (child instanceof BubbleTextView) { 2472 BubbleTextView icon = (BubbleTextView) child; 2473 icon.clearPressedBackground(); 2474 } 2475 2476 Object dragObject = child.getTag(); 2477 if (!(dragObject instanceof ItemInfo)) { 2478 String msg = "Drag started with a view that has no tag set. This " 2479 + "will cause a crash (issue 11627249) down the line. " 2480 + "View: " + child + " tag: " + child.getTag(); 2481 throw new IllegalStateException(msg); 2482 } 2483 2484 if (child.getParent() instanceof ShortcutAndWidgetContainer) { 2485 mDragSourceInternal = (ShortcutAndWidgetContainer) child.getParent(); 2486 } 2487 2488 DragView dv = mDragController.startDrag(b, dragLayerX, dragLayerY, source, 2489 (ItemInfo) dragObject, DragController.DRAG_ACTION_MOVE, dragVisualizeOffset, 2490 dragRect, scale, accessible); 2491 dv.setIntrinsicIconScaleFactor(source.getIntrinsicIconScaleFactor()); 2492 2493 b.recycle(); 2494 2495 if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) { 2496 mLauncher.enterSpringLoadedDragMode(); 2497 } 2498 } 2499 2500 public void beginExternalDragShared(View child, DragSource source) { 2501 DeviceProfile grid = mLauncher.getDeviceProfile(); 2502 int iconSize = grid.iconSizePx; 2503 2504 // Notify launcher of drag start 2505 mLauncher.onDragStarted(child); 2506 2507 // Compose a new drag bitmap that is of the icon size 2508 AtomicInteger padding = new AtomicInteger(DRAG_BITMAP_PADDING); 2509 final Bitmap tmpB = createDragBitmap(child, padding); 2510 Bitmap b = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888); 2511 Paint p = new Paint(); 2512 p.setFilterBitmap(true); 2513 mCanvas.setBitmap(b); 2514 mCanvas.drawBitmap(tmpB, new Rect(0, 0, tmpB.getWidth(), tmpB.getHeight()), 2515 new Rect(0, 0, iconSize, iconSize), p); 2516 mCanvas.setBitmap(null); 2517 2518 // Find the child's location on the screen 2519 int bmpWidth = tmpB.getWidth(); 2520 float iconScale = (float) bmpWidth / iconSize; 2521 float scale = mLauncher.getDragLayer().getLocationInDragLayer(child, mTempXY) * iconScale; 2522 int dragLayerX = Math.round(mTempXY[0] - (bmpWidth - scale * child.getWidth()) / 2); 2523 int dragLayerY = Math.round(mTempXY[1]); 2524 2525 // Note: The drag region is used to calculate drag layer offsets, but the 2526 // dragVisualizeOffset in addition to the dragRect (the size) to position the outline. 2527 Point dragVisualizeOffset = new Point(-padding.get() / 2, padding.get() / 2); 2528 Rect dragRect = new Rect(0, 0, iconSize, iconSize); 2529 2530 Object dragObject = child.getTag(); 2531 if (!(dragObject instanceof ItemInfo)) { 2532 String msg = "Drag started with a view that has no tag set. This " 2533 + "will cause a crash (issue 11627249) down the line. " 2534 + "View: " + child + " tag: " + child.getTag(); 2535 throw new IllegalStateException(msg); 2536 } 2537 2538 // Start the drag 2539 DragView dv = mDragController.startDrag(b, dragLayerX, dragLayerY, source, 2540 (ItemInfo) dragObject, DragController.DRAG_ACTION_MOVE, dragVisualizeOffset, 2541 dragRect, scale, false); 2542 dv.setIntrinsicIconScaleFactor(source.getIntrinsicIconScaleFactor()); 2543 2544 // Recycle temporary bitmaps 2545 tmpB.recycle(); 2546 2547 if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) { 2548 mLauncher.enterSpringLoadedDragMode(); 2549 } 2550 } 2551 2552 public boolean transitionStateShouldAllowDrop() { 2553 return ((!isSwitchingState() || mTransitionProgress > 0.5f) && 2554 (mState == State.NORMAL || mState == State.SPRING_LOADED)); 2555 } 2556 2557 /** 2558 * {@inheritDoc} 2559 */ 2560 public boolean acceptDrop(DragObject d) { 2561 // If it's an external drop (e.g. from All Apps), check if it should be accepted 2562 CellLayout dropTargetLayout = mDropToLayout; 2563 if (d.dragSource != this) { 2564 // Don't accept the drop if we're not over a screen at time of drop 2565 if (dropTargetLayout == null) { 2566 return false; 2567 } 2568 if (!transitionStateShouldAllowDrop()) return false; 2569 2570 mDragViewVisualCenter = d.getVisualCenter(mDragViewVisualCenter); 2571 2572 // We want the point to be mapped to the dragTarget. 2573 if (mLauncher.isHotseatLayout(dropTargetLayout)) { 2574 mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter); 2575 } else { 2576 mapPointFromSelfToChild(dropTargetLayout, mDragViewVisualCenter); 2577 } 2578 2579 int spanX = 1; 2580 int spanY = 1; 2581 if (mDragInfo != null) { 2582 final CellLayout.CellInfo dragCellInfo = mDragInfo; 2583 spanX = dragCellInfo.spanX; 2584 spanY = dragCellInfo.spanY; 2585 } else { 2586 spanX = d.dragInfo.spanX; 2587 spanY = d.dragInfo.spanY; 2588 } 2589 2590 int minSpanX = spanX; 2591 int minSpanY = spanY; 2592 if (d.dragInfo instanceof PendingAddWidgetInfo) { 2593 minSpanX = ((PendingAddWidgetInfo) d.dragInfo).minSpanX; 2594 minSpanY = ((PendingAddWidgetInfo) d.dragInfo).minSpanY; 2595 } 2596 2597 mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], 2598 (int) mDragViewVisualCenter[1], minSpanX, minSpanY, dropTargetLayout, 2599 mTargetCell); 2600 float distance = dropTargetLayout.getDistanceFromCell(mDragViewVisualCenter[0], 2601 mDragViewVisualCenter[1], mTargetCell); 2602 if (mCreateUserFolderOnDrop && willCreateUserFolder(d.dragInfo, 2603 dropTargetLayout, mTargetCell, distance, true)) { 2604 return true; 2605 } 2606 2607 if (mAddToExistingFolderOnDrop && willAddToExistingUserFolder(d.dragInfo, 2608 dropTargetLayout, mTargetCell, distance)) { 2609 return true; 2610 } 2611 2612 int[] resultSpan = new int[2]; 2613 mTargetCell = dropTargetLayout.performReorder((int) mDragViewVisualCenter[0], 2614 (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, 2615 null, mTargetCell, resultSpan, CellLayout.MODE_ACCEPT_DROP); 2616 boolean foundCell = mTargetCell[0] >= 0 && mTargetCell[1] >= 0; 2617 2618 // Don't accept the drop if there's no room for the item 2619 if (!foundCell) { 2620 // Don't show the message if we are dropping on the AllApps button and the hotseat 2621 // is full 2622 boolean isHotseat = mLauncher.isHotseatLayout(dropTargetLayout); 2623 if (mTargetCell != null && isHotseat && !FeatureFlags.NO_ALL_APPS_ICON) { 2624 Hotseat hotseat = mLauncher.getHotseat(); 2625 if (mLauncher.getDeviceProfile().inv.isAllAppsButtonRank( 2626 hotseat.getOrderInHotseat(mTargetCell[0], mTargetCell[1]))) { 2627 return false; 2628 } 2629 } 2630 2631 mLauncher.showOutOfSpaceMessage(isHotseat); 2632 return false; 2633 } 2634 } 2635 2636 long screenId = getIdForScreen(dropTargetLayout); 2637 if (screenId == EXTRA_EMPTY_SCREEN_ID) { 2638 commitExtraEmptyScreen(); 2639 } 2640 2641 return true; 2642 } 2643 2644 boolean willCreateUserFolder(ItemInfo info, CellLayout target, int[] targetCell, 2645 float distance, boolean considerTimeout) { 2646 if (distance > mMaxDistanceForFolderCreation) return false; 2647 View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); 2648 return willCreateUserFolder(info, dropOverView, considerTimeout); 2649 } 2650 2651 boolean willCreateUserFolder(ItemInfo info, View dropOverView, boolean considerTimeout) { 2652 if (dropOverView != null) { 2653 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) dropOverView.getLayoutParams(); 2654 if (lp.useTmpCoords && (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY)) { 2655 return false; 2656 } 2657 } 2658 2659 boolean hasntMoved = false; 2660 if (mDragInfo != null) { 2661 hasntMoved = dropOverView == mDragInfo.cell; 2662 } 2663 2664 if (dropOverView == null || hasntMoved || (considerTimeout && !mCreateUserFolderOnDrop)) { 2665 return false; 2666 } 2667 2668 boolean aboveShortcut = (dropOverView.getTag() instanceof ShortcutInfo); 2669 boolean willBecomeShortcut = 2670 (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || 2671 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT || 2672 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT); 2673 2674 return (aboveShortcut && willBecomeShortcut); 2675 } 2676 2677 boolean willAddToExistingUserFolder(ItemInfo dragInfo, CellLayout target, int[] targetCell, 2678 float distance) { 2679 if (distance > mMaxDistanceForFolderCreation) return false; 2680 View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); 2681 return willAddToExistingUserFolder(dragInfo, dropOverView); 2682 2683 } 2684 boolean willAddToExistingUserFolder(ItemInfo dragInfo, View dropOverView) { 2685 if (dropOverView != null) { 2686 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) dropOverView.getLayoutParams(); 2687 if (lp.useTmpCoords && (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY)) { 2688 return false; 2689 } 2690 } 2691 2692 if (dropOverView instanceof FolderIcon) { 2693 FolderIcon fi = (FolderIcon) dropOverView; 2694 if (fi.acceptDrop(dragInfo)) { 2695 return true; 2696 } 2697 } 2698 return false; 2699 } 2700 2701 boolean createUserFolderIfNecessary(View newView, long container, CellLayout target, 2702 int[] targetCell, float distance, boolean external, DragView dragView, 2703 Runnable postAnimationRunnable) { 2704 if (distance > mMaxDistanceForFolderCreation) return false; 2705 View v = target.getChildAt(targetCell[0], targetCell[1]); 2706 2707 boolean hasntMoved = false; 2708 if (mDragInfo != null) { 2709 CellLayout cellParent = getParentCellLayoutForView(mDragInfo.cell); 2710 hasntMoved = (mDragInfo.cellX == targetCell[0] && 2711 mDragInfo.cellY == targetCell[1]) && (cellParent == target); 2712 } 2713 2714 if (v == null || hasntMoved || !mCreateUserFolderOnDrop) return false; 2715 mCreateUserFolderOnDrop = false; 2716 final long screenId = getIdForScreen(target); 2717 2718 boolean aboveShortcut = (v.getTag() instanceof ShortcutInfo); 2719 boolean willBecomeShortcut = (newView.getTag() instanceof ShortcutInfo); 2720 2721 if (aboveShortcut && willBecomeShortcut) { 2722 ShortcutInfo sourceInfo = (ShortcutInfo) newView.getTag(); 2723 ShortcutInfo destInfo = (ShortcutInfo) v.getTag(); 2724 // if the drag started here, we need to remove it from the workspace 2725 if (!external) { 2726 getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell); 2727 } 2728 2729 Rect folderLocation = new Rect(); 2730 float scale = mLauncher.getDragLayer().getDescendantRectRelativeToSelf(v, folderLocation); 2731 target.removeView(v); 2732 2733 FolderIcon fi = 2734 mLauncher.addFolder(target, container, screenId, targetCell[0], targetCell[1]); 2735 destInfo.cellX = -1; 2736 destInfo.cellY = -1; 2737 sourceInfo.cellX = -1; 2738 sourceInfo.cellY = -1; 2739 2740 // If the dragView is null, we can't animate 2741 boolean animate = dragView != null; 2742 if (animate) { 2743 // In order to keep everything continuous, we hand off the currently rendered 2744 // folder background to the newly created icon. This preserves animation state. 2745 fi.setFolderBackground(mFolderCreateBg); 2746 mFolderCreateBg = new FolderIcon.PreviewBackground(); 2747 fi.performCreateAnimation(destInfo, v, sourceInfo, dragView, folderLocation, scale, 2748 postAnimationRunnable); 2749 } else { 2750 fi.addItem(destInfo); 2751 fi.addItem(sourceInfo); 2752 } 2753 return true; 2754 } 2755 return false; 2756 } 2757 2758 boolean addToExistingFolderIfNecessary(View newView, CellLayout target, int[] targetCell, 2759 float distance, DragObject d, boolean external) { 2760 if (distance > mMaxDistanceForFolderCreation) return false; 2761 2762 View dropOverView = target.getChildAt(targetCell[0], targetCell[1]); 2763 if (!mAddToExistingFolderOnDrop) return false; 2764 mAddToExistingFolderOnDrop = false; 2765 2766 if (dropOverView instanceof FolderIcon) { 2767 FolderIcon fi = (FolderIcon) dropOverView; 2768 if (fi.acceptDrop(d.dragInfo)) { 2769 fi.onDrop(d); 2770 2771 // if the drag started here, we need to remove it from the workspace 2772 if (!external) { 2773 getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell); 2774 } 2775 return true; 2776 } 2777 } 2778 return false; 2779 } 2780 2781 @Override 2782 public void prepareAccessibilityDrop() { } 2783 2784 public void onDrop(final DragObject d) { 2785 mDragViewVisualCenter = d.getVisualCenter(mDragViewVisualCenter); 2786 CellLayout dropTargetLayout = mDropToLayout; 2787 2788 // We want the point to be mapped to the dragTarget. 2789 if (dropTargetLayout != null) { 2790 if (mLauncher.isHotseatLayout(dropTargetLayout)) { 2791 mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter); 2792 } else { 2793 mapPointFromSelfToChild(dropTargetLayout, mDragViewVisualCenter); 2794 } 2795 } 2796 2797 int snapScreen = -1; 2798 boolean resizeOnDrop = false; 2799 if (d.dragSource != this) { 2800 final int[] touchXY = new int[] { (int) mDragViewVisualCenter[0], 2801 (int) mDragViewVisualCenter[1] }; 2802 onDropExternal(touchXY, d.dragInfo, dropTargetLayout, false, d); 2803 } else if (mDragInfo != null) { 2804 final View cell = mDragInfo.cell; 2805 2806 if (dropTargetLayout != null && !d.cancelled) { 2807 // Move internally 2808 boolean hasMovedLayouts = (getParentCellLayoutForView(cell) != dropTargetLayout); 2809 boolean hasMovedIntoHotseat = mLauncher.isHotseatLayout(dropTargetLayout); 2810 long container = hasMovedIntoHotseat ? 2811 LauncherSettings.Favorites.CONTAINER_HOTSEAT : 2812 LauncherSettings.Favorites.CONTAINER_DESKTOP; 2813 long screenId = (mTargetCell[0] < 0) ? 2814 mDragInfo.screenId : getIdForScreen(dropTargetLayout); 2815 int spanX = mDragInfo != null ? mDragInfo.spanX : 1; 2816 int spanY = mDragInfo != null ? mDragInfo.spanY : 1; 2817 // First we find the cell nearest to point at which the item is 2818 // dropped, without any consideration to whether there is an item there. 2819 2820 mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], (int) 2821 mDragViewVisualCenter[1], spanX, spanY, dropTargetLayout, mTargetCell); 2822 float distance = dropTargetLayout.getDistanceFromCell(mDragViewVisualCenter[0], 2823 mDragViewVisualCenter[1], mTargetCell); 2824 2825 // If the item being dropped is a shortcut and the nearest drop 2826 // cell also contains a shortcut, then create a folder with the two shortcuts. 2827 if (!mInScrollArea && createUserFolderIfNecessary(cell, container, 2828 dropTargetLayout, mTargetCell, distance, false, d.dragView, null)) { 2829 return; 2830 } 2831 2832 if (addToExistingFolderIfNecessary(cell, dropTargetLayout, mTargetCell, 2833 distance, d, false)) { 2834 return; 2835 } 2836 2837 // Aside from the special case where we're dropping a shortcut onto a shortcut, 2838 // we need to find the nearest cell location that is vacant 2839 ItemInfo item = d.dragInfo; 2840 int minSpanX = item.spanX; 2841 int minSpanY = item.spanY; 2842 if (item.minSpanX > 0 && item.minSpanY > 0) { 2843 minSpanX = item.minSpanX; 2844 minSpanY = item.minSpanY; 2845 } 2846 2847 int[] resultSpan = new int[2]; 2848 mTargetCell = dropTargetLayout.performReorder((int) mDragViewVisualCenter[0], 2849 (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, cell, 2850 mTargetCell, resultSpan, CellLayout.MODE_ON_DROP); 2851 2852 boolean foundCell = mTargetCell[0] >= 0 && mTargetCell[1] >= 0; 2853 2854 // if the widget resizes on drop 2855 if (foundCell && (cell instanceof AppWidgetHostView) && 2856 (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY)) { 2857 resizeOnDrop = true; 2858 item.spanX = resultSpan[0]; 2859 item.spanY = resultSpan[1]; 2860 AppWidgetHostView awhv = (AppWidgetHostView) cell; 2861 AppWidgetResizeFrame.updateWidgetSizeRanges(awhv, mLauncher, resultSpan[0], 2862 resultSpan[1]); 2863 } 2864 2865 if (getScreenIdForPageIndex(mCurrentPage) != screenId && !hasMovedIntoHotseat) { 2866 snapScreen = getPageIndexForScreenId(screenId); 2867 snapToPage(snapScreen); 2868 } 2869 2870 if (foundCell) { 2871 final ItemInfo info = (ItemInfo) cell.getTag(); 2872 if (hasMovedLayouts) { 2873 // Reparent the view 2874 CellLayout parentCell = getParentCellLayoutForView(cell); 2875 if (parentCell != null) { 2876 parentCell.removeView(cell); 2877 } else if (ProviderConfig.IS_DOGFOOD_BUILD) { 2878 throw new NullPointerException("mDragInfo.cell has null parent"); 2879 } 2880 addInScreen(cell, container, screenId, mTargetCell[0], mTargetCell[1], 2881 info.spanX, info.spanY); 2882 } 2883 2884 // update the item's position after drop 2885 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams(); 2886 lp.cellX = lp.tmpCellX = mTargetCell[0]; 2887 lp.cellY = lp.tmpCellY = mTargetCell[1]; 2888 lp.cellHSpan = item.spanX; 2889 lp.cellVSpan = item.spanY; 2890 lp.isLockedToGrid = true; 2891 2892 if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT && 2893 cell instanceof LauncherAppWidgetHostView) { 2894 final CellLayout cellLayout = dropTargetLayout; 2895 // We post this call so that the widget has a chance to be placed 2896 // in its final location 2897 2898 final LauncherAppWidgetHostView hostView = (LauncherAppWidgetHostView) cell; 2899 AppWidgetProviderInfo pInfo = hostView.getAppWidgetInfo(); 2900 if (pInfo != null && pInfo.resizeMode != AppWidgetProviderInfo.RESIZE_NONE 2901 && !d.accessibleDrag) { 2902 mDelayedResizeRunnable = new Runnable() { 2903 public void run() { 2904 if (!isPageMoving() && !mIsSwitchingState) { 2905 DragLayer dragLayer = mLauncher.getDragLayer(); 2906 dragLayer.addResizeFrame(info, hostView, cellLayout); 2907 } 2908 } 2909 }; 2910 } 2911 } 2912 2913 LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId, lp.cellX, 2914 lp.cellY, item.spanX, item.spanY); 2915 } else { 2916 // If we can't find a drop location, we return the item to its original position 2917 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams(); 2918 mTargetCell[0] = lp.cellX; 2919 mTargetCell[1] = lp.cellY; 2920 CellLayout layout = (CellLayout) cell.getParent().getParent(); 2921 layout.markCellsAsOccupiedForView(cell); 2922 } 2923 } 2924 2925 final CellLayout parent = (CellLayout) cell.getParent().getParent(); 2926 // Prepare it to be animated into its new position 2927 // This must be called after the view has been re-parented 2928 final Runnable onCompleteRunnable = new Runnable() { 2929 @Override 2930 public void run() { 2931 mAnimatingViewIntoPlace = false; 2932 updateChildrenLayersEnabled(false); 2933 } 2934 }; 2935 mAnimatingViewIntoPlace = true; 2936 if (d.dragView.hasDrawn()) { 2937 final ItemInfo info = (ItemInfo) cell.getTag(); 2938 boolean isWidget = info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET 2939 || info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET; 2940 if (isWidget) { 2941 int animationType = resizeOnDrop ? ANIMATE_INTO_POSITION_AND_RESIZE : 2942 ANIMATE_INTO_POSITION_AND_DISAPPEAR; 2943 animateWidgetDrop(info, parent, d.dragView, 2944 onCompleteRunnable, animationType, cell, false); 2945 } else { 2946 int duration = snapScreen < 0 ? -1 : ADJACENT_SCREEN_DROP_DURATION; 2947 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, cell, duration, 2948 onCompleteRunnable, this); 2949 } 2950 } else { 2951 d.deferDragViewCleanupPostAnimation = false; 2952 cell.setVisibility(VISIBLE); 2953 } 2954 parent.onDropChild(cell); 2955 } 2956 } 2957 2958 /** 2959 * Computes the area relative to dragLayer which is used to display a page. 2960 */ 2961 public void getPageAreaRelativeToDragLayer(Rect outArea) { 2962 CellLayout child = (CellLayout) getChildAt(getNextPage()); 2963 if (child == null) { 2964 return; 2965 } 2966 ShortcutAndWidgetContainer boundingLayout = child.getShortcutsAndWidgets(); 2967 2968 // Use the absolute left instead of the child left, as we want the visible area 2969 // irrespective of the visible child. Since the view can only scroll horizontally, the 2970 // top position is not affected. 2971 mTempXY[0] = getViewportOffsetX() + getPaddingLeft() + boundingLayout.getLeft(); 2972 mTempXY[1] = child.getTop() + boundingLayout.getTop(); 2973 2974 float scale = mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempXY); 2975 outArea.set(mTempXY[0], mTempXY[1], 2976 (int) (mTempXY[0] + scale * boundingLayout.getMeasuredWidth()), 2977 (int) (mTempXY[1] + scale * boundingLayout.getMeasuredHeight())); 2978 } 2979 2980 @Override 2981 public void onDragEnter(DragObject d) { 2982 if (ENFORCE_DRAG_EVENT_ORDER) { 2983 enfoceDragParity("onDragEnter", 1, 1); 2984 } 2985 2986 mCreateUserFolderOnDrop = false; 2987 mAddToExistingFolderOnDrop = false; 2988 2989 mDropToLayout = null; 2990 CellLayout layout = getCurrentDropLayout(); 2991 setCurrentDropLayout(layout); 2992 setCurrentDragOverlappingLayout(layout); 2993 2994 if (!workspaceInModalState() && FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) { 2995 mLauncher.getDragLayer().showPageHints(); 2996 } 2997 } 2998 2999 @Override 3000 public void onDragExit(DragObject d) { 3001 if (ENFORCE_DRAG_EVENT_ORDER) { 3002 enfoceDragParity("onDragExit", -1, 0); 3003 } 3004 3005 // Here we store the final page that will be dropped to, if the workspace in fact 3006 // receives the drop 3007 if (mInScrollArea) { 3008 if (isPageMoving()) { 3009 // If the user drops while the page is scrolling, we should use that page as the 3010 // destination instead of the page that is being hovered over. 3011 mDropToLayout = (CellLayout) getPageAt(getNextPage()); 3012 } else { 3013 mDropToLayout = mDragOverlappingLayout; 3014 } 3015 } else { 3016 mDropToLayout = mDragTargetLayout; 3017 } 3018 3019 if (mDragMode == DRAG_MODE_CREATE_FOLDER) { 3020 mCreateUserFolderOnDrop = true; 3021 } else if (mDragMode == DRAG_MODE_ADD_TO_FOLDER) { 3022 mAddToExistingFolderOnDrop = true; 3023 } 3024 3025 // Reset the scroll area and previous drag target 3026 onResetScrollArea(); 3027 setCurrentDropLayout(null); 3028 setCurrentDragOverlappingLayout(null); 3029 3030 mSpringLoadedDragController.cancel(); 3031 3032 mLauncher.getDragLayer().hidePageHints(); 3033 } 3034 3035 private void enfoceDragParity(String event, int update, int expectedValue) { 3036 enfoceDragParity(this, event, update, expectedValue); 3037 for (int i = 0; i < getChildCount(); i++) { 3038 enfoceDragParity(getChildAt(i), event, update, expectedValue); 3039 } 3040 } 3041 3042 private void enfoceDragParity(View v, String event, int update, int expectedValue) { 3043 Object tag = v.getTag(R.id.drag_event_parity); 3044 int value = tag == null ? 0 : (Integer) tag; 3045 value += update; 3046 v.setTag(R.id.drag_event_parity, value); 3047 3048 if (value != expectedValue) { 3049 Log.e(TAG, event + ": Drag contract violated: " + value); 3050 } 3051 } 3052 3053 void setCurrentDropLayout(CellLayout layout) { 3054 if (mDragTargetLayout != null) { 3055 mDragTargetLayout.revertTempState(); 3056 mDragTargetLayout.onDragExit(); 3057 } 3058 mDragTargetLayout = layout; 3059 if (mDragTargetLayout != null) { 3060 mDragTargetLayout.onDragEnter(); 3061 } 3062 cleanupReorder(true); 3063 cleanupFolderCreation(); 3064 setCurrentDropOverCell(-1, -1); 3065 } 3066 3067 void setCurrentDragOverlappingLayout(CellLayout layout) { 3068 if (mDragOverlappingLayout != null) { 3069 mDragOverlappingLayout.setIsDragOverlapping(false); 3070 } 3071 mDragOverlappingLayout = layout; 3072 if (mDragOverlappingLayout != null) { 3073 mDragOverlappingLayout.setIsDragOverlapping(true); 3074 } 3075 // Invalidating the scrim will also force this CellLayout 3076 // to be invalidated so that it is highlighted if necessary. 3077 mLauncher.getDragLayer().invalidateScrim(); 3078 } 3079 3080 public CellLayout getCurrentDragOverlappingLayout() { 3081 return mDragOverlappingLayout; 3082 } 3083 3084 void setCurrentDropOverCell(int x, int y) { 3085 if (x != mDragOverX || y != mDragOverY) { 3086 mDragOverX = x; 3087 mDragOverY = y; 3088 setDragMode(DRAG_MODE_NONE); 3089 } 3090 } 3091 3092 void setDragMode(int dragMode) { 3093 if (dragMode != mDragMode) { 3094 if (dragMode == DRAG_MODE_NONE) { 3095 cleanupAddToFolder(); 3096 // We don't want to cancel the re-order alarm every time the target cell changes 3097 // as this feels to slow / unresponsive. 3098 cleanupReorder(false); 3099 cleanupFolderCreation(); 3100 } else if (dragMode == DRAG_MODE_ADD_TO_FOLDER) { 3101 cleanupReorder(true); 3102 cleanupFolderCreation(); 3103 } else if (dragMode == DRAG_MODE_CREATE_FOLDER) { 3104 cleanupAddToFolder(); 3105 cleanupReorder(true); 3106 } else if (dragMode == DRAG_MODE_REORDER) { 3107 cleanupAddToFolder(); 3108 cleanupFolderCreation(); 3109 } 3110 mDragMode = dragMode; 3111 } 3112 } 3113 3114 private void cleanupFolderCreation() { 3115 if (mFolderCreateBg != null) { 3116 mFolderCreateBg.animateToRest(); 3117 } 3118 mFolderCreationAlarm.setOnAlarmListener(null); 3119 mFolderCreationAlarm.cancelAlarm(); 3120 } 3121 3122 private void cleanupAddToFolder() { 3123 if (mDragOverFolderIcon != null) { 3124 mDragOverFolderIcon.onDragExit(null); 3125 mDragOverFolderIcon = null; 3126 } 3127 } 3128 3129 private void cleanupReorder(boolean cancelAlarm) { 3130 // Any pending reorders are canceled 3131 if (cancelAlarm) { 3132 mReorderAlarm.cancelAlarm(); 3133 } 3134 mLastReorderX = -1; 3135 mLastReorderY = -1; 3136 } 3137 3138 /* 3139 * 3140 * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's 3141 * coordinate space. The argument xy is modified with the return result. 3142 */ 3143 void mapPointFromSelfToChild(View v, float[] xy) { 3144 xy[0] = xy[0] - v.getLeft(); 3145 xy[1] = xy[1] - v.getTop(); 3146 } 3147 3148 boolean isPointInSelfOverHotseat(int x, int y) { 3149 mTempXY[0] = x; 3150 mTempXY[1] = y; 3151 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempXY, true); 3152 View hotseat = mLauncher.getHotseat(); 3153 return mTempXY[0] >= hotseat.getLeft() && 3154 mTempXY[0] <= hotseat.getRight() && 3155 mTempXY[1] >= hotseat.getTop() && 3156 mTempXY[1] <= hotseat.getBottom(); 3157 } 3158 3159 void mapPointFromSelfToHotseatLayout(Hotseat hotseat, float[] xy) { 3160 mTempXY[0] = (int) xy[0]; 3161 mTempXY[1] = (int) xy[1]; 3162 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempXY, true); 3163 mLauncher.getDragLayer().mapCoordInSelfToDescendent(hotseat.getLayout(), mTempXY); 3164 3165 xy[0] = mTempXY[0]; 3166 xy[1] = mTempXY[1]; 3167 } 3168 3169 /* 3170 * 3171 * Convert the 2D coordinate xy from this CellLayout's coordinate space to 3172 * the parent View's coordinate space. The argument xy is modified with the return result. 3173 * 3174 */ 3175 void mapPointFromChildToSelf(View v, float[] xy) { 3176 xy[0] += v.getLeft(); 3177 xy[1] += v.getTop(); 3178 } 3179 3180 static private float squaredDistance(float[] point1, float[] point2) { 3181 float distanceX = point1[0] - point2[0]; 3182 float distanceY = point2[1] - point2[1]; 3183 return distanceX * distanceX + distanceY * distanceY; 3184 } 3185 3186 /* 3187 * 3188 * This method returns the CellLayout that is currently being dragged to. In order to drag 3189 * to a CellLayout, either the touch point must be directly over the CellLayout, or as a second 3190 * strategy, we see if the dragView is overlapping any CellLayout and choose the closest one 3191 * 3192 * Return null if no CellLayout is currently being dragged over 3193 * 3194 */ 3195 private CellLayout findMatchingPageForDragOver( 3196 DragView dragView, float originX, float originY, boolean exact) { 3197 // We loop through all the screens (ie CellLayouts) and see which ones overlap 3198 // with the item being dragged and then choose the one that's closest to the touch point 3199 final int screenCount = getChildCount(); 3200 CellLayout bestMatchingScreen = null; 3201 float smallestDistSoFar = Float.MAX_VALUE; 3202 3203 for (int i = 0; i < screenCount; i++) { 3204 // The custom content screen is not a valid drag over option 3205 if (mScreenOrder.get(i) == CUSTOM_CONTENT_SCREEN_ID) { 3206 continue; 3207 } 3208 3209 CellLayout cl = (CellLayout) getChildAt(i); 3210 3211 final float[] touchXy = {originX, originY}; 3212 mapPointFromSelfToChild(cl, touchXy); 3213 3214 if (touchXy[0] >= 0 && touchXy[0] <= cl.getWidth() && 3215 touchXy[1] >= 0 && touchXy[1] <= cl.getHeight()) { 3216 return cl; 3217 } 3218 3219 if (!exact) { 3220 // Get the center of the cell layout in screen coordinates 3221 final float[] cellLayoutCenter = mTempCellLayoutCenterCoordinates; 3222 cellLayoutCenter[0] = cl.getWidth()/2; 3223 cellLayoutCenter[1] = cl.getHeight()/2; 3224 mapPointFromChildToSelf(cl, cellLayoutCenter); 3225 3226 touchXy[0] = originX; 3227 touchXy[1] = originY; 3228 3229 // Calculate the distance between the center of the CellLayout 3230 // and the touch point 3231 float dist = squaredDistance(touchXy, cellLayoutCenter); 3232 3233 if (dist < smallestDistSoFar) { 3234 smallestDistSoFar = dist; 3235 bestMatchingScreen = cl; 3236 } 3237 } 3238 } 3239 return bestMatchingScreen; 3240 } 3241 3242 private boolean isDragWidget(DragObject d) { 3243 return (d.dragInfo instanceof LauncherAppWidgetInfo || 3244 d.dragInfo instanceof PendingAddWidgetInfo); 3245 } 3246 3247 public void onDragOver(DragObject d) { 3248 // Skip drag over events while we are dragging over side pages 3249 if (mInScrollArea || !transitionStateShouldAllowDrop()) return; 3250 3251 CellLayout layout = null; 3252 ItemInfo item = d.dragInfo; 3253 if (item == null) { 3254 if (ProviderConfig.IS_DOGFOOD_BUILD) { 3255 throw new NullPointerException("DragObject has null info"); 3256 } 3257 return; 3258 } 3259 3260 // Ensure that we have proper spans for the item that we are dropping 3261 if (item.spanX < 0 || item.spanY < 0) throw new RuntimeException("Improper spans found"); 3262 mDragViewVisualCenter = d.getVisualCenter(mDragViewVisualCenter); 3263 3264 final View child = (mDragInfo == null) ? null : mDragInfo.cell; 3265 // Identify whether we have dragged over a side page 3266 if (workspaceInModalState()) { 3267 if (mLauncher.getHotseat() != null && !isDragWidget(d)) { 3268 if (isPointInSelfOverHotseat(d.x, d.y)) { 3269 layout = mLauncher.getHotseat().getLayout(); 3270 } 3271 } 3272 if (layout == null) { 3273 layout = findMatchingPageForDragOver(d.dragView, d.x, d.y, false); 3274 } 3275 if (layout != mDragTargetLayout) { 3276 setCurrentDropLayout(layout); 3277 setCurrentDragOverlappingLayout(layout); 3278 3279 boolean isInSpringLoadedMode = (mState == State.SPRING_LOADED); 3280 if (isInSpringLoadedMode) { 3281 if (mLauncher.isHotseatLayout(layout)) { 3282 mSpringLoadedDragController.cancel(); 3283 } else { 3284 mSpringLoadedDragController.setAlarm(mDragTargetLayout); 3285 } 3286 } 3287 } 3288 } else { 3289 // Test to see if we are over the hotseat otherwise just use the current page 3290 if (mLauncher.getHotseat() != null && !isDragWidget(d)) { 3291 if (isPointInSelfOverHotseat(d.x, d.y)) { 3292 layout = mLauncher.getHotseat().getLayout(); 3293 } 3294 } 3295 if (layout == null) { 3296 layout = getCurrentDropLayout(); 3297 } 3298 if (layout != mDragTargetLayout) { 3299 setCurrentDropLayout(layout); 3300 setCurrentDragOverlappingLayout(layout); 3301 } 3302 } 3303 3304 // Handle the drag over 3305 if (mDragTargetLayout != null) { 3306 // We want the point to be mapped to the dragTarget. 3307 if (mLauncher.isHotseatLayout(mDragTargetLayout)) { 3308 mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter); 3309 } else { 3310 mapPointFromSelfToChild(mDragTargetLayout, mDragViewVisualCenter); 3311 } 3312 3313 int minSpanX = item.spanX; 3314 int minSpanY = item.spanY; 3315 if (item.minSpanX > 0 && item.minSpanY > 0) { 3316 minSpanX = item.minSpanX; 3317 minSpanY = item.minSpanY; 3318 } 3319 3320 mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], 3321 (int) mDragViewVisualCenter[1], minSpanX, minSpanY, 3322 mDragTargetLayout, mTargetCell); 3323 int reorderX = mTargetCell[0]; 3324 int reorderY = mTargetCell[1]; 3325 3326 setCurrentDropOverCell(mTargetCell[0], mTargetCell[1]); 3327 3328 float targetCellDistance = mDragTargetLayout.getDistanceFromCell( 3329 mDragViewVisualCenter[0], mDragViewVisualCenter[1], mTargetCell); 3330 3331 manageFolderFeedback(mDragTargetLayout, mTargetCell, targetCellDistance, d); 3332 3333 boolean nearestDropOccupied = mDragTargetLayout.isNearestDropLocationOccupied((int) 3334 mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], item.spanX, 3335 item.spanY, child, mTargetCell); 3336 3337 if (!nearestDropOccupied) { 3338 mDragTargetLayout.visualizeDropLocation(child, mDragOutline, 3339 mTargetCell[0], mTargetCell[1], item.spanX, item.spanY, false, d); 3340 } else if ((mDragMode == DRAG_MODE_NONE || mDragMode == DRAG_MODE_REORDER) 3341 && !mReorderAlarm.alarmPending() && (mLastReorderX != reorderX || 3342 mLastReorderY != reorderY)) { 3343 3344 int[] resultSpan = new int[2]; 3345 mDragTargetLayout.performReorder((int) mDragViewVisualCenter[0], 3346 (int) mDragViewVisualCenter[1], minSpanX, minSpanY, item.spanX, item.spanY, 3347 child, mTargetCell, resultSpan, CellLayout.MODE_SHOW_REORDER_HINT); 3348 3349 // Otherwise, if we aren't adding to or creating a folder and there's no pending 3350 // reorder, then we schedule a reorder 3351 ReorderAlarmListener listener = new ReorderAlarmListener(mDragViewVisualCenter, 3352 minSpanX, minSpanY, item.spanX, item.spanY, d, child); 3353 mReorderAlarm.setOnAlarmListener(listener); 3354 mReorderAlarm.setAlarm(REORDER_TIMEOUT); 3355 } 3356 3357 if (mDragMode == DRAG_MODE_CREATE_FOLDER || mDragMode == DRAG_MODE_ADD_TO_FOLDER || 3358 !nearestDropOccupied) { 3359 if (mDragTargetLayout != null) { 3360 mDragTargetLayout.revertTempState(); 3361 } 3362 } 3363 } 3364 } 3365 3366 private void manageFolderFeedback(CellLayout targetLayout, 3367 int[] targetCell, float distance, DragObject dragObject) { 3368 if (distance > mMaxDistanceForFolderCreation) return; 3369 3370 final View dragOverView = mDragTargetLayout.getChildAt(mTargetCell[0], mTargetCell[1]); 3371 ItemInfo info = dragObject.dragInfo; 3372 boolean userFolderPending = willCreateUserFolder(info, dragOverView, false); 3373 if (mDragMode == DRAG_MODE_NONE && userFolderPending && 3374 !mFolderCreationAlarm.alarmPending()) { 3375 3376 FolderCreationAlarmListener listener = new 3377 FolderCreationAlarmListener(targetLayout, targetCell[0], targetCell[1]); 3378 3379 if (!dragObject.accessibleDrag) { 3380 mFolderCreationAlarm.setOnAlarmListener(listener); 3381 mFolderCreationAlarm.setAlarm(FOLDER_CREATION_TIMEOUT); 3382 } else { 3383 listener.onAlarm(mFolderCreationAlarm); 3384 } 3385 3386 if (dragObject.stateAnnouncer != null) { 3387 dragObject.stateAnnouncer.announce(WorkspaceAccessibilityHelper 3388 .getDescriptionForDropOver(dragOverView, getContext())); 3389 } 3390 return; 3391 } 3392 3393 boolean willAddToFolder = willAddToExistingUserFolder(info, dragOverView); 3394 if (willAddToFolder && mDragMode == DRAG_MODE_NONE) { 3395 mDragOverFolderIcon = ((FolderIcon) dragOverView); 3396 mDragOverFolderIcon.onDragEnter(info); 3397 if (targetLayout != null) { 3398 targetLayout.clearDragOutlines(); 3399 } 3400 setDragMode(DRAG_MODE_ADD_TO_FOLDER); 3401 3402 if (dragObject.stateAnnouncer != null) { 3403 dragObject.stateAnnouncer.announce(WorkspaceAccessibilityHelper 3404 .getDescriptionForDropOver(dragOverView, getContext())); 3405 } 3406 return; 3407 } 3408 3409 if (mDragMode == DRAG_MODE_ADD_TO_FOLDER && !willAddToFolder) { 3410 setDragMode(DRAG_MODE_NONE); 3411 } 3412 if (mDragMode == DRAG_MODE_CREATE_FOLDER && !userFolderPending) { 3413 setDragMode(DRAG_MODE_NONE); 3414 } 3415 } 3416 3417 class FolderCreationAlarmListener implements OnAlarmListener { 3418 CellLayout layout; 3419 int cellX; 3420 int cellY; 3421 3422 FolderIcon.PreviewBackground bg = new FolderIcon.PreviewBackground(); 3423 3424 public FolderCreationAlarmListener(CellLayout layout, int cellX, int cellY) { 3425 this.layout = layout; 3426 this.cellX = cellX; 3427 this.cellY = cellY; 3428 3429 DeviceProfile grid = mLauncher.getDeviceProfile(); 3430 BubbleTextView cell = (BubbleTextView) layout.getChildAt(cellX, cellY); 3431 3432 bg.setup(getResources().getDisplayMetrics(), grid, null, 3433 cell.getMeasuredWidth(), cell.getPaddingTop()); 3434 3435 // The full preview background should appear behind the icon 3436 bg.isClipping = false; 3437 } 3438 3439 public void onAlarm(Alarm alarm) { 3440 mFolderCreateBg = bg; 3441 mFolderCreateBg.animateToAccept(layout, cellX, cellY); 3442 layout.clearDragOutlines(); 3443 setDragMode(DRAG_MODE_CREATE_FOLDER); 3444 } 3445 } 3446 3447 class ReorderAlarmListener implements OnAlarmListener { 3448 float[] dragViewCenter; 3449 int minSpanX, minSpanY, spanX, spanY; 3450 DragObject dragObject; 3451 View child; 3452 3453 public ReorderAlarmListener(float[] dragViewCenter, int minSpanX, int minSpanY, int spanX, 3454 int spanY, DragObject dragObject, View child) { 3455 this.dragViewCenter = dragViewCenter; 3456 this.minSpanX = minSpanX; 3457 this.minSpanY = minSpanY; 3458 this.spanX = spanX; 3459 this.spanY = spanY; 3460 this.child = child; 3461 this.dragObject = dragObject; 3462 } 3463 3464 public void onAlarm(Alarm alarm) { 3465 int[] resultSpan = new int[2]; 3466 mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], 3467 (int) mDragViewVisualCenter[1], minSpanX, minSpanY, mDragTargetLayout, 3468 mTargetCell); 3469 mLastReorderX = mTargetCell[0]; 3470 mLastReorderY = mTargetCell[1]; 3471 3472 mTargetCell = mDragTargetLayout.performReorder((int) mDragViewVisualCenter[0], 3473 (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, 3474 child, mTargetCell, resultSpan, CellLayout.MODE_DRAG_OVER); 3475 3476 if (mTargetCell[0] < 0 || mTargetCell[1] < 0) { 3477 mDragTargetLayout.revertTempState(); 3478 } else { 3479 setDragMode(DRAG_MODE_REORDER); 3480 } 3481 3482 boolean resize = resultSpan[0] != spanX || resultSpan[1] != spanY; 3483 mDragTargetLayout.visualizeDropLocation(child, mDragOutline, 3484 mTargetCell[0], mTargetCell[1], resultSpan[0], resultSpan[1], resize, dragObject); 3485 } 3486 } 3487 3488 @Override 3489 public void getHitRectRelativeToDragLayer(Rect outRect) { 3490 // We want the workspace to have the whole area of the display (it will find the correct 3491 // cell layout to drop to in the existing drag/drop logic. 3492 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(this, outRect); 3493 } 3494 3495 /** 3496 * Drop an item that didn't originate on one of the workspace screens. 3497 * It may have come from Launcher (e.g. from all apps or customize), or it may have 3498 * come from another app altogether. 3499 * 3500 * NOTE: This can also be called when we are outside of a drag event, when we want 3501 * to add an item to one of the workspace screens. 3502 */ 3503 private void onDropExternal(final int[] touchXY, final ItemInfo dragInfo, 3504 final CellLayout cellLayout, boolean insertAtFirst, DragObject d) { 3505 final Runnable exitSpringLoadedRunnable = new Runnable() { 3506 @Override 3507 public void run() { 3508 mLauncher.exitSpringLoadedDragModeDelayed(true, 3509 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null); 3510 } 3511 }; 3512 3513 ItemInfo info = dragInfo; 3514 int spanX = info.spanX; 3515 int spanY = info.spanY; 3516 if (mDragInfo != null) { 3517 spanX = mDragInfo.spanX; 3518 spanY = mDragInfo.spanY; 3519 } 3520 3521 final long container = mLauncher.isHotseatLayout(cellLayout) ? 3522 LauncherSettings.Favorites.CONTAINER_HOTSEAT : 3523 LauncherSettings.Favorites.CONTAINER_DESKTOP; 3524 final long screenId = getIdForScreen(cellLayout); 3525 if (!mLauncher.isHotseatLayout(cellLayout) 3526 && screenId != getScreenIdForPageIndex(mCurrentPage) 3527 && mState != State.SPRING_LOADED) { 3528 snapToScreenId(screenId, null); 3529 } 3530 3531 if (info instanceof PendingAddItemInfo) { 3532 final PendingAddItemInfo pendingInfo = (PendingAddItemInfo) dragInfo; 3533 3534 boolean findNearestVacantCell = true; 3535 if (pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) { 3536 mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY, 3537 cellLayout, mTargetCell); 3538 float distance = cellLayout.getDistanceFromCell(mDragViewVisualCenter[0], 3539 mDragViewVisualCenter[1], mTargetCell); 3540 if (willCreateUserFolder(d.dragInfo, cellLayout, mTargetCell, distance, true) 3541 || willAddToExistingUserFolder( 3542 d.dragInfo, cellLayout, mTargetCell, distance)) { 3543 findNearestVacantCell = false; 3544 } 3545 } 3546 3547 final ItemInfo item = d.dragInfo; 3548 boolean updateWidgetSize = false; 3549 if (findNearestVacantCell) { 3550 int minSpanX = item.spanX; 3551 int minSpanY = item.spanY; 3552 if (item.minSpanX > 0 && item.minSpanY > 0) { 3553 minSpanX = item.minSpanX; 3554 minSpanY = item.minSpanY; 3555 } 3556 int[] resultSpan = new int[2]; 3557 mTargetCell = cellLayout.performReorder((int) mDragViewVisualCenter[0], 3558 (int) mDragViewVisualCenter[1], minSpanX, minSpanY, info.spanX, info.spanY, 3559 null, mTargetCell, resultSpan, CellLayout.MODE_ON_DROP_EXTERNAL); 3560 3561 if (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY) { 3562 updateWidgetSize = true; 3563 } 3564 item.spanX = resultSpan[0]; 3565 item.spanY = resultSpan[1]; 3566 } 3567 3568 Runnable onAnimationCompleteRunnable = new Runnable() { 3569 @Override 3570 public void run() { 3571 // Normally removeExtraEmptyScreen is called in Workspace#onDragEnd, but when 3572 // adding an item that may not be dropped right away (due to a config activity) 3573 // we defer the removal until the activity returns. 3574 deferRemoveExtraEmptyScreen(); 3575 3576 // When dragging and dropping from customization tray, we deal with creating 3577 // widgets/shortcuts/folders in a slightly different way 3578 mLauncher.addPendingItem(pendingInfo, container, screenId, mTargetCell, 3579 item.spanX, item.spanY); 3580 } 3581 }; 3582 boolean isWidget = pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET 3583 || pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET; 3584 3585 AppWidgetHostView finalView = isWidget ? 3586 ((PendingAddWidgetInfo) pendingInfo).boundWidget : null; 3587 3588 if (finalView != null && updateWidgetSize) { 3589 AppWidgetResizeFrame.updateWidgetSizeRanges(finalView, mLauncher, item.spanX, 3590 item.spanY); 3591 } 3592 3593 int animationStyle = ANIMATE_INTO_POSITION_AND_DISAPPEAR; 3594 if (isWidget && ((PendingAddWidgetInfo) pendingInfo).info != null && 3595 ((PendingAddWidgetInfo) pendingInfo).info.configure != null) { 3596 animationStyle = ANIMATE_INTO_POSITION_AND_REMAIN; 3597 } 3598 animateWidgetDrop(info, cellLayout, d.dragView, onAnimationCompleteRunnable, 3599 animationStyle, finalView, true); 3600 } else { 3601 // This is for other drag/drop cases, like dragging from All Apps 3602 View view = null; 3603 3604 switch (info.itemType) { 3605 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: 3606 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: 3607 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: 3608 if (info.container == NO_ID && info instanceof AppInfo) { 3609 // Came from all apps -- make a copy 3610 info = ((AppInfo) info).makeShortcut(); 3611 } 3612 view = mLauncher.createShortcut(cellLayout, (ShortcutInfo) info); 3613 break; 3614 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: 3615 view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher, cellLayout, 3616 (FolderInfo) info, mIconCache); 3617 break; 3618 default: 3619 throw new IllegalStateException("Unknown item type: " + info.itemType); 3620 } 3621 3622 // First we find the cell nearest to point at which the item is 3623 // dropped, without any consideration to whether there is an item there. 3624 if (touchXY != null) { 3625 mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY, 3626 cellLayout, mTargetCell); 3627 float distance = cellLayout.getDistanceFromCell(mDragViewVisualCenter[0], 3628 mDragViewVisualCenter[1], mTargetCell); 3629 d.postAnimationRunnable = exitSpringLoadedRunnable; 3630 if (createUserFolderIfNecessary(view, container, cellLayout, mTargetCell, distance, 3631 true, d.dragView, d.postAnimationRunnable)) { 3632 return; 3633 } 3634 if (addToExistingFolderIfNecessary(view, cellLayout, mTargetCell, distance, d, 3635 true)) { 3636 return; 3637 } 3638 } 3639 3640 if (touchXY != null) { 3641 // when dragging and dropping, just find the closest free spot 3642 mTargetCell = cellLayout.performReorder((int) mDragViewVisualCenter[0], 3643 (int) mDragViewVisualCenter[1], 1, 1, 1, 1, 3644 null, mTargetCell, null, CellLayout.MODE_ON_DROP_EXTERNAL); 3645 } else { 3646 cellLayout.findCellForSpan(mTargetCell, 1, 1); 3647 } 3648 // Add the item to DB before adding to screen ensures that the container and other 3649 // values of the info is properly updated. 3650 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screenId, 3651 mTargetCell[0], mTargetCell[1]); 3652 3653 addInScreen(view, container, screenId, mTargetCell[0], mTargetCell[1], info.spanX, 3654 info.spanY, insertAtFirst); 3655 cellLayout.onDropChild(view); 3656 cellLayout.getShortcutsAndWidgets().measureChild(view); 3657 3658 if (d.dragView != null) { 3659 // We wrap the animation call in the temporary set and reset of the current 3660 // cellLayout to its final transform -- this means we animate the drag view to 3661 // the correct final location. 3662 setFinalTransitionTransform(cellLayout); 3663 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, view, 3664 exitSpringLoadedRunnable, this); 3665 resetTransitionTransform(cellLayout); 3666 } 3667 } 3668 } 3669 3670 public Bitmap createWidgetBitmap(ItemInfo widgetInfo, View layout) { 3671 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(widgetInfo, false); 3672 int visibility = layout.getVisibility(); 3673 layout.setVisibility(VISIBLE); 3674 3675 int width = MeasureSpec.makeMeasureSpec(unScaledSize[0], MeasureSpec.EXACTLY); 3676 int height = MeasureSpec.makeMeasureSpec(unScaledSize[1], MeasureSpec.EXACTLY); 3677 Bitmap b = Bitmap.createBitmap(unScaledSize[0], unScaledSize[1], 3678 Bitmap.Config.ARGB_8888); 3679 mCanvas.setBitmap(b); 3680 3681 layout.measure(width, height); 3682 layout.layout(0, 0, unScaledSize[0], unScaledSize[1]); 3683 layout.draw(mCanvas); 3684 mCanvas.setBitmap(null); 3685 layout.setVisibility(visibility); 3686 return b; 3687 } 3688 3689 private void getFinalPositionForDropAnimation(int[] loc, float[] scaleXY, 3690 DragView dragView, CellLayout layout, ItemInfo info, int[] targetCell, boolean scale) { 3691 // Now we animate the dragView, (ie. the widget or shortcut preview) into its final 3692 // location and size on the home screen. 3693 int spanX = info.spanX; 3694 int spanY = info.spanY; 3695 3696 Rect r = estimateItemPosition(layout, targetCell[0], targetCell[1], spanX, spanY); 3697 loc[0] = r.left; 3698 loc[1] = r.top; 3699 3700 setFinalTransitionTransform(layout); 3701 float cellLayoutScale = 3702 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(layout, loc, true); 3703 resetTransitionTransform(layout); 3704 3705 float dragViewScaleX; 3706 float dragViewScaleY; 3707 if (scale) { 3708 dragViewScaleX = (1.0f * r.width()) / dragView.getMeasuredWidth(); 3709 dragViewScaleY = (1.0f * r.height()) / dragView.getMeasuredHeight(); 3710 } else { 3711 dragViewScaleX = 1f; 3712 dragViewScaleY = 1f; 3713 } 3714 3715 // The animation will scale the dragView about its center, so we need to center about 3716 // the final location. 3717 loc[0] -= (dragView.getMeasuredWidth() - cellLayoutScale * r.width()) / 2 3718 - Math.ceil(layout.getUnusedHorizontalSpace() / 2f); 3719 loc[1] -= (dragView.getMeasuredHeight() - cellLayoutScale * r.height()) / 2; 3720 3721 scaleXY[0] = dragViewScaleX * cellLayoutScale; 3722 scaleXY[1] = dragViewScaleY * cellLayoutScale; 3723 } 3724 3725 public void animateWidgetDrop(ItemInfo info, CellLayout cellLayout, final DragView dragView, 3726 final Runnable onCompleteRunnable, int animationType, final View finalView, 3727 boolean external) { 3728 Rect from = new Rect(); 3729 mLauncher.getDragLayer().getViewRectRelativeToSelf(dragView, from); 3730 3731 int[] finalPos = new int[2]; 3732 float scaleXY[] = new float[2]; 3733 boolean scalePreview = !(info instanceof PendingAddShortcutInfo); 3734 getFinalPositionForDropAnimation(finalPos, scaleXY, dragView, cellLayout, info, mTargetCell, 3735 scalePreview); 3736 3737 Resources res = mLauncher.getResources(); 3738 final int duration = res.getInteger(R.integer.config_dropAnimMaxDuration) - 200; 3739 3740 boolean isWidget = info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET || 3741 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET; 3742 if ((animationType == ANIMATE_INTO_POSITION_AND_RESIZE || external) && finalView != null) { 3743 Bitmap crossFadeBitmap = createWidgetBitmap(info, finalView); 3744 dragView.setCrossFadeBitmap(crossFadeBitmap); 3745 dragView.crossFade((int) (duration * 0.8f)); 3746 } else if (isWidget && external) { 3747 scaleXY[0] = scaleXY[1] = Math.min(scaleXY[0], scaleXY[1]); 3748 } 3749 3750 DragLayer dragLayer = mLauncher.getDragLayer(); 3751 if (animationType == CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION) { 3752 mLauncher.getDragLayer().animateViewIntoPosition(dragView, finalPos, 0f, 0.1f, 0.1f, 3753 DragLayer.ANIMATION_END_DISAPPEAR, onCompleteRunnable, duration); 3754 } else { 3755 int endStyle; 3756 if (animationType == ANIMATE_INTO_POSITION_AND_REMAIN) { 3757 endStyle = DragLayer.ANIMATION_END_REMAIN_VISIBLE; 3758 } else { 3759 endStyle = DragLayer.ANIMATION_END_DISAPPEAR; 3760 } 3761 3762 Runnable onComplete = new Runnable() { 3763 @Override 3764 public void run() { 3765 if (finalView != null) { 3766 finalView.setVisibility(VISIBLE); 3767 } 3768 if (onCompleteRunnable != null) { 3769 onCompleteRunnable.run(); 3770 } 3771 } 3772 }; 3773 dragLayer.animateViewIntoPosition(dragView, from.left, from.top, finalPos[0], 3774 finalPos[1], 1, 1, 1, scaleXY[0], scaleXY[1], onComplete, endStyle, 3775 duration, this); 3776 } 3777 } 3778 3779 public void setFinalTransitionTransform(CellLayout layout) { 3780 if (isSwitchingState()) { 3781 mCurrentScale = getScaleX(); 3782 setScaleX(mStateTransitionAnimation.getFinalScale()); 3783 setScaleY(mStateTransitionAnimation.getFinalScale()); 3784 } 3785 } 3786 public void resetTransitionTransform(CellLayout layout) { 3787 if (isSwitchingState()) { 3788 setScaleX(mCurrentScale); 3789 setScaleY(mCurrentScale); 3790 } 3791 } 3792 3793 public WorkspaceStateTransitionAnimation getStateTransitionAnimation() { 3794 return mStateTransitionAnimation; 3795 } 3796 3797 /** 3798 * Return the current {@link CellLayout}, correctly picking the destination 3799 * screen while a scroll is in progress. 3800 */ 3801 public CellLayout getCurrentDropLayout() { 3802 return (CellLayout) getChildAt(getNextPage()); 3803 } 3804 3805 /** 3806 * Return the current CellInfo describing our current drag; this method exists 3807 * so that Launcher can sync this object with the correct info when the activity is created/ 3808 * destroyed 3809 * 3810 */ 3811 public CellLayout.CellInfo getDragInfo() { 3812 return mDragInfo; 3813 } 3814 3815 public int getCurrentPageOffsetFromCustomContent() { 3816 return getNextPage() - numCustomPages(); 3817 } 3818 3819 /** 3820 * Calculate the nearest cell where the given object would be dropped. 3821 * 3822 * pixelX and pixelY should be in the coordinate system of layout 3823 */ 3824 @Thunk int[] findNearestArea(int pixelX, int pixelY, 3825 int spanX, int spanY, CellLayout layout, int[] recycle) { 3826 return layout.findNearestArea( 3827 pixelX, pixelY, spanX, spanY, recycle); 3828 } 3829 3830 void setup(DragController dragController) { 3831 mSpringLoadedDragController = new SpringLoadedDragController(mLauncher); 3832 mDragController = dragController; 3833 3834 // hardware layers on children are enabled on startup, but should be disabled until 3835 // needed 3836 updateChildrenLayersEnabled(false); 3837 } 3838 3839 /** 3840 * Called at the end of a drag which originated on the workspace. 3841 */ 3842 public void onDropCompleted(final View target, final DragObject d, 3843 final boolean isFlingToDelete, final boolean success) { 3844 if (mDeferDropAfterUninstall) { 3845 mDeferredAction = new Runnable() { 3846 public void run() { 3847 onDropCompleted(target, d, isFlingToDelete, success); 3848 mDeferredAction = null; 3849 } 3850 }; 3851 return; 3852 } 3853 3854 boolean beingCalledAfterUninstall = mDeferredAction != null; 3855 3856 if (success && !(beingCalledAfterUninstall && !mUninstallSuccessful)) { 3857 if (target != this && mDragInfo != null) { 3858 removeWorkspaceItem(mDragInfo.cell); 3859 } 3860 } else if (mDragInfo != null) { 3861 final CellLayout cellLayout = mLauncher.getCellLayout( 3862 mDragInfo.container, mDragInfo.screenId); 3863 if (cellLayout != null) { 3864 cellLayout.onDropChild(mDragInfo.cell); 3865 } else if (ProviderConfig.IS_DOGFOOD_BUILD) { 3866 throw new RuntimeException("Invalid state: cellLayout == null in " 3867 + "Workspace#onDropCompleted. Please file a bug. "); 3868 }; 3869 } 3870 if ((d.cancelled || (beingCalledAfterUninstall && !mUninstallSuccessful)) 3871 && mDragInfo.cell != null) { 3872 mDragInfo.cell.setVisibility(VISIBLE); 3873 } 3874 mDragOutline = null; 3875 mDragInfo = null; 3876 3877 if (!isFlingToDelete) { 3878 // Fling to delete already exits spring loaded mode after the animation finishes. 3879 mLauncher.exitSpringLoadedDragModeDelayed(success, 3880 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, mDelayedResizeRunnable); 3881 mDelayedResizeRunnable = null; 3882 } 3883 } 3884 3885 /** 3886 * For opposite operation. See {@link #addInScreen}. 3887 */ 3888 public void removeWorkspaceItem(View v) { 3889 CellLayout parentCell = getParentCellLayoutForView(v); 3890 if (parentCell != null) { 3891 parentCell.removeView(v); 3892 } else if (ProviderConfig.IS_DOGFOOD_BUILD) { 3893 // When an app is uninstalled using the drop target, we wait until resume to remove 3894 // the icon. We also remove all the corresponding items from the workspace at 3895 // {@link Launcher#bindComponentsRemoved}. That call can come before or after 3896 // {@link Launcher#mOnResumeCallbacks} depending on how busy the worker thread is. 3897 Log.e(TAG, "mDragInfo.cell has null parent"); 3898 } 3899 if (v instanceof DropTarget) { 3900 mDragController.removeDropTarget((DropTarget) v); 3901 } 3902 } 3903 3904 /** 3905 * Removes all folder listeners 3906 */ 3907 public void removeFolderListeners() { 3908 mapOverItems(false, new ItemOperator() { 3909 @Override 3910 public boolean evaluate(ItemInfo info, View view) { 3911 if (view instanceof FolderIcon) { 3912 ((FolderIcon) view).removeListeners(); 3913 } 3914 return false; 3915 } 3916 }); 3917 } 3918 3919 @Override 3920 public void deferCompleteDropAfterUninstallActivity() { 3921 mDeferDropAfterUninstall = true; 3922 } 3923 3924 /// maybe move this into a smaller part 3925 @Override 3926 public void onDragObjectRemoved(boolean success) { 3927 mDeferDropAfterUninstall = false; 3928 mUninstallSuccessful = success; 3929 if (mDeferredAction != null) { 3930 mDeferredAction.run(); 3931 } 3932 } 3933 3934 @Override 3935 public float getIntrinsicIconScaleFactor() { 3936 return 1f; 3937 } 3938 3939 @Override 3940 public boolean supportsFlingToDelete() { 3941 return true; 3942 } 3943 3944 @Override 3945 public boolean supportsAppInfoDropTarget() { 3946 return !FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND; 3947 } 3948 3949 @Override 3950 public boolean supportsDeleteDropTarget() { 3951 return true; 3952 } 3953 3954 @Override 3955 public void onFlingToDelete(DragObject d, PointF vec) { 3956 // Do nothing 3957 } 3958 3959 @Override 3960 public void onFlingToDeleteCompleted() { 3961 // Do nothing 3962 } 3963 3964 public boolean isDropEnabled() { 3965 return true; 3966 } 3967 3968 @Override 3969 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) { 3970 // We don't dispatch restoreInstanceState to our children using this code path. 3971 // Some pages will be restored immediately as their items are bound immediately, and 3972 // others we will need to wait until after their items are bound. 3973 mSavedStates = container; 3974 } 3975 3976 public void restoreInstanceStateForChild(int child) { 3977 if (mSavedStates != null) { 3978 mRestoredPages.add(child); 3979 CellLayout cl = (CellLayout) getChildAt(child); 3980 if (cl != null) { 3981 cl.restoreInstanceState(mSavedStates); 3982 } 3983 } 3984 } 3985 3986 public void restoreInstanceStateForRemainingPages() { 3987 int count = getChildCount(); 3988 for (int i = 0; i < count; i++) { 3989 if (!mRestoredPages.contains(i)) { 3990 restoreInstanceStateForChild(i); 3991 } 3992 } 3993 mRestoredPages.clear(); 3994 mSavedStates = null; 3995 } 3996 3997 @Override 3998 public void scrollLeft() { 3999 if (!workspaceInModalState() && !mIsSwitchingState) { 4000 super.scrollLeft(); 4001 } 4002 Folder openFolder = getOpenFolder(); 4003 if (openFolder != null) { 4004 openFolder.completeDragExit(); 4005 } 4006 } 4007 4008 @Override 4009 public void scrollRight() { 4010 if (!workspaceInModalState() && !mIsSwitchingState) { 4011 super.scrollRight(); 4012 } 4013 Folder openFolder = getOpenFolder(); 4014 if (openFolder != null) { 4015 openFolder.completeDragExit(); 4016 } 4017 } 4018 4019 @Override 4020 public boolean onEnterScrollArea(int x, int y, int direction) { 4021 // Ignore the scroll area if we are dragging over the hot seat 4022 boolean isPortrait = !mLauncher.getDeviceProfile().isLandscape; 4023 if (mLauncher.getHotseat() != null && isPortrait) { 4024 Rect r = new Rect(); 4025 mLauncher.getHotseat().getHitRect(r); 4026 if (r.contains(x, y)) { 4027 return false; 4028 } 4029 } 4030 4031 boolean result = false; 4032 if (!workspaceInModalState() && !mIsSwitchingState && getOpenFolder() == null) { 4033 mInScrollArea = true; 4034 4035 final int page = getNextPage() + 4036 (direction == DragController.SCROLL_LEFT ? -1 : 1); 4037 // We always want to exit the current layout to ensure parity of enter / exit 4038 setCurrentDropLayout(null); 4039 4040 if (0 <= page && page < getChildCount()) { 4041 // Ensure that we are not dragging over to the custom content screen 4042 if (getScreenIdForPageIndex(page) == CUSTOM_CONTENT_SCREEN_ID) { 4043 return false; 4044 } 4045 4046 CellLayout layout = (CellLayout) getChildAt(page); 4047 setCurrentDragOverlappingLayout(layout); 4048 4049 // Workspace is responsible for drawing the edge glow on adjacent pages, 4050 // so we need to redraw the workspace when this may have changed. 4051 invalidate(); 4052 result = true; 4053 } 4054 } 4055 return result; 4056 } 4057 4058 @Override 4059 public boolean onExitScrollArea() { 4060 boolean result = false; 4061 if (mInScrollArea) { 4062 invalidate(); 4063 CellLayout layout = getCurrentDropLayout(); 4064 setCurrentDropLayout(layout); 4065 setCurrentDragOverlappingLayout(layout); 4066 4067 result = true; 4068 mInScrollArea = false; 4069 } 4070 return result; 4071 } 4072 4073 private void onResetScrollArea() { 4074 setCurrentDragOverlappingLayout(null); 4075 mInScrollArea = false; 4076 } 4077 4078 /** 4079 * Returns a specific CellLayout 4080 */ 4081 CellLayout getParentCellLayoutForView(View v) { 4082 ArrayList<CellLayout> layouts = getWorkspaceAndHotseatCellLayouts(); 4083 for (CellLayout layout : layouts) { 4084 if (layout.getShortcutsAndWidgets().indexOfChild(v) > -1) { 4085 return layout; 4086 } 4087 } 4088 return null; 4089 } 4090 4091 /** 4092 * Returns a list of all the CellLayouts in the workspace. 4093 */ 4094 ArrayList<CellLayout> getWorkspaceAndHotseatCellLayouts() { 4095 ArrayList<CellLayout> layouts = new ArrayList<CellLayout>(); 4096 int screenCount = getChildCount(); 4097 for (int screen = 0; screen < screenCount; screen++) { 4098 layouts.add(((CellLayout) getChildAt(screen))); 4099 } 4100 if (mLauncher.getHotseat() != null) { 4101 layouts.add(mLauncher.getHotseat().getLayout()); 4102 } 4103 return layouts; 4104 } 4105 4106 /** 4107 * We should only use this to search for specific children. Do not use this method to modify 4108 * ShortcutsAndWidgetsContainer directly. Includes ShortcutAndWidgetContainers from 4109 * the hotseat and workspace pages 4110 */ 4111 ArrayList<ShortcutAndWidgetContainer> getAllShortcutAndWidgetContainers() { 4112 ArrayList<ShortcutAndWidgetContainer> childrenLayouts = new ArrayList<>(); 4113 int screenCount = getChildCount(); 4114 for (int screen = 0; screen < screenCount; screen++) { 4115 childrenLayouts.add(((CellLayout) getChildAt(screen)).getShortcutsAndWidgets()); 4116 } 4117 if (mLauncher.getHotseat() != null) { 4118 childrenLayouts.add(mLauncher.getHotseat().getLayout().getShortcutsAndWidgets()); 4119 } 4120 return childrenLayouts; 4121 } 4122 4123 public View getHomescreenIconByItemId(final long id) { 4124 return getFirstMatch(new ItemOperator() { 4125 4126 @Override 4127 public boolean evaluate(ItemInfo info, View v) { 4128 return info != null && info.id == id; 4129 } 4130 }); 4131 } 4132 4133 public View getViewForTag(final Object tag) { 4134 return getFirstMatch(new ItemOperator() { 4135 4136 @Override 4137 public boolean evaluate(ItemInfo info, View v) { 4138 return info == tag; 4139 } 4140 }); 4141 } 4142 4143 public LauncherAppWidgetHostView getWidgetForAppWidgetId(final int appWidgetId) { 4144 return (LauncherAppWidgetHostView) getFirstMatch(new ItemOperator() { 4145 4146 @Override 4147 public boolean evaluate(ItemInfo info, View v) { 4148 return (info instanceof LauncherAppWidgetInfo) && 4149 ((LauncherAppWidgetInfo) info).appWidgetId == appWidgetId; 4150 } 4151 }); 4152 } 4153 4154 public View getFirstMatch(final ItemOperator operator) { 4155 final View[] value = new View[1]; 4156 mapOverItems(MAP_NO_RECURSE, new ItemOperator() { 4157 @Override 4158 public boolean evaluate(ItemInfo info, View v) { 4159 if (operator.evaluate(info, v)) { 4160 value[0] = v; 4161 return true; 4162 } 4163 return false; 4164 } 4165 }); 4166 return value[0]; 4167 } 4168 4169 void clearDropTargets() { 4170 mapOverItems(MAP_NO_RECURSE, new ItemOperator() { 4171 @Override 4172 public boolean evaluate(ItemInfo info, View v) { 4173 if (v instanceof DropTarget) { 4174 mDragController.removeDropTarget((DropTarget) v); 4175 } 4176 // not done, process all the shortcuts 4177 return false; 4178 } 4179 }); 4180 } 4181 4182 // Removes ALL items that match a given package name, this is usually called when a package 4183 // has been removed and we want to remove all components (widgets, shortcuts, apps) that 4184 // belong to that package. 4185 void removeItemsByPackageName(final HashSet<String> packageNames, final UserHandleCompat user) { 4186 // Filter out all the ItemInfos that this is going to affect 4187 final HashSet<ItemInfo> infos = new HashSet<ItemInfo>(); 4188 final HashSet<ComponentName> cns = new HashSet<ComponentName>(); 4189 ArrayList<CellLayout> cellLayouts = getWorkspaceAndHotseatCellLayouts(); 4190 for (CellLayout layoutParent : cellLayouts) { 4191 ViewGroup layout = layoutParent.getShortcutsAndWidgets(); 4192 int childCount = layout.getChildCount(); 4193 for (int i = 0; i < childCount; ++i) { 4194 View view = layout.getChildAt(i); 4195 infos.add((ItemInfo) view.getTag()); 4196 } 4197 } 4198 LauncherModel.ItemInfoFilter filter = new LauncherModel.ItemInfoFilter() { 4199 @Override 4200 public boolean filterItem(ItemInfo parent, ItemInfo info, 4201 ComponentName cn) { 4202 if (packageNames.contains(cn.getPackageName()) 4203 && info.user.equals(user)) { 4204 cns.add(cn); 4205 return true; 4206 } 4207 return false; 4208 } 4209 }; 4210 LauncherModel.filterItemInfos(infos, filter); 4211 4212 // Remove the affected components 4213 removeItemsByComponentName(cns, user); 4214 } 4215 4216 /** 4217 * Removes items that match the item info specified. When applications are removed 4218 * as a part of an update, this is called to ensure that other widgets and application 4219 * shortcuts are not removed. 4220 */ 4221 void removeItemsByComponentName(final HashSet<ComponentName> componentNames, 4222 final UserHandleCompat user) { 4223 ArrayList<CellLayout> cellLayouts = getWorkspaceAndHotseatCellLayouts(); 4224 for (final CellLayout layoutParent: cellLayouts) { 4225 final ViewGroup layout = layoutParent.getShortcutsAndWidgets(); 4226 4227 final HashMap<ItemInfo, View> children = new HashMap<ItemInfo, View>(); 4228 for (int j = 0; j < layout.getChildCount(); j++) { 4229 final View view = layout.getChildAt(j); 4230 children.put((ItemInfo) view.getTag(), view); 4231 } 4232 4233 final ArrayList<View> childrenToRemove = new ArrayList<View>(); 4234 final HashMap<FolderInfo, ArrayList<ShortcutInfo>> folderAppsToRemove = 4235 new HashMap<FolderInfo, ArrayList<ShortcutInfo>>(); 4236 LauncherModel.ItemInfoFilter filter = new LauncherModel.ItemInfoFilter() { 4237 @Override 4238 public boolean filterItem(ItemInfo parent, ItemInfo info, 4239 ComponentName cn) { 4240 if (parent instanceof FolderInfo) { 4241 if (componentNames.contains(cn) && info.user.equals(user)) { 4242 FolderInfo folder = (FolderInfo) parent; 4243 ArrayList<ShortcutInfo> appsToRemove; 4244 if (folderAppsToRemove.containsKey(folder)) { 4245 appsToRemove = folderAppsToRemove.get(folder); 4246 } else { 4247 appsToRemove = new ArrayList<ShortcutInfo>(); 4248 folderAppsToRemove.put(folder, appsToRemove); 4249 } 4250 appsToRemove.add((ShortcutInfo) info); 4251 return true; 4252 } 4253 } else { 4254 if (componentNames.contains(cn) && info.user.equals(user)) { 4255 childrenToRemove.add(children.get(info)); 4256 return true; 4257 } 4258 } 4259 return false; 4260 } 4261 }; 4262 LauncherModel.filterItemInfos(children.keySet(), filter); 4263 4264 // Remove all the apps from their folders 4265 for (FolderInfo folder : folderAppsToRemove.keySet()) { 4266 ArrayList<ShortcutInfo> appsToRemove = folderAppsToRemove.get(folder); 4267 for (ShortcutInfo info : appsToRemove) { 4268 folder.remove(info, false); 4269 } 4270 } 4271 4272 // Remove all the other children 4273 for (View child : childrenToRemove) { 4274 // Note: We can not remove the view directly from CellLayoutChildren as this 4275 // does not re-mark the spaces as unoccupied. 4276 layoutParent.removeViewInLayout(child); 4277 if (child instanceof DropTarget) { 4278 mDragController.removeDropTarget((DropTarget) child); 4279 } 4280 } 4281 4282 if (childrenToRemove.size() > 0) { 4283 layout.requestLayout(); 4284 layout.invalidate(); 4285 } 4286 } 4287 4288 // Strip all the empty screens 4289 stripEmptyScreens(); 4290 } 4291 4292 public interface ItemOperator { 4293 /** 4294 * Process the next itemInfo, possibly with side-effect on the next item. 4295 * 4296 * @param info info for the shortcut 4297 * @param view view for the shortcut 4298 * @return true if done, false to continue the map 4299 */ 4300 public boolean evaluate(ItemInfo info, View view); 4301 } 4302 4303 /** 4304 * Map the operator over the shortcuts and widgets, return the first-non-null value. 4305 * 4306 * @param recurse true: iterate over folder children. false: op get the folders themselves. 4307 * @param op the operator to map over the shortcuts 4308 */ 4309 void mapOverItems(boolean recurse, ItemOperator op) { 4310 ArrayList<ShortcutAndWidgetContainer> containers = getAllShortcutAndWidgetContainers(); 4311 final int containerCount = containers.size(); 4312 for (int containerIdx = 0; containerIdx < containerCount; containerIdx++) { 4313 ShortcutAndWidgetContainer container = containers.get(containerIdx); 4314 // map over all the shortcuts on the workspace 4315 final int itemCount = container.getChildCount(); 4316 for (int itemIdx = 0; itemIdx < itemCount; itemIdx++) { 4317 View item = container.getChildAt(itemIdx); 4318 ItemInfo info = (ItemInfo) item.getTag(); 4319 if (recurse && info instanceof FolderInfo && item instanceof FolderIcon) { 4320 FolderIcon folder = (FolderIcon) item; 4321 ArrayList<View> folderChildren = folder.getFolder().getItemsInReadingOrder(); 4322 // map over all the children in the folder 4323 final int childCount = folderChildren.size(); 4324 for (int childIdx = 0; childIdx < childCount; childIdx++) { 4325 View child = folderChildren.get(childIdx); 4326 info = (ItemInfo) child.getTag(); 4327 if (op.evaluate(info, child)) { 4328 return; 4329 } 4330 } 4331 } else { 4332 if (op.evaluate(info, item)) { 4333 return; 4334 } 4335 } 4336 } 4337 } 4338 } 4339 4340 void updateShortcuts(ArrayList<ShortcutInfo> shortcuts) { 4341 int total = shortcuts.size(); 4342 final HashSet<ShortcutInfo> updates = new HashSet<ShortcutInfo>(total); 4343 final HashSet<Long> folderIds = new HashSet<>(); 4344 4345 for (int i = 0; i < total; i++) { 4346 ShortcutInfo s = shortcuts.get(i); 4347 updates.add(s); 4348 folderIds.add(s.container); 4349 } 4350 4351 mapOverItems(MAP_RECURSE, new ItemOperator() { 4352 @Override 4353 public boolean evaluate(ItemInfo info, View v) { 4354 if (info instanceof ShortcutInfo && v instanceof BubbleTextView && 4355 updates.contains(info)) { 4356 ShortcutInfo si = (ShortcutInfo) info; 4357 BubbleTextView shortcut = (BubbleTextView) v; 4358 Drawable oldIcon = getTextViewIcon(shortcut); 4359 boolean oldPromiseState = (oldIcon instanceof PreloadIconDrawable) 4360 && ((PreloadIconDrawable) oldIcon).hasNotCompleted(); 4361 shortcut.applyFromShortcutInfo(si, mIconCache, 4362 si.isPromise() != oldPromiseState); 4363 } 4364 // process all the shortcuts 4365 return false; 4366 } 4367 }); 4368 4369 // Update folder icons 4370 mapOverItems(MAP_NO_RECURSE, new ItemOperator() { 4371 @Override 4372 public boolean evaluate(ItemInfo info, View v) { 4373 if (info instanceof FolderInfo && folderIds.contains(info.id)) { 4374 ((FolderInfo) info).itemsChanged(false); 4375 } 4376 // process all the shortcuts 4377 return false; 4378 } 4379 }); 4380 } 4381 4382 public void removeAbandonedPromise(String packageName, UserHandleCompat user) { 4383 HashSet<String> packages = new HashSet<>(1); 4384 packages.add(packageName); 4385 LauncherModel.deletePackageFromDatabase(mLauncher, packageName, user); 4386 removeItemsByPackageName(packages, user); 4387 } 4388 4389 public void updateRestoreItems(final HashSet<ItemInfo> updates) { 4390 mapOverItems(MAP_RECURSE, new ItemOperator() { 4391 @Override 4392 public boolean evaluate(ItemInfo info, View v) { 4393 if (info instanceof ShortcutInfo && v instanceof BubbleTextView 4394 && updates.contains(info)) { 4395 ((BubbleTextView) v).applyState(false); 4396 } else if (v instanceof PendingAppWidgetHostView 4397 && info instanceof LauncherAppWidgetInfo 4398 && updates.contains(info)) { 4399 ((PendingAppWidgetHostView) v).applyState(); 4400 } 4401 // process all the shortcuts 4402 return false; 4403 } 4404 }); 4405 } 4406 4407 public void widgetsRestored(final ArrayList<LauncherAppWidgetInfo> changedInfo) { 4408 if (!changedInfo.isEmpty()) { 4409 DeferredWidgetRefresh widgetRefresh = new DeferredWidgetRefresh(changedInfo, 4410 mLauncher.getAppWidgetHost()); 4411 4412 LauncherAppWidgetInfo item = changedInfo.get(0); 4413 final AppWidgetProviderInfo widgetInfo; 4414 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) { 4415 widgetInfo = AppWidgetManagerCompat 4416 .getInstance(mLauncher).findProvider(item.providerName, item.user); 4417 } else { 4418 widgetInfo = AppWidgetManagerCompat.getInstance(mLauncher) 4419 .getAppWidgetInfo(item.appWidgetId); 4420 } 4421 4422 if (widgetInfo != null) { 4423 // Re-inflate the widgets which have changed status 4424 widgetRefresh.run(); 4425 } else { 4426 // widgetRefresh will automatically run when the packages are updated. 4427 // For now just update the progress bars 4428 mapOverItems(MAP_NO_RECURSE, new ItemOperator() { 4429 @Override 4430 public boolean evaluate(ItemInfo info, View view) { 4431 if (view instanceof PendingAppWidgetHostView 4432 && changedInfo.contains(info)) { 4433 ((LauncherAppWidgetInfo) info).installProgress = 100; 4434 ((PendingAppWidgetHostView) view).applyState(); 4435 } 4436 // process all the shortcuts 4437 return false; 4438 } 4439 }); 4440 } 4441 } 4442 } 4443 4444 private void moveToScreen(int page, boolean animate) { 4445 if (!workspaceInModalState()) { 4446 if (animate) { 4447 snapToPage(page); 4448 } else { 4449 setCurrentPage(page); 4450 } 4451 } 4452 View child = getChildAt(page); 4453 if (child != null) { 4454 child.requestFocus(); 4455 } 4456 } 4457 4458 void moveToDefaultScreen(boolean animate) { 4459 moveToScreen(getDefaultPage(), animate); 4460 } 4461 4462 void moveToCustomContentScreen(boolean animate) { 4463 if (hasCustomContent()) { 4464 int ccIndex = getPageIndexForScreenId(CUSTOM_CONTENT_SCREEN_ID); 4465 if (animate) { 4466 snapToPage(ccIndex); 4467 } else { 4468 setCurrentPage(ccIndex); 4469 } 4470 View child = getChildAt(ccIndex); 4471 if (child != null) { 4472 child.requestFocus(); 4473 } 4474 } 4475 exitWidgetResizeMode(); 4476 } 4477 4478 @Override 4479 protected String getPageIndicatorDescription() { 4480 return getResources().getString(R.string.all_apps_button_label); 4481 } 4482 4483 @Override 4484 protected String getCurrentPageDescription() { 4485 if (hasCustomContent() && getNextPage() == 0) { 4486 return mCustomContentDescription; 4487 } 4488 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; 4489 return getPageDescription(page); 4490 } 4491 4492 private String getPageDescription(int page) { 4493 int delta = numCustomPages(); 4494 int nScreens = getChildCount() - delta; 4495 int extraScreenId = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID); 4496 if (extraScreenId >= 0 && nScreens > 1) { 4497 if (page == extraScreenId) { 4498 return getContext().getString(R.string.workspace_new_page); 4499 } 4500 nScreens--; 4501 } 4502 if (nScreens == 0) { 4503 // When the workspace is not loaded, we do not know how many screen will be bound. 4504 return getContext().getString(R.string.all_apps_home_button_label); 4505 } 4506 return getContext().getString(R.string.workspace_scroll_format, 4507 page + 1 - delta, nScreens); 4508 } 4509 4510 @Override 4511 public void fillInLaunchSourceData(View v, ItemInfo info, Target target, Target targetParent) { 4512 target.itemType = LauncherLogProto.APP_ICON; 4513 target.gridX = info.cellX; 4514 target.gridY = info.cellY; 4515 target.pageIndex = getCurrentPage(); 4516 targetParent.containerType = LauncherLogProto.WORKSPACE; 4517 } 4518 4519 /** 4520 * Used as a workaround to ensure that the AppWidgetService receives the 4521 * PACKAGE_ADDED broadcast before updating widgets. 4522 */ 4523 private class DeferredWidgetRefresh implements Runnable { 4524 private final ArrayList<LauncherAppWidgetInfo> mInfos; 4525 private final LauncherAppWidgetHost mHost; 4526 private final Handler mHandler; 4527 4528 private boolean mRefreshPending; 4529 4530 public DeferredWidgetRefresh(ArrayList<LauncherAppWidgetInfo> infos, 4531 LauncherAppWidgetHost host) { 4532 mInfos = infos; 4533 mHost = host; 4534 mHandler = new Handler(); 4535 mRefreshPending = true; 4536 4537 mHost.addProviderChangeListener(this); 4538 // Force refresh after 10 seconds, if we don't get the provider changed event. 4539 // This could happen when the provider is no longer available in the app. 4540 mHandler.postDelayed(this, 10000); 4541 } 4542 4543 @Override 4544 public void run() { 4545 mHost.removeProviderChangeListener(this); 4546 mHandler.removeCallbacks(this); 4547 4548 if (!mRefreshPending) { 4549 return; 4550 } 4551 4552 mRefreshPending = false; 4553 4554 mapOverItems(MAP_NO_RECURSE, new ItemOperator() { 4555 @Override 4556 public boolean evaluate(ItemInfo info, View view) { 4557 if (view instanceof PendingAppWidgetHostView && mInfos.contains(info)) { 4558 PendingAppWidgetHostView hostView = (PendingAppWidgetHostView) view; 4559 mLauncher.removeItem(view, info, false /* deleteFromDb */); 4560 mLauncher.bindAppWidget((LauncherAppWidgetInfo) info); 4561 } 4562 // process all the shortcuts 4563 return false; 4564 } 4565 }); 4566 } 4567 } 4568 4569 public interface OnStateChangeListener { 4570 4571 /** 4572 * Called when the workspace state is changing. 4573 * @param toState final state 4574 * @param targetAnim animation which will be played during the transition or null. 4575 */ 4576 void prepareStateChange(State toState, AnimatorSet targetAnim); 4577 } 4578 4579 public static final boolean isQsbContainerPage(int pageNo) { 4580 return pageNo == 0; 4581 } 4582} 4583