From 224eb7c974df77c123e4efd2093b07a6a2358f9c Mon Sep 17 00:00:00 2001 From: jonathanmos <48201295+jonathanmos@users.noreply.github.com> Date: Wed, 13 Nov 2024 12:50:13 +0200 Subject: [PATCH] RUM-7024: Add ImageViewMapper internal constructor --- .../internal/DefaultRecorderProvider.kt | 10 ++-- .../internal/utils/ImageViewUtils.kt | 6 +-- .../recorder/mapper/ImageViewMapper.kt | 50 +++++++++++++------ .../internal/utils/ImageViewUtilsTest.kt | 20 ++++---- .../recorder/mapper/ImageViewMapperTest.kt | 9 ++-- 5 files changed, 60 insertions(+), 35 deletions(-) diff --git a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/DefaultRecorderProvider.kt b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/DefaultRecorderProvider.kt index fcbfffa56d..bbe80d462c 100644 --- a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/DefaultRecorderProvider.kt +++ b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/DefaultRecorderProvider.kt @@ -41,6 +41,7 @@ import com.datadog.android.sessionreplay.internal.resources.ResourceDataStoreMan import com.datadog.android.sessionreplay.internal.storage.RecordWriter import com.datadog.android.sessionreplay.internal.storage.ResourcesWriter import com.datadog.android.sessionreplay.internal.time.SessionReplayTimeProvider +import com.datadog.android.sessionreplay.internal.utils.ImageViewUtils import com.datadog.android.sessionreplay.recorder.OptionSelectorDetector import com.datadog.android.sessionreplay.recorder.mapper.EditTextMapper import com.datadog.android.sessionreplay.recorder.mapper.ImageViewMapper @@ -96,10 +97,11 @@ internal class DefaultRecorderProvider( val viewBoundsResolver: ViewBoundsResolver = DefaultViewBoundsResolver val drawableToColorMapper: DrawableToColorMapper = DrawableToColorMapper.getDefault() val imageViewMapper = ImageViewMapper( - viewIdentifierResolver, - colorStringFormatter, - viewBoundsResolver, - drawableToColorMapper + viewIdentifierResolver = viewIdentifierResolver, + colorStringFormatter = colorStringFormatter, + viewBoundsResolver = viewBoundsResolver, + drawableToColorMapper = drawableToColorMapper, + imageViewUtils = ImageViewUtils ) val textViewMapper = TextViewMapper( viewIdentifierResolver, diff --git a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/utils/ImageViewUtils.kt b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/utils/ImageViewUtils.kt index d95ab4886e..d5ca9d6b26 100644 --- a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/utils/ImageViewUtils.kt +++ b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/utils/ImageViewUtils.kt @@ -62,13 +62,13 @@ internal object ImageViewUtils { } internal fun resolveContentRectWithScaling( - view: ImageView, + imageView: ImageView, drawable: Drawable ): Rect { val drawableWidthPx = drawable.intrinsicWidth val drawableHeightPx = drawable.intrinsicHeight - val parentRect = resolveParentRectAbsPosition(view) + val parentRect = resolveParentRectAbsPosition(imageView) val childRect = Rect( 0, @@ -79,7 +79,7 @@ internal object ImageViewUtils { val resultRect: Rect - when (view.scaleType) { + when (imageView.scaleType) { ImageView.ScaleType.FIT_START -> { val contentRect = scaleRectToFitParent(parentRect, childRect) resultRect = positionRectAtStart(parentRect, contentRect) diff --git a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/recorder/mapper/ImageViewMapper.kt b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/recorder/mapper/ImageViewMapper.kt index 2f88122b69..2217dd4a08 100644 --- a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/recorder/mapper/ImageViewMapper.kt +++ b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/recorder/mapper/ImageViewMapper.kt @@ -23,17 +23,38 @@ import com.datadog.android.sessionreplay.utils.ViewIdentifierResolver /** * A [WireframeMapper] implementation to map an [ImageView] component. */ -open class ImageViewMapper( - viewIdentifierResolver: ViewIdentifierResolver, - colorStringFormatter: ColorStringFormatter, - viewBoundsResolver: ViewBoundsResolver, - drawableToColorMapper: DrawableToColorMapper -) : BaseAsyncBackgroundWireframeMapper( - viewIdentifierResolver, - colorStringFormatter, - viewBoundsResolver, - drawableToColorMapper -) { +open class ImageViewMapper : BaseAsyncBackgroundWireframeMapper { + private val imageViewUtils: ImageViewUtils + + @Suppress("Unused") // used by external mappers + constructor( + viewIdentifierResolver: ViewIdentifierResolver, + colorStringFormatter: ColorStringFormatter, + viewBoundsResolver: ViewBoundsResolver, + drawableToColorMapper: DrawableToColorMapper + ) : this( + viewIdentifierResolver, + colorStringFormatter, + viewBoundsResolver, + drawableToColorMapper, + ImageViewUtils + ) + + internal constructor( + viewIdentifierResolver: ViewIdentifierResolver, + colorStringFormatter: ColorStringFormatter, + viewBoundsResolver: ViewBoundsResolver, + drawableToColorMapper: DrawableToColorMapper, + imageViewUtils: ImageViewUtils + ) : super( + viewIdentifierResolver, + colorStringFormatter, + viewBoundsResolver, + drawableToColorMapper + ) { + this.imageViewUtils = imageViewUtils + } + @UiThread override fun map( view: ImageView, @@ -48,17 +69,18 @@ open class ImageViewMapper( val drawable = view.drawable?.current ?: return wireframes - val parentRect = ImageViewUtils.resolveParentRectAbsPosition(view) - val contentRect = ImageViewUtils.resolveContentRectWithScaling(view, drawable) + val parentRect = imageViewUtils.resolveParentRectAbsPosition(view) + val contentRect = imageViewUtils.resolveContentRectWithScaling(view, drawable) val resources = view.resources val density = resources.displayMetrics.density val clipping = if (view.cropToPadding) { - ImageViewUtils.calculateClipping(parentRect, contentRect, density) + imageViewUtils.calculateClipping(parentRect, contentRect, density) } else { null } + val contentXPosInDp = contentRect.left.densityNormalized(density).toLong() val contentYPosInDp = contentRect.top.densityNormalized(density).toLong() val contentWidthPx = contentRect.width() diff --git a/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/utils/ImageViewUtilsTest.kt b/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/utils/ImageViewUtilsTest.kt index e21d45ef89..45bf0adcdd 100644 --- a/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/utils/ImageViewUtilsTest.kt +++ b/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/utils/ImageViewUtilsTest.kt @@ -278,7 +278,7 @@ internal class ImageViewUtilsTest { // When val result = testedImageViewUtils.resolveContentRectWithScaling( - view = mockImageView, + imageView = mockImageView, drawable = mockDrawable ) @@ -326,7 +326,7 @@ internal class ImageViewUtilsTest { // When val result = testedImageViewUtils.resolveContentRectWithScaling( - view = mockImageView, + imageView = mockImageView, drawable = mockDrawable ) @@ -374,7 +374,7 @@ internal class ImageViewUtilsTest { // When val result = testedImageViewUtils.resolveContentRectWithScaling( - view = mockImageView, + imageView = mockImageView, drawable = mockDrawable ) @@ -422,7 +422,7 @@ internal class ImageViewUtilsTest { // When val result = testedImageViewUtils.resolveContentRectWithScaling( - view = mockImageView, + imageView = mockImageView, drawable = mockDrawable ) @@ -477,7 +477,7 @@ internal class ImageViewUtilsTest { // When val result = testedImageViewUtils.resolveContentRectWithScaling( - view = mockImageView, + imageView = mockImageView, drawable = mockDrawable ) @@ -525,7 +525,7 @@ internal class ImageViewUtilsTest { // When val result = testedImageViewUtils.resolveContentRectWithScaling( - view = mockImageView, + imageView = mockImageView, drawable = mockDrawable ) @@ -577,7 +577,7 @@ internal class ImageViewUtilsTest { // When val result = testedImageViewUtils.resolveContentRectWithScaling( - view = mockImageView, + imageView = mockImageView, drawable = mockDrawable ) @@ -627,7 +627,7 @@ internal class ImageViewUtilsTest { // When val result = testedImageViewUtils.resolveContentRectWithScaling( - view = mockImageView, + imageView = mockImageView, drawable = mockDrawable ) @@ -673,7 +673,7 @@ internal class ImageViewUtilsTest { // When val result = testedImageViewUtils.resolveContentRectWithScaling( - view = mockImageView, + imageView = mockImageView, drawable = mockDrawable ) @@ -718,7 +718,7 @@ internal class ImageViewUtilsTest { // When val result = testedImageViewUtils.resolveContentRectWithScaling( - view = mockImageView, + imageView = mockImageView, drawable = mockDrawable ) diff --git a/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/recorder/mapper/ImageViewMapperTest.kt b/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/recorder/mapper/ImageViewMapperTest.kt index 0deb4b2543..668fb020cd 100644 --- a/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/recorder/mapper/ImageViewMapperTest.kt +++ b/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/recorder/mapper/ImageViewMapperTest.kt @@ -193,10 +193,11 @@ internal class ImageViewMapperTest { ) testedMapper = ImageViewMapper( - mockViewIdentifierResolver, - mockColorStringFormatter, - mockViewBoundsResolver, - mockDrawableToColorMapper + viewIdentifierResolver = mockViewIdentifierResolver, + colorStringFormatter = mockColorStringFormatter, + viewBoundsResolver = mockViewBoundsResolver, + drawableToColorMapper = mockDrawableToColorMapper, + imageViewUtils = stubImageViewUtils ) }