Skip to content

Commit

Permalink
Cache image resource ids
Browse files Browse the repository at this point in the history
Reviewed By: AaaChiuuu

Differential Revision: D2895691

fb-gh-sync-id: 40824c6fdf5bdd8f0b724a7c81645d3ea911bccf
  • Loading branch information
andreicoman11 authored and facebook-github-bot-5 committed Feb 5, 2016
1 parent 10c0758 commit fef4196
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public String getName() {
return REACT_CLASS;
}

private ResourceDrawableIdHelper mResourceDrawableIdHelper;
private @Nullable AbstractDraweeControllerBuilder mDraweeControllerBuilder;
private final @Nullable Object mCallerContext;

Expand All @@ -40,12 +41,14 @@ public ReactImageManager(
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() {
Expand All @@ -70,13 +73,13 @@ public ReactImageView createViewInstance(ThemedReactContext context) {
// 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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
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;
Expand Down Expand Up @@ -208,7 +207,9 @@ public void setScaleType(ScalingUtils.ScaleType scaleType) {
mIsDirty = true;
}

public void setSource(@Nullable String source) {
public void setSource(
@Nullable String source,
ResourceDrawableIdHelper resourceDrawableIdHelper) {
mUri = null;
if (source != null) {
try {
Expand All @@ -221,7 +222,7 @@ public void setSource(@Nullable String source) {
// 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;
Expand All @@ -230,8 +231,10 @@ public void setSource(@Nullable String source) {
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;
Expand Down Expand Up @@ -349,27 +352,4 @@ private static boolean shouldResize(@Nullable Uri uri) {
// 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Integer> mResourceDrawableIdMap;

public ResourceDrawableIdHelper() {
mResourceDrawableIdMap = new HashMap<String, Integer>();
}

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;
}
}

0 comments on commit fef4196

Please sign in to comment.