Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f644021a authored by Steve Elliott's avatar Steve Elliott
Browse files

[kairos] Fix casting error in ActivatedKairosSpec

The `uninit` value is used as a sentinel to determine if the kairos spec
is currently active. If the state is not the same instance as `uninit`,
then we know that it is the value returned from the spec activation, and
so we can safely cast it.

The bug: `uninit` is a different instance when recomponsing, and so the
instance equality check fails, even though the value is *not* an
instance of `T`. This causes the cast to fail, which crashes the
process.

The solution: make `uninit` a private singleton that lives outside of
the composition. There will only ever be a single instance, and so the
instance equality check will now suffice.

Flag: com.android.systemui.status_bar_mobile_icon_kairos
Bug: 383172066
Change-Id: I57d06aba0e6d5b11ee13990eb78f7065314a5500
parent 62fbd662
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -41,20 +41,21 @@ fun <T> ActivatedKairosSpec(
    kairosNetwork: KairosNetwork,
    block: @Composable (T) -> Unit,
) {
    val uninit = Any()
    var state by remember { mutableStateOf<Any?>(uninit) }
    var state by remember { mutableStateOf<Any?>(Uninitialized) }
    LaunchedEffect(key1 = Unit) {
        kairosNetwork.activateSpec {
            val v = buildSpec.applySpec()
            launchEffect {
                state = v
                awaitClose { state = uninit }
                awaitClose { state = Uninitialized }
            }
        }
    }
    state.let {
        if (it !== uninit) {
        if (it !== Uninitialized) {
            @Suppress("UNCHECKED_CAST") block(it as T)
        }
    }
}

private object Uninitialized