Skip to content

Commit

Permalink
Fix image loading error when null source is used
Browse files Browse the repository at this point in the history
Summary:
D39423391 (6bdcb49) 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
  • Loading branch information
javache authored and facebook-github-bot committed Oct 10, 2022
1 parent e4dff28 commit 9255eea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
Expand Up @@ -67,8 +67,6 @@
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];

Expand Down Expand Up @@ -309,30 +307,30 @@ public void setSource(@Nullable ReadableArray sources) {
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);
}
}

Expand Down Expand Up @@ -568,8 +566,7 @@ private boolean isTiled() {
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);
Expand Down
Expand Up @@ -9,14 +9,15 @@

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;
Expand All @@ -30,6 +31,10 @@ public ImageSource(Context context, String source, double width, double height)
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;
Expand Down Expand Up @@ -57,7 +62,7 @@ public String getSource() {

/** 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. */
Expand Down

0 comments on commit 9255eea

Please sign in to comment.