diff options
| -rw-r--r-- | core/java/android/view/DisplayCutout.java | 45 | ||||
| -rw-r--r-- | core/tests/coretests/src/android/view/DisplayCutoutTest.java | 13 |
2 files changed, 39 insertions, 19 deletions
diff --git a/core/java/android/view/DisplayCutout.java b/core/java/android/view/DisplayCutout.java index 31fc16188814..0ab856ec5b4d 100644 --- a/core/java/android/view/DisplayCutout.java +++ b/core/java/android/view/DisplayCutout.java @@ -554,26 +554,12 @@ public final class DisplayCutout { */ public DisplayCutout inset(int insetLeft, int insetTop, int insetRight, int insetBottom) { if (insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0 - || isBoundsEmpty()) { + || (isBoundsEmpty() && mWaterfallInsets.equals(Insets.NONE))) { return this; } - Rect safeInsets = new Rect(mSafeInsets); - - // Note: it's not really well defined what happens when the inset is negative, because we - // don't know if the safe inset needs to expand in general. - if (insetTop > 0 || safeInsets.top > 0) { - safeInsets.top = atLeastZero(safeInsets.top - insetTop); - } - if (insetBottom > 0 || safeInsets.bottom > 0) { - safeInsets.bottom = atLeastZero(safeInsets.bottom - insetBottom); - } - if (insetLeft > 0 || safeInsets.left > 0) { - safeInsets.left = atLeastZero(safeInsets.left - insetLeft); - } - if (insetRight > 0 || safeInsets.right > 0) { - safeInsets.right = atLeastZero(safeInsets.right - insetRight); - } + Rect safeInsets = insetInsets(insetLeft, insetTop, insetRight, insetBottom, + new Rect(mSafeInsets)); // If we are not cutting off part of the cutout by insetting it on bottom/right, and we also // don't move it around, we can avoid the allocation and copy of the instance. @@ -581,6 +567,9 @@ public final class DisplayCutout { return this; } + Rect waterfallInsets = insetInsets(insetLeft, insetTop, insetRight, insetBottom, + mWaterfallInsets.toRect()); + Rect[] bounds = mBounds.getRects(); for (int i = 0; i < bounds.length; ++i) { if (!bounds[i].equals(ZERO_RECT)) { @@ -588,7 +577,27 @@ public final class DisplayCutout { } } - return new DisplayCutout(safeInsets, mWaterfallInsets, bounds, false /* copyArguments */); + return new DisplayCutout(safeInsets, Insets.of(waterfallInsets), bounds, + false /* copyArguments */); + } + + private Rect insetInsets(int insetLeft, int insetTop, int insetRight, int insetBottom, + Rect insets) { + // Note: it's not really well defined what happens when the inset is negative, because we + // don't know if the safe inset needs to expand in general. + if (insetTop > 0 || insets.top > 0) { + insets.top = atLeastZero(insets.top - insetTop); + } + if (insetBottom > 0 || insets.bottom > 0) { + insets.bottom = atLeastZero(insets.bottom - insetBottom); + } + if (insetLeft > 0 || insets.left > 0) { + insets.left = atLeastZero(insets.left - insetLeft); + } + if (insetRight > 0 || insets.right > 0) { + insets.right = atLeastZero(insets.right - insetRight); + } + return insets; } /** diff --git a/core/tests/coretests/src/android/view/DisplayCutoutTest.java b/core/tests/coretests/src/android/view/DisplayCutoutTest.java index 7c2b98f3e167..d02c6d588585 100644 --- a/core/tests/coretests/src/android/view/DisplayCutoutTest.java +++ b/core/tests/coretests/src/android/view/DisplayCutoutTest.java @@ -230,6 +230,16 @@ public class DisplayCutoutTest { } @Test + public void inset_insets_withWaterfallCutout() throws Exception { + DisplayCutout cutout = createCutoutWaterfallOnly(Insets.of(0, 10, 0, 10)).inset(1, 2, 3, 4); + + assertEquals(cutout.getSafeInsetLeft(), 0); + assertEquals(cutout.getSafeInsetTop(), 8); + assertEquals(cutout.getSafeInsetRight(), 0); + assertEquals(cutout.getSafeInsetBottom(), 6); + } + + @Test public void inset_insets_consumeInset() throws Exception { DisplayCutout cutout = mCutoutTop.inset(0, 1000, 0, 0); @@ -457,7 +467,8 @@ public class DisplayCutoutTest { private static DisplayCutout createCutoutWaterfallOnly(Insets waterfallInsets) { return new DisplayCutout( - Insets.of(20, 0, 20, 0), + Insets.of(waterfallInsets.left, waterfallInsets.top, waterfallInsets.right, + waterfallInsets.bottom), ZERO_RECT, ZERO_RECT, ZERO_RECT, |