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