Permalink
Browse files

Ensure `ResourceDrawableIdHelper` is thread-safe

Reviewed By: jaegs

Differential Revision: D4696625

fbshipit-source-id: e0aa7870ba02d8e6542c436d7f775bb251cf91ae
  • Loading branch information...
ashwinb authored and facebook-github-bot committed Mar 17, 2017
1 parent 95c1926 commit 11814a5a81a1850a6c28c78252afa68be8348815
@@ -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,25 +22,30 @@
/**
* 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>();
}
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 int getResourceDrawableId(Context context, @Nullable String name) {
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) {

0 comments on commit 11814a5

Please sign in to comment.