diff options
Diffstat (limited to 'android/view/DisplayCutout.java')
-rw-r--r-- | android/view/DisplayCutout.java | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/android/view/DisplayCutout.java b/android/view/DisplayCutout.java index 66a9c6c0..f59c0b50 100644 --- a/android/view/DisplayCutout.java +++ b/android/view/DisplayCutout.java @@ -31,6 +31,7 @@ import android.os.Parcel; import android.os.Parcelable; import android.text.TextUtils; import android.util.Log; +import android.util.Pair; import android.util.PathParser; import android.util.proto.ProtoOutputStream; @@ -75,15 +76,19 @@ public final class DisplayCutout { false /* copyArguments */); + private static final Pair<Path, DisplayCutout> NULL_PAIR = new Pair<>(null, null); private static final Object CACHE_LOCK = new Object(); + @GuardedBy("CACHE_LOCK") private static String sCachedSpec; @GuardedBy("CACHE_LOCK") private static int sCachedDisplayWidth; @GuardedBy("CACHE_LOCK") + private static int sCachedDisplayHeight; + @GuardedBy("CACHE_LOCK") private static float sCachedDensity; @GuardedBy("CACHE_LOCK") - private static DisplayCutout sCachedCutout; + private static Pair<Path, DisplayCutout> sCachedCutout = NULL_PAIR; private final Rect mSafeInsets; private final Region mBounds; @@ -347,7 +352,7 @@ public final class DisplayCutout { } /** - * Creates an instance according to @android:string/config_mainBuiltInDisplayCutout. + * Creates the bounding path according to @android:string/config_mainBuiltInDisplayCutout. * * @hide */ @@ -357,6 +362,16 @@ public final class DisplayCutout { } /** + * Creates an instance according to @android:string/config_mainBuiltInDisplayCutout. + * + * @hide + */ + public static Path pathFromResources(Resources res, int displayWidth, int displayHeight) { + return pathAndDisplayCutoutFromSpec(res.getString(R.string.config_mainBuiltInDisplayCutout), + displayWidth, displayHeight, res.getDisplayMetrics().density).first; + } + + /** * Creates an instance according to the supplied {@link android.util.PathParser.PathData} spec. * * @hide @@ -364,11 +379,17 @@ public final class DisplayCutout { @VisibleForTesting(visibility = PRIVATE) public static DisplayCutout fromSpec(String spec, int displayWidth, int displayHeight, float density) { + return pathAndDisplayCutoutFromSpec(spec, displayWidth, displayHeight, density).second; + } + + private static Pair<Path, DisplayCutout> pathAndDisplayCutoutFromSpec(String spec, + int displayWidth, int displayHeight, float density) { if (TextUtils.isEmpty(spec)) { - return null; + return NULL_PAIR; } synchronized (CACHE_LOCK) { if (spec.equals(sCachedSpec) && sCachedDisplayWidth == displayWidth + && sCachedDisplayHeight == displayHeight && sCachedDensity == density) { return sCachedCutout; } @@ -398,7 +419,7 @@ public final class DisplayCutout { p = PathParser.createPathFromPathData(spec); } catch (Throwable e) { Log.wtf(TAG, "Could not inflate cutout: ", e); - return null; + return NULL_PAIR; } final Matrix m = new Matrix(); @@ -414,7 +435,7 @@ public final class DisplayCutout { bottomPath = PathParser.createPathFromPathData(bottomSpec); } catch (Throwable e) { Log.wtf(TAG, "Could not inflate bottom cutout: ", e); - return null; + return NULL_PAIR; } // Keep top transform m.postTranslate(0, displayHeight); @@ -422,10 +443,11 @@ public final class DisplayCutout { p.addPath(bottomPath); } - final DisplayCutout result = fromBounds(p); + final Pair<Path, DisplayCutout> result = new Pair<>(p, fromBounds(p)); synchronized (CACHE_LOCK) { sCachedSpec = spec; sCachedDisplayWidth = displayWidth; + sCachedDisplayHeight = displayHeight; sCachedDensity = density; sCachedCutout = result; } |