1 /*
2  * Copyright (C) 2023 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.shared.settings.data.repository
18 
19 import android.content.ContentResolver
20 import android.database.ContentObserver
21 import android.provider.Settings
22 import kotlinx.coroutines.CoroutineDispatcher
23 import kotlinx.coroutines.channels.awaitClose
24 import kotlinx.coroutines.flow.Flow
25 import kotlinx.coroutines.flow.callbackFlow
26 import kotlinx.coroutines.flow.flowOn
27 import kotlinx.coroutines.flow.map
28 import kotlinx.coroutines.withContext
29 
30 /**
31  * Simple implementation of [SecureSettingsRepository].
32  *
33  * This repository doesn't guarantee to provide value across different users, and therefore
34  * shouldn't be used in SystemUI. For that see: [UserAwareSecureSettingsRepository]
35  */
36 // TODO: b/377244768 - Move to Theme/WallpaperPicker, away from SystemUI.
37 class SecureSettingsRepositoryImpl(
38     private val contentResolver: ContentResolver,
39     private val backgroundDispatcher: CoroutineDispatcher,
40 ) : SecureSettingsRepository {
41 
intSettingnull42     override fun intSetting(name: String, defaultValue: Int): Flow<Int> {
43         return callbackFlow {
44                 val observer =
45                     object : ContentObserver(null) {
46                         override fun onChange(selfChange: Boolean) {
47                             trySend(Unit)
48                         }
49                     }
50 
51                 contentResolver.registerContentObserver(
52                     Settings.Secure.getUriFor(name),
53                     /* notifyForDescendants= */ false,
54                     observer,
55                 )
56                 send(Unit)
57 
58                 awaitClose { contentResolver.unregisterContentObserver(observer) }
59             }
60             .map { Settings.Secure.getInt(contentResolver, name, defaultValue) }
61             // The above work is done on the background thread (which is important for accessing
62             // settings through the content resolver).
63             .flowOn(backgroundDispatcher)
64     }
65 
boolSettingnull66     override fun boolSetting(name: String, defaultValue: Boolean): Flow<Boolean> {
67         return intSetting(name, if (defaultValue) 1 else 0).map { it != 0 }
68     }
69 
setIntnull70     override suspend fun setInt(name: String, value: Int) {
71         withContext(backgroundDispatcher) { Settings.Secure.putInt(contentResolver, name, value) }
72     }
73 
getIntnull74     override suspend fun getInt(name: String, defaultValue: Int): Int {
75         return withContext(backgroundDispatcher) {
76             Settings.Secure.getInt(contentResolver, name, defaultValue)
77         }
78     }
79 
getStringnull80     override suspend fun getString(name: String): String? {
81         return withContext(backgroundDispatcher) {
82             Settings.Secure.getString(contentResolver, name)
83         }
84     }
85 }
86