From 113c28722b0f461a195f1f425349241eba3528c9 Mon Sep 17 00:00:00 2001 From: shawnlin Date: Tue, 28 Dec 2021 21:18:05 +0800 Subject: Add an public api to create a DisplayCutout with path for test only Currently, there is no way to construct a DisplayCutout with cutout path. Androidx and camera app need an way to create a DisplayCutout with path data for testing and they can only access public API. For test purpose, we add a public API for it. Bug: 212191782 Test: atest DisplayCutoutTests Change-Id: I3118b01447cb77df6245f76c9abcb01ef69e80ec --- core/api/current.txt | 12 ++++ core/java/android/view/DisplayCutout.java | 115 ++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) diff --git a/core/api/current.txt b/core/api/current.txt index e9749228b85c..bfee5de8bee4 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -48174,6 +48174,18 @@ package android.view { method @NonNull public android.graphics.Insets getWaterfallInsets(); } + public static final class DisplayCutout.Builder { + ctor public DisplayCutout.Builder(); + method @NonNull public android.view.DisplayCutout build(); + method @NonNull public android.view.DisplayCutout.Builder setBoundingRectBottom(@NonNull android.graphics.Rect); + method @NonNull public android.view.DisplayCutout.Builder setBoundingRectLeft(@NonNull android.graphics.Rect); + method @NonNull public android.view.DisplayCutout.Builder setBoundingRectRight(@NonNull android.graphics.Rect); + method @NonNull public android.view.DisplayCutout.Builder setBoundingRectTop(@NonNull android.graphics.Rect); + method @NonNull public android.view.DisplayCutout.Builder setCutoutPath(@NonNull android.graphics.Path); + method @NonNull public android.view.DisplayCutout.Builder setSafeInsets(@NonNull android.graphics.Insets); + method @NonNull public android.view.DisplayCutout.Builder setWaterfallInsets(@NonNull android.graphics.Insets); + } + public final class DragAndDropPermissions implements android.os.Parcelable { method public int describeContents(); method public void release(); diff --git a/core/java/android/view/DisplayCutout.java b/core/java/android/view/DisplayCutout.java index ae323226d9cc..9889eaaf12e3 100644 --- a/core/java/android/view/DisplayCutout.java +++ b/core/java/android/view/DisplayCutout.java @@ -1249,4 +1249,119 @@ public final class DisplayCutout { return String.valueOf(mInner); } } + + /** + * A Builder class to construct a DisplayCutout instance. + * + *

Note that this is only for tests purpose. For production code, developers should always + * use a {@link DisplayCutout} obtained from the system.

+ */ + public static final class Builder { + private Insets mSafeInsets = Insets.NONE; + private Insets mWaterfallInsets = Insets.NONE; + private Path mCutoutPath; + private final Rect mBoundingRectLeft = new Rect(); + private final Rect mBoundingRectTop = new Rect(); + private final Rect mBoundingRectRight = new Rect(); + private final Rect mBoundingRectBottom = new Rect(); + + /** + * Begin building a DisplayCutout. + */ + public Builder() { + } + + /** + * Construct a new {@link DisplayCutout} with the set parameters. + */ + @NonNull + public DisplayCutout build() { + final CutoutPathParserInfo info; + if (mCutoutPath != null) { + // Create a fake CutoutPathParserInfo and set it to sCachedCutoutPathParserInfo so + // that when getCutoutPath() is called, it will return the cached Path. + info = new CutoutPathParserInfo(0, 0, 0, "test", 0, 1f); + synchronized (CACHE_LOCK) { + DisplayCutout.sCachedCutoutPathParserInfo = info; + DisplayCutout.sCachedCutoutPath = mCutoutPath; + } + } else { + info = null; + } + return new DisplayCutout(mSafeInsets.toRect(), mWaterfallInsets, mBoundingRectLeft, + mBoundingRectTop, mBoundingRectRight, mBoundingRectBottom, info, false); + } + + /** + * Set the safe insets. If not set, the default value is {@link Insets#NONE}. + */ + @SuppressWarnings("MissingGetterMatchingBuilder") + @NonNull + public Builder setSafeInsets(@NonNull Insets safeInsets) { + mSafeInsets = safeInsets; + return this; + } + + /** + * Set the waterfall insets of the DisplayCutout. If not set, the default value is + * {@link Insets#NONE} + */ + @NonNull + public Builder setWaterfallInsets(@NonNull Insets waterfallInsets) { + mWaterfallInsets = waterfallInsets; + return this; + } + + /** + * Set a bounding rectangle for a non-functional area on the display which is located on + * the left of the screen. If not set, the default value is an empty rectangle. + */ + @NonNull + public Builder setBoundingRectLeft(@NonNull Rect boundingRectLeft) { + mBoundingRectLeft.set(boundingRectLeft); + return this; + } + + /** + * Set a bounding rectangle for a non-functional area on the display which is located on + * the top of the screen. If not set, the default value is an empty rectangle. + */ + @NonNull + public Builder setBoundingRectTop(@NonNull Rect boundingRectTop) { + mBoundingRectTop.set(boundingRectTop); + return this; + } + + /** + * Set a bounding rectangle for a non-functional area on the display which is located on + * the right of the screen. If not set, the default value is an empty rectangle. + */ + @NonNull + public Builder setBoundingRectRight(@NonNull Rect boundingRectRight) { + mBoundingRectRight.set(boundingRectRight); + return this; + } + + /** + * Set a bounding rectangle for a non-functional area on the display which is located on + * the bottom of the screen. If not set, the default value is an empty rectangle. + */ + @NonNull + public Builder setBoundingRectBottom(@NonNull Rect boundingRectBottom) { + mBoundingRectBottom.set(boundingRectBottom); + return this; + } + + /** + * Set the cutout {@link Path}. + * + * Note that not support creating/testing multiple display cutouts with setCutoutPath() in + * parallel. + */ + @NonNull + public Builder setCutoutPath(@NonNull Path cutoutPath) { + mCutoutPath = cutoutPath; + return this; + } + } } -- cgit v1.2.3-59-g8ed1b