1 /*
2  * Copyright 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 androidx.core.telecom.test.services
18 
19 import android.os.Build
20 import android.telecom.CallAudioState
21 import kotlinx.coroutines.flow.MutableStateFlow
22 import kotlinx.coroutines.flow.asStateFlow
23 
24 /** Tracks the current global mute state of the device */
25 class MuteStateResolver {
26     private val isCallAudioStateDeprecated =
27         Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE
28     private val mMuteState = MutableStateFlow(false)
29     val muteState = mMuteState.asStateFlow()
30 
31     /** The audio state of the device has changed for devices using API version < UDC */
onCallAudioStateChangednull32     fun onCallAudioStateChanged(audioState: CallAudioState?) {
33         if (audioState == null || isCallAudioStateDeprecated) return
34         mMuteState.value = audioState.isMuted
35     }
36 
37     /** The audio state of the device has changed for devices using API version UDC+ */
onMuteStateChangednull38     fun onMuteStateChanged(isMuted: Boolean) {
39         if (!isCallAudioStateDeprecated) return
40         mMuteState.value = isMuted
41     }
42 }
43