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 @file:OptIn(ExperimentalCoroutinesApi::class)
18 
19 package com.android.systemui.statusbar.notification.domain.interactor
20 
21 import com.android.systemui.deviceentry.domain.interactor.DeviceEntryFaceAuthInteractor
22 import com.android.systemui.keyguard.domain.interactor.KeyguardTransitionInteractor
23 import com.android.systemui.keyguard.shared.model.KeyguardState
24 import com.android.systemui.shade.domain.interactor.ShadeInteractor
25 import com.android.systemui.statusbar.notification.data.repository.HeadsUpRepository
26 import com.android.systemui.statusbar.notification.data.repository.HeadsUpRowRepository
27 import com.android.systemui.statusbar.notification.shared.HeadsUpRowKey
28 import javax.inject.Inject
29 import kotlinx.coroutines.ExperimentalCoroutinesApi
30 import kotlinx.coroutines.flow.Flow
31 import kotlinx.coroutines.flow.combine
32 import kotlinx.coroutines.flow.flatMapLatest
33 import kotlinx.coroutines.flow.flowOf
34 import kotlinx.coroutines.flow.map
35 
36 class HeadsUpNotificationInteractor
37 @Inject
38 constructor(
39     private val headsUpRepository: HeadsUpRepository,
40     private val faceAuthInteractor: DeviceEntryFaceAuthInteractor,
41     private val keyguardTransitionInteractor: KeyguardTransitionInteractor,
42     private val notificationsKeyguardInteractor: NotificationsKeyguardInteractor,
43     private val shadeInteractor: ShadeInteractor,
44 ) {
45 
46     val topHeadsUpRow: Flow<HeadsUpRowKey?> = headsUpRepository.topHeadsUpRow
47 
48     /** Set of currently pinned top-level heads up rows to be displayed. */
49     val pinnedHeadsUpRows: Flow<Set<HeadsUpRowKey>> =
50         headsUpRepository.activeHeadsUpRows.flatMapLatest { repositories ->
51             if (repositories.isNotEmpty()) {
52                 val toCombine: List<Flow<Pair<HeadsUpRowRepository, Boolean>>> =
53                     repositories.map { repo -> repo.isPinned.map { isPinned -> repo to isPinned } }
54                 combine(toCombine) { pairs ->
55                     pairs.filter { (_, isPinned) -> isPinned }.map { (repo, _) -> repo }.toSet()
56                 }
57             } else {
58                 // if the set is empty, there are no flows to combine
59                 flowOf(emptySet())
60             }
61         }
62 
63     /** Are there any pinned heads up rows to display? */
64     val hasPinnedRows: Flow<Boolean> =
65         headsUpRepository.activeHeadsUpRows.flatMapLatest { rows ->
66             if (rows.isNotEmpty()) {
67                 combine(rows.map { it.isPinned }) { pins -> pins.any { it } }
68             } else {
69                 // if the set is empty, there are no flows to combine
70                 flowOf(false)
71             }
72         }
73 
74     val isHeadsUpOrAnimatingAway: Flow<Boolean> =
75         combine(hasPinnedRows, headsUpRepository.isHeadsUpAnimatingAway) {
76             hasPinnedRows,
77             animatingAway ->
78             hasPinnedRows || animatingAway
79         }
80 
81     private val canShowHeadsUp: Flow<Boolean> =
82         combine(
83             faceAuthInteractor.isBypassEnabled,
84             shadeInteractor.isShadeFullyCollapsed,
85             keyguardTransitionInteractor.currentKeyguardState,
86             notificationsKeyguardInteractor.areNotificationsFullyHidden,
87         ) { isBypassEnabled, isShadeCollapsed, keyguardState, areNotificationsHidden ->
88             val isOnLockScreen = keyguardState == KeyguardState.LOCKSCREEN
89             when {
90                 areNotificationsHidden -> false // don't show when notification are hidden
91                 !isShadeCollapsed -> false // don't show when the shade is expanded
92                 isOnLockScreen -> isBypassEnabled // on the lock screen only show for bypass
93                 else -> true // show otherwise
94             }
95         }
96 
97     val showHeadsUpStatusBar: Flow<Boolean> =
98         combine(hasPinnedRows, canShowHeadsUp) { hasPinnedRows, canShowHeadsUp ->
99             hasPinnedRows && canShowHeadsUp
100         }
101 
102     fun headsUpRow(key: HeadsUpRowKey): HeadsUpRowInteractor =
103         HeadsUpRowInteractor(key as HeadsUpRowRepository)
104     fun elementKeyFor(key: HeadsUpRowKey) = (key as HeadsUpRowRepository).elementKey
105     fun setHeadsUpAnimatingAway(animatingAway: Boolean) {
106         headsUpRepository.setHeadsUpAnimatingAway(animatingAway)
107     }
108 }
109 
110 class HeadsUpRowInteractor(repository: HeadsUpRowRepository)
111