Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUM-3469: Resolve PorterDuffColorFilter case in drawable to color mapper #2319

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
}