|
| 1 | +/* |
| 2 | + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. |
| 3 | + * This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | + * Copyright 2016-Present Datadog, Inc. |
| 5 | + */ |
| 6 | +import android.graphics.drawable.Drawable |
| 7 | +import android.graphics.drawable.InsetDrawable |
| 8 | +import android.graphics.drawable.LayerDrawable |
| 9 | +import com.datadog.android.sessionreplay.model.MobileSegment |
| 10 | +import com.datadog.reactnative.sessionreplay.extensions.getRadius |
| 11 | +import com.datadog.reactnative.sessionreplay.utils.DrawableUtils |
| 12 | +import com.datadog.reactnative.sessionreplay.utils.formatAsRgba |
| 13 | +import com.facebook.react.common.annotations.UnstableReactNativeAPI |
| 14 | +import com.facebook.react.uimanager.LengthPercentage |
| 15 | +import com.facebook.react.uimanager.Spacing |
| 16 | +import com.facebook.react.uimanager.drawable.CSSBackgroundDrawable |
| 17 | + |
| 18 | +internal class ReactViewBackgroundDrawableUtils : DrawableUtils() { |
| 19 | + @OptIn(UnstableReactNativeAPI::class) |
| 20 | + override fun resolveShapeAndBorder( |
| 21 | + drawable: Drawable, |
| 22 | + opacity: Float, |
| 23 | + pixelDensity: Float |
| 24 | + ): Pair<MobileSegment.ShapeStyle?, MobileSegment.ShapeBorder?> { |
| 25 | + if (drawable !is CSSBackgroundDrawable) { |
| 26 | + return null to null |
| 27 | + } |
| 28 | + |
| 29 | + val borderProps = resolveBorder(drawable, pixelDensity) |
| 30 | + val backgroundColor = getBackgroundColor(drawable) |
| 31 | + val colorHexString = if (backgroundColor != null) { |
| 32 | + formatAsRgba(backgroundColor) |
| 33 | + } else { |
| 34 | + return null to borderProps |
| 35 | + } |
| 36 | + |
| 37 | + return MobileSegment.ShapeStyle( |
| 38 | + colorHexString, |
| 39 | + opacity, |
| 40 | + getBorderRadius(drawable) |
| 41 | + ) to borderProps |
| 42 | + } |
| 43 | + |
| 44 | + @OptIn(UnstableReactNativeAPI::class) |
| 45 | + override fun getReactBackgroundFromDrawable(drawable: Drawable?): Drawable? { |
| 46 | + return when(drawable) { |
| 47 | + is CSSBackgroundDrawable -> drawable |
| 48 | + is InsetDrawable -> getReactBackgroundFromDrawable(drawable.drawable) |
| 49 | + is LayerDrawable -> getDrawableFromLayerDrawable(drawable) |
| 50 | + else -> null |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @OptIn(UnstableReactNativeAPI::class) |
| 55 | + private fun getDrawableFromLayerDrawable(layerDrawable: LayerDrawable): Drawable? { |
| 56 | + for (layerNumber in 0 until layerDrawable.numberOfLayers) { |
| 57 | + val layer = layerDrawable.getDrawable(layerNumber) |
| 58 | + if (layer is CSSBackgroundDrawable) { |
| 59 | + return layer |
| 60 | + } |
| 61 | + } |
| 62 | + return null |
| 63 | + } |
| 64 | + |
| 65 | + @OptIn(UnstableReactNativeAPI::class) |
| 66 | + private fun getBorderRadius(drawable: CSSBackgroundDrawable): Float { |
| 67 | + val width = drawable.intrinsicWidth.toFloat() |
| 68 | + val height = drawable.intrinsicHeight.toFloat() |
| 69 | + val uniform = getBorderRadiusUniform(drawable) |
| 70 | + return uniform?.getRadius(width, height) ?: 0f |
| 71 | + } |
| 72 | + |
| 73 | + @OptIn(UnstableReactNativeAPI::class) |
| 74 | + private fun getBorderRadiusUniform( |
| 75 | + drawable: CSSBackgroundDrawable |
| 76 | + ): LengthPercentage? { |
| 77 | + return reflectionUtils.getDeclaredField( |
| 78 | + drawable.borderRadius, |
| 79 | + UNIFORM_FIELD_NAME |
| 80 | + ) as? LengthPercentage |
| 81 | + } |
| 82 | + |
| 83 | + @OptIn(UnstableReactNativeAPI::class) |
| 84 | + private fun getBackgroundColor( |
| 85 | + backgroundDrawable: CSSBackgroundDrawable |
| 86 | + ): Int? { |
| 87 | + return reflectionUtils.getDeclaredField( |
| 88 | + backgroundDrawable, |
| 89 | + COLOR_FIELD_NAME |
| 90 | + ) as? Int |
| 91 | + } |
| 92 | + |
| 93 | + @OptIn(UnstableReactNativeAPI::class) |
| 94 | + private fun resolveBorder( |
| 95 | + backgroundDrawable: CSSBackgroundDrawable, |
| 96 | + pixelDensity: Float |
| 97 | + ): MobileSegment.ShapeBorder { |
| 98 | + val borderWidth = (backgroundDrawable.fullBorderWidth / pixelDensity).toLong() |
| 99 | + val borderColor = formatAsRgba(backgroundDrawable.getBorderColor(Spacing.ALL)) |
| 100 | + |
| 101 | + return MobileSegment.ShapeBorder( |
| 102 | + color = borderColor, |
| 103 | + width = borderWidth |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + private companion object { |
| 108 | + private const val COLOR_FIELD_NAME = "mColor" |
| 109 | + private const val UNIFORM_FIELD_NAME = "uniform" |
| 110 | + } |
| 111 | +} |
0 commit comments