1 /* <lambda>null2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.systemui.util.settings.repository 18 19 import android.provider.Settings 20 import com.android.systemui.dagger.SysUISingleton 21 import com.android.systemui.dagger.qualifiers.Background 22 import com.android.systemui.user.data.repository.UserRepository 23 import com.android.systemui.util.settings.SecureSettings 24 import com.android.systemui.util.settings.SettingsProxy 25 import com.android.systemui.util.settings.SettingsProxyExt.observerFlow 26 import kotlinx.coroutines.CoroutineDispatcher 27 import kotlinx.coroutines.ExperimentalCoroutinesApi 28 import kotlinx.coroutines.flow.Flow 29 import kotlinx.coroutines.flow.distinctUntilChanged 30 import kotlinx.coroutines.flow.flatMapLatest 31 import kotlinx.coroutines.flow.flowOn 32 import kotlinx.coroutines.flow.map 33 import kotlinx.coroutines.flow.onStart 34 import javax.inject.Inject 35 36 /** 37 * Repository for observing values of [Settings.Secure] for the currently active user. That means 38 * when user is switched and the new user has different value, flow will emit new value. 39 */ 40 interface UserAwareSecureSettingsRepository { 41 42 /** 43 * Emits boolean value of the setting for active user. Also emits starting value when 44 * subscribed. 45 * See: [SettingsProxy.getBool]. 46 */ 47 fun boolSettingForActiveUser(name: String, defaultValue: Boolean = false): Flow<Boolean> 48 } 49 50 @SysUISingleton 51 @OptIn(ExperimentalCoroutinesApi::class) 52 class UserAwareSecureSettingsRepositoryImpl @Inject constructor( 53 private val secureSettings: SecureSettings, 54 private val userRepository: UserRepository, 55 @Background private val backgroundDispatcher: CoroutineDispatcher, 56 ) : UserAwareSecureSettingsRepository { 57 boolSettingForActiveUsernull58 override fun boolSettingForActiveUser(name: String, defaultValue: Boolean): Flow<Boolean> = 59 userRepository.selectedUserInfo 60 .flatMapLatest { userInfo -> settingObserver(name, defaultValue, userInfo.id) } 61 .distinctUntilChanged() 62 .flowOn(backgroundDispatcher) 63 settingObservernull64 private fun settingObserver(name: String, defaultValue: Boolean, userId: Int): Flow<Boolean> { 65 return secureSettings 66 .observerFlow(userId, name) 67 .onStart { emit(Unit) } 68 .map { secureSettings.getBoolForUser(name, defaultValue, userId) } 69 } 70 }