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