• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.deviceconfig.data.repository
18 
19 import android.provider.DeviceConfig
20 import com.android.systemui.dagger.SysUISingleton
21 import com.android.systemui.dagger.qualifiers.Background
22 import com.android.systemui.util.DeviceConfigProxy
23 import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
24 import java.util.concurrent.Executor
25 import javax.inject.Inject
26 import kotlinx.coroutines.CoroutineDispatcher
27 import kotlinx.coroutines.channels.awaitClose
28 import kotlinx.coroutines.flow.Flow
29 import kotlinx.coroutines.flow.flowOn
30 
31 @SysUISingleton
32 class DeviceConfigRepository
33 @Inject
34 constructor(
35     @Background private val backgroundExecutor: Executor,
36     @Background private val backgroundDispatcher: CoroutineDispatcher,
37     private val dataSource: DeviceConfigProxy,
38 ) {
39 
40     fun property(namespace: String, name: String, default: Boolean): Flow<Boolean> {
41         return conflatedCallbackFlow {
42                 val listener = { properties: DeviceConfig.Properties ->
43                     if (properties.keyset.contains(name)) {
44                         trySend(properties.getBoolean(name, default))
45                     }
46                 }
47 
48                 dataSource.addOnPropertiesChangedListener(
49                     namespace,
50                     backgroundExecutor,
51                     listener,
52                 )
53                 trySend(dataSource.getBoolean(namespace, name, default))
54 
55                 awaitClose { dataSource.removeOnPropertiesChangedListener(listener) }
56             }
57             .flowOn(backgroundDispatcher)
58     }
59 }
60