1 /*
<lambda>null2  * Copyright (C) 2024 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.privacy.OngoingPrivacyChip
22 import com.android.systemui.privacy.PrivacyDialogController
23 import com.android.systemui.privacy.PrivacyDialogControllerV2
24 import com.android.systemui.privacy.PrivacyItem
25 import com.android.systemui.shade.data.repository.PrivacyChipRepository
26 import com.android.systemui.statusbar.policy.DeviceProvisionedController
27 import javax.inject.Inject
28 import kotlinx.coroutines.CoroutineScope
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 @SysUISingleton
36 class PrivacyChipInteractor
37 @Inject
38 constructor(
39     @Application applicationScope: CoroutineScope,
40     private val repository: PrivacyChipRepository,
41     private val privacyDialogController: PrivacyDialogController,
42     private val privacyDialogControllerV2: PrivacyDialogControllerV2,
43     private val deviceProvisionedController: DeviceProvisionedController,
44 ) {
45     /** The list of PrivacyItems to be displayed by the privacy chip. */
46     val privacyItems: StateFlow<List<PrivacyItem>> = repository.privacyItems
47 
48     /** Whether or not mic & camera indicators are enabled in the device privacy config. */
49     val isMicCameraIndicationEnabled: StateFlow<Boolean> = repository.isMicCameraIndicationEnabled
50 
51     /** Whether or not location indicators are enabled in the device privacy config. */
52     val isLocationIndicationEnabled: StateFlow<Boolean> = repository.isLocationIndicationEnabled
53 
54     /** Whether or not the privacy chip should be visible. */
55     val isChipVisible: StateFlow<Boolean> =
56         privacyItems
57             .map { it.isNotEmpty() }
58             .stateIn(
59                 scope = applicationScope,
60                 started = SharingStarted.WhileSubscribed(),
61                 initialValue = false,
62             )
63 
64     /** Whether or not the privacy chip is enabled in the device privacy config. */
65     val isChipEnabled: StateFlow<Boolean> =
66         combine(
67                 isMicCameraIndicationEnabled,
68                 isLocationIndicationEnabled,
69             ) { micCamera, location ->
70                 micCamera || location
71             }
72             .stateIn(
73                 scope = applicationScope,
74                 started = SharingStarted.WhileSubscribed(),
75                 initialValue = false,
76             )
77 
78     /** Notifies that the privacy chip was clicked. */
79     fun onPrivacyChipClicked(privacyChip: OngoingPrivacyChip) {
80         if (!deviceProvisionedController.isDeviceProvisioned) return
81 
82         if (repository.isSafetyCenterEnabled.value) {
83             privacyDialogControllerV2.showDialog(privacyChip.context, privacyChip)
84         } else {
85             privacyDialogController.showDialog(privacyChip.context)
86         }
87     }
88 }
89