//xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper:imagehelperAndroid (#44001)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/44001

Changelog: [Internal]

Reviewed By: sammy-SC

Differential Revision: D55725532

fbshipit-source-id: 5fa4e774247e79b0e43d04f58dcb150d682de090
This commit is contained in:
Andrew Datsenko
2024-04-10 07:41:29 -07:00
committed by Facebook GitHub Bot
parent ea566ad70d
commit 54757ca017
3 changed files with 86 additions and 98 deletions
@@ -6204,16 +6204,23 @@ public class com/facebook/react/views/image/ReactImageView : com/facebook/drawee
public fun updateCallerContext (Ljava/lang/Object;)V
}
public class com/facebook/react/views/imagehelper/ImageSource {
public final class com/facebook/react/views/imagehelper/ImageSource {
public static final field Companion Lcom/facebook/react/views/imagehelper/ImageSource$Companion;
public fun <init> (Landroid/content/Context;Ljava/lang/String;)V
public fun <init> (Landroid/content/Context;Ljava/lang/String;D)V
public fun <init> (Landroid/content/Context;Ljava/lang/String;DD)V
public synthetic fun <init> (Landroid/content/Context;Ljava/lang/String;DDILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun equals (Ljava/lang/Object;)Z
public fun getSize ()D
public fun getSource ()Ljava/lang/String;
public static fun getTransparentBitmapImageSource (Landroid/content/Context;)Lcom/facebook/react/views/imagehelper/ImageSource;
public fun getUri ()Landroid/net/Uri;
public final fun getSize ()D
public final fun getSource ()Ljava/lang/String;
public static final fun getTransparentBitmapImageSource (Landroid/content/Context;)Lcom/facebook/react/views/imagehelper/ImageSource;
public final fun getUri ()Landroid/net/Uri;
public fun hashCode ()I
public fun isResource ()Z
public final fun isResource ()Z
}
public final class com/facebook/react/views/imagehelper/ImageSource$Companion {
public final fun getTransparentBitmapImageSource (Landroid/content/Context;)Lcom/facebook/react/views/imagehelper/ImageSource;
}
public final class com/facebook/react/views/imagehelper/MultiSourceHelper {
@@ -1,92 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.views.imagehelper;
import android.content.Context;
import android.net.Uri;
import java.util.Objects;
/** Class describing an image source (network URI or resource) and size. */
public class ImageSource {
private static final String TRANSPARENT_BITMAP_URI =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";
private Uri mUri;
private String mSource;
private double mSize;
private boolean isResource;
public ImageSource(Context context, String source, double width, double height) {
mSource = source;
mSize = width * height;
// Important: we compute the URI here so that we don't need to hold a reference to the context,
// potentially causing leaks.
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;
if (o == null || getClass() != o.getClass()) return false;
ImageSource that = (ImageSource) o;
return Double.compare(that.mSize, mSize) == 0
&& isResource == that.isResource
&& Objects.equals(mUri, that.mUri)
&& Objects.equals(mSource, that.mSource);
}
@Override
public int hashCode() {
return Objects.hash(mUri, mSource, mSize, isResource);
}
public ImageSource(Context context, String source) {
this(context, source, 0.0d, 0.0d);
}
/** Get the source of this image, as it was passed to the constructor. */
public String getSource() {
return mSource;
}
/** Get the URI for this image - can be either a parsed network URI or a resource URI. */
public Uri getUri() {
return mUri;
}
/** Get the area of this image. */
public double getSize() {
return mSize;
}
/** Get whether this image source represents an Android resource or a network URI. */
public boolean isResource() {
return isResource;
}
private Uri computeUri(Context context) {
try {
Uri uri = Uri.parse(mSource);
// Verify scheme is set, so that relative uri (used by static resources) are not handled.
return uri.getScheme() == null ? computeLocalUri(context) : uri;
} catch (Exception e) {
return computeLocalUri(context);
}
}
private Uri computeLocalUri(Context context) {
isResource = true;
return ResourceDrawableIdHelper.getInstance().getResourceDrawableUri(context, mSource);
}
}
@@ -0,0 +1,73 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.views.imagehelper
import android.content.Context
import android.net.Uri
import java.util.Objects
/** Class describing an image source (network URI or resource) and size. */
public class ImageSource
@JvmOverloads
constructor(
context: Context,
/** Get the source of this image, as it was passed to the constructor. */
public val source: String?,
width: Double = 0.0,
height: Double = 0.0
) {
/** Get the URI for this image - can be either a parsed network URI or a resource URI. */
public val uri: Uri = computeUri(context)
/** Get the area of this image. */
public val size: Double = width * height
/** Get whether this image source represents an Android resource or a network URI. */
public var isResource: Boolean = false
private set
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
if (other == null || javaClass != other.javaClass) {
return false
}
val that = other as ImageSource
return java.lang.Double.compare(that.size, size) == 0 &&
isResource == that.isResource &&
uri == that.uri &&
source == that.source
}
override fun hashCode(): Int = Objects.hash(uri, source, size, isResource)
private fun computeUri(context: Context): Uri =
try {
val uri = Uri.parse(source)
// Verify scheme is set, so that relative uri (used by static resources) are not handled.
if (uri.scheme == null) computeLocalUri(context) else uri
} catch (e: NullPointerException) {
computeLocalUri(context)
}
private fun computeLocalUri(context: Context): Uri {
isResource = true
return ResourceDrawableIdHelper.instance.getResourceDrawableUri(context, source)
}
public companion object {
private const val TRANSPARENT_BITMAP_URI =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="
@JvmStatic
public fun getTransparentBitmapImageSource(context: Context): ImageSource =
ImageSource(context, TRANSPARENT_BITMAP_URI)
}
}