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

Commit 00f305ba authored by Peter Kalauskas's avatar Peter Kalauskas
Browse files

Adjust withContext usage for SettingsProxy

 - Only call withContext if current dispatcher is different than the
   one used by settings. This will reduce overhead of calling
   withContext when changing contexts is not needed.

 - Remove deprecated nameCoroutine() call, which was not performing
   tracing correctly.

Bug: 289353932
Bug: 330299944
Test: atest SystemUITests
Flag: EXEMPT refactor
Change-Id: Id836fd952ebde4473847cc5433ce7f1ca0615c7d
parent 008ca7b3
Loading
Loading
Loading
Loading
+17 −12
Original line number Diff line number Diff line
@@ -24,12 +24,10 @@ import androidx.annotation.AnyThread
import androidx.annotation.WorkerThread
import com.android.app.tracing.TraceUtils.trace
import com.android.app.tracing.coroutines.launchTraced as launch
import com.android.app.tracing.coroutines.nameCoroutine
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import com.android.app.tracing.coroutines.withContextTraced as withContext
import kotlin.coroutines.coroutineContext
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.withContext

/**
 * Used to interact with mainly with Settings.Global, but can also be used for Settings.System and
@@ -53,9 +51,16 @@ interface SettingsProxy {
    val settingsScope: CoroutineScope

    @OptIn(ExperimentalStdlibApi::class)
    fun settingsDispatcherContext(name: String): CoroutineContext {
        return (settingsScope.coroutineContext[CoroutineDispatcher] ?: EmptyCoroutineContext) +
            nameCoroutine(name)
    suspend fun executeOnSettingsScopeDispatcher(name: String, block: () -> Unit) {
        val settingsDispatcher = settingsScope.coroutineContext[CoroutineDispatcher]
        if (
            settingsDispatcher != null &&
                settingsDispatcher != coroutineContext[CoroutineDispatcher]
        ) {
            withContext(name, settingsDispatcher) { block() }
        } else {
            trace(name) { block() }
        }
    }

    /**
@@ -87,7 +92,7 @@ interface SettingsProxy {
     * wish to synchronize execution.
     */
    suspend fun registerContentObserver(name: String, settingsObserver: ContentObserver) {
        withContext(settingsDispatcherContext("registerContentObserver-A")) {
        executeOnSettingsScopeDispatcher("registerContentObserver-A") {
            registerContentObserverSync(getUriFor(name), settingsObserver)
        }
    }
@@ -139,7 +144,7 @@ interface SettingsProxy {
     * wish to synchronize execution.
     */
    suspend fun registerContentObserver(uri: Uri, settingsObserver: ContentObserver) {
        withContext(settingsDispatcherContext("registerContentObserver-B")) {
        executeOnSettingsScopeDispatcher("registerContentObserver-B") {
            registerContentObserverSync(uri, settingsObserver)
        }
    }
@@ -197,7 +202,7 @@ interface SettingsProxy {
        notifyForDescendants: Boolean,
        settingsObserver: ContentObserver,
    ) {
        withContext(settingsDispatcherContext("registerContentObserver-C")) {
        executeOnSettingsScopeDispatcher("registerContentObserver-C") {
            registerContentObserverSync(getUriFor(name), notifyForDescendants, settingsObserver)
        }
    }
@@ -266,7 +271,7 @@ interface SettingsProxy {
        notifyForDescendants: Boolean,
        settingsObserver: ContentObserver,
    ) {
        withContext(settingsDispatcherContext("registerContentObserver-D")) {
        executeOnSettingsScopeDispatcher("registerContentObserver-D") {
            registerContentObserverSync(uri, notifyForDescendants, settingsObserver)
        }
    }
@@ -326,7 +331,7 @@ interface SettingsProxy {
     * async block if they wish to synchronize execution.
     */
    suspend fun unregisterContentObserver(settingsObserver: ContentObserver) {
        withContext(settingsDispatcherContext("unregisterContentObserver")) {
        executeOnSettingsScopeDispatcher("unregisterContentObserver") {
            unregisterContentObserverSync(settingsObserver)
        }
    }
+6 −7
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ import com.android.systemui.util.settings.SettingsProxy.Companion.parseFloat
import com.android.systemui.util.settings.SettingsProxy.Companion.parseFloatOrThrow
import com.android.systemui.util.settings.SettingsProxy.Companion.parseLongOrThrow
import com.android.systemui.util.settings.SettingsProxy.Companion.parseLongOrUseDefault
import kotlinx.coroutines.withContext

/**
 * Used to interact with per-user Settings.Secure and Settings.System settings (but not
@@ -71,7 +70,7 @@ interface UserSettingsProxy : SettingsProxy {
    }

    override suspend fun registerContentObserver(uri: Uri, settingsObserver: ContentObserver) {
        withContext(settingsDispatcherContext("registerContentObserver-A")) {
        executeOnSettingsScopeDispatcher("registerContentObserver-A") {
            registerContentObserverForUserSync(uri, settingsObserver, userId)
        }
    }
@@ -96,7 +95,7 @@ interface UserSettingsProxy : SettingsProxy {
        notifyForDescendants: Boolean,
        settingsObserver: ContentObserver,
    ) {
        withContext(settingsDispatcherContext("registerContentObserver-B")) {
        executeOnSettingsScopeDispatcher("registerContentObserver-B") {
            registerContentObserverForUserSync(uri, notifyForDescendants, settingsObserver, userId)
        }
    }
@@ -141,7 +140,7 @@ interface UserSettingsProxy : SettingsProxy {
        settingsObserver: ContentObserver,
        userHandle: Int,
    ) {
        withContext(settingsDispatcherContext("registerContentObserverForUser-A")) {
        executeOnSettingsScopeDispatcher("registerContentObserverForUser-A") {
            registerContentObserverForUserSync(name, settingsObserver, userHandle)
        }
    }
@@ -186,7 +185,7 @@ interface UserSettingsProxy : SettingsProxy {
        settingsObserver: ContentObserver,
        userHandle: Int,
    ) {
        withContext(settingsDispatcherContext("registerContentObserverForUser-B")) {
        executeOnSettingsScopeDispatcher("registerContentObserverForUser-B") {
            registerContentObserverForUserSync(uri, settingsObserver, userHandle)
        }
    }
@@ -264,7 +263,7 @@ interface UserSettingsProxy : SettingsProxy {
        settingsObserver: ContentObserver,
        userHandle: Int,
    ) {
        withContext(settingsDispatcherContext("registerContentObserverForUser-C")) {
        executeOnSettingsScopeDispatcher("registerContentObserverForUser-C") {
            registerContentObserverForUserSync(
                name,
                notifyForDescendants,
@@ -332,7 +331,7 @@ interface UserSettingsProxy : SettingsProxy {
        settingsObserver: ContentObserver,
        userHandle: Int,
    ) {
        withContext(settingsDispatcherContext("registerContentObserverForUser-D")) {
        executeOnSettingsScopeDispatcher("registerContentObserverForUser-D") {
            registerContentObserverForUserSync(
                uri,
                notifyForDescendants,