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

Commit 7a022439 authored by Ryota Okuji's avatar Ryota Okuji
Browse files

Add SecureSettingsForUserRepository

This repository allows observing values for the specified user.

Flag: EXEMPT adding utility class
Bug: 30873608
Test: CL:30873608
Change-Id: I1f8cb9c25678254f01eebcc03782ea65c3894d9c
parent a5f8e9ba
Loading
Loading
Loading
Loading
+35 −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.settings.repository

import android.provider.Settings
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.util.settings.SecureSettings
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CoroutineDispatcher

/** Repository observing values of a [Settings.Secure] for the specified user. */
@SysUISingleton
class SecureSettingsForUserRepository
@Inject
constructor(
    secureSettings: SecureSettings,
    @Background backgroundDispatcher: CoroutineDispatcher,
    @Background backgroundContext: CoroutineContext,
) : SettingsForUserRepository(secureSettings, backgroundDispatcher, backgroundContext)
+66 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.settings.repository

import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.util.settings.SettingsProxyExt.observerFlow
import com.android.systemui.util.settings.UserSettingsProxy
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.withContext

/**
 * Repository observing values of a [UserSettingsProxy] for the specified user. This repository
 * should be used for any system that tracks the desired user internally (e.g. the Quick Settings
 * tiles system). In other cases, use a [UserAwareSettingsRepository] instead.
 */
abstract class SettingsForUserRepository(
    private val userSettings: UserSettingsProxy,
    @Background private val backgroundDispatcher: CoroutineDispatcher,
    @Background private val backgroundContext: CoroutineContext,
) {
    fun boolSettingForUser(
        userId: Int,
        name: String,
        defaultValue: Boolean = false,
    ): Flow<Boolean> =
        settingObserver(name, userId) { userSettings.getBoolForUser(name, defaultValue, userId) }
            .distinctUntilChanged()
            .flowOn(backgroundDispatcher)

    fun <T> settingObserver(name: String, userId: Int, settingsReader: () -> T): Flow<T> {
        return userSettings
            .observerFlow(userId, name)
            .onStart { emit(Unit) }
            .map { settingsReader.invoke() }
    }

    suspend fun setBoolForUser(userId: Int, name: String, value: Boolean) {
        withContext(backgroundContext) { userSettings.putBoolForUser(name, value, userId) }
    }

    suspend fun getBoolForUser(userId: Int, name: String, defaultValue: Boolean = false): Boolean {
        return withContext(backgroundContext) {
            userSettings.getBoolForUser(name, defaultValue, userId)
        }
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -33,7 +33,8 @@ import kotlinx.coroutines.withContext
/**
 * Repository for observing values of a [UserSettingsProxy], for the currently active user. That
 * means that when the user is switched and the new user has a different value, the flow will emit
 * the new value.
 * the new value. For any system that tracks the desired user internally (e.g. the Quick Settings
 * tiles system), use a [SettingsForUserRepository] instead.
 */
// TODO: b/377244768 - Make internal when UserAwareSecureSettingsRepository can be made internal.
abstract class UserAwareSettingsRepository(
+28 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.settings.data.repository

import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.backgroundCoroutineContext
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.util.settings.fakeSettings
import com.android.systemui.util.settings.repository.SecureSettingsForUserRepository

val Kosmos.secureSettingsForUserRepository by
    Kosmos.Fixture {
        SecureSettingsForUserRepository(fakeSettings, testDispatcher, backgroundCoroutineContext)
    }