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 18 package com.android.wallpaper.settings.data.repository 19 20 import android.content.ContentResolver 21 import android.database.ContentObserver 22 import android.provider.Settings 23 import kotlinx.coroutines.CoroutineDispatcher 24 import kotlinx.coroutines.channels.awaitClose 25 import kotlinx.coroutines.flow.Flow 26 import kotlinx.coroutines.flow.callbackFlow 27 import kotlinx.coroutines.flow.flowOn 28 import kotlinx.coroutines.flow.map 29 import kotlinx.coroutines.withContext 30 31 /** Defines interface for classes that can provide access to data from [Settings.Secure]. */ 32 interface SecureSettingsRepository { 33 34 /** Returns a [Flow] tracking the value of a setting as an [Int]. */ intSettingnull35 fun intSetting( 36 name: String, 37 defaultValue: Int = 0, 38 ): Flow<Int> 39 40 /** Updates the value of the setting with the given name. */ 41 suspend fun set( 42 name: String, 43 value: Int, 44 ) 45 46 suspend fun get( 47 name: String, 48 defaultValue: Int = 0, 49 ): Int 50 } 51 52 class SecureSettingsRepositoryImpl( 53 private val contentResolver: ContentResolver, 54 private val backgroundDispatcher: CoroutineDispatcher, 55 ) : SecureSettingsRepository { 56 57 override fun intSetting( 58 name: String, 59 defaultValue: Int, 60 ): Flow<Int> { 61 return callbackFlow { 62 val observer = 63 object : ContentObserver(null) { 64 override fun onChange(selfChange: Boolean) { 65 trySend(Unit) 66 } 67 } 68 69 contentResolver.registerContentObserver( 70 Settings.Secure.getUriFor(name), 71 /* notifyForDescendants= */ false, 72 observer, 73 ) 74 send(Unit) 75 76 awaitClose { contentResolver.unregisterContentObserver(observer) } 77 } 78 .map { Settings.Secure.getInt(contentResolver, name, defaultValue) } 79 // The above work is done on the background thread (which is important for accessing 80 // settings through the content resolver). 81 .flowOn(backgroundDispatcher) 82 } 83 84 override suspend fun set(name: String, value: Int) { 85 withContext(backgroundDispatcher) { 86 Settings.Secure.putInt( 87 contentResolver, 88 name, 89 value, 90 ) 91 } 92 } 93 94 override suspend fun get(name: String, defaultValue: Int): Int { 95 return withContext(backgroundDispatcher) { 96 Settings.Secure.getInt( 97 contentResolver, 98 name, 99 defaultValue, 100 ) 101 } 102 } 103 } 104