From f5f7ba0be01c9ec424916dadcfe20b790ec3d1b8 Mon Sep 17 00:00:00 2001 From: Alan Lee Date: Mon, 8 Apr 2024 19:43:47 -0700 Subject: [PATCH] add nullability annotation for DefaultStyleValuesUtil (#43980) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43980 add nullability annotation to DefaultStyleValuesUtil.java Changelog: [Internal] internal Reviewed By: cortinico Differential Revision: D55784913 fbshipit-source-id: f51b4a469f7fdcf59981675344995023cd8e6bec --- .../react/views/text/DefaultStyleValuesUtil.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/DefaultStyleValuesUtil.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/DefaultStyleValuesUtil.java index 6dffc0cce83..86e64ac7488 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/DefaultStyleValuesUtil.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/DefaultStyleValuesUtil.java @@ -11,8 +11,11 @@ import android.content.Context; import android.content.res.ColorStateList; import android.content.res.Resources; import android.content.res.TypedArray; +import androidx.annotation.Nullable; +import com.facebook.infer.annotation.Nullsafe; /** Utility class that access default values from style */ +@Nullsafe(Nullsafe.Mode.LOCAL) public final class DefaultStyleValuesUtil { private DefaultStyleValuesUtil() { @@ -25,6 +28,7 @@ public final class DefaultStyleValuesUtil { * @param context The Context * @return The ColorStateList for the hint text as defined in the style */ + @Nullable public static ColorStateList getDefaultTextColorHint(Context context) { return getDefaultTextAttribute(context, android.R.attr.textColorHint); } @@ -35,6 +39,7 @@ public final class DefaultStyleValuesUtil { * @param context The Context * @return The ColorStateList for the text as defined in the style */ + @Nullable public static ColorStateList getDefaultTextColor(Context context) { return getDefaultTextAttribute(context, android.R.attr.textColor); } @@ -46,9 +51,17 @@ public final class DefaultStyleValuesUtil { * @return The int for the highlight color as defined in the style */ public static int getDefaultTextColorHighlight(Context context) { - return getDefaultTextAttribute(context, android.R.attr.textColorHighlight).getDefaultColor(); + ColorStateList defaultTextAttribute = + getDefaultTextAttribute(context, android.R.attr.textColorHighlight); + if (defaultTextAttribute == null) { + // it would be rare that the highlight color is not defined in the theme, + // but if it is, return black + return 0; + } + return defaultTextAttribute.getDefaultColor(); } + @Nullable private static ColorStateList getDefaultTextAttribute(Context context, int attribute) { Resources.Theme theme = context.getTheme(); TypedArray textAppearances = null;