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.policy.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 whether the user has completed the setup steps. This information can change
38  * some policy related to display.
39  */
40 interface UserSetupRepository {
41     /** Whether the user has completed the setup steps. */
42     val isUserSetUp: StateFlow<Boolean>
43 }
44 
45 @OptIn(ExperimentalCoroutinesApi::class)
46 @SysUISingleton
47 class UserSetupRepositoryImpl
48 @Inject
49 constructor(
50     private val deviceProvisionedController: DeviceProvisionedController,
51     @Background private val bgDispatcher: CoroutineDispatcher,
52     @Application scope: CoroutineScope,
53 ) : UserSetupRepository {
54     override val isUserSetUp: StateFlow<Boolean> =
<lambda>null55         conflatedCallbackFlow {
56                 val callback =
57                     object : DeviceProvisionedController.DeviceProvisionedListener {
58                         override fun onUserSetupChanged() {
59                             trySend(Unit)
60                         }
61                     }
62 
63                 deviceProvisionedController.addCallback(callback)
64 
65                 awaitClose { deviceProvisionedController.removeCallback(callback) }
66             }
<lambda>null67             .onStart { emit(Unit) }
<lambda>null68             .mapLatest { fetchUserSetupState() }
69             .stateIn(scope, started = SharingStarted.WhileSubscribed(), initialValue = false)
70 
fetchUserSetupStatenull71     private suspend fun fetchUserSetupState(): Boolean =
72         withContext(bgDispatcher) { deviceProvisionedController.isCurrentUserSetup }
73 }
74