diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index f5e39468f8cf..c97aba4fcb19 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -6129,12 +6129,17 @@ public class com/facebook/react/views/imagehelper/MultiSourceHelper$MultiSourceR public fun getBestResultInCache ()Lcom/facebook/react/views/imagehelper/ImageSource; } -public class com/facebook/react/views/imagehelper/ResourceDrawableIdHelper { - public fun clear ()V - public static fun getInstance ()Lcom/facebook/react/views/imagehelper/ResourceDrawableIdHelper; - public fun getResourceDrawable (Landroid/content/Context;Ljava/lang/String;)Landroid/graphics/drawable/Drawable; - public fun getResourceDrawableId (Landroid/content/Context;Ljava/lang/String;)I - public fun getResourceDrawableUri (Landroid/content/Context;Ljava/lang/String;)Landroid/net/Uri; +public final class com/facebook/react/views/imagehelper/ResourceDrawableIdHelper { + public static final field Companion Lcom/facebook/react/views/imagehelper/ResourceDrawableIdHelper$Companion; + public final fun clear ()V + public static final fun getInstance ()Lcom/facebook/react/views/imagehelper/ResourceDrawableIdHelper; + public final fun getResourceDrawable (Landroid/content/Context;Ljava/lang/String;)Landroid/graphics/drawable/Drawable; + public final fun getResourceDrawableId (Landroid/content/Context;Ljava/lang/String;)I + public final fun getResourceDrawableUri (Landroid/content/Context;Ljava/lang/String;)Landroid/net/Uri; +} + +public final class com/facebook/react/views/imagehelper/ResourceDrawableIdHelper$Companion { + public final fun getInstance ()Lcom/facebook/react/views/imagehelper/ResourceDrawableIdHelper; } public class com/facebook/react/views/modal/ModalHostShadowNode$$PropsSetter : com/facebook/react/uimanager/ViewManagerPropertyUpdater$ShadowNodeSetter { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.java deleted file mode 100644 index 5ef44f22dd96..000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.react.views.imagehelper; - -import android.content.Context; -import android.graphics.drawable.Drawable; -import android.net.Uri; -import androidx.annotation.Nullable; -import com.facebook.infer.annotation.Nullsafe; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; -import javax.annotation.concurrent.ThreadSafe; - -/** Helper class for obtaining information about local images. */ -@Nullsafe(Nullsafe.Mode.LOCAL) -@ThreadSafe -public class ResourceDrawableIdHelper { - - private Map mResourceDrawableIdMap; - - private static final String LOCAL_RESOURCE_SCHEME = "res"; - private static volatile ResourceDrawableIdHelper sResourceDrawableIdHelper; - - private ResourceDrawableIdHelper() { - mResourceDrawableIdMap = new HashMap(); - } - - public static ResourceDrawableIdHelper getInstance() { - if (sResourceDrawableIdHelper == null) { - synchronized (ResourceDrawableIdHelper.class) { - if (sResourceDrawableIdHelper == null) { - sResourceDrawableIdHelper = new ResourceDrawableIdHelper(); - } - } - } - return sResourceDrawableIdHelper; - } - - public synchronized void clear() { - mResourceDrawableIdMap.clear(); - } - - public int getResourceDrawableId(Context context, @Nullable String name) { - if (name == null || name.isEmpty()) { - return 0; - } - name = name.toLowerCase(Locale.ROOT).replace("-", "_"); - - // name could be a resource id. - try { - return Integer.parseInt(name); - } catch (NumberFormatException e) { - // Do nothing. - } - - synchronized (this) { - if (mResourceDrawableIdMap.containsKey(name)) { - return mResourceDrawableIdMap.get(name); - } - int id = context.getResources().getIdentifier(name, "drawable", context.getPackageName()); - mResourceDrawableIdMap.put(name, id); - return id; - } - } - - public @Nullable Drawable getResourceDrawable(Context context, @Nullable String name) { - int resId = getResourceDrawableId(context, name); - return resId > 0 ? context.getResources().getDrawable(resId) : null; - } - - public Uri getResourceDrawableUri(Context context, @Nullable String name) { - int resId = getResourceDrawableId(context, name); - return resId > 0 - ? new Uri.Builder().scheme(LOCAL_RESOURCE_SCHEME).path(String.valueOf(resId)).build() - : Uri.EMPTY; - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.kt new file mode 100644 index 000000000000..67e8eef58282 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.kt @@ -0,0 +1,70 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.views.imagehelper + +import android.content.Context +import android.graphics.drawable.Drawable +import android.net.Uri +import androidx.core.content.res.ResourcesCompat +import javax.annotation.concurrent.ThreadSafe + +/** Helper class for obtaining information about local images. */ +@ThreadSafe +public class ResourceDrawableIdHelper private constructor() { + private val resourceDrawableIdMap: MutableMap = HashMap() + + @Synchronized + public fun clear() { + resourceDrawableIdMap.clear() + } + + public fun getResourceDrawableId(context: Context, name: String?): Int { + if (name.isNullOrEmpty()) { + return 0 + } + val normalizedName = name.lowercase().replace("-", "_") + + // name could be a resource id. + try { + return normalizedName.toInt() + } catch (e: NumberFormatException) { + // Do nothing. + } + + synchronized(this) { + if (resourceDrawableIdMap.containsKey(normalizedName)) { + return resourceDrawableIdMap.get(normalizedName)!! + } + return context.resources.getIdentifier(normalizedName, "drawable", context.packageName).also { + resourceDrawableIdMap[normalizedName] = it + } + } + } + + public fun getResourceDrawable(context: Context, name: String?): Drawable? { + val resId = getResourceDrawableId(context, name) + return if (resId > 0) ResourcesCompat.getDrawable(context.resources, resId, null) else null + } + + public fun getResourceDrawableUri(context: Context, name: String?): Uri { + val resId = getResourceDrawableId(context, name) + return if (resId > 0) { + Uri.Builder().scheme(LOCAL_RESOURCE_SCHEME).path(resId.toString()).build() + } else { + Uri.EMPTY + } + } + + public companion object { + private const val LOCAL_RESOURCE_SCHEME = "res" + private val resourceDrawableIdHelper: ResourceDrawableIdHelper = ResourceDrawableIdHelper() + @JvmStatic + public val instance: ResourceDrawableIdHelper + get() = resourceDrawableIdHelper + } +}