Skip to content

Commit

Permalink
RUM-3469: Resolve PorterDuffColorFilter case in drawable to color mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ambushwork committed Oct 10, 2024
1 parent 489655b commit d9097b8
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 d9097b8

Please sign in to comment.