1 /*
2  * 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 package com.android.systemui.statusbar.pipeline.mobile.data.repository
18 
19 import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
20 import com.android.systemui.dagger.SysUISingleton
21 import com.android.systemui.dagger.qualifiers.Application
22 import com.android.systemui.dagger.qualifiers.Background
23 import com.android.systemui.statusbar.policy.DeviceProvisionedController
24 import javax.inject.Inject
25 import kotlinx.coroutines.CoroutineDispatcher
26 import kotlinx.coroutines.CoroutineScope
27 import kotlinx.coroutines.ExperimentalCoroutinesApi
28 import kotlinx.coroutines.channels.awaitClose
29 import kotlinx.coroutines.flow.SharingStarted
30 import kotlinx.coroutines.flow.StateFlow
31 import kotlinx.coroutines.flow.mapLatest
32 import kotlinx.coroutines.flow.onStart
33 import kotlinx.coroutines.flow.stateIn
34 import kotlinx.coroutines.withContext
35 
36 /**
37  * Repository to observe the state of [DeviceProvisionedController.isUserSetup]. This information
38  * can change some policy related to display
39  */
40 interface UserSetupRepository {
41     /** Observable tracking [DeviceProvisionedController.isUserSetup] */
42     val isUserSetupFlow: StateFlow<Boolean>
43 }
44 
45 @Suppress("EXPERIMENTAL_IS_NOT_ENABLED")
46 @OptIn(ExperimentalCoroutinesApi::class)
47 @SysUISingleton
48 class UserSetupRepositoryImpl
49 @Inject
50 constructor(
51     private val deviceProvisionedController: DeviceProvisionedController,
52     @Background private val bgDispatcher: CoroutineDispatcher,
53     @Application scope: CoroutineScope,
54 ) : UserSetupRepository {
55     /** State flow that tracks [DeviceProvisionedController.isUserSetup] */
56     override val isUserSetupFlow: StateFlow<Boolean> =
57         conflatedCallbackFlow {
58                 val callback =
59                     object : DeviceProvisionedController.DeviceProvisionedListener {
60                         override fun onUserSetupChanged() {
61                             trySend(Unit)
62                         }
63                     }
64 
65                 deviceProvisionedController.addCallback(callback)
66 
67                 awaitClose { deviceProvisionedController.removeCallback(callback) }
68             }
69             .onStart { emit(Unit) }
70             .mapLatest { fetchUserSetupState() }
71             .stateIn(scope, started = SharingStarted.WhileSubscribed(), initialValue = false)
72 
73     private suspend fun fetchUserSetupState(): Boolean =
74         withContext(bgDispatcher) { deviceProvisionedController.isCurrentUserSetup }
75 }
76