• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.statusbar.notification.ui.viewbinder
18 
19 import com.android.app.tracing.coroutines.launchTraced as launch
20 import com.android.systemui.statusbar.chips.ui.viewmodel.OngoingActivityChipsViewModel
21 import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow
22 import com.android.systemui.statusbar.notification.shared.HeadsUpRowKey
23 import com.android.systemui.statusbar.notification.stack.NotificationStackScrollLayout
24 import com.android.systemui.statusbar.notification.stack.ui.viewmodel.NotificationListViewModel
25 import com.android.systemui.util.kotlin.sample
26 import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
27 import javax.inject.Inject
28 import kotlinx.coroutines.channels.awaitClose
29 import kotlinx.coroutines.coroutineScope
30 import kotlinx.coroutines.flow.Flow
31 import kotlinx.coroutines.flow.combine
32 
33 class HeadsUpNotificationViewBinder
34 @Inject
35 constructor(
36     private val viewModel: NotificationListViewModel,
37     private val ongoingActivityChipsViewModel: OngoingActivityChipsViewModel,
38 ) {
39     suspend fun bindHeadsUpNotifications(parentView: NotificationStackScrollLayout): Unit =
40         coroutineScope {
41             launch {
42                 var previousKeys = emptySet<HeadsUpRowKey>()
43                 combine(
44                         viewModel.pinnedHeadsUpRowKeys,
45                         viewModel.activeHeadsUpRowKeys,
46                         ongoingActivityChipsViewModel.visibleChipKeys,
47                         ::Triple,
48                     )
49                     .sample(viewModel.headsUpAnimationsEnabled, ::Pair)
50                     .collect { (newKeys, animationsEnabled) ->
51                         val pinned = newKeys.first
52                         val all = newKeys.second
53                         val statusBarChips: List<String> = newKeys.third
54 
55                         val added = all.union(pinned) - previousKeys
56                         val removed = previousKeys - pinned
57                         previousKeys = pinned
58                         Pair(added, removed)
59 
60                         if (animationsEnabled) {
61                             added.forEach { key ->
62                                 val row = obtainView(key)
63                                 val hasStatusBarChip = statusBarChips.contains(row.key)
64                                 parentView.generateHeadsUpAnimation(
65                                     row,
66                                     /* isHeadsUp = */ true,
67                                     hasStatusBarChip,
68                                 )
69                             }
70                             removed.forEach { key ->
71                                 val row = obtainView(key)
72                                 val hasStatusBarChip = statusBarChips.contains(row.key)
73                                 if (!parentView.isBeingDragged()) {
74                                     parentView.generateHeadsUpAnimation(
75                                         row,
76                                         /* isHeadsUp= */ false,
77                                         hasStatusBarChip,
78                                     )
79                                 }
80                                 row.markHeadsUpSeen()
81                             }
82                         }
83                     }
84             }
85             launch {
86                 viewModel.topHeadsUpRow.collect { key ->
87                     parentView.setTopHeadsUpRow(key?.let(::obtainView))
88                 }
89             }
90             launch {
91                 viewModel.hasPinnedHeadsUpRow.collect { parentView.setInHeadsUpPinnedMode(it) }
92             }
93             launch {
94                 parentView.isHeadsUpAnimatingAway.collect { viewModel.setHeadsUpAnimatingAway(it) }
95             }
96         }
97 
98     private fun obtainView(key: HeadsUpRowKey): ExpandableNotificationRow {
99         return viewModel.elementKeyFor(key) as ExpandableNotificationRow
100     }
101 }
102 
103 private val NotificationStackScrollLayout.isHeadsUpAnimatingAway: Flow<Boolean>
<lambda>null104     get() = conflatedCallbackFlow {
105         setHeadsUpAnimatingAwayListener { animatingAway -> trySend(animatingAway) }
106         awaitClose { setHeadsUpAnimatingAwayListener(null) }
107     }
108