• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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.qs.footer.data.repository
18 
19 import com.android.systemui.common.coroutine.ChannelExt.trySendWithFailureLogging
20 import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
21 import com.android.systemui.dagger.SysUISingleton
22 import com.android.systemui.qs.FgsManagerController
23 import javax.inject.Inject
24 import kotlinx.coroutines.channels.awaitClose
25 import kotlinx.coroutines.flow.Flow
26 import kotlinx.coroutines.flow.distinctUntilChanged
27 import kotlinx.coroutines.flow.flatMapLatest
28 import kotlinx.coroutines.flow.flowOf
29 import kotlinx.coroutines.flow.map
30 import kotlinx.coroutines.flow.merge
31 
32 interface ForegroundServicesRepository {
33     /**
34      * The number of packages with a service running in the foreground.
35      *
36      * Note that this will be equal to 0 if [FgsManagerController.isAvailable] is false.
37      */
38     val foregroundServicesCount: Flow<Int>
39 
40     /**
41      * Whether there were new changes to the foreground packages since a dialog was last shown.
42      *
43      * Note that this will be equal to `false` if [FgsManagerController.showFooterDot] is false.
44      */
45     val hasNewChanges: Flow<Boolean>
46 }
47 
48 @SysUISingleton
49 class ForegroundServicesRepositoryImpl
50 @Inject
51 constructor(
52     fgsManagerController: FgsManagerController,
53 ) : ForegroundServicesRepository {
54     override val foregroundServicesCount: Flow<Int> =
55         fgsManagerController.isAvailable
isAvailablenull56             .flatMapLatest { isAvailable ->
57                 if (!isAvailable) {
58                     return@flatMapLatest flowOf(0)
59                 }
60 
61                 conflatedCallbackFlow {
62                     fun updateState(numberOfPackages: Int) {
63                         trySendWithFailureLogging(numberOfPackages, TAG)
64                     }
65 
66                     val listener =
67                         object : FgsManagerController.OnNumberOfPackagesChangedListener {
68                             override fun onNumberOfPackagesChanged(numberOfPackages: Int) {
69                                 updateState(numberOfPackages)
70                             }
71                         }
72 
73                     fgsManagerController.addOnNumberOfPackagesChangedListener(listener)
74                     updateState(fgsManagerController.numRunningPackages)
75                     awaitClose {
76                         fgsManagerController.removeOnNumberOfPackagesChangedListener(listener)
77                     }
78                 }
79             }
80             .distinctUntilChanged()
81 
82     override val hasNewChanges: Flow<Boolean> =
showFooterDotnull83         fgsManagerController.showFooterDot.flatMapLatest { showFooterDot ->
84             if (!showFooterDot) {
85                 return@flatMapLatest flowOf(false)
86             }
87 
88             // A flow that emits whenever the FGS dialog is dismissed.
89             val dialogDismissedEvents = conflatedCallbackFlow {
90                 fun updateState() {
91                     trySendWithFailureLogging(
92                         Unit,
93                         TAG,
94                     )
95                 }
96 
97                 val listener =
98                     object : FgsManagerController.OnDialogDismissedListener {
99                         override fun onDialogDismissed() {
100                             updateState()
101                         }
102                     }
103 
104                 fgsManagerController.addOnDialogDismissedListener(listener)
105                 awaitClose { fgsManagerController.removeOnDialogDismissedListener(listener) }
106             }
107 
108             // Query [fgsManagerController.newChangesSinceDialogWasDismissed] everytime the dialog
109             // is dismissed or when [foregroundServices] is changing.
110             merge(
111                     foregroundServicesCount,
112                     dialogDismissedEvents,
113                 )
114                 .map { fgsManagerController.newChangesSinceDialogWasDismissed }
115                 .distinctUntilChanged()
116         }
117 
118     companion object {
119         private const val TAG = "ForegroundServicesRepositoryImpl"
120     }
121 }
122