diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index df25ef1f266..bbad17e405b 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -5871,9 +5871,9 @@ public abstract interface class com/facebook/react/viewmanagers/UnimplementedNat public abstract fun setName (Landroid/view/View;Ljava/lang/String;)V } -public class com/facebook/react/views/common/ContextUtils { - public fun ()V - public static fun findContextOfType (Landroid/content/Context;Ljava/lang/Class;)Ljava/lang/Object; +public final class com/facebook/react/views/common/ContextUtils { + public static final field INSTANCE Lcom/facebook/react/views/common/ContextUtils; + public static final fun findContextOfType (Landroid/content/Context;Ljava/lang/Class;)Ljava/lang/Object; } public class com/facebook/react/views/common/ViewUtils { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/common/ContextUtils.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/common/ContextUtils.kt similarity index 51% rename from packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/common/ContextUtils.java rename to packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/common/ContextUtils.kt index 040070d45fd..9ca1d9fcc72 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/common/ContextUtils.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/common/ContextUtils.kt @@ -5,18 +5,15 @@ * LICENSE file in the root directory of this source tree. */ -package com.facebook.react.views.common; +package com.facebook.react.views.common -import android.content.Context; -import android.content.ContextWrapper; -import androidx.annotation.Nullable; -import com.facebook.infer.annotation.Nullsafe; +import android.content.Context +import android.content.ContextWrapper /** * Class containing static methods involving manipulations of Contexts and their related subclasses. */ -@Nullsafe(Nullsafe.Mode.LOCAL) -public class ContextUtils { +public object ContextUtils { /** * Returns the nearest context in the chain (as defined by ContextWrapper.getBaseContext()) which @@ -24,23 +21,23 @@ public class ContextUtils { * * @param context Initial context * @param clazz Class instance to look for - * @param * @return the first context which is an instance of the specified class, or null if none exists */ - public static @Nullable T findContextOfType( - @Nullable Context context, Class clazz) { - while (!(clazz.isInstance(context))) { - if (context instanceof ContextWrapper) { - Context baseContext = ((ContextWrapper) context).getBaseContext(); - if (context == baseContext) { - return null; + @JvmStatic + public fun findContextOfType(context: Context?, clazz: Class): T? { + var currentContext = context + while (!clazz.isInstance(currentContext)) { + if (currentContext is ContextWrapper) { + val baseContext = currentContext.baseContext + if (currentContext === baseContext) { + return null } else { - context = baseContext; + currentContext = baseContext } } else { - return null; + return null } } - return (T) context; + @Suppress("UNCHECKED_CAST") return currentContext as T? } }