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.statusbar.policy.data.repository
18 
19 import android.app.NotificationManager
20 import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
21 import com.android.systemui.statusbar.policy.ZenModeController
22 import dagger.Binds
23 import dagger.Module
24 import javax.inject.Inject
25 import kotlinx.coroutines.channels.awaitClose
26 import kotlinx.coroutines.flow.Flow
27 
28 /**
29  * A repository that holds information about the status and configuration of Zen Mode (or Do Not
30  * Disturb/DND Mode).
31  */
32 interface ZenModeRepository {
33     val zenMode: Flow<Int>
34     val consolidatedNotificationPolicy: Flow<NotificationManager.Policy?>
35 }
36 
37 class ZenModeRepositoryImpl
38 @Inject
39 constructor(
40     private val zenModeController: ZenModeController,
41 ) : ZenModeRepository {
42     // TODO(b/308591859): ZenModeController should use flows instead of callbacks. The
43     // conflatedCallbackFlows here should be replaced eventually, see:
44     // https://docs.google.com/document/d/1gAiuYupwUAFdbxkDXa29A4aFNu7XoCd7sCIk31WTnHU/edit?resourcekey=0-J4ZBiUhLhhQnNobAcI2vIw
45 
<lambda>null46     override val zenMode: Flow<Int> = conflatedCallbackFlow {
47         val callback =
48             object : ZenModeController.Callback {
49                 override fun onZenChanged(zen: Int) {
50                     trySend(zen)
51                 }
52             }
53         zenModeController.addCallback(callback)
54         trySend(zenModeController.zen)
55 
56         awaitClose { zenModeController.removeCallback(callback) }
57     }
58 
59     override val consolidatedNotificationPolicy: Flow<NotificationManager.Policy?> =
<lambda>null60         conflatedCallbackFlow {
61             val callback =
62                 object : ZenModeController.Callback {
63                     override fun onConsolidatedPolicyChanged(policy: NotificationManager.Policy?) {
64                         trySend(policy)
65                     }
66                 }
67             zenModeController.addCallback(callback)
68             trySend(zenModeController.consolidatedPolicy)
69 
70             awaitClose { zenModeController.removeCallback(callback) }
71         }
72 }
73 
74 @Module
75 interface ZenModeRepositoryModule {
bindImplnull76     @Binds fun bindImpl(impl: ZenModeRepositoryImpl): ZenModeRepository
77 }
78