Migrate ResourceDrawableHelper.java (#43765)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/43765

## Changelog:
[Internal] -

As in the title, migrates the corresponding file to Kotlin.

Reviewed By: cortinico

Differential Revision: D55636177

fbshipit-source-id: 9e7f94475185953f22fdcb0b4c94b84d4fc4e931
This commit is contained in:
Ruslan Shestopalyuk
2024-04-03 06:00:05 -07:00
committed by Facebook GitHub Bot
parent 11108f3763
commit f2bdec2835
3 changed files with 81 additions and 89 deletions
@@ -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 {
@@ -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<String, Integer> mResourceDrawableIdMap;
private static final String LOCAL_RESOURCE_SCHEME = "res";
private static volatile ResourceDrawableIdHelper sResourceDrawableIdHelper;
private ResourceDrawableIdHelper() {
mResourceDrawableIdMap = new HashMap<String, Integer>();
}
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;
}
}
@@ -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<String, Int> = 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
}
}