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

Commit 2ca9f557 authored by Jernej Virag's avatar Jernej Virag
Browse files

Add @SettingsBackground dispatcher

The purpose of this executor is to allow code to dispatch operations to
background threads which are order sensitive (e.g. register/unregister
calls). Currently this is backed by a single background Looper which
runs sequentially.

Bug: 350930202
Bug: 327558308
Flag: EXEMPT no code changes in existing code
Test: n/a
Change-Id: Ie9257dc3afe6694c53418f9d4594edce4a650744
parent 2714a8ac
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
/*
 * 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 { }
+12 −2
Original line number Diff line number Diff line
@@ -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

@@ -77,6 +79,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
+2 −2
Original line number Diff line number Diff line
@@ -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;
    }
+4 −4
Original line number Diff line number Diff line
@@ -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;
+4 −4
Original line number Diff line number Diff line
@@ -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;