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 
17 package com.android.systemui.shade.domain.interactor
18 
19 import com.android.systemui.dagger.SysUISingleton
20 import com.android.systemui.dagger.qualifiers.Application
21 import com.android.systemui.keyguard.data.repository.KeyguardRepository
22 import com.android.systemui.statusbar.disableflags.data.repository.DisableFlagsRepository
23 import com.android.systemui.statusbar.pipeline.mobile.data.repository.UserSetupRepository
24 import com.android.systemui.statusbar.policy.DeviceProvisionedController
25 import com.android.systemui.user.domain.interactor.UserInteractor
26 import javax.inject.Inject
27 import kotlinx.coroutines.CoroutineScope
28 import kotlinx.coroutines.flow.Flow
29 import kotlinx.coroutines.flow.SharingStarted
30 import kotlinx.coroutines.flow.StateFlow
31 import kotlinx.coroutines.flow.combine
32 import kotlinx.coroutines.flow.map
33 import kotlinx.coroutines.flow.stateIn
34 
35 /** Business logic for shade interactions. */
36 @SysUISingleton
37 class ShadeInteractor
38 @Inject
39 constructor(
40     @Application scope: CoroutineScope,
41     disableFlagsRepository: DisableFlagsRepository,
42     keyguardRepository: KeyguardRepository,
43     userSetupRepository: UserSetupRepository,
44     deviceProvisionedController: DeviceProvisionedController,
45     userInteractor: UserInteractor,
46 ) {
47     /** Emits true if the shade is currently allowed and false otherwise. */
48     val isShadeEnabled: StateFlow<Boolean> =
49         disableFlagsRepository.disableFlags
50             .map { it.isShadeEnabled() }
51             .stateIn(scope, SharingStarted.Eagerly, initialValue = false)
52 
53     /** Emits true if the shade can be expanded from QQS to QS and false otherwise. */
54     val isExpandToQsEnabled: Flow<Boolean> =
55         combine(
56             disableFlagsRepository.disableFlags,
57             isShadeEnabled,
58             keyguardRepository.isDozing,
59             userSetupRepository.isUserSetupFlow,
60         ) { disableFlags, isShadeEnabled, isDozing, isUserSetup ->
61             deviceProvisionedController.isDeviceProvisioned &&
62                 // Disallow QS during setup if it's a simple user switcher. (The user intends to
63                 // use the lock screen user switcher, QS is not needed.)
64                 (isUserSetup || !userInteractor.isSimpleUserSwitcher) &&
65                 isShadeEnabled &&
66                 disableFlags.isQuickSettingsEnabled() &&
67                 !isDozing
68         }
69 }
70