From 54757ca017d34e7ccde413ae4a9dd5a21f794f13 Mon Sep 17 00:00:00 2001 From: Andrew Datsenko Date: Wed, 10 Apr 2024 07:41:29 -0700 Subject: [PATCH] //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 --- .../ReactAndroid/api/ReactAndroid.api | 19 ++-- .../react/views/imagehelper/ImageSource.java | 92 ------------------- .../react/views/imagehelper/ImageSource.kt | 73 +++++++++++++++ 3 files changed, 86 insertions(+), 98 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ImageSource.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ImageSource.kt diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index bb08f31c64e..366c59121cf 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -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 (Landroid/content/Context;Ljava/lang/String;)V + public fun (Landroid/content/Context;Ljava/lang/String;D)V public fun (Landroid/content/Context;Ljava/lang/String;DD)V + public synthetic fun (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 { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ImageSource.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ImageSource.java deleted file mode 100644 index 6a5361be969..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ImageSource.java +++ /dev/null @@ -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); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ImageSource.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ImageSource.kt new file mode 100644 index 00000000000..60205147861 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ImageSource.kt @@ -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) + } +}