Skip to content

Commit

Permalink
Merge pull request #2319 from DataDog/yl/it/fix-drawable-to-color-issue
Browse files Browse the repository at this point in the history
RUM-3469: Resolve PorterDuffColorFilter case in drawable to color mapper
  • Loading branch information
ambushwork authored Oct 11, 2024
2 parents 489655b + d9097b8 commit 995b82c
Showing 1 changed file with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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 {
Expand All @@ -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
}
}
}

0 comments on commit 995b82c

Please sign in to comment.