From d9097b8b3a03426a82c9d03c439034d4d7498a14 Mon Sep 17 00:00:00 2001 From: luyi Date: Thu, 10 Oct 2024 15:05:31 +0200 Subject: [PATCH] RUM-3469: Resolve PorterDuffColorFilter case in drawable to color mapper --- .../utils/LegacyDrawableToColorMapper.kt | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/utils/LegacyDrawableToColorMapper.kt b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/utils/LegacyDrawableToColorMapper.kt index b0d7e8d7b8..a18ab54556 100644 --- a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/utils/LegacyDrawableToColorMapper.kt +++ b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/utils/LegacyDrawableToColorMapper.kt @@ -7,7 +7,9 @@ package com.datadog.android.sessionreplay.utils //noinspection SuspiciousImport +import android.annotation.SuppressLint import android.graphics.Paint +import android.graphics.PorterDuffColorFilter import android.graphics.drawable.ColorDrawable import android.graphics.drawable.Drawable import android.graphics.drawable.GradientDrawable @@ -118,15 +120,31 @@ open class LegacyDrawableToColorMapper : DrawableToColorMapper { } if (fillPaint == null) return null - - val fillColor: Int = fillPaint.color + val filterColor = try { + mColorField?.get(fillPaint.colorFilter) as? Int ?: fillPaint.color + } catch (e: IllegalArgumentException) { + internalLogger.log( + InternalLogger.Level.WARN, + InternalLogger.Target.MAINTAINER, + { "Unable to read ColorFilter.mColorField field through reflection" }, + e + ) + fillPaint.color + } catch (e: IllegalAccessException) { + internalLogger.log( + InternalLogger.Level.WARN, + InternalLogger.Target.MAINTAINER, + { "Unable to read ColorFilter.mColorField field through reflection" }, + e + ) + fillPaint.color + } val fillAlpha = (fillPaint.alpha * drawable.alpha) / MAX_ALPHA_VALUE return if (fillAlpha == 0) { null } else { - // TODO RUM-3469 resolve other color filter types - mergeColorAndAlpha(fillColor, fillAlpha) + mergeColorAndAlpha(filterColor, fillAlpha) } } @@ -151,6 +169,7 @@ open class LegacyDrawableToColorMapper : DrawableToColorMapper { } companion object { + @SuppressLint("DiscouragedPrivateApi") @Suppress("PrivateAPI", "SwallowedException", "TooGenericExceptionCaught") internal val fillPaintField = try { GradientDrawable::class.java.getDeclaredField("mFillPaint").apply { @@ -163,5 +182,18 @@ open class LegacyDrawableToColorMapper : DrawableToColorMapper { } catch (e: NullPointerException) { null } + + @Suppress("PrivateAPI", "SwallowedException", "TooGenericExceptionCaught") + internal val mColorField = try { + PorterDuffColorFilter::class.java.getDeclaredField("mColor").apply { + this.isAccessible = true + } + } catch (e: NoSuchFieldException) { + null + } catch (e: SecurityException) { + null + } catch (e: NullPointerException) { + null + } } }