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

Commit e31cde2e authored by Bharat Singh's avatar Bharat Singh Committed by Android (Google) Code Review
Browse files

Merge "[SysUI][Compose] Add support for adding unsafe LifecycleRegisty to...

Merge "[SysUI][Compose] Add support for adding unsafe LifecycleRegisty to enable ops on non-main thread" into main
parents fb167541 9107ce8b
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -48,7 +48,8 @@ import com.android.systemui.lifecycle.ViewLifecycleOwner
 */
object ComposeInitializer {
    /** Function to be called on your window root view's [View.onAttachedToWindow] function. */
    fun onAttachedToWindow(root: View) {
    @JvmOverloads
    fun onAttachedToWindow(root: View, useSeparateThreadUnsafe: Boolean = false) {
        if (root.findViewTreeLifecycleOwner() != null) {
            error("root $root already has a LifecycleOwner")
        }
@@ -64,7 +65,7 @@ object ComposeInitializer {

        // The lifecycle owner, which is STARTED when [root] is visible and RESUMED when [root] is
        // both visible and focused.
        val lifecycleOwner = ViewLifecycleOwner(root)
        val lifecycleOwner = ViewLifecycleOwner(root, useSeparateThreadUnsafe)

        // We create a trivial implementation of [SavedStateRegistryOwner] that does not do any save
        // or restore because SystemUI process is always running and top-level windows using this
+12 −4
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@

package com.android.systemui.lifecycle

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewTreeObserver
import androidx.annotation.MainThread
@@ -24,6 +25,7 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.lifecycle.lifecycleScope
import com.android.app.tracing.coroutines.launchTraced as launch
import com.android.systemui.coroutines.newTracingContext
import com.android.systemui.util.Assert
import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
@@ -41,7 +43,6 @@ import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import com.android.app.tracing.coroutines.launchTraced as launch

/**
 * Runs the given [block] every time the [View] becomes attached (or immediately after calling this
@@ -146,13 +147,20 @@ private fun createLifecycleOwnerAndRun(
 * └───────────────┴───────────────────┴──────────────┴─────────────────┘
 * ```
 */
class ViewLifecycleOwner(private val view: View) : LifecycleOwner {
class ViewLifecycleOwner(private val view: View, useSeparateThreadUnsafe: Boolean = false) :
    LifecycleOwner {

    private val windowVisibleListener =
        ViewTreeObserver.OnWindowVisibilityChangeListener { updateState() }
    private val windowFocusListener = ViewTreeObserver.OnWindowFocusChangeListener { updateState() }

    private val registry = LifecycleRegistry(this)
    @SuppressLint("VisibleForTests") // required due to "createUnsafe" usage
    private val registry =
        if (useSeparateThreadUnsafe) {
            LifecycleRegistry.createUnsafe(this)
        } else {
            LifecycleRegistry(this)
        }

    fun onCreate() {
        registry.currentState = Lifecycle.State.CREATED
@@ -262,7 +270,7 @@ enum class WindowLifecycleState {
     * Indicates that the [View] is attached to a [android.view.Window], and the window is visible
     * and focused.
     */
    FOCUSED
    FOCUSED,
}

private val View.isAttached