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  * Defines interface for classes that can provide access to data from [Settings.Secure].
32  * This repository doesn't guarantee to provide value across different users. For that
33  * see: [UserAwareSecureSettingsRepository]
34  */
35 interface SecureSettingsRepository {
36 
37     /** Returns a [Flow] tracking the value of a setting as an [Int]. */
intSettingnull38     fun intSetting(
39         name: String,
40         defaultValue: Int = 0,
41     ): Flow<Int>
42 
43     /** Updates the value of the setting with the given name. */
44     suspend fun setInt(
45         name: String,
46         value: Int,
47     )
48 
49     suspend fun getInt(
50         name: String,
51         defaultValue: Int = 0,
52     ): Int
53 
54     suspend fun getString(name: String): String?
55 }
56 
57 class SecureSettingsRepositoryImpl(
58     private val contentResolver: ContentResolver,
59     private val backgroundDispatcher: CoroutineDispatcher,
60 ) : SecureSettingsRepository {
61 
62     override fun intSetting(
63         name: String,
64         defaultValue: Int,
65     ): Flow<Int> {
66         return callbackFlow {
67                 val observer =
68                     object : ContentObserver(null) {
69                         override fun onChange(selfChange: Boolean) {
70                             trySend(Unit)
71                         }
72                     }
73 
74                 contentResolver.registerContentObserver(
75                     Settings.Secure.getUriFor(name),
76                     /* notifyForDescendants= */ false,
77                     observer,
78                 )
79                 send(Unit)
80 
81                 awaitClose { contentResolver.unregisterContentObserver(observer) }
82             }
83             .map { Settings.Secure.getInt(contentResolver, name, defaultValue) }
84             // The above work is done on the background thread (which is important for accessing
85             // settings through the content resolver).
86             .flowOn(backgroundDispatcher)
87     }
88 
89     override suspend fun setInt(name: String, value: Int) {
90         withContext(backgroundDispatcher) {
91             Settings.Secure.putInt(
92                 contentResolver,
93                 name,
94                 value,
95             )
96         }
97     }
98 
99     override suspend fun getInt(name: String, defaultValue: Int): Int {
100         return withContext(backgroundDispatcher) {
101             Settings.Secure.getInt(
102                 contentResolver,
103                 name,
104                 defaultValue,
105             )
106         }
107     }
108 
109     override suspend fun getString(name: String): String? {
110         return withContext(backgroundDispatcher) {
111             Settings.Secure.getString(
112                 contentResolver,
113                 name,
114             )
115         }
116     }
117 }
118