diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageManager.java index 8b3298647..4231d207d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageManager.java @@ -32,6 +32,7 @@ public class ReactImageManager extends SimpleViewManager { return REACT_CLASS; } + private ResourceDrawableIdHelper mResourceDrawableIdHelper; private @Nullable AbstractDraweeControllerBuilder mDraweeControllerBuilder; private final @Nullable Object mCallerContext; @@ -40,12 +41,14 @@ public class ReactImageManager extends SimpleViewManager { Object callerContext) { mDraweeControllerBuilder = draweeControllerBuilder; mCallerContext = callerContext; + mResourceDrawableIdHelper = new ResourceDrawableIdHelper(); } public ReactImageManager() { // Lazily initialize as FrescoModule have not been initialized yet mDraweeControllerBuilder = null; mCallerContext = null; + mResourceDrawableIdHelper = new ResourceDrawableIdHelper(); } public AbstractDraweeControllerBuilder getDraweeControllerBuilder() { @@ -70,13 +73,13 @@ public class ReactImageManager extends SimpleViewManager { // In JS this is Image.props.source.uri @ReactProp(name = "src") public void setSource(ReactImageView view, @Nullable String source) { - view.setSource(source); + view.setSource(source, mResourceDrawableIdHelper); } // In JS this is Image.props.loadingIndicatorSource.uri @ReactProp(name = "loadingIndicatorSrc") public void setLoadingIndicatorSource(ReactImageView view, @Nullable String source) { - view.setLoadingIndicatorSource(source); + view.setLoadingIndicatorSource(source, mResourceDrawableIdHelper); } @ReactProp(name = "borderColor", customType = "Color") diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageView.java b/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageView.java index be57d799d..1c020acdc 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageView.java @@ -15,7 +15,6 @@ import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; -import android.graphics.drawable.Drawable; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; @@ -208,7 +207,9 @@ public class ReactImageView extends GenericDraweeView { mIsDirty = true; } - public void setSource(@Nullable String source) { + public void setSource( + @Nullable String source, + ResourceDrawableIdHelper resourceDrawableIdHelper) { mUri = null; if (source != null) { try { @@ -221,7 +222,7 @@ public class ReactImageView extends GenericDraweeView { // ignore malformed uri, then attempt to extract resource ID. } if (mUri == null) { - mUri = getResourceDrawableUri(getContext(), source); + mUri = resourceDrawableIdHelper.getResourceDrawableUri(getContext(), source); mIsLocalImage = true; } else { mIsLocalImage = false; @@ -230,8 +231,10 @@ public class ReactImageView extends GenericDraweeView { mIsDirty = true; } - public void setLoadingIndicatorSource(@Nullable String name) { - Drawable drawable = getResourceDrawable(getContext(), name); + public void setLoadingIndicatorSource( + @Nullable String name, + ResourceDrawableIdHelper resourceDrawableIdHelper) { + Drawable drawable = resourceDrawableIdHelper.getResourceDrawable(getContext(), name); mLoadingImageDrawable = drawable != null ? (Drawable) new AutoRotateDrawable(drawable, 1000) : null; mIsDirty = true; @@ -349,27 +352,4 @@ public class ReactImageView extends GenericDraweeView { // has no control over the original size return uri != null && (UriUtil.isLocalContentUri(uri) || UriUtil.isLocalFileUri(uri)); } - - private static int getResourceDrawableId(Context context, @Nullable String name) { - if (name == null || name.isEmpty()) { - return 0; - } - return context.getResources().getIdentifier( - name.toLowerCase().replace("-", "_"), - "drawable", - context.getPackageName()); - } - - private static @Nullable Drawable getResourceDrawable(Context context, @Nullable String name) { - int resId = getResourceDrawableId(context, name); - return resId > 0 ? context.getResources().getDrawable(resId) : null; - } - - private static Uri getResourceDrawableUri(Context context, @Nullable String name) { - int resId = getResourceDrawableId(context, name); - return resId > 0 ? new Uri.Builder() - .scheme(UriUtil.LOCAL_RESOURCE_SCHEME) - .path(String.valueOf(resId)) - .build() : Uri.EMPTY; - } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/image/ResourceDrawableIdHelper.java b/ReactAndroid/src/main/java/com/facebook/react/views/image/ResourceDrawableIdHelper.java new file mode 100644 index 000000000..17d930601 --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/views/image/ResourceDrawableIdHelper.java @@ -0,0 +1,55 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +package com.facebook.react.views.image; + +import javax.annotation.Nullable; + +import java.util.HashMap; +import java.util.Map; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.net.Uri; + +import com.facebook.common.util.UriUtil; + +/** + * Helper class for obtaining information about local images. + */ +/* package */ class ResourceDrawableIdHelper { + + private Map mResourceDrawableIdMap; + + public ResourceDrawableIdHelper() { + mResourceDrawableIdMap = new HashMap(); + } + + public int getResourceDrawableId(Context context, @Nullable String name) { + if (name == null || name.isEmpty()) { + return 0; + } + name = name.toLowerCase().replace("-", "_"); + 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(UriUtil.LOCAL_RESOURCE_SCHEME) + .path(String.valueOf(resId)) + .build() : Uri.EMPTY; + } +}