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.dagger.SysUISingleton 20 import com.android.systemui.dagger.qualifiers.Background 21 import com.android.systemui.statusbar.policy.DeviceProvisionedController 22 import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow 23 import javax.inject.Inject 24 import kotlinx.coroutines.CoroutineDispatcher 25 import kotlinx.coroutines.CoroutineScope 26 import kotlinx.coroutines.channels.awaitClose 27 import kotlinx.coroutines.flow.SharingStarted 28 import kotlinx.coroutines.flow.StateFlow 29 import kotlinx.coroutines.flow.mapLatest 30 import kotlinx.coroutines.flow.onStart 31 import kotlinx.coroutines.flow.stateIn 32 import kotlinx.coroutines.withContext 33 34 /** 35 * Repository to observe whether the user has completed the setup steps. This information can change 36 * some policy related to display. 37 */ 38 interface UserSetupRepository { 39 /** Whether the user has completed the setup steps. */ 40 val isUserSetUp: StateFlow<Boolean> 41 } 42 43 @SysUISingleton 44 class UserSetupRepositoryImpl 45 @Inject 46 constructor( 47 private val deviceProvisionedController: DeviceProvisionedController, 48 @Background private val bgDispatcher: CoroutineDispatcher, 49 @Background scope: CoroutineScope, 50 ) : UserSetupRepository { 51 override val isUserSetUp: StateFlow<Boolean> = <lambda>null52 conflatedCallbackFlow { 53 val callback = 54 object : DeviceProvisionedController.DeviceProvisionedListener { 55 override fun onUserSetupChanged() { 56 trySend(Unit) 57 } 58 } 59 60 deviceProvisionedController.addCallback(callback) 61 62 awaitClose { deviceProvisionedController.removeCallback(callback) } 63 } <lambda>null64 .onStart { emit(Unit) } <lambda>null65 .mapLatest { fetchUserSetupState() } 66 .stateIn(scope, started = SharingStarted.WhileSubscribed(), initialValue = false) 67 fetchUserSetupStatenull68 private suspend fun fetchUserSetupState(): Boolean = 69 withContext(bgDispatcher) { deviceProvisionedController.isCurrentUserSetup } 70 } 71