diff options
5 files changed, 56 insertions, 12 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/util/kotlin/SettingsSingleThreadBackground.java b/packages/SystemUI/src/com/android/systemui/util/kotlin/SettingsSingleThreadBackground.java new file mode 100644 index 000000000000..e13981dfe5c7 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/util/kotlin/SettingsSingleThreadBackground.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.util.kotlin; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; + +import javax.inject.Qualifier; + +/** + * This is a background executor/dispatcher which guarantees that **within that instance**, + * operations will execute in the same order as they were dispatched. + * This is useful for background operations like register/unregister where ordering is important. + */ +@Qualifier +@Documented +@Retention(RUNTIME) +public @interface SettingsSingleThreadBackground { } diff --git a/packages/SystemUI/src/com/android/systemui/util/kotlin/SysUICoroutinesModule.kt b/packages/SystemUI/src/com/android/systemui/util/kotlin/SysUICoroutinesModule.kt index b836016eb2ef..a03221e03467 100644 --- a/packages/SystemUI/src/com/android/systemui/util/kotlin/SysUICoroutinesModule.kt +++ b/packages/SystemUI/src/com/android/systemui/util/kotlin/SysUICoroutinesModule.kt @@ -16,6 +16,7 @@ package com.android.systemui.util.kotlin +import android.os.Handler import com.android.systemui.dagger.SysUISingleton import com.android.systemui.dagger.qualifiers.Application import com.android.systemui.dagger.qualifiers.Background @@ -23,15 +24,16 @@ import com.android.systemui.dagger.qualifiers.Tracing import com.android.systemui.dagger.qualifiers.UiBackground import dagger.Module import dagger.Provides -import java.util.concurrent.Executor -import kotlin.coroutines.CoroutineContext import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.android.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.newFixedThreadPoolContext import kotlinx.coroutines.plus +import java.util.concurrent.Executor +import kotlin.coroutines.CoroutineContext private const val LIMIT_BACKGROUND_DISPATCHER_THREADS = true @@ -78,6 +80,14 @@ class SysUICoroutinesModule { } @Provides + @SysUISingleton + @SettingsSingleThreadBackground + fun settingsBgDispatcher(@Background bgHandler: Handler): CoroutineDispatcher { + // Handlers are guaranteed to be sequential so we use that one for now. + return bgHandler.asCoroutineDispatcher("SettingsBg") + } + + @Provides @Background @SysUISingleton fun bgCoroutineContext( diff --git a/packages/SystemUI/src/com/android/systemui/util/settings/GlobalSettingsImpl.java b/packages/SystemUI/src/com/android/systemui/util/settings/GlobalSettingsImpl.java index 4438763aa765..816f55db65a0 100644 --- a/packages/SystemUI/src/com/android/systemui/util/settings/GlobalSettingsImpl.java +++ b/packages/SystemUI/src/com/android/systemui/util/settings/GlobalSettingsImpl.java @@ -23,7 +23,7 @@ import android.content.ContentResolver; import android.net.Uri; import android.provider.Settings; -import com.android.systemui.dagger.qualifiers.Background; +import com.android.systemui.util.kotlin.SettingsSingleThreadBackground; import kotlinx.coroutines.CoroutineDispatcher; @@ -37,7 +37,7 @@ class GlobalSettingsImpl implements GlobalSettings { @Inject GlobalSettingsImpl(ContentResolver contentResolver, - @Background CoroutineDispatcher bgDispatcher) { + @SettingsSingleThreadBackground CoroutineDispatcher bgDispatcher) { mContentResolver = contentResolver; mBgDispatcher = bgDispatcher; } diff --git a/packages/SystemUI/src/com/android/systemui/util/settings/SecureSettingsImpl.java b/packages/SystemUI/src/com/android/systemui/util/settings/SecureSettingsImpl.java index 38ad5d0d0cab..9c98f43e2501 100644 --- a/packages/SystemUI/src/com/android/systemui/util/settings/SecureSettingsImpl.java +++ b/packages/SystemUI/src/com/android/systemui/util/settings/SecureSettingsImpl.java @@ -22,13 +22,13 @@ import android.provider.Settings; import androidx.annotation.NonNull; -import com.android.systemui.dagger.qualifiers.Background; import com.android.systemui.settings.UserTracker; - -import kotlinx.coroutines.CoroutineDispatcher; +import com.android.systemui.util.kotlin.SettingsSingleThreadBackground; import javax.inject.Inject; +import kotlinx.coroutines.CoroutineDispatcher; + class SecureSettingsImpl implements SecureSettings { private final ContentResolver mContentResolver; private final UserTracker mUserTracker; @@ -36,7 +36,7 @@ class SecureSettingsImpl implements SecureSettings { @Inject SecureSettingsImpl(ContentResolver contentResolver, UserTracker userTracker, - @Background CoroutineDispatcher bgDispatcher) { + @SettingsSingleThreadBackground CoroutineDispatcher bgDispatcher) { mContentResolver = contentResolver; mUserTracker = userTracker; mBgDispatcher = bgDispatcher; diff --git a/packages/SystemUI/src/com/android/systemui/util/settings/SystemSettingsImpl.java b/packages/SystemUI/src/com/android/systemui/util/settings/SystemSettingsImpl.java index 68cc753bc48a..406d95bd8fd1 100644 --- a/packages/SystemUI/src/com/android/systemui/util/settings/SystemSettingsImpl.java +++ b/packages/SystemUI/src/com/android/systemui/util/settings/SystemSettingsImpl.java @@ -22,13 +22,13 @@ import android.provider.Settings; import androidx.annotation.NonNull; -import com.android.systemui.dagger.qualifiers.Background; import com.android.systemui.settings.UserTracker; - -import kotlinx.coroutines.CoroutineDispatcher; +import com.android.systemui.util.kotlin.SettingsSingleThreadBackground; import javax.inject.Inject; +import kotlinx.coroutines.CoroutineDispatcher; + class SystemSettingsImpl implements SystemSettings { private final ContentResolver mContentResolver; private final UserTracker mUserTracker; @@ -36,7 +36,7 @@ class SystemSettingsImpl implements SystemSettings { @Inject SystemSettingsImpl(ContentResolver contentResolver, UserTracker userTracker, - @Background CoroutineDispatcher bgDispatcher) { + @SettingsSingleThreadBackground CoroutineDispatcher bgDispatcher) { mContentResolver = contentResolver; mUserTracker = userTracker; mBgCoroutineDispatcher = bgDispatcher; |