forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate ResourceDrawableHelper.java (facebook#43765)
Summary: Pull Request resolved: facebook#43765 ## Changelog: [Internal] - As in the title, migrates the corresponding file to Kotlin. Reviewed By: cortinico Differential Revision: D55636177 fbshipit-source-id: 9e7f94475185953f22fdcb0b4c94b84d4fc4e931
- Loading branch information
1 parent
11108f3
commit f2bdec2
Showing
3 changed files
with
81 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 0 additions & 83 deletions
83
...tAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.java
This file was deleted.
Oops, something went wrong.
70 changes: 70 additions & 0 deletions
70
...actAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |