Ensure ResourceDrawableIdHelper is thread-safe

Reviewed By: jaegs

Differential Revision: D4696625

fbshipit-source-id: e0aa7870ba02d8e6542c436d7f775bb251cf91ae
This commit is contained in:
Ashwin Bharambe
2017-03-17 17:02:04 -07:00
committed by Facebook Github Bot
parent 95c1926193
commit 11814a5a81
@@ -10,6 +10,7 @@
package com.facebook.react.views.imagehelper;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;
import java.util.HashMap;
import java.util.Map;
@@ -21,12 +22,13 @@ import android.net.Uri;
/**
* Helper class for obtaining information about local images.
*/
@ThreadSafe
public class ResourceDrawableIdHelper {
private Map<String, Integer> mResourceDrawableIdMap;
private static final String LOCAL_RESOURCE_SCHEME = "res";
private static ResourceDrawableIdHelper sResourceDrawableIdHelper;
private static volatile ResourceDrawableIdHelper sResourceDrawableIdHelper;
private ResourceDrawableIdHelper() {
mResourceDrawableIdMap = new HashMap<String, Integer>();
@@ -34,12 +36,16 @@ public class ResourceDrawableIdHelper {
public static ResourceDrawableIdHelper getInstance() {
if (sResourceDrawableIdHelper == null) {
sResourceDrawableIdHelper = new ResourceDrawableIdHelper();
synchronized (ResourceDrawableIdHelper.class) {
if (sResourceDrawableIdHelper == null) {
sResourceDrawableIdHelper = new ResourceDrawableIdHelper();
}
}
}
return sResourceDrawableIdHelper;
}
public void clear() {
public synchronized void clear() {
mResourceDrawableIdMap.clear();
}
@@ -48,15 +54,18 @@ public class ResourceDrawableIdHelper {
return 0;
}
name = name.toLowerCase().replace("-", "_");
if (mResourceDrawableIdMap.containsKey(name)) {
return mResourceDrawableIdMap.get(name);
}
int id = context.getResources().getIdentifier(
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;
mResourceDrawableIdMap.put(name, id);
return id;
}
}
public @Nullable Drawable getResourceDrawable(Context context, @Nullable String name) {