1 /* <lambda>null2 * Copyright (C) 2022 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.systemui.util.settings 19 20 import android.annotation.UserIdInt 21 import android.database.ContentObserver 22 import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow 23 import kotlinx.coroutines.channels.awaitClose 24 import kotlinx.coroutines.flow.Flow 25 26 /** Kotlin extension functions for [SettingsProxy]. */ 27 object SettingsProxyExt { 28 29 /** Returns a flow of [Unit] that is invoked each time that content is updated. */ 30 fun UserSettingsProxy.observerFlow( 31 @UserIdInt userId: Int, 32 vararg names: String, 33 ): Flow<Unit> { 34 return conflatedCallbackFlow { 35 val observer = 36 object : ContentObserver(null) { 37 override fun onChange(selfChange: Boolean) { 38 trySend(Unit) 39 } 40 } 41 42 names.forEach { name -> registerContentObserverForUserSync(name, observer, userId) } 43 44 awaitClose { unregisterContentObserverSync(observer) } 45 } 46 } 47 48 /** Returns a flow of [Unit] that is invoked each time that content is updated. */ 49 fun SettingsProxy.observerFlow( 50 vararg names: String, 51 ): Flow<Unit> { 52 return conflatedCallbackFlow { 53 val observer = 54 object : ContentObserver(null) { 55 override fun onChange(selfChange: Boolean) { 56 trySend(Unit) 57 } 58 } 59 60 names.forEach { name -> registerContentObserverSync(name, observer) } 61 62 awaitClose { unregisterContentObserverSync(observer) } 63 } 64 } 65 } 66