Make volume slider in volume panel consistent with settings app

Screen recording:
- go/before-fix
- go/after-fix-and-flag-on

While in singleVolume mode, all volume sliders except Music stream will
be hidden in Settings app, e.g. call stream [1], alarm stream [2],
notification stream [3], ring stream [4]. This change adds the same
check in SysUI volume panel to be consistent with Settings app.

Otherwise although there are five streams in SysUI volume panel,
adjusting any of the volume slider will adjust all of the rest, which
is a wired behavior and seems like a UI bug.

[1] https://source.corp.google.com/h/googleplex-android/platform/superproject/main/+/main:packages/apps/Settings/src/com/android/settings/notification/CallVolumePreferenceController.java;l=38;bpv=0
[2] https://source.corp.google.com/h/googleplex-android/platform/superproject/main/+/main:packages/apps/Settings/src/com/android/settings/notification/AlarmVolumePreferenceController.java;l=36;bpv=0
[3] https://source.corp.google.com/h/googleplex-android/platform/superproject/main/+/main:packages/apps/Settings/src/com/android/settings/notification/NotificationVolumePreferenceController.java;l=100;bpv=0
[4] https://source.corp.google.com/h/googleplex-android/platform/superproject/main/+/main:packages/apps/Settings/src/com/android/settings/notification/SeparateRingVolumePreferenceController.java;l=89?q=RingVolumePreferenceController.java&sq=git:googleplex-android%2Fplatform%2Fsuperproject%2Fmain@main


Change-Id: Idd2c5cfe450aa6ff5409e6339fd7e33b24324750
Bug: b/373729625
Test: atest AudioSlidersInteractorTest, tested in desktop device with
single volume mode enabled
Flag: com.android.systemui.only_show_media_stream_slider_in_single_volume_mode
diff --git a/packages/SystemUI/aconfig/systemui.aconfig b/packages/SystemUI/aconfig/systemui.aconfig
index 651244a..c3dc65b 100644
--- a/packages/SystemUI/aconfig/systemui.aconfig
+++ b/packages/SystemUI/aconfig/systemui.aconfig
@@ -1507,3 +1507,13 @@
     purpose: PURPOSE_BUGFIX
   }
 }
+
+flag {
+   name: "only_show_media_stream_slider_in_single_volume_mode"
+   namespace: "systemui"
+   description: "When the device is in single volume mode, only show media stream slider and hide all other stream (e.g. call, notification, alarm, etc) sliders in volume panel"
+   bug: "373729625"
+   metadata {
+       purpose: PURPOSE_BUGFIX
+   }
+}
diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/volume/panel/component/volume/domain/interactor/AudioSlidersInteractorTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/volume/panel/component/volume/domain/interactor/AudioSlidersInteractorTest.kt
index 7361de7..76a1269 100644
--- a/packages/SystemUI/multivalentTests/src/com/android/systemui/volume/panel/component/volume/domain/interactor/AudioSlidersInteractorTest.kt
+++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/volume/panel/component/volume/domain/interactor/AudioSlidersInteractorTest.kt
@@ -17,16 +17,19 @@
 package com.android.systemui.volume.panel.component.volume.domain.interactor
 
 import android.media.AudioManager
+import android.platform.test.annotations.EnableFlags
 import android.testing.TestableLooper
 import androidx.test.ext.junit.runners.AndroidJUnit4
 import androidx.test.filters.SmallTest
 import com.android.settingslib.volume.shared.model.AudioStream
+import com.android.systemui.Flags
 import com.android.systemui.SysuiTestCase
 import com.android.systemui.coroutines.collectLastValue
 import com.android.systemui.kosmos.Kosmos
 import com.android.systemui.kosmos.testScope
 import com.android.systemui.testKosmos
 import com.android.systemui.volume.data.repository.audioRepository
+import com.android.systemui.volume.data.repository.audioSystemRepository
 import com.android.systemui.volume.panel.component.volume.domain.model.SliderType
 import com.google.common.truth.Truth.assertThat
 import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -91,4 +94,20 @@
                     ).map { SliderType.Stream(AudioStream(it)) })
             }
         }
+
+
+    @Test
+    @EnableFlags(Flags.FLAG_ONLY_SHOW_MEDIA_STREAM_SLIDER_IN_SINGLE_VOLUME_MODE)
+    fun shouldAddMusicStreamOnly_singleVolumeMode() =
+        with(kosmos) {
+            testScope.runTest {
+                audioSystemRepository.setIsSingleVolume(true)
+
+                val sliders by collectLastValue(underTest.volumePanelSliders)
+                runCurrent()
+
+                assertThat(sliders).isEqualTo(
+                    mutableListOf(SliderType.Stream(AudioStream(AudioManager.STREAM_MUSIC))))
+            }
+        }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/volume/dagger/AudioModule.kt b/packages/SystemUI/src/com/android/systemui/volume/dagger/AudioModule.kt
index 20d598a..617aaa7 100644
--- a/packages/SystemUI/src/com/android/systemui/volume/dagger/AudioModule.kt
+++ b/packages/SystemUI/src/com/android/systemui/volume/dagger/AudioModule.kt
@@ -27,6 +27,8 @@
 import com.android.settingslib.volume.data.repository.AudioSharingRepository
 import com.android.settingslib.volume.data.repository.AudioSharingRepositoryEmptyImpl
 import com.android.settingslib.volume.data.repository.AudioSharingRepositoryImpl
+import com.android.settingslib.volume.data.repository.AudioSystemRepository
+import com.android.settingslib.volume.data.repository.AudioSystemRepositoryImpl
 import com.android.settingslib.volume.domain.interactor.AudioModeInteractor
 import com.android.settingslib.volume.domain.interactor.AudioVolumeInteractor
 import com.android.settingslib.volume.shared.AudioManagerEventsReceiver
@@ -106,5 +108,11 @@
             notificationsSoundPolicyInteractor: NotificationsSoundPolicyInteractor,
         ): AudioVolumeInteractor =
             AudioVolumeInteractor(audioRepository, notificationsSoundPolicyInteractor)
+
+        @Provides
+        @SysUISingleton
+        fun provideAudioSystemRepository(
+            @Application context: Context,
+        ): AudioSystemRepository = AudioSystemRepositoryImpl(context)
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/volume/panel/component/volume/domain/interactor/AudioSlidersInteractor.kt b/packages/SystemUI/src/com/android/systemui/volume/panel/component/volume/domain/interactor/AudioSlidersInteractor.kt
index 4be680e..1e4afc0 100644
--- a/packages/SystemUI/src/com/android/systemui/volume/panel/component/volume/domain/interactor/AudioSlidersInteractor.kt
+++ b/packages/SystemUI/src/com/android/systemui/volume/panel/component/volume/domain/interactor/AudioSlidersInteractor.kt
@@ -17,8 +17,10 @@
 package com.android.systemui.volume.panel.component.volume.domain.interactor
 
 import android.media.AudioManager
+import com.android.settingslib.volume.data.repository.AudioSystemRepository
 import com.android.settingslib.volume.domain.interactor.AudioModeInteractor
 import com.android.settingslib.volume.shared.model.AudioStream
+import com.android.systemui.Flags
 import com.android.systemui.volume.panel.component.mediaoutput.domain.interactor.MediaOutputInteractor
 import com.android.systemui.volume.panel.component.mediaoutput.shared.model.MediaDeviceSession
 import com.android.systemui.volume.panel.component.mediaoutput.shared.model.isTheSameSession
@@ -41,6 +43,7 @@
     @VolumePanelScope scope: CoroutineScope,
     mediaOutputInteractor: MediaOutputInteractor,
     audioModeInteractor: AudioModeInteractor,
+    private val audioSystemRepository: AudioSystemRepository,
 ) {
 
     val volumePanelSliders: StateFlow<List<SliderType>> =
@@ -83,6 +86,16 @@
     }
 
     private fun MutableList<SliderType>.addStream(stream: Int) {
+        // Hide other streams except STREAM_MUSIC if the isSingleVolume mode is on. This makes sure
+        // the volume slider in volume panel is consistent with the volume slider inside system
+        // settings app.
+        if (Flags.onlyShowMediaStreamSliderInSingleVolumeMode() &&
+            audioSystemRepository.isSingleVolume &&
+            stream != AudioManager.STREAM_MUSIC
+        ) {
+            return
+        }
+
         add(SliderType.Stream(AudioStream(stream)))
     }
 }
diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/volume/data/repository/AudioSystemRepositoryKosmos.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/volume/data/repository/AudioSystemRepositoryKosmos.kt
new file mode 100644
index 0000000..ec2bf24
--- /dev/null
+++ b/packages/SystemUI/tests/utils/src/com/android/systemui/volume/data/repository/AudioSystemRepositoryKosmos.kt
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.volume.data.repository
+
+import com.android.systemui.kosmos.Kosmos
+
+val Kosmos.audioSystemRepository by Kosmos.Fixture { FakeAudioSystemRepository() }
diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/volume/data/repository/FakeAudioSystemRepository.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/volume/data/repository/FakeAudioSystemRepository.kt
new file mode 100644
index 0000000..0ff4dec
--- /dev/null
+++ b/packages/SystemUI/tests/utils/src/com/android/systemui/volume/data/repository/FakeAudioSystemRepository.kt
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.volume.data.repository
+
+import com.android.settingslib.volume.data.repository.AudioSystemRepository
+
+class FakeAudioSystemRepository : AudioSystemRepository {
+
+    override var isSingleVolume = false
+
+    fun setIsSingleVolume(singleVolume: Boolean) {
+        isSingleVolume = singleVolume
+    }
+}
\ No newline at end of file
diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/volume/panel/component/volume/domain/interactor/AudioSlidersInteractorKosmos.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/volume/panel/component/volume/domain/interactor/AudioSlidersInteractorKosmos.kt
index dd5bbf3..3bc920e 100644
--- a/packages/SystemUI/tests/utils/src/com/android/systemui/volume/panel/component/volume/domain/interactor/AudioSlidersInteractorKosmos.kt
+++ b/packages/SystemUI/tests/utils/src/com/android/systemui/volume/panel/component/volume/domain/interactor/AudioSlidersInteractorKosmos.kt
@@ -18,6 +18,7 @@
 
 import com.android.systemui.kosmos.Kosmos
 import com.android.systemui.kosmos.applicationCoroutineScope
+import com.android.systemui.volume.data.repository.audioSystemRepository
 import com.android.systemui.volume.domain.interactor.audioModeInteractor
 import com.android.systemui.volume.mediaOutputInteractor
 
@@ -27,5 +28,6 @@
             applicationCoroutineScope,
             mediaOutputInteractor,
             audioModeInteractor,
+            audioSystemRepository,
         )
     }