1 /*
2  * 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.settings.notification
18 
19 import android.app.NotificationManager
20 import android.app.NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS
21 import android.app.NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA
22 import android.content.Context
23 import android.media.AudioManager
24 import android.media.AudioManager.*
25 import android.provider.Settings.Global.ZEN_MODE_ALARMS
26 import android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS
27 import android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS
28 import android.service.notification.ZenModeConfig
29 
30 class VolumeHelper {
31     companion object {
isMutednull32         fun isMuted(context: Context, streamType: Int): Boolean {
33             val audioManager = context.getSystemService(AudioManager::class.java)
34             return audioManager.isStreamMute(streamType) && !isZenMuted(context, streamType)
35         }
36 
isZenMutednull37         fun isZenMuted(context: Context, streamType: Int): Boolean {
38             val notificationManager = context.getSystemService(NotificationManager::class.java)
39             val zenMode = notificationManager.getZenMode()
40             val notificationPolicy = notificationManager.getConsolidatedNotificationPolicy()
41             val isAllowAlarms =
42                 (notificationPolicy.priorityCategories and PRIORITY_CATEGORY_ALARMS) != 0
43             val isAllowMedia =
44                 (notificationPolicy.priorityCategories and PRIORITY_CATEGORY_MEDIA) != 0
45             val isAllowRinger =
46                 !ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(notificationPolicy)
47             return isNotificationOrRingStream(streamType)
48                     && zenMode == ZEN_MODE_ALARMS || zenMode == ZEN_MODE_NO_INTERRUPTIONS
49                     || (zenMode == ZEN_MODE_IMPORTANT_INTERRUPTIONS
50                     && (!isAllowRinger && isNotificationOrRingStream(streamType)
51                     || !isAllowMedia && isMediaStream(streamType)
52                     || !isAllowAlarms && isAlarmStream(streamType)))
53         }
54 
isNotificationOrRingStreamnull55         private fun isNotificationOrRingStream(streamType: Int) =
56             streamType == STREAM_RING || streamType == STREAM_NOTIFICATION
57 
58         private fun isAlarmStream(streamType: Int) = streamType == STREAM_ALARM
59 
60         private fun isMediaStream(streamType: Int) = streamType == STREAM_MUSIC
61     }
62 }