Test Cards grid rendering lifecycle

This commit is contained in:
ajp_anton
2026-08-29 23:21:05 +00:00
parent 179dc3a1db
commit 626776886a
4 changed files with 110 additions and 2 deletions
@@ -46,6 +46,17 @@ internal class OneUiCardsGridRenderer {
}
}
fun release(container: ViewGroup) {
val state = states.remove(container) ?: return
restoreNative(state)
state.attachListener?.let(container::removeOnAttachStateChangeListener)
state.layoutListener?.let { state.root?.removeOnLayoutChangeListener(it) }
state.host?.let { host ->
clearHost(host)
(host.parent as? ViewGroup)?.removeView(host)
}
}
private fun render(container: ViewGroup, state: State, active: Boolean) {
if (!active || !container.isAttachedToWindow) {
failOpen(state)
@@ -139,13 +150,14 @@ internal class OneUiCardsGridRenderer {
}
private fun notificationIcon(root: View, source: Source, size: Int): ImageView? {
val drawable = findDrawable(source.view)
var drawable = findDrawable(source.view)
?: runCatching {
val context = source.packageName?.let { root.context.createPackageContext(it, 0) }
?: root.context
source.smallIcon?.loadDrawable(context)
}.getOrNull()
?: return null
source.tintColor?.let { color -> drawable = drawable.mutate().apply { setTint(color) } }
val bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888)
val bounds = Rect(drawable.bounds)
try {
@@ -271,13 +283,21 @@ internal class OneUiCardsGridRenderer {
private fun dp(view: View, value: Int) = (value * view.resources.displayMetrics.density).toInt()
private fun state(container: ViewGroup) = states.getOrPut(container, ::State)
private fun state(container: ViewGroup) = states.getOrPut(container) {
State().also { state ->
state.attachListener = object : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(view: View) = Unit
override fun onViewDetachedFromWindow(view: View) = release(container)
}.also(container::addOnAttachStateChangeListener)
}
}
data class Source(
val key: String,
val view: View?,
val smallIcon: android.graphics.drawable.Icon?,
val packageName: String? = null,
val tintColor: Int? = null,
)
data class Config(
@@ -292,6 +312,7 @@ internal class OneUiCardsGridRenderer {
var host: FrameLayout? = null
var root: ViewGroup? = null
var layoutListener: View.OnLayoutChangeListener? = null
var attachListener: View.OnAttachStateChangeListener? = null
var active = false
var nativeSurface: View? = null
var nativeAlpha = 1f