1 /* 2 * Copyright 2017 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 17 package android.view; 18 19 import static android.util.DisplayMetrics.DENSITY_DEFAULT; 20 import static android.util.DisplayMetrics.DENSITY_DEVICE_STABLE; 21 import static android.view.DisplayCutoutProto.BOUND_BOTTOM; 22 import static android.view.DisplayCutoutProto.BOUND_LEFT; 23 import static android.view.DisplayCutoutProto.BOUND_RIGHT; 24 import static android.view.DisplayCutoutProto.BOUND_TOP; 25 import static android.view.DisplayCutoutProto.INSETS; 26 27 import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE; 28 29 import android.annotation.IntDef; 30 import android.annotation.NonNull; 31 import android.annotation.Nullable; 32 import android.content.res.Resources; 33 import android.graphics.Insets; 34 import android.graphics.Path; 35 import android.graphics.Rect; 36 import android.os.Parcel; 37 import android.os.Parcelable; 38 import android.text.TextUtils; 39 import android.util.Pair; 40 import android.util.proto.ProtoOutputStream; 41 42 import com.android.internal.R; 43 import com.android.internal.annotations.GuardedBy; 44 import com.android.internal.annotations.VisibleForTesting; 45 46 import java.lang.annotation.Retention; 47 import java.lang.annotation.RetentionPolicy; 48 import java.util.ArrayList; 49 import java.util.Arrays; 50 import java.util.List; 51 52 /** 53 * Represents the area of the display that is not functional for displaying content. 54 * 55 * <p>{@code DisplayCutout} is immutable. 56 */ 57 public final class DisplayCutout { 58 59 private static final String TAG = "DisplayCutout"; 60 61 /** 62 * Category for overlays that allow emulating a display cutout on devices that don't have 63 * one. 64 * 65 * @see android.content.om.IOverlayManager 66 * @hide 67 */ 68 public static final String EMULATION_OVERLAY_CATEGORY = 69 "com.android.internal.display_cutout_emulation"; 70 71 private static final Rect ZERO_RECT = new Rect(); 72 73 /** 74 * An instance where {@link #isEmpty()} returns {@code true}. 75 * 76 * @hide 77 */ 78 public static final DisplayCutout NO_CUTOUT = new DisplayCutout( 79 ZERO_RECT, Insets.NONE, ZERO_RECT, ZERO_RECT, ZERO_RECT, ZERO_RECT, 80 false /* copyArguments */); 81 82 83 private static final Pair<Path, DisplayCutout> NULL_PAIR = new Pair<>(null, null); 84 private static final Object CACHE_LOCK = new Object(); 85 86 @GuardedBy("CACHE_LOCK") 87 private static String sCachedSpec; 88 @GuardedBy("CACHE_LOCK") 89 private static int sCachedDisplayWidth; 90 @GuardedBy("CACHE_LOCK") 91 private static int sCachedDisplayHeight; 92 @GuardedBy("CACHE_LOCK") 93 private static float sCachedDensity; 94 @GuardedBy("CACHE_LOCK") 95 private static Pair<Path, DisplayCutout> sCachedCutout = NULL_PAIR; 96 @GuardedBy("CACHE_LOCK") 97 private static Insets sCachedWaterfallInsets; 98 99 private final Rect mSafeInsets; 100 @NonNull 101 private final Insets mWaterfallInsets; 102 103 104 /** 105 * The bound is at the left of the screen. 106 * @hide 107 */ 108 public static final int BOUNDS_POSITION_LEFT = 0; 109 110 /** 111 * The bound is at the top of the screen. 112 * @hide 113 */ 114 public static final int BOUNDS_POSITION_TOP = 1; 115 116 /** 117 * The bound is at the right of the screen. 118 * @hide 119 */ 120 public static final int BOUNDS_POSITION_RIGHT = 2; 121 122 /** 123 * The bound is at the bottom of the screen. 124 * @hide 125 */ 126 public static final int BOUNDS_POSITION_BOTTOM = 3; 127 128 /** 129 * The number of possible positions at which bounds can be located. 130 * @hide 131 */ 132 public static final int BOUNDS_POSITION_LENGTH = 4; 133 134 /** @hide */ 135 @IntDef(prefix = { "BOUNDS_POSITION_" }, value = { 136 BOUNDS_POSITION_LEFT, 137 BOUNDS_POSITION_TOP, 138 BOUNDS_POSITION_RIGHT, 139 BOUNDS_POSITION_BOTTOM 140 }) 141 @Retention(RetentionPolicy.SOURCE) 142 public @interface BoundsPosition {} 143 144 private static class Bounds { 145 private final Rect[] mRects; 146 Bounds(Rect left, Rect top, Rect right, Rect bottom, boolean copyArguments)147 private Bounds(Rect left, Rect top, Rect right, Rect bottom, boolean copyArguments) { 148 mRects = new Rect[BOUNDS_POSITION_LENGTH]; 149 mRects[BOUNDS_POSITION_LEFT] = getCopyOrRef(left, copyArguments); 150 mRects[BOUNDS_POSITION_TOP] = getCopyOrRef(top, copyArguments); 151 mRects[BOUNDS_POSITION_RIGHT] = getCopyOrRef(right, copyArguments); 152 mRects[BOUNDS_POSITION_BOTTOM] = getCopyOrRef(bottom, copyArguments); 153 154 } 155 Bounds(Rect[] rects, boolean copyArguments)156 private Bounds(Rect[] rects, boolean copyArguments) { 157 if (rects.length != BOUNDS_POSITION_LENGTH) { 158 throw new IllegalArgumentException( 159 "rects must have exactly 4 elements: rects=" + Arrays.toString(rects)); 160 } 161 if (copyArguments) { 162 mRects = new Rect[BOUNDS_POSITION_LENGTH]; 163 for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) { 164 mRects[i] = new Rect(rects[i]); 165 } 166 } else { 167 for (Rect rect : rects) { 168 if (rect == null) { 169 throw new IllegalArgumentException( 170 "rects must have non-null elements: rects=" 171 + Arrays.toString(rects)); 172 } 173 } 174 mRects = rects; 175 } 176 } 177 isEmpty()178 private boolean isEmpty() { 179 for (Rect rect : mRects) { 180 if (!rect.isEmpty()) { 181 return false; 182 } 183 } 184 return true; 185 } 186 getRect(@oundsPosition int pos)187 private Rect getRect(@BoundsPosition int pos) { 188 return new Rect(mRects[pos]); 189 } 190 getRects()191 private Rect[] getRects() { 192 Rect[] rects = new Rect[BOUNDS_POSITION_LENGTH]; 193 for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) { 194 rects[i] = new Rect(mRects[i]); 195 } 196 return rects; 197 } 198 199 @Override hashCode()200 public int hashCode() { 201 int result = 0; 202 for (Rect rect : mRects) { 203 result = result * 48271 + rect.hashCode(); 204 } 205 return result; 206 } 207 @Override equals(Object o)208 public boolean equals(Object o) { 209 if (o == this) { 210 return true; 211 } 212 if (o instanceof Bounds) { 213 Bounds b = (Bounds) o; 214 return Arrays.deepEquals(mRects, b.mRects); 215 } 216 return false; 217 } 218 219 @Override toString()220 public String toString() { 221 return "Bounds=" + Arrays.toString(mRects); 222 } 223 224 } 225 226 private final Bounds mBounds; 227 228 /** 229 * Creates a DisplayCutout instance. 230 * 231 * <p>Note that this is only useful for tests. For production code, developers should always 232 * use a {@link DisplayCutout} obtained from the system.</p> 233 * 234 * @param safeInsets the insets from each edge which avoid the display cutout as returned by 235 * {@link #getSafeInsetTop()} etc. 236 * @param boundLeft the left bounding rect of the display cutout in pixels. If null is passed, 237 * it's treated as an empty rectangle (0,0)-(0,0). 238 * @param boundTop the top bounding rect of the display cutout in pixels. If null is passed, 239 * it's treated as an empty rectangle (0,0)-(0,0). 240 * @param boundRight the right bounding rect of the display cutout in pixels. If null is 241 * passed, it's treated as an empty rectangle (0,0)-(0,0). 242 * @param boundBottom the bottom bounding rect of the display cutout in pixels. If null is 243 * passed, it's treated as an empty rectangle (0,0)-(0,0). 244 */ 245 // TODO(b/73953958): @VisibleForTesting(visibility = PRIVATE) DisplayCutout(@onNull Insets safeInsets, @Nullable Rect boundLeft, @Nullable Rect boundTop, @Nullable Rect boundRight, @Nullable Rect boundBottom)246 public DisplayCutout(@NonNull Insets safeInsets, @Nullable Rect boundLeft, 247 @Nullable Rect boundTop, @Nullable Rect boundRight, @Nullable Rect boundBottom) { 248 this(safeInsets.toRect(), Insets.NONE, boundLeft, boundTop, boundRight, boundBottom, true); 249 } 250 251 /** 252 * Creates a DisplayCutout instance. 253 * 254 * <p>Note that this is only useful for tests. For production code, developers should always 255 * use a {@link DisplayCutout} obtained from the system.</p> 256 * 257 * @param safeInsets the insets from each edge which avoid the display cutout as returned by 258 * {@link #getSafeInsetTop()} etc. 259 * @param boundLeft the left bounding rect of the display cutout in pixels. If null is passed, 260 * it's treated as an empty rectangle (0,0)-(0,0). 261 * @param boundTop the top bounding rect of the display cutout in pixels. If null is passed, 262 * it's treated as an empty rectangle (0,0)-(0,0). 263 * @param boundRight the right bounding rect of the display cutout in pixels. If null is 264 * passed, it's treated as an empty rectangle (0,0)-(0,0). 265 * @param boundBottom the bottom bounding rect of the display cutout in pixels. If null is 266 * passed, it's treated as an empty rectangle (0,0)-(0,0). 267 * @param waterfallInsets the insets for the curved areas in waterfall display. 268 */ DisplayCutout(@onNull Insets safeInsets, @Nullable Rect boundLeft, @Nullable Rect boundTop, @Nullable Rect boundRight, @Nullable Rect boundBottom, @NonNull Insets waterfallInsets)269 public DisplayCutout(@NonNull Insets safeInsets, @Nullable Rect boundLeft, 270 @Nullable Rect boundTop, @Nullable Rect boundRight, @Nullable Rect boundBottom, 271 @NonNull Insets waterfallInsets) { 272 this(safeInsets.toRect(), waterfallInsets, boundLeft, boundTop, boundRight, boundBottom, 273 true); 274 } 275 276 /** 277 * Creates a DisplayCutout instance. 278 * 279 * <p>Note that this is only useful for tests. For production code, developers should always 280 * use a {@link DisplayCutout} obtained from the system.</p> 281 * 282 * @param safeInsets the insets from each edge which avoid the display cutout as returned by 283 * {@link #getSafeInsetTop()} etc. 284 * @param boundingRects the bounding rects of the display cutouts as returned by 285 * {@link #getBoundingRects()} ()}. 286 * @deprecated Use {@link DisplayCutout#DisplayCutout(Insets, Rect, Rect, Rect, Rect)} instead. 287 */ 288 // TODO(b/73953958): @VisibleForTesting(visibility = PRIVATE) 289 @Deprecated DisplayCutout(@ullable Rect safeInsets, @Nullable List<Rect> boundingRects)290 public DisplayCutout(@Nullable Rect safeInsets, @Nullable List<Rect> boundingRects) { 291 this(safeInsets, Insets.NONE, extractBoundsFromList(safeInsets, boundingRects), 292 true /* copyArguments */); 293 } 294 295 /** 296 * Creates a DisplayCutout instance. 297 * 298 * @param safeInsets the insets from each edge which avoid the display cutout as returned by 299 * {@link #getSafeInsetTop()} etc. 300 * @param copyArguments if true, create a copy of the arguments. If false, the passed arguments 301 * are not copied and MUST remain unchanged forever. 302 */ DisplayCutout(Rect safeInsets, Insets waterfallInsets, Rect boundLeft, Rect boundTop, Rect boundRight, Rect boundBottom, boolean copyArguments)303 private DisplayCutout(Rect safeInsets, Insets waterfallInsets, Rect boundLeft, 304 Rect boundTop, Rect boundRight, Rect boundBottom, boolean copyArguments) { 305 mSafeInsets = getCopyOrRef(safeInsets, copyArguments); 306 mWaterfallInsets = waterfallInsets == null ? Insets.NONE : waterfallInsets; 307 mBounds = new Bounds(boundLeft, boundTop, boundRight, boundBottom, copyArguments); 308 } 309 DisplayCutout(Rect safeInsets, Insets waterfallInsets, Rect[] bounds, boolean copyArguments)310 private DisplayCutout(Rect safeInsets, Insets waterfallInsets, Rect[] bounds, 311 boolean copyArguments) { 312 mSafeInsets = getCopyOrRef(safeInsets, copyArguments); 313 mWaterfallInsets = waterfallInsets == null ? Insets.NONE : waterfallInsets; 314 mBounds = new Bounds(bounds, copyArguments); 315 } 316 DisplayCutout(Rect safeInsets, Insets waterfallInsets, Bounds bounds)317 private DisplayCutout(Rect safeInsets, Insets waterfallInsets, Bounds bounds) { 318 mSafeInsets = safeInsets; 319 mWaterfallInsets = waterfallInsets == null ? Insets.NONE : waterfallInsets; 320 mBounds = bounds; 321 322 } 323 getCopyOrRef(Rect r, boolean copyArguments)324 private static Rect getCopyOrRef(Rect r, boolean copyArguments) { 325 if (r == null) { 326 return ZERO_RECT; 327 } else if (copyArguments) { 328 return new Rect(r); 329 } else { 330 return r; 331 } 332 } 333 334 /** 335 * Returns the insets representing the curved areas of a waterfall display. 336 * 337 * A waterfall display has curved areas along the edges of the screen. Apps should be careful 338 * when showing UI and handling touch input in those insets because the curve may impair 339 * legibility and can frequently lead to unintended touch inputs. 340 * 341 * @return the insets for the curved areas of a waterfall display in pixels or {@code 342 * Insets.NONE} if there are no curved areas or they don't overlap with the window. 343 */ getWaterfallInsets()344 public @NonNull Insets getWaterfallInsets() { 345 return mWaterfallInsets; 346 } 347 348 349 /** 350 * Find the position of the bounding rect, and create an array of Rect whose index represents 351 * the position (= BoundsPosition). 352 * 353 * @hide 354 */ extractBoundsFromList(Rect safeInsets, List<Rect> boundingRects)355 public static Rect[] extractBoundsFromList(Rect safeInsets, List<Rect> boundingRects) { 356 Rect[] sortedBounds = new Rect[BOUNDS_POSITION_LENGTH]; 357 for (int i = 0; i < sortedBounds.length; ++i) { 358 sortedBounds[i] = ZERO_RECT; 359 } 360 if (safeInsets != null && boundingRects != null) { 361 // There is at most one non-functional area per short edge of the device, but none 362 // on the long edges, so either a) safeInsets.top and safeInsets.bottom is 0, or 363 // b) safeInsets.left and safeInset.right is 0. 364 final boolean topBottomInset = safeInsets.top > 0 || safeInsets.bottom > 0; 365 for (Rect bound : boundingRects) { 366 if (topBottomInset) { 367 if (bound.top == 0) { 368 sortedBounds[BOUNDS_POSITION_TOP] = bound; 369 } else { 370 sortedBounds[BOUNDS_POSITION_BOTTOM] = bound; 371 } 372 } else { 373 if (bound.left == 0) { 374 sortedBounds[BOUNDS_POSITION_LEFT] = bound; 375 } else { 376 sortedBounds[BOUNDS_POSITION_RIGHT] = bound; 377 } 378 } 379 } 380 } 381 return sortedBounds; 382 } 383 384 /** 385 * Returns true if there is no cutout, i.e. the bounds are empty. 386 * 387 * @hide 388 */ isBoundsEmpty()389 public boolean isBoundsEmpty() { 390 return mBounds.isEmpty(); 391 } 392 393 /** 394 * Returns true if the safe insets are empty (and therefore the current view does not 395 * overlap with the cutout or cutout area). 396 * 397 * @hide 398 */ isEmpty()399 public boolean isEmpty() { 400 return mSafeInsets.equals(ZERO_RECT); 401 } 402 403 /** 404 * Returns the inset from the top which avoids the display cutout in pixels. 405 * 406 * @see WindowInsets.Type#displayCutout() 407 */ getSafeInsetTop()408 public int getSafeInsetTop() { 409 return mSafeInsets.top; 410 } 411 412 /** 413 * Returns the inset from the bottom which avoids the display cutout in pixels. 414 * 415 * @see WindowInsets.Type#displayCutout() 416 */ getSafeInsetBottom()417 public int getSafeInsetBottom() { 418 return mSafeInsets.bottom; 419 } 420 421 /** 422 * Returns the inset from the left which avoids the display cutout in pixels. 423 * 424 * @see WindowInsets.Type#displayCutout() 425 */ getSafeInsetLeft()426 public int getSafeInsetLeft() { 427 return mSafeInsets.left; 428 } 429 430 /** 431 * Returns the inset from the right which avoids the display cutout in pixels. 432 * 433 * @see WindowInsets.Type#displayCutout() 434 */ getSafeInsetRight()435 public int getSafeInsetRight() { 436 return mSafeInsets.right; 437 } 438 439 /** 440 * Returns the safe insets in a rect in pixel units. 441 * 442 * @return a rect which is set to the safe insets. 443 * @hide 444 */ getSafeInsets()445 public Rect getSafeInsets() { 446 return new Rect(mSafeInsets); 447 } 448 449 /** 450 * Returns a list of {@code Rect}s, each of which is the bounding rectangle for a non-functional 451 * area on the display. 452 * 453 * There will be at most one non-functional area per short edge of the device, and none on 454 * the long edges. 455 * 456 * @return a list of bounding {@code Rect}s, one for each display cutout area. No empty Rect is 457 * returned. 458 */ 459 @NonNull getBoundingRects()460 public List<Rect> getBoundingRects() { 461 List<Rect> result = new ArrayList<>(); 462 for (Rect bound : getBoundingRectsAll()) { 463 if (!bound.isEmpty()) { 464 result.add(new Rect(bound)); 465 } 466 } 467 return result; 468 } 469 470 /** 471 * Returns an array of {@code Rect}s, each of which is the bounding rectangle for a non- 472 * functional area on the display. Ordinal value of BoundPosition is used as an index of 473 * the array. 474 * 475 * There will be at most one non-functional area per short edge of the device, and none on 476 * the long edges. 477 * 478 * @return an array of bounding {@code Rect}s, one for each display cutout area. This might 479 * contain ZERO_RECT, which means there is no cutout area at the position. 480 * 481 * @hide 482 */ getBoundingRectsAll()483 public Rect[] getBoundingRectsAll() { 484 return mBounds.getRects(); 485 } 486 487 /** 488 * Returns a bounding rectangle for a non-functional area on the display which is located on 489 * the left of the screen. 490 * 491 * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle 492 * is returned. 493 */ getBoundingRectLeft()494 public @NonNull Rect getBoundingRectLeft() { 495 return mBounds.getRect(BOUNDS_POSITION_LEFT); 496 } 497 498 /** 499 * Returns a bounding rectangle for a non-functional area on the display which is located on 500 * the top of the screen. 501 * 502 * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle 503 * is returned. 504 */ getBoundingRectTop()505 public @NonNull Rect getBoundingRectTop() { 506 return mBounds.getRect(BOUNDS_POSITION_TOP); 507 } 508 509 /** 510 * Returns a bounding rectangle for a non-functional area on the display which is located on 511 * the right of the screen. 512 * 513 * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle 514 * is returned. 515 */ getBoundingRectRight()516 public @NonNull Rect getBoundingRectRight() { 517 return mBounds.getRect(BOUNDS_POSITION_RIGHT); 518 } 519 520 /** 521 * Returns a bounding rectangle for a non-functional area on the display which is located on 522 * the bottom of the screen. 523 * 524 * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle 525 * is returned. 526 */ getBoundingRectBottom()527 public @NonNull Rect getBoundingRectBottom() { 528 return mBounds.getRect(BOUNDS_POSITION_BOTTOM); 529 } 530 531 @Override hashCode()532 public int hashCode() { 533 return (mSafeInsets.hashCode() * 48271 + mBounds.hashCode()) * 48271 534 + mWaterfallInsets.hashCode(); 535 } 536 537 @Override equals(Object o)538 public boolean equals(Object o) { 539 if (o == this) { 540 return true; 541 } 542 if (o instanceof DisplayCutout) { 543 DisplayCutout c = (DisplayCutout) o; 544 return mSafeInsets.equals(c.mSafeInsets) && mBounds.equals(c.mBounds) 545 && mWaterfallInsets.equals(c.mWaterfallInsets); 546 } 547 return false; 548 } 549 550 @Override toString()551 public String toString() { 552 return "DisplayCutout{insets=" + mSafeInsets 553 + " waterfall=" + mWaterfallInsets 554 + " boundingRect={" + mBounds + "}" 555 + "}"; 556 } 557 558 /** 559 * @hide 560 */ dumpDebug(ProtoOutputStream proto, long fieldId)561 public void dumpDebug(ProtoOutputStream proto, long fieldId) { 562 final long token = proto.start(fieldId); 563 mSafeInsets.dumpDebug(proto, INSETS); 564 mBounds.getRect(BOUNDS_POSITION_LEFT).dumpDebug(proto, BOUND_LEFT); 565 mBounds.getRect(BOUNDS_POSITION_TOP).dumpDebug(proto, BOUND_TOP); 566 mBounds.getRect(BOUNDS_POSITION_RIGHT).dumpDebug(proto, BOUND_RIGHT); 567 mBounds.getRect(BOUNDS_POSITION_BOTTOM).dumpDebug(proto, BOUND_BOTTOM); 568 mWaterfallInsets.toRect().dumpDebug(proto, INSETS); 569 proto.end(token); 570 } 571 572 /** 573 * Insets the reference frame of the cutout in the given directions. 574 * 575 * @return a copy of this instance which has been inset 576 * @hide 577 */ inset(int insetLeft, int insetTop, int insetRight, int insetBottom)578 public DisplayCutout inset(int insetLeft, int insetTop, int insetRight, int insetBottom) { 579 if (insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0 580 || (isBoundsEmpty() && mWaterfallInsets.equals(Insets.NONE))) { 581 return this; 582 } 583 584 Rect safeInsets = insetInsets(insetLeft, insetTop, insetRight, insetBottom, 585 new Rect(mSafeInsets)); 586 587 // If we are not cutting off part of the cutout by insetting it on bottom/right, and we also 588 // don't move it around, we can avoid the allocation and copy of the instance. 589 if (insetLeft == 0 && insetTop == 0 && mSafeInsets.equals(safeInsets)) { 590 return this; 591 } 592 593 Rect waterfallInsets = insetInsets(insetLeft, insetTop, insetRight, insetBottom, 594 mWaterfallInsets.toRect()); 595 596 Rect[] bounds = mBounds.getRects(); 597 for (int i = 0; i < bounds.length; ++i) { 598 if (!bounds[i].equals(ZERO_RECT)) { 599 bounds[i].offset(-insetLeft, -insetTop); 600 } 601 } 602 603 return new DisplayCutout(safeInsets, Insets.of(waterfallInsets), bounds, 604 false /* copyArguments */); 605 } 606 insetInsets(int insetLeft, int insetTop, int insetRight, int insetBottom, Rect insets)607 private Rect insetInsets(int insetLeft, int insetTop, int insetRight, int insetBottom, 608 Rect insets) { 609 // Note: it's not really well defined what happens when the inset is negative, because we 610 // don't know if the safe inset needs to expand in general. 611 if (insetTop > 0 || insets.top > 0) { 612 insets.top = atLeastZero(insets.top - insetTop); 613 } 614 if (insetBottom > 0 || insets.bottom > 0) { 615 insets.bottom = atLeastZero(insets.bottom - insetBottom); 616 } 617 if (insetLeft > 0 || insets.left > 0) { 618 insets.left = atLeastZero(insets.left - insetLeft); 619 } 620 if (insetRight > 0 || insets.right > 0) { 621 insets.right = atLeastZero(insets.right - insetRight); 622 } 623 return insets; 624 } 625 626 /** 627 * Returns a copy of this instance with the safe insets replaced with the parameter. 628 * 629 * @param safeInsets the new safe insets in pixels 630 * @return a copy of this instance with the safe insets replaced with the argument. 631 * 632 * @hide 633 */ replaceSafeInsets(Rect safeInsets)634 public DisplayCutout replaceSafeInsets(Rect safeInsets) { 635 return new DisplayCutout(new Rect(safeInsets), mWaterfallInsets, mBounds); 636 } 637 atLeastZero(int value)638 private static int atLeastZero(int value) { 639 return value < 0 ? 0 : value; 640 } 641 642 643 /** 644 * Creates an instance from a bounding rect. 645 * 646 * @hide 647 */ 648 @VisibleForTesting fromBoundingRect( int left, int top, int right, int bottom, @BoundsPosition int pos)649 public static DisplayCutout fromBoundingRect( 650 int left, int top, int right, int bottom, @BoundsPosition int pos) { 651 Rect[] bounds = new Rect[BOUNDS_POSITION_LENGTH]; 652 for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) { 653 bounds[i] = (pos == i) ? new Rect(left, top, right, bottom) : new Rect(); 654 } 655 return new DisplayCutout(ZERO_RECT, Insets.NONE, bounds, false /* copyArguments */); 656 } 657 658 /** 659 * Creates an instance from a bounding and waterfall insets. 660 * 661 * @hide 662 */ fromBoundsAndWaterfall(Rect[] bounds, Insets waterfallInsets)663 public static DisplayCutout fromBoundsAndWaterfall(Rect[] bounds, Insets waterfallInsets) { 664 return new DisplayCutout(ZERO_RECT, waterfallInsets, bounds, false /* copyArguments */); 665 } 666 667 /** 668 * Creates an instance from a bounding {@link Path}. 669 * 670 * @hide 671 */ fromBounds(Rect[] bounds)672 public static DisplayCutout fromBounds(Rect[] bounds) { 673 return new DisplayCutout(ZERO_RECT, Insets.NONE, bounds, false /* copyArguments */); 674 } 675 676 /** 677 * Creates the display cutout according to 678 * @android:string/config_mainBuiltInDisplayCutoutRectApproximation, which is the closest 679 * rectangle-base approximation of the cutout. 680 * 681 * @hide 682 */ fromResourcesRectApproximation(Resources res, int displayWidth, int displayHeight)683 public static DisplayCutout fromResourcesRectApproximation(Resources res, int displayWidth, int displayHeight) { 684 return fromSpec(res.getString(R.string.config_mainBuiltInDisplayCutoutRectApproximation), 685 displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT, 686 loadWaterfallInset(res)); 687 } 688 689 /** 690 * Creates an instance according to @android:string/config_mainBuiltInDisplayCutout. 691 * 692 * @hide 693 */ pathFromResources(Resources res, int displayWidth, int displayHeight)694 public static Path pathFromResources(Resources res, int displayWidth, int displayHeight) { 695 return pathAndDisplayCutoutFromSpec( 696 res.getString(R.string.config_mainBuiltInDisplayCutout), 697 displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT, 698 loadWaterfallInset(res)).first; 699 } 700 701 /** 702 * Creates an instance according to the supplied {@link android.util.PathParser.PathData} spec. 703 * 704 * @hide 705 */ 706 @VisibleForTesting(visibility = PRIVATE) fromSpec(String spec, int displayWidth, int displayHeight, float density, Insets waterfallInsets)707 public static DisplayCutout fromSpec(String spec, int displayWidth, int displayHeight, 708 float density, Insets waterfallInsets) { 709 return pathAndDisplayCutoutFromSpec( 710 spec, displayWidth, displayHeight, density, waterfallInsets).second; 711 } 712 pathAndDisplayCutoutFromSpec(String spec, int displayWidth, int displayHeight, float density, Insets waterfallInsets)713 private static Pair<Path, DisplayCutout> pathAndDisplayCutoutFromSpec(String spec, 714 int displayWidth, int displayHeight, float density, Insets waterfallInsets) { 715 if (TextUtils.isEmpty(spec) && waterfallInsets.equals(Insets.NONE)) { 716 return NULL_PAIR; 717 } 718 719 synchronized (CACHE_LOCK) { 720 if (spec.equals(sCachedSpec) && sCachedDisplayWidth == displayWidth 721 && sCachedDisplayHeight == displayHeight 722 && sCachedDensity == density 723 && waterfallInsets.equals(sCachedWaterfallInsets)) { 724 return sCachedCutout; 725 } 726 } 727 728 spec = spec.trim(); 729 730 CutoutSpecification cutoutSpec = new CutoutSpecification.Parser(density, 731 displayWidth, displayHeight).parse(spec); 732 Rect safeInset = cutoutSpec.getSafeInset(); 733 final Rect boundLeft = cutoutSpec.getLeftBound(); 734 final Rect boundTop = cutoutSpec.getTopBound(); 735 final Rect boundRight = cutoutSpec.getRightBound(); 736 final Rect boundBottom = cutoutSpec.getBottomBound(); 737 738 739 if (!waterfallInsets.equals(Insets.NONE)) { 740 safeInset.set( 741 Math.max(waterfallInsets.left, safeInset.left), 742 Math.max(waterfallInsets.top, safeInset.top), 743 Math.max(waterfallInsets.right, safeInset.right), 744 Math.max(waterfallInsets.bottom, safeInset.bottom)); 745 } 746 747 final DisplayCutout cutout = new DisplayCutout( 748 safeInset, waterfallInsets, boundLeft, boundTop, 749 boundRight, boundBottom, false /* copyArguments */); 750 final Pair<Path, DisplayCutout> result = new Pair<>(cutoutSpec.getPath(), cutout); 751 synchronized (CACHE_LOCK) { 752 sCachedSpec = spec; 753 sCachedDisplayWidth = displayWidth; 754 sCachedDisplayHeight = displayHeight; 755 sCachedDensity = density; 756 sCachedCutout = result; 757 sCachedWaterfallInsets = waterfallInsets; 758 } 759 return result; 760 } 761 loadWaterfallInset(Resources res)762 private static Insets loadWaterfallInset(Resources res) { 763 return Insets.of( 764 res.getDimensionPixelSize(R.dimen.waterfall_display_left_edge_size), 765 res.getDimensionPixelSize(R.dimen.waterfall_display_top_edge_size), 766 res.getDimensionPixelSize(R.dimen.waterfall_display_right_edge_size), 767 res.getDimensionPixelSize(R.dimen.waterfall_display_bottom_edge_size)); 768 } 769 770 /** 771 * Helper class for passing {@link DisplayCutout} through binder. 772 * 773 * Needed, because {@code readFromParcel} cannot be used with immutable classes. 774 * 775 * @hide 776 */ 777 public static final class ParcelableWrapper implements Parcelable { 778 779 private DisplayCutout mInner; 780 ParcelableWrapper()781 public ParcelableWrapper() { 782 this(NO_CUTOUT); 783 } 784 ParcelableWrapper(DisplayCutout cutout)785 public ParcelableWrapper(DisplayCutout cutout) { 786 mInner = cutout; 787 } 788 789 @Override describeContents()790 public int describeContents() { 791 return 0; 792 } 793 794 @Override writeToParcel(Parcel out, int flags)795 public void writeToParcel(Parcel out, int flags) { 796 writeCutoutToParcel(mInner, out, flags); 797 } 798 799 /** 800 * Writes a DisplayCutout to a {@link Parcel}. 801 * 802 * @see #readCutoutFromParcel(Parcel) 803 */ writeCutoutToParcel(DisplayCutout cutout, Parcel out, int flags)804 public static void writeCutoutToParcel(DisplayCutout cutout, Parcel out, int flags) { 805 if (cutout == null) { 806 out.writeInt(-1); 807 } else if (cutout == NO_CUTOUT) { 808 out.writeInt(0); 809 } else { 810 out.writeInt(1); 811 out.writeTypedObject(cutout.mSafeInsets, flags); 812 out.writeTypedArray(cutout.mBounds.getRects(), flags); 813 out.writeTypedObject(cutout.mWaterfallInsets, flags); 814 } 815 } 816 817 /** 818 * Similar to {@link Creator#createFromParcel(Parcel)}, but reads into an existing 819 * instance. 820 * 821 * Needed for AIDL out parameters. 822 */ readFromParcel(Parcel in)823 public void readFromParcel(Parcel in) { 824 mInner = readCutoutFromParcel(in); 825 } 826 827 public static final @android.annotation.NonNull Creator<ParcelableWrapper> CREATOR = new Creator<ParcelableWrapper>() { 828 @Override 829 public ParcelableWrapper createFromParcel(Parcel in) { 830 return new ParcelableWrapper(readCutoutFromParcel(in)); 831 } 832 833 @Override 834 public ParcelableWrapper[] newArray(int size) { 835 return new ParcelableWrapper[size]; 836 } 837 }; 838 839 /** 840 * Reads a DisplayCutout from a {@link Parcel}. 841 * 842 * @see #writeCutoutToParcel(DisplayCutout, Parcel, int) 843 */ readCutoutFromParcel(Parcel in)844 public static DisplayCutout readCutoutFromParcel(Parcel in) { 845 int variant = in.readInt(); 846 if (variant == -1) { 847 return null; 848 } 849 if (variant == 0) { 850 return NO_CUTOUT; 851 } 852 853 Rect safeInsets = in.readTypedObject(Rect.CREATOR); 854 Rect[] bounds = new Rect[BOUNDS_POSITION_LENGTH]; 855 in.readTypedArray(bounds, Rect.CREATOR); 856 Insets waterfallInsets = in.readTypedObject(Insets.CREATOR); 857 858 return new DisplayCutout( 859 safeInsets, waterfallInsets, bounds, false /* copyArguments */); 860 } 861 get()862 public DisplayCutout get() { 863 return mInner; 864 } 865 set(ParcelableWrapper cutout)866 public void set(ParcelableWrapper cutout) { 867 mInner = cutout.get(); 868 } 869 set(DisplayCutout cutout)870 public void set(DisplayCutout cutout) { 871 mInner = cutout; 872 } 873 874 @Override hashCode()875 public int hashCode() { 876 return mInner.hashCode(); 877 } 878 879 @Override equals(Object o)880 public boolean equals(Object o) { 881 return o instanceof ParcelableWrapper 882 && mInner.equals(((ParcelableWrapper) o).mInner); 883 } 884 885 @Override toString()886 public String toString() { 887 return String.valueOf(mInner); 888 } 889 } 890 } 891