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 package com.android.systemui.statusbar.policy.data.repository
17 
18 import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
19 import com.android.systemui.statusbar.policy.DeviceProvisionedController
20 import dagger.Binds
21 import dagger.Module
22 import javax.inject.Inject
23 import kotlinx.coroutines.channels.awaitClose
24 import kotlinx.coroutines.flow.Flow
25 
26 /** Tracks state related to device provisioning. */
27 interface DeviceProvisioningRepository {
28     /**
29      * Whether this device has been provisioned.
30      *
31      * @see android.provider.Settings.Global.DEVICE_PROVISIONED
32      */
33     val isDeviceProvisioned: Flow<Boolean>
34 }
35 
36 @Module
37 interface DeviceProvisioningRepositoryModule {
bindsImplnull38     @Binds fun bindsImpl(impl: DeviceProvisioningRepositoryImpl): DeviceProvisioningRepository
39 }
40 
41 class DeviceProvisioningRepositoryImpl
42 @Inject
43 constructor(
44     private val deviceProvisionedController: DeviceProvisionedController,
45 ) : DeviceProvisioningRepository {
46 
47     override val isDeviceProvisioned: Flow<Boolean> = conflatedCallbackFlow {
48         val listener =
49             object : DeviceProvisionedController.DeviceProvisionedListener {
50                 override fun onDeviceProvisionedChanged() {
51                     trySend(deviceProvisionedController.isDeviceProvisioned)
52                 }
53             }
54         deviceProvisionedController.addCallback(listener)
55         trySend(deviceProvisionedController.isDeviceProvisioned)
56         awaitClose { deviceProvisionedController.removeCallback(listener) }
57     }
58 }
59