Loading core/java/android/view/DisplayCutout.java +27 −18 Original line number Original line Diff line number Diff line Loading @@ -554,26 +554,12 @@ public final class DisplayCutout { */ */ public DisplayCutout inset(int insetLeft, int insetTop, int insetRight, int insetBottom) { public DisplayCutout inset(int insetLeft, int insetTop, int insetRight, int insetBottom) { if (insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0 if (insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0 || isBoundsEmpty()) { || (isBoundsEmpty() && mWaterfallInsets.equals(Insets.NONE))) { return this; return this; } } Rect safeInsets = new Rect(mSafeInsets); Rect safeInsets = insetInsets(insetLeft, insetTop, insetRight, insetBottom, 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); } // If we are not cutting off part of the cutout by insetting it on bottom/right, and we also // 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. // don't move it around, we can avoid the allocation and copy of the instance. Loading @@ -581,6 +567,9 @@ public final class DisplayCutout { return this; return this; } } Rect waterfallInsets = insetInsets(insetLeft, insetTop, insetRight, insetBottom, mWaterfallInsets.toRect()); Rect[] bounds = mBounds.getRects(); Rect[] bounds = mBounds.getRects(); for (int i = 0; i < bounds.length; ++i) { for (int i = 0; i < bounds.length; ++i) { if (!bounds[i].equals(ZERO_RECT)) { if (!bounds[i].equals(ZERO_RECT)) { Loading @@ -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; } } /** /** Loading core/tests/coretests/src/android/view/DisplayCutoutTest.java +12 −1 Original line number Original line Diff line number Diff line Loading @@ -229,6 +229,16 @@ public class DisplayCutoutTest { assertEquals(cutout.getSafeInsetBottom(), 96); assertEquals(cutout.getSafeInsetBottom(), 96); } } @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 @Test public void inset_insets_consumeInset() throws Exception { public void inset_insets_consumeInset() throws Exception { DisplayCutout cutout = mCutoutTop.inset(0, 1000, 0, 0); DisplayCutout cutout = mCutoutTop.inset(0, 1000, 0, 0); Loading Loading @@ -457,7 +467,8 @@ public class DisplayCutoutTest { private static DisplayCutout createCutoutWaterfallOnly(Insets waterfallInsets) { private static DisplayCutout createCutoutWaterfallOnly(Insets waterfallInsets) { return new DisplayCutout( 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, ZERO_RECT, ZERO_RECT, ZERO_RECT, Loading Loading
core/java/android/view/DisplayCutout.java +27 −18 Original line number Original line Diff line number Diff line Loading @@ -554,26 +554,12 @@ public final class DisplayCutout { */ */ public DisplayCutout inset(int insetLeft, int insetTop, int insetRight, int insetBottom) { public DisplayCutout inset(int insetLeft, int insetTop, int insetRight, int insetBottom) { if (insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0 if (insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0 || isBoundsEmpty()) { || (isBoundsEmpty() && mWaterfallInsets.equals(Insets.NONE))) { return this; return this; } } Rect safeInsets = new Rect(mSafeInsets); Rect safeInsets = insetInsets(insetLeft, insetTop, insetRight, insetBottom, 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); } // If we are not cutting off part of the cutout by insetting it on bottom/right, and we also // 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. // don't move it around, we can avoid the allocation and copy of the instance. Loading @@ -581,6 +567,9 @@ public final class DisplayCutout { return this; return this; } } Rect waterfallInsets = insetInsets(insetLeft, insetTop, insetRight, insetBottom, mWaterfallInsets.toRect()); Rect[] bounds = mBounds.getRects(); Rect[] bounds = mBounds.getRects(); for (int i = 0; i < bounds.length; ++i) { for (int i = 0; i < bounds.length; ++i) { if (!bounds[i].equals(ZERO_RECT)) { if (!bounds[i].equals(ZERO_RECT)) { Loading @@ -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; } } /** /** Loading
core/tests/coretests/src/android/view/DisplayCutoutTest.java +12 −1 Original line number Original line Diff line number Diff line Loading @@ -229,6 +229,16 @@ public class DisplayCutoutTest { assertEquals(cutout.getSafeInsetBottom(), 96); assertEquals(cutout.getSafeInsetBottom(), 96); } } @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 @Test public void inset_insets_consumeInset() throws Exception { public void inset_insets_consumeInset() throws Exception { DisplayCutout cutout = mCutoutTop.inset(0, 1000, 0, 0); DisplayCutout cutout = mCutoutTop.inset(0, 1000, 0, 0); Loading Loading @@ -457,7 +467,8 @@ public class DisplayCutoutTest { private static DisplayCutout createCutoutWaterfallOnly(Insets waterfallInsets) { private static DisplayCutout createCutoutWaterfallOnly(Insets waterfallInsets) { return new DisplayCutout( 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, ZERO_RECT, ZERO_RECT, ZERO_RECT, Loading