Merge "Take isAffectedByMute into account when trying to mute a stream" into main
diff --git a/packages/SettingsLib/src/com/android/settingslib/volume/domain/interactor/AudioVolumeInteractor.kt b/packages/SettingsLib/src/com/android/settingslib/volume/domain/interactor/AudioVolumeInteractor.kt
index 202ff40..bdd582d 100644
--- a/packages/SettingsLib/src/com/android/settingslib/volume/domain/interactor/AudioVolumeInteractor.kt
+++ b/packages/SettingsLib/src/com/android/settingslib/volume/domain/interactor/AudioVolumeInteractor.kt
@@ -61,6 +61,10 @@
     }
 
     suspend fun setMuted(audioStream: AudioStream, isMuted: Boolean) {
+        val streamModel = getAudioStream(audioStream).first()
+        if (!streamModel.isAffectedByMute) {
+            return
+        }
         if (audioStream.value == AudioManager.STREAM_RING) {
             val mode =
                 if (isMuted) AudioManager.RINGER_MODE_VIBRATE else AudioManager.RINGER_MODE_NORMAL
diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/volume/domain/interactor/AudioVolumeInteractorTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/volume/domain/interactor/AudioVolumeInteractorTest.kt
index 6e49e43..8a68417 100644
--- a/packages/SystemUI/multivalentTests/src/com/android/systemui/volume/domain/interactor/AudioVolumeInteractorTest.kt
+++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/volume/domain/interactor/AudioVolumeInteractorTest.kt
@@ -35,6 +35,7 @@
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.test.runCurrent
 import kotlinx.coroutines.test.runTest
+import org.junit.Before
 import org.junit.Test
 import org.junit.runner.RunWith
 
@@ -48,6 +49,20 @@
     private val underTest: AudioVolumeInteractor =
         with(kosmos) { AudioVolumeInteractor(audioRepository, notificationsSoundPolicyInteractor) }
 
+    @Before
+    fun setUp() =
+        with(kosmos) {
+            audioRepository.setAudioStreamModel(
+                audioRepository.getAudioStream(audioStream).value.copy(isAffectedByMute = true)
+            )
+            audioRepository.setAudioStreamModel(
+                audioRepository
+                    .getAudioStream(AudioStream(AudioManager.STREAM_RING))
+                    .value
+                    .copy(isAffectedByMute = true)
+            )
+        }
+
     @Test
     fun setMuted_mutesStream() {
         with(kosmos) {
@@ -189,10 +204,14 @@
             testScope.runTest {
                 val audioStreamModel by collectLastValue(underTest.getAudioStream(audioStream))
                 audioRepository.setAudioStreamModel(
-                    audioStreamModel!!.copy(isAffectedByMute = false)
+                    audioStreamModel!!.copy(isAffectedByMute = false, isMuted = false)
                 )
 
+                underTest.setMuted(audioStream, true)
+                runCurrent()
+
                 assertThat(audioStreamModel!!.isAffectedByMute).isFalse()
+                assertThat(audioStreamModel!!.isMuted).isFalse()
             }
         }
     }
diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/volume/data/repository/FakeAudioRepository.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/volume/data/repository/FakeAudioRepository.kt
index fcea9e7b..135cb14 100644
--- a/packages/SystemUI/tests/utils/src/com/android/systemui/volume/data/repository/FakeAudioRepository.kt
+++ b/packages/SystemUI/tests/utils/src/com/android/systemui/volume/data/repository/FakeAudioRepository.kt
@@ -22,7 +22,6 @@
 import com.android.settingslib.volume.shared.model.AudioStream
 import com.android.settingslib.volume.shared.model.AudioStreamModel
 import com.android.settingslib.volume.shared.model.RingerMode
-import kotlinx.coroutines.flow.Flow
 import kotlinx.coroutines.flow.MutableStateFlow
 import kotlinx.coroutines.flow.StateFlow
 import kotlinx.coroutines.flow.asStateFlow
@@ -61,7 +60,7 @@
             )
         }
 
-    override fun getAudioStream(audioStream: AudioStream): Flow<AudioStreamModel> =
+    override fun getAudioStream(audioStream: AudioStream): StateFlow<AudioStreamModel> =
         getAudioStreamModelState(audioStream).asStateFlow()
 
     override suspend fun setVolume(audioStream: AudioStream, volume: Int) {