From b069fca5c501a29a6170b759947456b8d57ba5bd Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Mon, 8 Apr 2024 06:40:56 -0700 Subject: [PATCH] Convert views/image interface files (#43742) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43742 ## Changelog: [Internal] - As in the title - taking a subset of the views/image Java files, those that are related to interfaces, and convert them to Kotlin. Reviewed By: tdn120 Differential Revision: D55589946 fbshipit-source-id: 60f03eaaca467821634d5b0195e42bfb931f65fb --- ...stener.java => GlobalImageLoadListener.kt} | 7 +- .../image/ReactCallerContextFactory.java | 26 ------ .../views/image/ReactCallerContextFactory.kt | 22 +++++ .../image/ReactImageDownloadListener.java | 80 ------------------- .../views/image/ReactImageDownloadListener.kt | 53 ++++++++++++ .../views/image/ScaleTypeStartInside.java | 40 ---------- .../react/views/image/ScaleTypeStartInside.kt | 41 ++++++++++ 7 files changed, 119 insertions(+), 150 deletions(-) rename packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/{GlobalImageLoadListener.java => GlobalImageLoadListener.kt} (79%) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactCallerContextFactory.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactCallerContextFactory.kt delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageDownloadListener.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageDownloadListener.kt delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ScaleTypeStartInside.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ScaleTypeStartInside.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/GlobalImageLoadListener.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/GlobalImageLoadListener.kt similarity index 79% rename from packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/GlobalImageLoadListener.java rename to packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/GlobalImageLoadListener.kt index 745b910a133a54..bcb68f2803a0f1 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/GlobalImageLoadListener.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/GlobalImageLoadListener.kt @@ -5,13 +5,12 @@ * LICENSE file in the root directory of this source tree. */ -package com.facebook.react.views.image; +package com.facebook.react.views.image -import android.net.Uri; +import android.net.Uri /** Listener interface for global image loading events. */ public interface GlobalImageLoadListener { - /** Called when a source has been set on an ImageView, but before it is actually loaded. */ - void onLoadAttempt(Uri uri); + public fun onLoadAttempt(uri: Uri?) } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactCallerContextFactory.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactCallerContextFactory.java deleted file mode 100644 index 183ff6d3d0ee79..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactCallerContextFactory.java +++ /dev/null @@ -1,26 +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.image; - -import androidx.annotation.Nullable; - -/** - * This interface is used from {@link ReactImageManager} to customize the CallerContext object - * associated with each instance of {@link ReactImageView}. - */ -public interface ReactCallerContextFactory { - - /** - * This method will be called at the time {@link ReactImageManager} creates {@link ReactImageView} - * - * @param surfaceName {@link String} used to log the name of the surface - * @return an {@link Object} that represents the CallerContext. - */ - @Nullable - Object getOrCreateCallerContext(@Nullable String surfaceName, @Nullable String analyticTag); -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactCallerContextFactory.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactCallerContextFactory.kt new file mode 100644 index 00000000000000..d5c5722411057f --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactCallerContextFactory.kt @@ -0,0 +1,22 @@ +/* + * 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.image + +/** + * This interface is used from [ReactImageManager] to customize the CallerContext object associated + * with each instance of [ReactImageView]. + */ +public fun interface ReactCallerContextFactory { + /** + * This method will be called at the time [ReactImageManager] creates [ReactImageView] + * + * @param surfaceName [String] used to log the name of the surface + * @return an [Object] that represents the CallerContext. + */ + public fun getOrCreateCallerContext(surfaceName: String?, analyticTag: String?): Any? +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageDownloadListener.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageDownloadListener.java deleted file mode 100644 index 921b423c983354..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageDownloadListener.java +++ /dev/null @@ -1,80 +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.image; - -import android.graphics.Canvas; -import android.graphics.ColorFilter; -import android.graphics.PixelFormat; -import android.graphics.drawable.Animatable; -import android.graphics.drawable.Drawable; -import com.facebook.drawee.controller.ControllerListener; -import com.facebook.drawee.drawable.ForwardingDrawable; -import com.facebook.infer.annotation.Nullsafe; -import javax.annotation.Nullable; - -@Nullsafe(Nullsafe.Mode.LOCAL) -class ReactImageDownloadListener extends ForwardingDrawable - implements ControllerListener { - - private static final int MAX_LEVEL = 10000; - - public ReactImageDownloadListener() { - super(new EmptyDrawable()); - } - - public void onProgressChange(int loaded, int total) {} - - @Override - protected boolean onLevelChange(int level) { - onProgressChange(level, MAX_LEVEL); - return super.onLevelChange(level); - } - - @Override - public void onSubmit(String id, Object callerContext) {} - - @Override - public void onFinalImageSet( - String id, @Nullable INFO imageInfo, @Nullable Animatable animatable) {} - - @Override - public void onIntermediateImageSet(String id, @Nullable INFO imageInfo) {} - - @Override - public void onIntermediateImageFailed(String id, Throwable throwable) {} - - @Override - public void onFailure(String id, Throwable throwable) {} - - @Override - public void onRelease(String id) {} - - /** A {@link Drawable} that renders nothing. */ - private static final class EmptyDrawable extends Drawable { - - @Override - public void draw(Canvas canvas) { - // Do nothing. - } - - @Override - public void setAlpha(int alpha) { - // Do nothing. - } - - @Override - public void setColorFilter(ColorFilter colorFilter) { - // Do nothing. - } - - @Override - public int getOpacity() { - return PixelFormat.OPAQUE; - } - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageDownloadListener.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageDownloadListener.kt new file mode 100644 index 00000000000000..feea9b70263fb1 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageDownloadListener.kt @@ -0,0 +1,53 @@ +/* + * 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.image + +import android.graphics.Canvas +import android.graphics.ColorFilter +import android.graphics.PixelFormat +import android.graphics.drawable.Animatable +import android.graphics.drawable.Drawable +import com.facebook.drawee.controller.ControllerListener +import com.facebook.drawee.drawable.ForwardingDrawable + +internal open class ReactImageDownloadListener : + ForwardingDrawable(EmptyDrawable()), ControllerListener { + open fun onProgressChange(loaded: Int, total: Int) = Unit + + override fun onLevelChange(level: Int): Boolean { + onProgressChange(level, MAX_LEVEL) + return super.onLevelChange(level) + } + + override fun onSubmit(id: String, callerContext: Any) = Unit + + override fun onFinalImageSet(id: String, imageInfo: INFO?, animatable: Animatable?) = Unit + + override fun onIntermediateImageSet(id: String, imageInfo: INFO?) = Unit + + override fun onIntermediateImageFailed(id: String, throwable: Throwable) = Unit + + override fun onFailure(id: String, throwable: Throwable) = Unit + + override fun onRelease(id: String) = Unit + + /** A [Drawable] that renders nothing. */ + private class EmptyDrawable : Drawable() { + override fun draw(canvas: Canvas) = Unit + + override fun setAlpha(alpha: Int) = Unit + + override fun setColorFilter(colorFilter: ColorFilter?) = Unit + + override fun getOpacity(): Int = PixelFormat.OPAQUE + } + + companion object { + private const val MAX_LEVEL = 10000 + } +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ScaleTypeStartInside.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ScaleTypeStartInside.java deleted file mode 100644 index 137e153c4f1324..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ScaleTypeStartInside.java +++ /dev/null @@ -1,40 +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.image; - -import android.graphics.Matrix; -import android.graphics.Rect; -import com.facebook.drawee.drawable.ScalingUtils; -import com.facebook.infer.annotation.Nullsafe; - -@Nullsafe(Nullsafe.Mode.LOCAL) -class ScaleTypeStartInside extends ScalingUtils.AbstractScaleType { - public static final ScalingUtils.ScaleType INSTANCE = new ScaleTypeStartInside(); - - @Override - public void getTransformImpl( - Matrix outTransform, - Rect parentRect, - int childWidth, - int childHeight, - float focusX, - float focusY, - float scaleX, - float scaleY) { - float scale = Math.min(Math.min(scaleX, scaleY), 1.0f); - float dx = parentRect.left; - float dy = parentRect.top; - outTransform.setScale(scale, scale); - outTransform.postTranslate(Math.round(dx), Math.round(dy)); - } - - @Override - public String toString() { - return "start_inside"; - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ScaleTypeStartInside.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ScaleTypeStartInside.kt new file mode 100644 index 00000000000000..dc41a3d29a32ca --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ScaleTypeStartInside.kt @@ -0,0 +1,41 @@ +/* + * 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.image + +import android.graphics.Matrix +import android.graphics.Rect +import com.facebook.drawee.drawable.ScalingUtils +import com.facebook.drawee.drawable.ScalingUtils.AbstractScaleType +import kotlin.math.min + +internal class ScaleTypeStartInside : AbstractScaleType() { + override fun getTransformImpl( + outTransform: Matrix, + parentRect: Rect, + childWidth: Int, + childHeight: Int, + focusX: Float, + focusY: Float, + scaleX: Float, + scaleY: Float + ) { + val scale = min(scaleX, scaleY).coerceAtMost(1.0f) + val dx = parentRect.left.toFloat() + val dy = parentRect.top.toFloat() + outTransform.setScale(scale, scale) + outTransform.postTranslate(Math.round(dx).toFloat(), Math.round(dy).toFloat()) + } + + override fun toString(): String { + return "start_inside" + } + + companion object { + val INSTANCE: ScalingUtils.ScaleType = ScaleTypeStartInside() + } +}