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.settingslib.statusbar.notification.data.repository 18 19 import android.app.NotificationManager 20 import android.content.BroadcastReceiver 21 import android.content.Context 22 import android.content.Intent 23 import android.content.IntentFilter 24 import com.android.settingslib.statusbar.notification.data.model.ZenMode 25 import kotlin.coroutines.CoroutineContext 26 import kotlinx.coroutines.CoroutineScope 27 import kotlinx.coroutines.channels.awaitClose 28 import kotlinx.coroutines.flow.SharingStarted 29 import kotlinx.coroutines.flow.StateFlow 30 import kotlinx.coroutines.flow.callbackFlow 31 import kotlinx.coroutines.flow.filter 32 import kotlinx.coroutines.flow.flowOn 33 import kotlinx.coroutines.flow.map 34 import kotlinx.coroutines.flow.onStart 35 import kotlinx.coroutines.flow.shareIn 36 import kotlinx.coroutines.flow.stateIn 37 import kotlinx.coroutines.launch 38 39 /** Provides state of volume policy and restrictions imposed by notifications. */ 40 interface NotificationsSoundPolicyRepository { 41 42 /** @see NotificationManager.getNotificationPolicy */ 43 val notificationPolicy: StateFlow<NotificationManager.Policy?> 44 45 /** @see NotificationManager.getZenMode */ 46 val zenMode: StateFlow<ZenMode?> 47 } 48 49 class NotificationsSoundPolicyRepositoryImpl( 50 private val context: Context, 51 private val notificationManager: NotificationManager, 52 scope: CoroutineScope, 53 backgroundCoroutineContext: CoroutineContext, 54 ) : NotificationsSoundPolicyRepository { 55 56 private val notificationBroadcasts = <lambda>null57 callbackFlow { 58 val receiver = 59 object : BroadcastReceiver() { 60 override fun onReceive(context: Context?, intent: Intent?) { 61 intent?.action?.let { action -> launch { send(action) } } 62 } 63 } 64 65 context.registerReceiver( 66 receiver, 67 IntentFilter().apply { 68 addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED) 69 addAction(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED) 70 } 71 ) 72 73 awaitClose { context.unregisterReceiver(receiver) } 74 } 75 .shareIn( 76 started = SharingStarted.WhileSubscribed(), 77 scope = scope, 78 ) 79 80 override val notificationPolicy: StateFlow<NotificationManager.Policy?> = 81 notificationBroadcasts <lambda>null82 .filter { NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED == it } <lambda>null83 .map { notificationManager.consolidatedNotificationPolicy } <lambda>null84 .onStart { emit(notificationManager.consolidatedNotificationPolicy) } 85 .flowOn(backgroundCoroutineContext) 86 .stateIn(scope, SharingStarted.WhileSubscribed(), null) 87 88 override val zenMode: StateFlow<ZenMode?> = 89 notificationBroadcasts <lambda>null90 .filter { NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED == it } <lambda>null91 .map { ZenMode(notificationManager.zenMode) } <lambda>null92 .onStart { emit(ZenMode(notificationManager.zenMode)) } 93 .flowOn(backgroundCoroutineContext) 94 .stateIn(scope, SharingStarted.WhileSubscribed(), null) 95 } 96