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

Commit 9e576911 authored by Peter Kalauskas's avatar Peter Kalauskas Committed by Android (Google) Code Review
Browse files

Merge "Add CoroutineTracingContext to repeatWhenAttached" into main

parents 6ea0cc1e b135255f
Loading
Loading
Loading
Loading
+15 −10
Original line number Diff line number Diff line
@@ -24,12 +24,13 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.lifecycle.lifecycleScope
import com.android.app.tracing.coroutines.createCoroutineTracingContext
import com.android.app.tracing.coroutines.launch
import com.android.systemui.util.Assert
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.DisposableHandle
import kotlinx.coroutines.launch

/**
 * Runs the given [block] every time the [View] becomes attached (or immediately after calling this
@@ -66,7 +67,8 @@ fun View.repeatWhenAttached(
    // dispatcher to use. We don't want it to run on the Dispatchers.Default thread pool as
    // default behavior. Instead, we want it to run on the view's UI thread since the user will
    // presumably want to call view methods that require being called from said UI thread.
    val lifecycleCoroutineContext = Dispatchers.Main + coroutineContext
    val lifecycleCoroutineContext =
        Dispatchers.Main + createCoroutineTracingContext() + coroutineContext
    var lifecycleOwner: ViewLifecycleOwner? = null
    val onAttachListener =
        object : View.OnAttachStateChangeListener {
@@ -97,8 +99,7 @@ fun View.repeatWhenAttached(
            )
    }

    return object : DisposableHandle {
        override fun dispose() {
    return DisposableHandle {
        Assert.isMainThread()

        lifecycleOwner?.onDestroy()
@@ -106,7 +107,6 @@ fun View.repeatWhenAttached(
        view.removeOnAttachStateChangeListener(onAttachListener)
    }
}
}

private fun createLifecycleOwnerAndRun(
    view: View,
@@ -115,7 +115,12 @@ private fun createLifecycleOwnerAndRun(
): ViewLifecycleOwner {
    return ViewLifecycleOwner(view).apply {
        onCreate()
        lifecycleScope.launch(coroutineContext) { block(view) }
        lifecycleScope.launch(
            "ViewLifecycleOwner(${view::class.java.simpleName})",
            coroutineContext
        ) {
            block(view)
        }
    }
}

+3 −2
Original line number Diff line number Diff line
@@ -91,7 +91,8 @@ constructor(
            )
        controller.init()

        applicationScope.launch(bgHandler.asCoroutineDispatcher()) {
        val bgDispatcher = bgHandler.asCoroutineDispatcher("@UnfoldBg Handler")
        applicationScope.launch(bgDispatcher) {
            powerInteractor.screenPowerState.collect {
                if (it == ScreenPowerState.SCREEN_ON) {
                    readyCallback = null
@@ -99,7 +100,7 @@ constructor(
            }
        }

        applicationScope.launch(bgHandler.asCoroutineDispatcher()) {
        applicationScope.launch(bgDispatcher) {
            deviceStateRepository.state
                .map { it == DeviceStateRepository.DeviceState.FOLDED }
                .distinctUntilChanged()