diff --git a/api/current.txt b/api/current.txt index c132efb31ce47..7f7c2ede4418f 100755 --- a/api/current.txt +++ b/api/current.txt @@ -46630,7 +46630,12 @@ package android.view { } public final class DisplayCutout { - ctor public DisplayCutout(android.graphics.Rect, java.util.List); + ctor public DisplayCutout(android.graphics.Insets, android.graphics.Rect, android.graphics.Rect, android.graphics.Rect, android.graphics.Rect); + ctor public deprecated DisplayCutout(android.graphics.Rect, java.util.List); + method public android.graphics.Rect getBoundingRectBottom(); + method public android.graphics.Rect getBoundingRectLeft(); + method public android.graphics.Rect getBoundingRectRight(); + method public android.graphics.Rect getBoundingRectTop(); method public java.util.List getBoundingRects(); method public int getSafeInsetBottom(); method public int getSafeInsetLeft(); diff --git a/core/java/android/view/DisplayCutout.java b/core/java/android/view/DisplayCutout.java index 5f80d31651a8c..f428c798d7f59 100644 --- a/core/java/android/view/DisplayCutout.java +++ b/core/java/android/view/DisplayCutout.java @@ -18,12 +18,19 @@ package android.view; import static android.util.DisplayMetrics.DENSITY_DEFAULT; import static android.util.DisplayMetrics.DENSITY_DEVICE_STABLE; -import static android.view.DisplayCutoutProto.BOUNDS; +import static android.view.DisplayCutoutProto.BOUND_BOTTOM; +import static android.view.DisplayCutoutProto.BOUND_LEFT; +import static android.view.DisplayCutoutProto.BOUND_RIGHT; +import static android.view.DisplayCutoutProto.BOUND_TOP; import static android.view.DisplayCutoutProto.INSETS; import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE; +import android.annotation.IntDef; +import android.annotation.NonNull; +import android.annotation.Nullable; import android.content.res.Resources; +import android.graphics.Insets; import android.graphics.Matrix; import android.graphics.Path; import android.graphics.Rect; @@ -42,7 +49,10 @@ import com.android.internal.R; import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** @@ -68,14 +78,14 @@ public final class DisplayCutout { "com.android.internal.display_cutout_emulation"; private static final Rect ZERO_RECT = new Rect(); - private static final Region EMPTY_REGION = new Region(); /** * An instance where {@link #isEmpty()} returns {@code true}. * * @hide */ - public static final DisplayCutout NO_CUTOUT = new DisplayCutout(ZERO_RECT, EMPTY_REGION, + public static final DisplayCutout NO_CUTOUT = new DisplayCutout( + ZERO_RECT, ZERO_RECT, ZERO_RECT, ZERO_RECT, ZERO_RECT, false /* copyArguments */); @@ -94,7 +104,152 @@ public final class DisplayCutout { private static Pair sCachedCutout = NULL_PAIR; private final Rect mSafeInsets; - private final Region mBounds; + + + /** + * The bound is at the left of the screen. + * @hide + */ + public static final int BOUNDS_POSITION_LEFT = 0; + + /** + * The bound is at the top of the screen. + * @hide + */ + public static final int BOUNDS_POSITION_TOP = 1; + + /** + * The bound is at the right of the screen. + * @hide + */ + public static final int BOUNDS_POSITION_RIGHT = 2; + + /** + * The bound is at the bottom of the screen. + * @hide + */ + public static final int BOUNDS_POSITION_BOTTOM = 3; + + /** + * The number of possible positions at which bounds can be located. + * @hide + */ + public static final int BOUNDS_POSITION_LENGTH = 4; + + /** @hide */ + @IntDef(prefix = { "BOUNDS_POSITION_" }, value = { + BOUNDS_POSITION_LEFT, + BOUNDS_POSITION_TOP, + BOUNDS_POSITION_RIGHT, + BOUNDS_POSITION_BOTTOM + }) + @Retention(RetentionPolicy.SOURCE) + public @interface BoundsPosition {} + + private static class Bounds { + private final Rect[] mRects; + + private Bounds(Rect left, Rect top, Rect right, Rect bottom, boolean copyArguments) { + mRects = new Rect[BOUNDS_POSITION_LENGTH]; + mRects[BOUNDS_POSITION_LEFT] = getCopyOrRef(left, copyArguments); + mRects[BOUNDS_POSITION_TOP] = getCopyOrRef(top, copyArguments); + mRects[BOUNDS_POSITION_RIGHT] = getCopyOrRef(right, copyArguments); + mRects[BOUNDS_POSITION_BOTTOM] = getCopyOrRef(bottom, copyArguments); + + } + + private Bounds(Rect[] rects, boolean copyArguments) { + if (rects.length != BOUNDS_POSITION_LENGTH) { + throw new IllegalArgumentException( + "rects must have exactly 4 elements: rects=" + Arrays.toString(rects)); + } + if (copyArguments) { + mRects = new Rect[BOUNDS_POSITION_LENGTH]; + for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) { + mRects[i] = new Rect(rects[i]); + } + } else { + for (Rect rect : rects) { + if (rect == null) { + throw new IllegalArgumentException( + "rects must have non-null elements: rects=" + + Arrays.toString(rects)); + } + } + mRects = rects; + } + } + + private boolean isEmpty() { + for (Rect rect : mRects) { + if (!rect.isEmpty()) { + return false; + } + } + return true; + } + + private Rect getRect(@BoundsPosition int pos) { + return new Rect(mRects[pos]); + } + + private Rect[] getRects() { + Rect[] rects = new Rect[BOUNDS_POSITION_LENGTH]; + for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) { + rects[i] = new Rect(mRects[i]); + } + return rects; + } + + @Override + public int hashCode() { + int result = 0; + for (Rect rect : mRects) { + result = result * 48271 + rect.hashCode(); + } + return result; + } + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } + if (o instanceof Bounds) { + Bounds b = (Bounds) o; + return Arrays.deepEquals(mRects, b.mRects); + } + return false; + } + + @Override + public String toString() { + return "Bounds=" + Arrays.toString(mRects); + } + + } + + private final Bounds mBounds; + + /** + * Creates a DisplayCutout instance. + * + * @param safeInsets the insets from each edge which avoid the display cutout as returned by + * {@link #getSafeInsetTop()} etc. + * @param boundLeft the left bounding rect of the display cutout in pixels. If null is passed, + * it's treated as an empty rectangle (0,0)-(0,0). + * @param boundTop the top bounding rect of the display cutout in pixels. If null is passed, + * it's treated as an empty rectangle (0,0)-(0,0). + * @param boundRight the right bounding rect of the display cutout in pixels. If null is + * passed, it's treated as an empty rectangle (0,0)-(0,0). + * @param boundBottom the bottom bounding rect of the display cutout in pixels. If null is + * passed, it's treated as an empty rectangle (0,0)-(0,0). + */ + // TODO(b/73953958): @VisibleForTesting(visibility = PRIVATE) + public DisplayCutout( + Insets safeInsets, @Nullable Rect boundLeft, @Nullable Rect boundTop, + @Nullable Rect boundRight, @Nullable Rect boundBottom) { + this(safeInsets.toRect(), boundLeft, boundTop, boundRight, boundBottom, true); + } /** * Creates a DisplayCutout instance. @@ -103,25 +258,85 @@ public final class DisplayCutout { * {@link #getSafeInsetTop()} etc. * @param boundingRects the bounding rects of the display cutouts as returned by * {@link #getBoundingRects()} ()}. + * @deprecated Use {@link DisplayCutout#DisplayCutout(Insets, Rect, Rect, Rect, Rect)} instead. */ // TODO(b/73953958): @VisibleForTesting(visibility = PRIVATE) + @Deprecated public DisplayCutout(Rect safeInsets, List boundingRects) { - this(safeInsets != null ? new Rect(safeInsets) : ZERO_RECT, - boundingRectsToRegion(boundingRects), + this(safeInsets, extractBoundsFromList(safeInsets, boundingRects), true /* copyArguments */); } /** * Creates a DisplayCutout instance. * + * @param safeInsets the insets from each edge which avoid the display cutout as returned by + * {@link #getSafeInsetTop()} etc. * @param copyArguments if true, create a copy of the arguments. If false, the passed arguments * are not copied and MUST remain unchanged forever. */ - private DisplayCutout(Rect safeInsets, Region bounds, boolean copyArguments) { - mSafeInsets = safeInsets == null ? ZERO_RECT : - (copyArguments ? new Rect(safeInsets) : safeInsets); - mBounds = bounds == null ? Region.obtain() : - (copyArguments ? Region.obtain(bounds) : bounds); + private DisplayCutout(Rect safeInsets, Rect boundLeft, Rect boundTop, Rect boundRight, + Rect boundBottom, boolean copyArguments) { + mSafeInsets = getCopyOrRef(safeInsets, copyArguments); + mBounds = new Bounds(boundLeft, boundTop, boundRight, boundBottom, copyArguments); + } + + private DisplayCutout(Rect safeInsets, Rect[] bounds, boolean copyArguments) { + mSafeInsets = getCopyOrRef(safeInsets, copyArguments); + mBounds = new Bounds(bounds, copyArguments); + } + + private DisplayCutout(Rect safeInsets, Bounds bounds) { + mSafeInsets = safeInsets; + mBounds = bounds; + + } + + private static Rect getCopyOrRef(Rect r, boolean copyArguments) { + if (r == null) { + return ZERO_RECT; + } else if (copyArguments) { + return new Rect(r); + } else { + return r; + } + } + + /** + * Find the position of the bounding rect, and create an array of Rect whose index represents + * the position (= BoundsPosition). + * + * @hide + */ + public static Rect[] extractBoundsFromList(Rect safeInsets, List boundingRects) { + Rect[] sortedBounds = new Rect[BOUNDS_POSITION_LENGTH]; + for (int i = 0; i < sortedBounds.length; ++i) { + sortedBounds[i] = ZERO_RECT; + } + for (Rect bound : boundingRects) { + // There will be at most one non-functional area per short edge of the device, and none + // on the long edges, so either safeInsets.right or safeInsets.bottom must be 0. + // TODO(b/117199965): Refine the logic to handle edge cases. + if (bound.left == 0) { + sortedBounds[BOUNDS_POSITION_LEFT] = bound; + } else if (bound.top == 0) { + sortedBounds[BOUNDS_POSITION_TOP] = bound; + } else if (safeInsets.right > 0) { + sortedBounds[BOUNDS_POSITION_RIGHT] = bound; + } else if (safeInsets.bottom > 0) { + sortedBounds[BOUNDS_POSITION_BOTTOM] = bound; + } + } + return sortedBounds; + } + + /** + * Returns true if there is no cutout, i.e. the bounds are empty. + * + * @hide + */ + public boolean isBoundsEmpty() { + return mBounds.isEmpty(); } /** @@ -134,15 +349,6 @@ public final class DisplayCutout { return mSafeInsets.equals(ZERO_RECT); } - /** - * Returns true if there is no cutout, i.e. the bounds are empty. - * - * @hide - */ - public boolean isBoundsEmpty() { - return mBounds.isEmpty(); - } - /** Returns the inset from the top which avoids the display cutout in pixels. */ public int getSafeInsetTop() { return mSafeInsets.top; @@ -173,30 +379,6 @@ public final class DisplayCutout { return new Rect(mSafeInsets); } - /** - * Returns the bounding region of the cutout. - * - *

- * Note: There may be more than one cutout, in which case the returned - * {@code Region} will be non-contiguous and its bounding rect will be meaningless without - * intersecting it first. - * - * Example: - *

-     *     // Getting the bounding rectangle of the top display cutout
-     *     Region bounds = displayCutout.getBounds();
-     *     bounds.op(0, 0, Integer.MAX_VALUE, displayCutout.getSafeInsetTop(), Region.Op.INTERSECT);
-     *     Rect topDisplayCutout = bounds.getBoundingRect();
-     * 
- * - * @return the bounding region of the cutout. Coordinates are relative - * to the top-left corner of the content view and in pixel units. - * @hide - */ - public Region getBounds() { - return Region.obtain(mBounds); - } - /** * Returns a list of {@code Rect}s, each of which is the bounding rectangle for a non-functional * area on the display. @@ -204,39 +386,83 @@ public final class DisplayCutout { * There will be at most one non-functional area per short edge of the device, and none on * the long edges. * - * @return a list of bounding {@code Rect}s, one for each display cutout area. + * @return a list of bounding {@code Rect}s, one for each display cutout area. No empty Rect is + * returned. */ public List getBoundingRects() { List result = new ArrayList<>(); - Region bounds = Region.obtain(); - // top - bounds.set(mBounds); - bounds.op(0, 0, Integer.MAX_VALUE, getSafeInsetTop(), Region.Op.INTERSECT); - if (!bounds.isEmpty()) { - result.add(bounds.getBounds()); + for (Rect bound : getBoundingRectsAll()) { + if (!bound.isEmpty()) { + result.add(new Rect(bound)); + } } - // left - bounds.set(mBounds); - bounds.op(0, 0, getSafeInsetLeft(), Integer.MAX_VALUE, Region.Op.INTERSECT); - if (!bounds.isEmpty()) { - result.add(bounds.getBounds()); - } - // right & bottom - bounds.set(mBounds); - bounds.op(getSafeInsetLeft() + 1, getSafeInsetTop() + 1, - Integer.MAX_VALUE, Integer.MAX_VALUE, Region.Op.INTERSECT); - if (!bounds.isEmpty()) { - result.add(bounds.getBounds()); - } - bounds.recycle(); return result; } + /** + * Returns an array of {@code Rect}s, each of which is the bounding rectangle for a non- + * functional area on the display. Ordinal value of BoundPosition is used as an index of + * the array. + * + * There will be at most one non-functional area per short edge of the device, and none on + * the long edges. + * + * @return an array of bounding {@code Rect}s, one for each display cutout area. This might + * contain ZERO_RECT, which means there is no cutout area at the position. + * + * @hide + */ + public Rect[] getBoundingRectsAll() { + return mBounds.getRects(); + } + + /** + * Returns a bounding rectangle for a non-functional area on the display which is located on + * the left of the screen. + * + * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle + * is returned. + */ + public @NonNull Rect getBoundingRectLeft() { + return mBounds.getRect(BOUNDS_POSITION_LEFT); + } + + /** + * Returns a bounding rectangle for a non-functional area on the display which is located on + * the top of the screen. + * + * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle + * is returned. + */ + public @NonNull Rect getBoundingRectTop() { + return mBounds.getRect(BOUNDS_POSITION_TOP); + } + + /** + * Returns a bounding rectangle for a non-functional area on the display which is located on + * the right of the screen. + * + * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle + * is returned. + */ + public @NonNull Rect getBoundingRectRight() { + return mBounds.getRect(BOUNDS_POSITION_RIGHT); + } + + /** + * Returns a bounding rectangle for a non-functional area on the display which is located on + * the bottom of the screen. + * + * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle + * is returned. + */ + public @NonNull Rect getBoundingRectBottom() { + return mBounds.getRect(BOUNDS_POSITION_BOTTOM); + } + @Override public int hashCode() { - int result = mSafeInsets.hashCode(); - result = result * 31 + mBounds.getBounds().hashCode(); - return result; + return mSafeInsets.hashCode() * 48271 + mBounds.hashCode(); } @Override @@ -246,8 +472,7 @@ public final class DisplayCutout { } if (o instanceof DisplayCutout) { DisplayCutout c = (DisplayCutout) o; - return mSafeInsets.equals(c.mSafeInsets) - && mBounds.equals(c.mBounds); + return mSafeInsets.equals(c.mSafeInsets) && mBounds.equals(c.mBounds); } return false; } @@ -255,7 +480,7 @@ public final class DisplayCutout { @Override public String toString() { return "DisplayCutout{insets=" + mSafeInsets - + " boundingRect=" + mBounds.getBounds() + + " boundingRect={" + mBounds + "}" + "}"; } @@ -265,7 +490,10 @@ public final class DisplayCutout { public void writeToProto(ProtoOutputStream proto, long fieldId) { final long token = proto.start(fieldId); mSafeInsets.writeToProto(proto, INSETS); - mBounds.getBounds().writeToProto(proto, BOUNDS); + mBounds.getRect(BOUNDS_POSITION_LEFT).writeToProto(proto, BOUND_LEFT); + mBounds.getRect(BOUNDS_POSITION_TOP).writeToProto(proto, BOUND_TOP); + mBounds.getRect(BOUNDS_POSITION_RIGHT).writeToProto(proto, BOUND_RIGHT); + mBounds.getRect(BOUNDS_POSITION_BOTTOM).writeToProto(proto, BOUND_BOTTOM); proto.end(token); } @@ -276,13 +504,12 @@ public final class DisplayCutout { * @hide */ public DisplayCutout inset(int insetLeft, int insetTop, int insetRight, int insetBottom) { - if (mBounds.isEmpty() + if (isBoundsEmpty() || insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0) { return this; } Rect safeInsets = new Rect(mSafeInsets); - Region bounds = Region.obtain(mBounds); // 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. @@ -299,7 +526,13 @@ public final class DisplayCutout { safeInsets.right = atLeastZero(safeInsets.right - insetRight); } - bounds.translate(-insetLeft, -insetTop); + Rect[] bounds = mBounds.getRects(); + for (int i = 0; i < bounds.length; ++i) { + if (!bounds[i].equals(ZERO_RECT)) { + bounds[i].offset(-insetLeft, -insetTop); + } + } + return new DisplayCutout(safeInsets, bounds, false /* copyArguments */); } @@ -312,7 +545,7 @@ public final class DisplayCutout { * @hide */ public DisplayCutout replaceSafeInsets(Rect safeInsets) { - return new DisplayCutout(new Rect(safeInsets), mBounds, false /* copyArguments */); + return new DisplayCutout(new Rect(safeInsets), mBounds); } private static int atLeastZero(int value) { @@ -326,10 +559,13 @@ public final class DisplayCutout { * @hide */ @VisibleForTesting - public static DisplayCutout fromBoundingRect(int left, int top, int right, int bottom) { - Region r = Region.obtain(); - r.set(left, top, right, bottom); - return fromBounds(r); + public static DisplayCutout fromBoundingRect( + int left, int top, int right, int bottom, @BoundsPosition int pos) { + Rect[] bounds = new Rect[BOUNDS_POSITION_LENGTH]; + for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) { + bounds[i] = (pos == i) ? new Rect(left, top, right, bottom) : new Rect(); + } + return new DisplayCutout(ZERO_RECT, bounds, false /* copyArguments */); } /** @@ -337,8 +573,8 @@ public final class DisplayCutout { * * @hide */ - public static DisplayCutout fromBounds(Region region) { - return new DisplayCutout(ZERO_RECT, region, false /* copyArguments */); + public static DisplayCutout fromBounds(Rect[] bounds) { + return new DisplayCutout(ZERO_RECT, bounds, false /* copyArguments */); } /** @@ -423,10 +659,11 @@ public final class DisplayCutout { m.postTranslate(offsetX, 0); p.transform(m); - final Rect tmpRect = new Rect(); - toRectAndAddToRegion(p, r, tmpRect); - final int topInset = tmpRect.bottom; + Rect boundTop = new Rect(); + toRectAndAddToRegion(p, r, boundTop); + final int topInset = boundTop.bottom; + Rect boundBottom = null; final int bottomInset; if (bottomSpec != null) { final Path bottomPath; @@ -440,15 +677,17 @@ public final class DisplayCutout { m.postTranslate(0, displayHeight); bottomPath.transform(m); p.addPath(bottomPath); - toRectAndAddToRegion(bottomPath, r, tmpRect); - bottomInset = displayHeight - tmpRect.top; + boundBottom = new Rect(); + toRectAndAddToRegion(bottomPath, r, boundBottom); + bottomInset = displayHeight - boundBottom.top; } else { bottomInset = 0; } - // Reuse tmpRect as the inset rect we store into the DisplayCutout instance. - tmpRect.set(0, topInset, 0, bottomInset); - final DisplayCutout cutout = new DisplayCutout(tmpRect, r, false /* copyArguments */); + Rect safeInset = new Rect(0, topInset, 0, bottomInset); + final DisplayCutout cutout = new DisplayCutout( + safeInset, null /* boundLeft */, boundTop, null /* boundRight */, boundBottom, + false /* copyArguments */); final Pair result = new Pair<>(p, cutout); synchronized (CACHE_LOCK) { @@ -468,16 +707,6 @@ public final class DisplayCutout { inoutRegion.op(inoutRect, Op.UNION); } - private static Region boundingRectsToRegion(List rects) { - Region result = Region.obtain(); - if (rects != null) { - for (Rect r : rects) { - result.op(r, Region.Op.UNION); - } - } - return result; - } - /** * Helper class for passing {@link DisplayCutout} through binder. * @@ -520,7 +749,7 @@ public final class DisplayCutout { } else { out.writeInt(1); out.writeTypedObject(cutout.mSafeInsets, flags); - out.writeTypedObject(cutout.mBounds, flags); + out.writeTypedArray(cutout.mBounds.getRects(), flags); } } @@ -561,7 +790,8 @@ public final class DisplayCutout { } Rect safeInsets = in.readTypedObject(Rect.CREATOR); - Region bounds = in.readTypedObject(Region.CREATOR); + Rect[] bounds = new Rect[BOUNDS_POSITION_LENGTH]; + in.readTypedArray(bounds, Rect.CREATOR); return new DisplayCutout(safeInsets, bounds, false /* copyArguments */); } diff --git a/core/proto/android/view/displaycutout.proto b/core/proto/android/view/displaycutout.proto index f4744daa42856..0a3310148145c 100644 --- a/core/proto/android/view/displaycutout.proto +++ b/core/proto/android/view/displaycutout.proto @@ -26,5 +26,9 @@ message DisplayCutoutProto { option (.android.msg_privacy).dest = DEST_AUTOMATIC; optional .android.graphics.RectProto insets = 1; - optional .android.graphics.RectProto bounds = 2; + reserved 2; + optional .android.graphics.RectProto bound_left = 3; + optional .android.graphics.RectProto bound_top = 4; + optional .android.graphics.RectProto bound_right = 5; + optional .android.graphics.RectProto bound_bottom = 6; } diff --git a/core/tests/coretests/src/android/view/DisplayCutoutTest.java b/core/tests/coretests/src/android/view/DisplayCutoutTest.java index fe45fe7d3aaf1..c8d994c4f6c12 100644 --- a/core/tests/coretests/src/android/view/DisplayCutoutTest.java +++ b/core/tests/coretests/src/android/view/DisplayCutoutTest.java @@ -17,6 +17,7 @@ package android.view; import static android.view.DisplayCutout.NO_CUTOUT; +import static android.view.DisplayCutout.extractBoundsFromList; import static android.view.DisplayCutout.fromSpec; import static org.hamcrest.Matchers.equalTo; @@ -28,6 +29,7 @@ import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import android.graphics.Insets; import android.graphics.Rect; import android.os.Parcel; import android.platform.test.annotations.Presubmit; @@ -39,19 +41,69 @@ import org.junit.Test; import org.junit.runner.RunWith; import java.util.Arrays; +import java.util.Collections; + @RunWith(AndroidJUnit4.class) @SmallTest @Presubmit public class DisplayCutoutTest { + private static final Rect ZERO_RECT = new Rect(); + /** This is not a consistent cutout. Useful for verifying insets in one go though. */ final DisplayCutout mCutoutNumbers = new DisplayCutout( - new Rect(1, 2, 3, 4), - Arrays.asList(new Rect(5, 6, 7, 8))); + Insets.of(5, 6, 7, 8) /* safeInsets */, + null /* boundLeft */, + new Rect(9, 0, 10, 1) /* boundTop */, + null /* boundRight */, + null /* boundBottom */); final DisplayCutout mCutoutTop = createCutoutTop(); + @Test + public void testExtractBoundsFromList_left() { + Rect safeInsets = new Rect(10, 0, 0, 0); + Rect bound = new Rect(0, 80, 10, 120); + assertThat(extractBoundsFromList(safeInsets, Collections.singletonList(bound)), + equalTo(new Rect[]{bound, ZERO_RECT, ZERO_RECT, ZERO_RECT})); + } + + @Test + public void testExtractBoundsFromList_top() { + Rect safeInsets = new Rect(0, 10, 0, 0); + Rect bound = new Rect(80, 0, 120, 10); + assertThat(extractBoundsFromList(safeInsets, Collections.singletonList(bound)), + equalTo(new Rect[]{ZERO_RECT, bound, ZERO_RECT, ZERO_RECT})); + } + + @Test + public void testExtractBoundsFromList_right() { + Rect safeInsets = new Rect(0, 0, 10, 0); + Rect bound = new Rect(190, 80, 200, 120); + assertThat(extractBoundsFromList(safeInsets, Collections.singletonList(bound)), + equalTo(new Rect[]{ZERO_RECT, ZERO_RECT, bound, ZERO_RECT})); + } + + @Test + public void testExtractBoundsFromList_bottom() { + Rect safeInsets = new Rect(0, 0, 0, 10); + Rect bound = new Rect(80, 190, 120, 200); + assertThat(extractBoundsFromList(safeInsets, Collections.singletonList(bound)), + equalTo(new Rect[]{ZERO_RECT, ZERO_RECT, ZERO_RECT, bound})); + } + + @Test + public void testExtractBoundsFromList_top_and_bottom() { + Rect safeInsets = new Rect(0, 1, 0, 10); + Rect boundTop = new Rect(80, 0, 120, 10); + Rect boundBottom = new Rect(80, 190, 120, 200); + assertThat(extractBoundsFromList(safeInsets, + Arrays.asList(new Rect[]{boundTop, boundBottom})), + equalTo(new Rect[]{ZERO_RECT, boundTop, ZERO_RECT, boundBottom})); + } + + @Test public void hasCutout() throws Exception { assertTrue(NO_CUTOUT.isEmpty()); @@ -59,18 +111,13 @@ public class DisplayCutoutTest { } @Test - public void getSafeInsets() throws Exception { - assertEquals(1, mCutoutNumbers.getSafeInsetLeft()); - assertEquals(2, mCutoutNumbers.getSafeInsetTop()); - assertEquals(3, mCutoutNumbers.getSafeInsetRight()); - assertEquals(4, mCutoutNumbers.getSafeInsetBottom()); + public void testGetSafeInsets() throws Exception { + assertEquals(5, mCutoutNumbers.getSafeInsetLeft()); + assertEquals(6, mCutoutNumbers.getSafeInsetTop()); + assertEquals(7, mCutoutNumbers.getSafeInsetRight()); + assertEquals(8, mCutoutNumbers.getSafeInsetBottom()); - assertEquals(new Rect(1, 2, 3, 4), mCutoutNumbers.getSafeInsets()); - } - - @Test - public void getBoundingRect() throws Exception { - assertEquals(new Rect(50, 0, 75, 100), mCutoutTop.getBounds().getBounds()); + assertEquals(new Rect(5, 6, 7, 8), mCutoutNumbers.getSafeInsets()); } @Test @@ -102,30 +149,30 @@ public class DisplayCutoutTest { public void inset_insets_withLeftCutout() throws Exception { DisplayCutout cutout = createCutoutWithInsets(100, 0, 0, 0).inset(1, 2, 3, 4); - assertEquals(cutout.getSafeInsetLeft(), 99); - assertEquals(cutout.getSafeInsetTop(), 0); - assertEquals(cutout.getSafeInsetRight(), 0); - assertEquals(cutout.getSafeInsetBottom(), 0); + assertEquals(99, cutout.getSafeInsetLeft()); + assertEquals(0, cutout.getSafeInsetTop()); + assertEquals(0, cutout.getSafeInsetRight()); + assertEquals(0, cutout.getSafeInsetBottom()); } @Test public void inset_insets_withTopCutout() throws Exception { DisplayCutout cutout = mCutoutTop.inset(1, 2, 3, 4); - assertEquals(cutout.getSafeInsetLeft(), 0); - assertEquals(cutout.getSafeInsetTop(), 98); - assertEquals(cutout.getSafeInsetRight(), 0); - assertEquals(cutout.getSafeInsetBottom(), 0); + assertEquals(0, cutout.getSafeInsetLeft()); + assertEquals(98, cutout.getSafeInsetTop()); + assertEquals(0, cutout.getSafeInsetRight()); + assertEquals(0, cutout.getSafeInsetBottom()); } @Test public void inset_insets_withRightCutout() throws Exception { DisplayCutout cutout = createCutoutWithInsets(0, 0, 100, 0).inset(1, 2, 3, 4); - assertEquals(cutout.getSafeInsetLeft(), 0); - assertEquals(cutout.getSafeInsetTop(), 0); - assertEquals(cutout.getSafeInsetRight(), 97); - assertEquals(cutout.getSafeInsetBottom(), 0); + assertEquals(0, cutout.getSafeInsetLeft()); + assertEquals(0, cutout.getSafeInsetTop()); + assertEquals(97, cutout.getSafeInsetRight()); + assertEquals(0, cutout.getSafeInsetBottom()); } @Test @@ -153,16 +200,20 @@ public class DisplayCutoutTest { @Test public void inset_bounds() throws Exception { DisplayCutout cutout = mCutoutTop.inset(1, 2, 3, 4); - - assertEquals(new Rect(49, -2, 74, 98), cutout.getBounds().getBounds()); + assertThat(cutout.getBoundingRectsAll(), equalTo( + new Rect[]{ ZERO_RECT, new Rect(49, -2, 74, 98), ZERO_RECT, ZERO_RECT })); } + + // TODO: Deprecate fromBoundingRect. + /* @Test public void fromBoundingPolygon() throws Exception { assertEquals( new Rect(50, 0, 75, 100), DisplayCutout.fromBoundingRect(50, 0, 75, 100).getBounds().getBounds()); } + */ @Test public void parcel_unparcel_regular() { @@ -231,6 +282,10 @@ public class DisplayCutoutTest { DisplayCutout cutout = fromSpec("M -50,0 v 20 h 100 v -20 z" + "@bottom M -50,0 v -10,0 h 100 v 20 z", 200, 400, 2f); assertThat(cutout.getSafeInsets(), equalTo(new Rect(0, 20, 0, 10))); + assertThat(cutout.getBoundingRectsAll(), equalTo(new Rect[]{ + ZERO_RECT, new Rect(50, 0, 150, 20), + ZERO_RECT, new Rect(50, 390, 150, 410) + })); } @Test @@ -281,8 +336,10 @@ public class DisplayCutoutTest { } private static DisplayCutout createCutoutWithInsets(int left, int top, int right, int bottom) { + Insets safeInset = Insets.of(left, top, right, bottom); + Rect boundTop = new Rect(50, 0, 75, 100); return new DisplayCutout( - new Rect(left, top, right, bottom), - Arrays.asList(new Rect(50, 0, 75, 100))); + safeInset, null /* boundLeft */, boundTop, null /* boundRight */, + null /* boundBottom */); } } diff --git a/core/tests/coretests/src/com/android/internal/widget/ActionBarOverlayLayoutTest.java b/core/tests/coretests/src/com/android/internal/widget/ActionBarOverlayLayoutTest.java index cac4e88c8edc1..218566e8cdf0d 100644 --- a/core/tests/coretests/src/com/android/internal/widget/ActionBarOverlayLayoutTest.java +++ b/core/tests/coretests/src/com/android/internal/widget/ActionBarOverlayLayoutTest.java @@ -27,6 +27,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import android.content.Context; +import android.graphics.Insets; import android.graphics.Rect; import android.support.test.InstrumentationRegistry; import android.support.test.filters.SmallTest; @@ -44,18 +45,20 @@ import org.junit.Test; import org.junit.runner.RunWith; import java.lang.reflect.Field; -import java.util.Arrays; @RunWith(AndroidJUnit4.class) @SmallTest public class ActionBarOverlayLayoutTest { - private static final Rect TOP_INSET_5 = new Rect(0, 5, 0, 0); - private static final Rect TOP_INSET_25 = new Rect(0, 25, 0, 0); - private static final Rect ZERO_INSET = new Rect(0, 0, 0, 0); + private static final Insets TOP_INSET_5 = Insets.of(0, 5, 0, 0); + private static final Insets TOP_INSET_25 = Insets.of(0, 25, 0, 0); private static final DisplayCutout CONSUMED_CUTOUT = null; - private static final DisplayCutout CUTOUT_5 = new DisplayCutout(TOP_INSET_5, Arrays.asList( - new Rect(100, 0, 200, 5))); + private static final DisplayCutout CUTOUT_5 = new DisplayCutout( + TOP_INSET_5, + null /* boundLeft */, + new Rect(100, 0, 200, 5), + null /* boundRight */, + null /* boundBottom*/); private static final int EXACTLY_1000 = makeMeasureSpec(1000, EXACTLY); private Context mContext; @@ -112,7 +115,7 @@ public class ActionBarOverlayLayoutTest { mLayout.measure(EXACTLY_1000, EXACTLY_1000); - assertThat(mContentInsetsListener.captured, is(insetsWith(ZERO_INSET, CONSUMED_CUTOUT))); + assertThat(mContentInsetsListener.captured, is(insetsWith(Insets.NONE, CONSUMED_CUTOUT))); } @Test @@ -136,7 +139,7 @@ public class ActionBarOverlayLayoutTest { mLayout.measure(EXACTLY_1000, EXACTLY_1000); - assertThat(mContentInsetsListener.captured, is(insetsWith(ZERO_INSET, NO_CUTOUT))); + assertThat(mContentInsetsListener.captured, is(insetsWith(Insets.NONE, NO_CUTOUT))); } @Test @@ -160,11 +163,11 @@ public class ActionBarOverlayLayoutTest { mLayout.measure(EXACTLY_1000, EXACTLY_1000); - assertThat(mContentInsetsListener.captured, is(insetsWith(ZERO_INSET, NO_CUTOUT))); + assertThat(mContentInsetsListener.captured, is(insetsWith(Insets.NONE, NO_CUTOUT))); } - private WindowInsets insetsWith(Rect content, DisplayCutout cutout) { - return new WindowInsets(content, null, null, false, false, cutout); + private WindowInsets insetsWith(Insets content, DisplayCutout cutout) { + return new WindowInsets(content.toRect(), null, null, false, false, cutout); } private ViewGroup createViewGroupWithId(int id) { diff --git a/graphics/java/android/graphics/Insets.java b/graphics/java/android/graphics/Insets.java index c3449dd4fb471..de110c8493387 100644 --- a/graphics/java/android/graphics/Insets.java +++ b/graphics/java/android/graphics/Insets.java @@ -72,6 +72,15 @@ public final class Insets { return (r == null) ? NONE : of(r.left, r.top, r.right, r.bottom); } + /** + * Returns a Rect intance with the appropriate values. + * + * @hide + */ + public @NonNull Rect toRect() { + return new Rect(left, top, right, bottom); + } + /** * Two Insets instances are equal iff they belong to the same class and their fields are * pairwise equal. diff --git a/packages/SystemUI/src/com/android/systemui/RegionInterceptingFrameLayout.java b/packages/SystemUI/src/com/android/systemui/RegionInterceptingFrameLayout.java index 646f69eede942..6dc2d67a1c78a 100644 --- a/packages/SystemUI/src/com/android/systemui/RegionInterceptingFrameLayout.java +++ b/packages/SystemUI/src/com/android/systemui/RegionInterceptingFrameLayout.java @@ -76,7 +76,7 @@ public class RegionInterceptingFrameLayout extends FrameLayout { continue; } - internalInsetsInfo.touchableRegion.op(riv.getInterceptRegion(), Op.UNION); + internalInsetsInfo.touchableRegion.op(unionRegion, Op.UNION); } }; diff --git a/packages/SystemUI/src/com/android/systemui/ScreenDecorations.java b/packages/SystemUI/src/com/android/systemui/ScreenDecorations.java index 48181bc629a86..4d24d82bd7ee6 100644 --- a/packages/SystemUI/src/com/android/systemui/ScreenDecorations.java +++ b/packages/SystemUI/src/com/android/systemui/ScreenDecorations.java @@ -76,6 +76,9 @@ import com.android.systemui.tuner.TunerService; import com.android.systemui.tuner.TunerService.Tunable; import com.android.systemui.util.leak.RotationUtils; +import java.util.ArrayList; +import java.util.List; + import androidx.annotation.VisibleForTesting; /** @@ -108,6 +111,23 @@ public class ScreenDecorations extends SystemUI implements Tunable { private boolean mPendingRotationChange; private Handler mHandler; + /** + * Converts a set of {@link Rect}s into a {@link Region} + * + * @hide + */ + public static Region rectsToRegion(List rects) { + Region result = Region.obtain(); + if (rects != null) { + for (Rect r : rects) { + if (r != null && !r.isEmpty()) { + result.op(r, Region.Op.UNION); + } + } + } + return result; + } + @Override public void start() { mHandler = startHandlerThread(); @@ -539,7 +559,7 @@ public class ScreenDecorations extends SystemUI implements Tunable { private final DisplayInfo mInfo = new DisplayInfo(); private final Paint mPaint = new Paint(); - private final Region mBounds = new Region(); + private final List mBounds = new ArrayList(); private final Rect mBoundingRect = new Rect(); private final Path mBoundingPath = new Path(); private final int[] mLocation = new int[2]; @@ -629,12 +649,12 @@ public class ScreenDecorations extends SystemUI implements Tunable { mStart = isStart(); requestLayout(); getDisplay().getDisplayInfo(mInfo); - mBounds.setEmpty(); + mBounds.clear(); mBoundingRect.setEmpty(); mBoundingPath.reset(); int newVisible; if (shouldDrawCutout(getContext()) && hasCutout()) { - mBounds.set(mInfo.displayCutout.getBounds()); + mBounds.addAll(mInfo.displayCutout.getBoundingRects()); localBounds(mBoundingRect); updateBoundingPath(); invalidate(); @@ -713,32 +733,17 @@ public class ScreenDecorations extends SystemUI implements Tunable { public static void boundsFromDirection(DisplayCutout displayCutout, int gravity, Rect out) { - Region bounds = boundsFromDirection(displayCutout, gravity); - out.set(bounds.getBounds()); - bounds.recycle(); - } - - public static Region boundsFromDirection(DisplayCutout displayCutout, int gravity) { - Region bounds = displayCutout.getBounds(); switch (gravity) { case Gravity.TOP: - bounds.op(0, 0, Integer.MAX_VALUE, displayCutout.getSafeInsetTop(), - Region.Op.INTERSECT); - break; + out.set(displayCutout.getBoundingRectTop()); case Gravity.LEFT: - bounds.op(0, 0, displayCutout.getSafeInsetLeft(), Integer.MAX_VALUE, - Region.Op.INTERSECT); - break; + out.set(displayCutout.getBoundingRectLeft()); case Gravity.BOTTOM: - bounds.op(0, displayCutout.getSafeInsetTop() + 1, Integer.MAX_VALUE, - Integer.MAX_VALUE, Region.Op.INTERSECT); - break; + out.set(displayCutout.getBoundingRectBottom()); case Gravity.RIGHT: - bounds.op(displayCutout.getSafeInsetLeft() + 1, 0, Integer.MAX_VALUE, - Integer.MAX_VALUE, Region.Op.INTERSECT); - break; + out.set(displayCutout.getBoundingRectRight()); } - return bounds; + out.setEmpty(); } private void localBounds(Rect out) { @@ -771,7 +776,8 @@ public class ScreenDecorations extends SystemUI implements Tunable { } View rootView = getRootView(); - Region cutoutBounds = mInfo.displayCutout.getBounds(); + Region cutoutBounds = rectsToRegion( + mInfo.displayCutout.getBoundingRects()); // Transform to window's coordinate space rootView.getLocationOnScreen(mLocation); diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/HeadsUpManagerPhone.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/HeadsUpManagerPhone.java index cfc327116303c..976327a49f584 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/HeadsUpManagerPhone.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/HeadsUpManagerPhone.java @@ -22,6 +22,8 @@ import android.content.Context; import android.content.res.Configuration; import android.content.res.Resources; import androidx.collection.ArraySet; + +import android.graphics.Rect; import android.graphics.Region; import android.graphics.Region.Op; import android.util.Log; @@ -324,11 +326,10 @@ public class HeadsUpManagerPhone extends HeadsUpManager implements Dumpable, // Expand touchable region such that we also catch touches that just start below the notch // area. - Region bounds = ScreenDecorations.DisplayCutoutView.boundsFromDirection( - cutout, Gravity.TOP); - bounds.translate(0, mDisplayCutoutTouchableRegionSize); + Rect bounds = new Rect(); + ScreenDecorations.DisplayCutoutView.boundsFromDirection(cutout, Gravity.TOP, bounds); + bounds.offset(0, mDisplayCutoutTouchableRegionSize); region.op(bounds, Op.UNION); - bounds.recycle(); } @Override diff --git a/packages/SystemUI/tests/src/com/android/systemui/ScreenDecorationsTest.java b/packages/SystemUI/tests/src/com/android/systemui/ScreenDecorationsTest.java index cc969177ab2e2..b84f85bdbc4c7 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/ScreenDecorationsTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/ScreenDecorationsTest.java @@ -16,6 +16,7 @@ package com.android.systemui; import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY; +import static com.android.systemui.ScreenDecorations.rectsToRegion; import static com.android.systemui.tuner.TunablePadding.FLAG_END; import static com.android.systemui.tuner.TunablePadding.FLAG_START; @@ -35,6 +36,7 @@ import static org.mockito.Mockito.when; import android.app.Fragment; import android.content.res.Configuration; +import android.graphics.Rect; import android.os.Handler; import android.support.test.filters.SmallTest; import android.testing.AndroidTestingRunner; @@ -58,6 +60,8 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import java.util.Collections; + @RunWithLooper @RunWith(AndroidTestingRunner.class) @SmallTest @@ -240,4 +244,11 @@ public class ScreenDecorationsTest extends SysuiTestCase { mScreenDecorations.onConfigurationChanged(null); assertEquals(mScreenDecorations.mRoundedDefault, 5); } + + @Test + public void testBoundingRectsToRegion() throws Exception { + Rect rect = new Rect(1, 2, 3, 4); + assertThat(rectsToRegion(Collections.singletonList(rect)).getBounds(), is(rect)); + } + } diff --git a/services/core/java/com/android/server/wm/DisplayContent.java b/services/core/java/com/android/server/wm/DisplayContent.java index 236982fd76ac5..02b372f130579 100644 --- a/services/core/java/com/android/server/wm/DisplayContent.java +++ b/services/core/java/com/android/server/wm/DisplayContent.java @@ -114,7 +114,6 @@ import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP; import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING; import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW; import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE; -import static com.android.server.wm.utils.CoordinateTransforms.transformPhysicalToLogicalCoordinates; import android.annotation.CallSuper; import android.annotation.NonNull; @@ -152,6 +151,7 @@ import android.view.SurfaceSession; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.ToBooleanFunction; import com.android.server.policy.WindowManagerPolicy; +import com.android.server.wm.utils.DisplayRotationUtil; import com.android.server.wm.utils.RotationCache; import com.android.server.wm.utils.WmDisplayCutout; @@ -334,6 +334,7 @@ class DisplayContent extends WindowContainer bounds = WmDisplayCutout.computeSafeInsets( + final Rect[] newBounds = mRotationUtil.getRotatedBounds( + WmDisplayCutout.computeSafeInsets( cutout, mInitialDisplayWidth, mInitialDisplayHeight) - .getDisplayCutout().getBoundingRects(); - transformPhysicalToLogicalCoordinates(rotation, mInitialDisplayWidth, mInitialDisplayHeight, - mTmpMatrix); - final Region region = Region.obtain(); - for (int i = 0; i < bounds.size(); i++) { - final Rect rect = bounds.get(i); - final RectF rectF = new RectF(bounds.get(i)); - mTmpMatrix.mapRect(rectF); - rectF.round(rect); - region.op(rect, Op.UNION); - } - - return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(region), + .getDisplayCutout().getBoundingRectsAll(), + rotation, mInitialDisplayWidth, mInitialDisplayHeight); + return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds), rotated ? mInitialDisplayHeight : mInitialDisplayWidth, rotated ? mInitialDisplayWidth : mInitialDisplayHeight); } diff --git a/services/core/java/com/android/server/wm/utils/DisplayRotationUtil.java b/services/core/java/com/android/server/wm/utils/DisplayRotationUtil.java new file mode 100644 index 0000000000000..9f307bb0f98f4 --- /dev/null +++ b/services/core/java/com/android/server/wm/utils/DisplayRotationUtil.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wm.utils; + +import static android.view.DisplayCutout.BOUNDS_POSITION_LENGTH; +import static android.view.Surface.ROTATION_0; +import static android.view.Surface.ROTATION_180; +import static android.view.Surface.ROTATION_270; +import static android.view.Surface.ROTATION_90; + +import static com.android.server.wm.utils.CoordinateTransforms.transformPhysicalToLogicalCoordinates; + +import android.graphics.Matrix; +import android.graphics.Rect; +import android.graphics.RectF; + +import com.android.internal.annotations.VisibleForTesting; + +/** + * Utility to compute bounds after rotating the screen. + */ +public class DisplayRotationUtil { + private final Matrix mTmpMatrix = new Matrix(); + + private static int getRotationToBoundsOffset(int rotation) { + switch (rotation) { + case ROTATION_0: + return 0; + case ROTATION_90: + return -1; + case ROTATION_180: + return 2; + case ROTATION_270: + return 1; + default: + // should not happen + return 0; + } + } + + @VisibleForTesting + static int getBoundIndexFromRotation(int i, int rotation) { + return Math.floorMod(i + getRotationToBoundsOffset(rotation), + BOUNDS_POSITION_LENGTH); + } + + /** + * Compute bounds after rotating teh screen. + * + * @param bounds Bounds before the rotation. The array must contain exactly 4 non-null elements. + * @param rotation rotation constant defined in android.view.Surface. + * @param initialDisplayWidth width of the display before the rotation. + * @param initialDisplayHeight height of the display before the rotation. + * @return Bounds after the rotation. + * + * @hide + */ + public Rect[] getRotatedBounds( + Rect[] bounds, int rotation, int initialDisplayWidth, int initialDisplayHeight) { + if (bounds.length != BOUNDS_POSITION_LENGTH) { + throw new IllegalArgumentException( + "bounds must have exactly 4 elements: bounds=" + bounds); + } + if (rotation == ROTATION_0) { + return bounds; + } + transformPhysicalToLogicalCoordinates(rotation, initialDisplayWidth, initialDisplayHeight, + mTmpMatrix); + Rect[] newBounds = new Rect[BOUNDS_POSITION_LENGTH]; + for (int i = 0; i < bounds.length; i++) { + + final Rect rect = bounds[i]; + if (!rect.isEmpty()) { + final RectF rectF = new RectF(rect); + mTmpMatrix.mapRect(rectF); + rectF.round(rect); + } + newBounds[getBoundIndexFromRotation(i, rotation)] = rect; + } + return newBounds; + } +} diff --git a/services/tests/servicestests/src/com/android/server/policy/PhoneWindowManagerTestBase.java b/services/tests/servicestests/src/com/android/server/policy/PhoneWindowManagerTestBase.java index acd065e90a0c2..e16f118cd63bc 100644 --- a/services/tests/servicestests/src/com/android/server/policy/PhoneWindowManagerTestBase.java +++ b/services/tests/servicestests/src/com/android/server/policy/PhoneWindowManagerTestBase.java @@ -16,6 +16,10 @@ package com.android.server.policy; +import static android.view.DisplayCutout.BOUNDS_POSITION_BOTTOM; +import static android.view.DisplayCutout.BOUNDS_POSITION_LEFT; +import static android.view.DisplayCutout.BOUNDS_POSITION_RIGHT; +import static android.view.DisplayCutout.BOUNDS_POSITION_TOP; import static android.view.Surface.ROTATION_0; import static android.view.Surface.ROTATION_180; import static android.view.Surface.ROTATION_270; @@ -179,8 +183,25 @@ public class PhoneWindowManagerTestBase { transformPhysicalToLogicalCoordinates(rotation, DISPLAY_WIDTH, DISPLAY_HEIGHT, m); m.mapRect(rectF); + int pos = -1; + switch (rotation) { + case ROTATION_0: + pos = BOUNDS_POSITION_TOP; + break; + case ROTATION_90: + pos = BOUNDS_POSITION_LEFT; + break; + case ROTATION_180: + pos = BOUNDS_POSITION_BOTTOM; + break; + case ROTATION_270: + pos = BOUNDS_POSITION_RIGHT; + break; + } + + return DisplayCutout.fromBoundingRect((int) rectF.left, (int) rectF.top, - (int) rectF.right, (int) rectF.bottom); + (int) rectF.right, (int) rectF.bottom, pos); } static class TestContextWrapper extends ContextWrapper { diff --git a/services/tests/servicestests/src/com/android/server/wm/DisplayContentTests.java b/services/tests/servicestests/src/com/android/server/wm/DisplayContentTests.java index b3303049be394..88b876a3b75b6 100644 --- a/services/tests/servicestests/src/com/android/server/wm/DisplayContentTests.java +++ b/services/tests/servicestests/src/com/android/server/wm/DisplayContentTests.java @@ -24,6 +24,8 @@ import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; import static android.view.Display.DEFAULT_DISPLAY; +import static android.view.DisplayCutout.BOUNDS_POSITION_LEFT; +import static android.view.DisplayCutout.BOUNDS_POSITION_TOP; import static android.view.DisplayCutout.fromBoundingRect; import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION; import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG; @@ -454,7 +456,7 @@ public class DisplayContentTests extends WindowTestsBase { dc.mInitialDisplayHeight = 400; Rect r = new Rect(80, 0, 120, 10); final DisplayCutout cutout = new WmDisplayCutout( - fromBoundingRect(r.left, r.top, r.right, r.bottom), null) + fromBoundingRect(r.left, r.top, r.right, r.bottom, BOUNDS_POSITION_TOP), null) .computeSafeInsets(200, 400).getDisplayCutout(); dc.mInitialDisplayCutout = cutout; @@ -484,7 +486,7 @@ public class DisplayContentTests extends WindowTestsBase { final Rect r1 = new Rect(left, top, right, bottom); final DisplayCutout cutout = new WmDisplayCutout( - fromBoundingRect(r1.left, r1.top, r1.right, r1.bottom), null) + fromBoundingRect(r1.left, r1.top, r1.right, r1.bottom, BOUNDS_POSITION_TOP), null) .computeSafeInsets(displayWidth, displayHeight).getDisplayCutout(); dc.mInitialDisplayCutout = cutout; @@ -501,7 +503,7 @@ public class DisplayContentTests extends WindowTestsBase { // | | ------------- final Rect r = new Rect(top, left, bottom, right); assertEquals(new WmDisplayCutout( - fromBoundingRect(r.left, r.top, r.right, r.bottom), null) + fromBoundingRect(r.left, r.top, r.right, r.bottom, BOUNDS_POSITION_LEFT), null) .computeSafeInsets(displayHeight, displayWidth) .getDisplayCutout(), dc.getDisplayInfo().displayCutout); } diff --git a/services/tests/servicestests/src/com/android/server/wm/WindowFrameTests.java b/services/tests/servicestests/src/com/android/server/wm/WindowFrameTests.java index 0886729735243..7cd131420049b 100644 --- a/services/tests/servicestests/src/com/android/server/wm/WindowFrameTests.java +++ b/services/tests/servicestests/src/com/android/server/wm/WindowFrameTests.java @@ -16,12 +16,12 @@ package com.android.server.wm; +import static android.view.DisplayCutout.BOUNDS_POSITION_TOP; import static android.view.DisplayCutout.fromBoundingRect; import static android.view.WindowManager.LayoutParams.FILL_PARENT; import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import android.app.ActivityManager.TaskDescription; import android.content.res.Configuration; @@ -474,7 +474,8 @@ public class WindowFrameTests extends WindowTestsBase { final Rect pf = new Rect(0, 0, 1000, 2000); // Create a display cutout of size 50x50, aligned top-center final WmDisplayCutout cutout = WmDisplayCutout.computeSafeInsets( - fromBoundingRect(500, 0, 550, 50), pf.width(), pf.height()); + fromBoundingRect(500, 0, 550, 50, BOUNDS_POSITION_TOP), + pf.width(), pf.height()); final WindowFrames windowFrames = w.getWindowFrames(); windowFrames.setFrames(pf, pf, pf, pf, pf, pf, pf, pf); @@ -499,7 +500,8 @@ public class WindowFrameTests extends WindowTestsBase { final Rect pf = new Rect(0, -500, 1000, 1500); // Create a display cutout of size 50x50, aligned top-center final WmDisplayCutout cutout = WmDisplayCutout.computeSafeInsets( - fromBoundingRect(500, 0, 550, 50), pf.width(), pf.height()); + fromBoundingRect(500, 0, 550, 50, BOUNDS_POSITION_TOP), + pf.width(), pf.height()); final WindowFrames windowFrames = w.getWindowFrames(); windowFrames.setFrames(pf, pf, pf, pf, pf, pf, pf, pf); diff --git a/services/tests/servicestests/src/com/android/server/wm/WindowStateTests.java b/services/tests/servicestests/src/com/android/server/wm/WindowStateTests.java index b7cc9ce7a6191..3637baf13c727 100644 --- a/services/tests/servicestests/src/com/android/server/wm/WindowStateTests.java +++ b/services/tests/servicestests/src/com/android/server/wm/WindowStateTests.java @@ -49,6 +49,7 @@ import static org.mockito.Mockito.reset; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; +import android.graphics.Insets; import android.graphics.Matrix; import android.graphics.Rect; import android.platform.test.annotations.Presubmit; @@ -404,8 +405,12 @@ public class WindowStateTests extends WindowTestsBase { WindowFrames wf = app.getWindowFrames(); wf.mParentFrame.set(7, 10, 185, 380); wf.mDisplayFrame.set(wf.mParentFrame); - final DisplayCutout cutout = new DisplayCutout(new Rect(0, 15, 0, 22), - Arrays.asList(new Rect(95, 0, 105, 15), new Rect(95, 378, 105, 400))); + final DisplayCutout cutout = new DisplayCutout( + Insets.of(0, 15, 0, 22) /* safeInset */, + null /* boundLeft */, + new Rect(95, 0, 105, 15), + null /* boundRight */, + new Rect(95, 378, 105, 400)); wf.setDisplayCutout(new WmDisplayCutout(cutout, new Size(200, 400))); app.computeFrameLw(); diff --git a/services/tests/servicestests/src/com/android/server/wm/utils/DisplayRotationUtilTest.java b/services/tests/servicestests/src/com/android/server/wm/utils/DisplayRotationUtilTest.java new file mode 100644 index 0000000000000..ba8869b1692d2 --- /dev/null +++ b/services/tests/servicestests/src/com/android/server/wm/utils/DisplayRotationUtilTest.java @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.server.wm.utils; + +import static android.view.DisplayCutout.BOUNDS_POSITION_BOTTOM; +import static android.view.DisplayCutout.BOUNDS_POSITION_LEFT; +import static android.view.DisplayCutout.BOUNDS_POSITION_RIGHT; +import static android.view.DisplayCutout.BOUNDS_POSITION_TOP; +import static android.view.Surface.ROTATION_0; +import static android.view.Surface.ROTATION_180; +import static android.view.Surface.ROTATION_270; +import static android.view.Surface.ROTATION_90; +import static com.android.server.wm.utils.DisplayRotationUtil.getBoundIndexFromRotation; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +import android.graphics.Rect; +import android.platform.test.annotations.Presubmit; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; + + + +/** + * Tests for {@link DisplayRotationUtil} + * + * Run with: atest DisplayRotationUtilTest + */ +@RunWith(AndroidJUnit4.class) +@SmallTest +@Presubmit +public class DisplayRotationUtilTest { + private static Rect ZERO_RECT = new Rect(); + + @Test + public void testGetBoundIndexFromRotation_rot0() { + assertThat(getBoundIndexFromRotation(BOUNDS_POSITION_LEFT, ROTATION_0), + equalTo(BOUNDS_POSITION_LEFT)); + assertThat(getBoundIndexFromRotation(BOUNDS_POSITION_TOP, ROTATION_0), + equalTo(BOUNDS_POSITION_TOP)); + assertThat(getBoundIndexFromRotation(BOUNDS_POSITION_RIGHT, ROTATION_0), + equalTo(BOUNDS_POSITION_RIGHT)); + assertThat(getBoundIndexFromRotation(BOUNDS_POSITION_BOTTOM, ROTATION_0), + equalTo(BOUNDS_POSITION_BOTTOM)); + } + + @Test + public void testGetBoundIndexFromRotation_rot90() { + assertThat(getBoundIndexFromRotation(BOUNDS_POSITION_LEFT, ROTATION_90), + equalTo(BOUNDS_POSITION_BOTTOM)); + assertThat(getBoundIndexFromRotation(BOUNDS_POSITION_TOP, ROTATION_90), + equalTo(BOUNDS_POSITION_LEFT)); + assertThat(getBoundIndexFromRotation(BOUNDS_POSITION_RIGHT, ROTATION_90), + equalTo(BOUNDS_POSITION_TOP)); + assertThat(getBoundIndexFromRotation(BOUNDS_POSITION_BOTTOM, ROTATION_90), + equalTo(BOUNDS_POSITION_RIGHT)); + } + + @Test + public void testGetBoundIndexFromRotation_rot180() { + assertThat(getBoundIndexFromRotation(BOUNDS_POSITION_LEFT, ROTATION_180), + equalTo(BOUNDS_POSITION_RIGHT)); + assertThat(getBoundIndexFromRotation(BOUNDS_POSITION_TOP, ROTATION_180), + equalTo(BOUNDS_POSITION_BOTTOM)); + assertThat(getBoundIndexFromRotation(BOUNDS_POSITION_RIGHT, ROTATION_180), + equalTo(BOUNDS_POSITION_LEFT)); + assertThat(getBoundIndexFromRotation(BOUNDS_POSITION_BOTTOM, ROTATION_180), + equalTo(BOUNDS_POSITION_TOP)); + } + + @Test + public void testGetBoundIndexFromRotation_rot270() { + assertThat(getBoundIndexFromRotation(BOUNDS_POSITION_LEFT, ROTATION_270), + equalTo(BOUNDS_POSITION_TOP)); + assertThat(getBoundIndexFromRotation(BOUNDS_POSITION_TOP, ROTATION_270), + equalTo(BOUNDS_POSITION_RIGHT)); + assertThat(getBoundIndexFromRotation(BOUNDS_POSITION_RIGHT, ROTATION_270), + equalTo(BOUNDS_POSITION_BOTTOM)); + assertThat(getBoundIndexFromRotation(BOUNDS_POSITION_BOTTOM, ROTATION_270), + equalTo(BOUNDS_POSITION_LEFT)); + + } + + @Test + public void testGetRotatedBounds_top_rot0() { + DisplayRotationUtil util = new DisplayRotationUtil(); + Rect[] bounds = new Rect[] { ZERO_RECT, new Rect(50,0,150,10), ZERO_RECT, ZERO_RECT }; + assertThat(util.getRotatedBounds(bounds, ROTATION_0, 200, 300), + equalTo(bounds)); + } + + @Test + public void testGetRotatedBounds_top_rot90() { + DisplayRotationUtil util = new DisplayRotationUtil(); + Rect[] bounds = new Rect[] { ZERO_RECT, new Rect(50,0,150,10), ZERO_RECT, ZERO_RECT }; + assertThat(util.getRotatedBounds(bounds, ROTATION_90, 200, 300), + equalTo(new Rect[] { new Rect(0, 50, 10, 150), ZERO_RECT, ZERO_RECT, ZERO_RECT })); + } + + @Test + public void testGetRotatedBounds_top_rot180() { + DisplayRotationUtil util = new DisplayRotationUtil(); + Rect[] bounds = new Rect[] { ZERO_RECT, new Rect(50,0,150,10), ZERO_RECT, ZERO_RECT }; + assertThat(util.getRotatedBounds(bounds, ROTATION_180, 200, 300), + equalTo(new Rect[] { ZERO_RECT, ZERO_RECT, ZERO_RECT, new Rect(50, 290, 150, 300) })); + } + + @Test + public void testGetRotatedBounds_top_rot270() { + DisplayRotationUtil util = new DisplayRotationUtil(); + Rect[] bounds = new Rect[] { ZERO_RECT, new Rect(50,0,150,10), ZERO_RECT, ZERO_RECT }; + assertThat(util.getRotatedBounds(bounds, ROTATION_270, 200, 300), + equalTo(new Rect[] { ZERO_RECT, ZERO_RECT, new Rect(290, 50, 300, 150), ZERO_RECT })); + } + + @Test + public void testGetRotatedBounds_left_rot0() { + DisplayRotationUtil util = new DisplayRotationUtil(); + Rect[] bounds = new Rect[] { new Rect(0, 50, 10, 150), ZERO_RECT, ZERO_RECT, ZERO_RECT }; + assertThat(util.getRotatedBounds(bounds, ROTATION_0, 300, 200), + equalTo(bounds)); + } + + @Test + public void testGetRotatedBounds_left_rot90() { + DisplayRotationUtil util = new DisplayRotationUtil(); + Rect[] bounds = new Rect[] { new Rect(0, 50, 10, 150), ZERO_RECT, ZERO_RECT, ZERO_RECT }; + assertThat(util.getRotatedBounds(bounds, ROTATION_90, 300, 200), + equalTo(new Rect[]{ ZERO_RECT, ZERO_RECT, ZERO_RECT, new Rect(50, 290, 150, 300) })); + } + + @Test + public void testGetRotatedBounds_left_rot180() { + DisplayRotationUtil util = new DisplayRotationUtil(); + Rect[] bounds = new Rect[] { new Rect(0, 50, 10, 150), ZERO_RECT, ZERO_RECT, ZERO_RECT }; + assertThat(util.getRotatedBounds(bounds, ROTATION_180, 300, 200), + equalTo(new Rect[]{ ZERO_RECT, ZERO_RECT, new Rect(290, 50, 300, 150), ZERO_RECT })); + } + + @Test + public void testGetRotatedBounds_left_rot270() { + DisplayRotationUtil util = new DisplayRotationUtil(); + Rect[] bounds = new Rect[] { new Rect(0, 50, 10, 150), ZERO_RECT, ZERO_RECT, ZERO_RECT }; + assertThat(util.getRotatedBounds(bounds, ROTATION_270, 300, 200), + equalTo(new Rect[]{ ZERO_RECT, new Rect(50, 0, 150, 10), ZERO_RECT, ZERO_RECT })); + } +} diff --git a/services/tests/servicestests/src/com/android/server/wm/utils/WmDisplayCutoutTest.java b/services/tests/servicestests/src/com/android/server/wm/utils/WmDisplayCutoutTest.java index 9ce3dca7e0964..c5e35e7304c4b 100644 --- a/services/tests/servicestests/src/com/android/server/wm/utils/WmDisplayCutoutTest.java +++ b/services/tests/servicestests/src/com/android/server/wm/utils/WmDisplayCutoutTest.java @@ -18,11 +18,19 @@ package com.android.server.wm.utils; import static android.view.DisplayCutout.NO_CUTOUT; +import static android.view.DisplayCutout.BOUNDS_POSITION_BOTTOM; +import static android.view.DisplayCutout.BOUNDS_POSITION_LEFT; +import static android.view.DisplayCutout.BOUNDS_POSITION_RIGHT; +import static android.view.DisplayCutout.BOUNDS_POSITION_TOP; import static android.view.DisplayCutout.fromBoundingRect; +import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertThat; + +import android.graphics.Insets; import android.graphics.Rect; import android.platform.test.annotations.Presubmit; import android.util.Size; @@ -45,15 +53,17 @@ import java.util.Arrays; @SmallTest @Presubmit public class WmDisplayCutoutTest { + private static final Rect ZERO_RECT = new Rect(); private final DisplayCutout mCutoutTop = new DisplayCutout( - new Rect(0, 100, 0, 0), - Arrays.asList(new Rect(50, 0, 75, 100))); + Insets.of(0, 100, 0, 0), + null /* boundLeft */, new Rect(50, 0, 75, 100) /* boundTop */, + null /* boundRight */, null /* boundBottom */); @Test public void calculateRelativeTo_top() { WmDisplayCutout cutout = WmDisplayCutout.computeSafeInsets( - fromBoundingRect(0, 0, 100, 20), 200, 400) + fromBoundingRect(0, 0, 100, 20, BOUNDS_POSITION_TOP), 200, 400) .calculateRelativeTo(new Rect(5, 5, 95, 195)); assertEquals(new Rect(0, 15, 0, 0), cutout.getDisplayCutout().getSafeInsets()); @@ -62,7 +72,7 @@ public class WmDisplayCutoutTest { @Test public void calculateRelativeTo_left() { WmDisplayCutout cutout = WmDisplayCutout.computeSafeInsets( - fromBoundingRect(0, 0, 20, 100), 400, 200) + fromBoundingRect(0, 0, 20, 100, BOUNDS_POSITION_LEFT), 400, 200) .calculateRelativeTo(new Rect(5, 5, 195, 95)); assertEquals(new Rect(15, 0, 0, 0), cutout.getDisplayCutout().getSafeInsets()); @@ -71,7 +81,7 @@ public class WmDisplayCutoutTest { @Test public void calculateRelativeTo_bottom() { WmDisplayCutout cutout = WmDisplayCutout.computeSafeInsets( - fromBoundingRect(0, 180, 100, 200), 100, 200) + fromBoundingRect(0, 180, 100, 200, BOUNDS_POSITION_BOTTOM), 100, 200) .calculateRelativeTo(new Rect(5, 5, 95, 195)); assertEquals(new Rect(0, 0, 0, 15), cutout.getDisplayCutout().getSafeInsets()); @@ -80,7 +90,7 @@ public class WmDisplayCutoutTest { @Test public void calculateRelativeTo_right() { WmDisplayCutout cutout = WmDisplayCutout.computeSafeInsets( - fromBoundingRect(180, 0, 200, 100), 200, 100) + fromBoundingRect(180, 0, 200, 100, BOUNDS_POSITION_RIGHT), 200, 100) .calculateRelativeTo(new Rect(5, 5, 195, 95)); assertEquals(new Rect(0, 0, 15, 0), cutout.getDisplayCutout().getSafeInsets()); @@ -89,16 +99,17 @@ public class WmDisplayCutoutTest { @Test public void calculateRelativeTo_bounds() { WmDisplayCutout cutout = WmDisplayCutout.computeSafeInsets( - fromBoundingRect(0, 0, 100, 20), 200, 400) + fromBoundingRect(0, 0, 100, 20, BOUNDS_POSITION_TOP), 200, 400) .calculateRelativeTo(new Rect(5, 10, 95, 180)); - assertEquals(new Rect(-5, -10, 95, 10), cutout.getDisplayCutout().getBounds().getBounds()); + assertThat(cutout.getDisplayCutout().getBoundingRectTop(), + equalTo(new Rect(-5, -10, 95, 10))); } @Test public void computeSafeInsets_top() { WmDisplayCutout cutout = WmDisplayCutout.computeSafeInsets( - fromBoundingRect(0, 0, 100, 20), 200, 400); + fromBoundingRect(0, 0, 100, 20, BOUNDS_POSITION_TOP), 200, 400); assertEquals(new Rect(0, 20, 0, 0), cutout.getDisplayCutout().getSafeInsets()); } @@ -106,7 +117,7 @@ public class WmDisplayCutoutTest { @Test public void computeSafeInsets_left() { WmDisplayCutout cutout = WmDisplayCutout.computeSafeInsets( - fromBoundingRect(0, 0, 20, 100), 400, 200); + fromBoundingRect(0, 0, 20, 100, BOUNDS_POSITION_LEFT), 400, 200); assertEquals(new Rect(20, 0, 0, 0), cutout.getDisplayCutout().getSafeInsets()); } @@ -114,7 +125,7 @@ public class WmDisplayCutoutTest { @Test public void computeSafeInsets_bottom() { WmDisplayCutout cutout = WmDisplayCutout.computeSafeInsets( - fromBoundingRect(0, 180, 100, 200), 100, 200); + fromBoundingRect(0, 180, 100, 200, BOUNDS_POSITION_BOTTOM), 100, 200); assertEquals(new Rect(0, 0, 0, 20), cutout.getDisplayCutout().getSafeInsets()); } @@ -122,7 +133,7 @@ public class WmDisplayCutoutTest { @Test public void computeSafeInsets_right() { WmDisplayCutout cutout = WmDisplayCutout.computeSafeInsets( - fromBoundingRect(180, 0, 200, 100), 200, 100); + fromBoundingRect(180, 0, 200, 100, BOUNDS_POSITION_RIGHT), 200, 100); assertEquals(new Rect(0, 0, 20, 0), cutout.getDisplayCutout().getSafeInsets()); } @@ -132,8 +143,7 @@ public class WmDisplayCutoutTest { DisplayCutout cutout = WmDisplayCutout.computeSafeInsets(mCutoutTop, 1000, 2000).getDisplayCutout(); - assertEquals(mCutoutTop.getBounds().getBounds(), - cutout.getBounds().getBounds()); + assertEquals(mCutoutTop.getBoundingRects(), cutout.getBoundingRects()); } @Test