Fix image loading error when null source is used

Summary:
D39423391 (https://github.com/facebook/react-native/commit/6bdcb49966882bcf696d42066e289783c003dcfc) caused Android to receive more empty / null URI's, which caused incorrect network requests to be sent.

Changelog: [Internal]

Reviewed By: cipolleschi

Differential Revision: D40224924

fbshipit-source-id: 2de7920cc66a51c2967afe440614db11821aedd7
This commit is contained in:
Pieter De Baets
2022-10-10 10:13:29 -07:00
committed by Facebook GitHub Bot
parent e4dff28c58
commit 9255eeabb3
2 changed files with 30 additions and 28 deletions
@@ -67,8 +67,6 @@ import java.util.List;
public class ReactImageView extends GenericDraweeView {
public static final int REMOTE_IMAGE_FADE_DURATION_MS = 300;
public static final String REMOTE_TRANSPARENT_BITMAP_URI =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";
private static float[] sComputedCornerRadii = new float[4];
@@ -309,30 +307,30 @@ public class ReactImageView extends GenericDraweeView {
List<ImageSource> tmpSources = new LinkedList<>();
if (sources == null || sources.size() == 0) {
ImageSource imageSource = new ImageSource(getContext(), REMOTE_TRANSPARENT_BITMAP_URI);
tmpSources.add(ImageSource.getTransparentBitmapImageSource(getContext()));
} else if (sources.size() == 1) {
// Optimize for the case where we have just one uri, case in which we don't need the sizes
ReadableMap source = sources.getMap(0);
ImageSource imageSource = new ImageSource(getContext(), source.getString("uri"));
if (Uri.EMPTY.equals(imageSource.getUri())) {
warnImageSource(source.getString("uri"));
imageSource = ImageSource.getTransparentBitmapImageSource(getContext());
}
tmpSources.add(imageSource);
} else {
// Optimize for the case where we have just one uri, case in which we don't need the sizes
if (sources.size() == 1) {
ReadableMap source = sources.getMap(0);
String uri = source.getString("uri");
ImageSource imageSource = new ImageSource(getContext(), uri);
tmpSources.add(imageSource);
for (int idx = 0; idx < sources.size(); idx++) {
ReadableMap source = sources.getMap(idx);
ImageSource imageSource =
new ImageSource(
getContext(),
source.getString("uri"),
source.getDouble("width"),
source.getDouble("height"));
if (Uri.EMPTY.equals(imageSource.getUri())) {
warnImageSource(uri);
}
} else {
for (int idx = 0; idx < sources.size(); idx++) {
ReadableMap source = sources.getMap(idx);
String uri = source.getString("uri");
ImageSource imageSource =
new ImageSource(
getContext(), uri, source.getDouble("width"), source.getDouble("height"));
tmpSources.add(imageSource);
if (Uri.EMPTY.equals(imageSource.getUri())) {
warnImageSource(uri);
}
warnImageSource(source.getString("uri"));
imageSource = ImageSource.getTransparentBitmapImageSource(getContext());
}
tmpSources.add(imageSource);
}
}
@@ -568,8 +566,7 @@ public class ReactImageView extends GenericDraweeView {
private void setSourceImage() {
mImageSource = null;
if (mSources.isEmpty()) {
ImageSource imageSource = new ImageSource(getContext(), REMOTE_TRANSPARENT_BITMAP_URI);
mSources.add(imageSource);
mSources.add(ImageSource.getTransparentBitmapImageSource(getContext()));
} else if (hasMultipleSources()) {
MultiSourceResult multiSource =
MultiSourceHelper.getBestSourceForSize(getWidth(), getHeight(), mSources);
@@ -9,14 +9,15 @@ package com.facebook.react.views.imagehelper;
import android.content.Context;
import android.net.Uri;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Assertions;
import java.util.Objects;
/** Class describing an image source (network URI or resource) and size. */
public class ImageSource {
private @Nullable Uri mUri;
private static final String TRANSPARENT_BITMAP_URI =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";
private Uri mUri;
private String mSource;
private double mSize;
private boolean isResource;
@@ -30,6 +31,10 @@ public class ImageSource {
mUri = computeUri(context);
}
public static ImageSource getTransparentBitmapImageSource(Context context) {
return new ImageSource(context, TRANSPARENT_BITMAP_URI);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@@ -57,7 +62,7 @@ public class ImageSource {
/** Get the URI for this image - can be either a parsed network URI or a resource URI. */
public Uri getUri() {
return Assertions.assertNotNull(mUri);
return mUri;
}
/** Get the area of this image. */