Test Cards grid rendering lifecycle
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user