Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 26eab8d3 authored by Anton Potapov's avatar Anton Potapov Committed by Android (Google) Code Review
Browse files

Merge "Add muting/unmuting when volume change to/from min value" into main

parents 7272cb74 050d56f0
Loading
Loading
Loading
Loading
+16 −10
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.settingslib.statusbar.notification.data.repository

import android.app.NotificationManager
import android.provider.Settings
import com.android.settingslib.statusbar.notification.data.model.ZenMode
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -28,10 +29,14 @@ class FakeNotificationsSoundPolicyRepository : NotificationsSoundPolicyRepositor
    override val notificationPolicy: StateFlow<NotificationManager.Policy?>
        get() = mutableNotificationPolicy.asStateFlow()

    private val mutableZenMode = MutableStateFlow<ZenMode?>(null)
    private val mutableZenMode = MutableStateFlow<ZenMode?>(ZenMode(Settings.Global.ZEN_MODE_OFF))
    override val zenMode: StateFlow<ZenMode?>
        get() = mutableZenMode.asStateFlow()

    init {
        updateNotificationPolicy()
    }

    fun updateNotificationPolicy(policy: NotificationManager.Policy?) {
        mutableNotificationPolicy.value = policy
    }
@@ -48,7 +53,8 @@ fun FakeNotificationsSoundPolicyRepository.updateNotificationPolicy(
    suppressedVisualEffects: Int = NotificationManager.Policy.SUPPRESSED_EFFECTS_UNSET,
    state: Int = NotificationManager.Policy.STATE_UNSET,
    priorityConversationSenders: Int = NotificationManager.Policy.CONVERSATION_SENDERS_NONE,
) = updateNotificationPolicy(
) =
    updateNotificationPolicy(
        NotificationManager.Policy(
            priorityCategories,
            priorityCallSenders,
+18 −8
Original line number Diff line number Diff line
@@ -72,7 +72,11 @@ interface AudioRepository {

    suspend fun setVolume(audioStream: AudioStream, volume: Int)

    suspend fun setMuted(audioStream: AudioStream, isMuted: Boolean)
    /**
     * Mutes and un-mutes [audioStream]. Returns true when the state changes and false the
     * otherwise.
     */
    suspend fun setMuted(audioStream: AudioStream, isMuted: Boolean): Boolean

    suspend fun setRingerMode(audioStream: AudioStream, mode: RingerMode)

@@ -164,13 +168,19 @@ class AudioRepositoryImpl(
            audioManager.setStreamVolume(audioStream.value, volume, 0)
        }

    override suspend fun setMuted(audioStream: AudioStream, isMuted: Boolean) =
        withContext(backgroundCoroutineContext) {
    override suspend fun setMuted(audioStream: AudioStream, isMuted: Boolean): Boolean {
        return withContext(backgroundCoroutineContext) {
            if (isMuted == audioManager.isStreamMute(audioStream.value)) {
                false
            } else {
                audioManager.adjustStreamVolume(
                    audioStream.value,
                    if (isMuted) AudioManager.ADJUST_MUTE else AudioManager.ADJUST_UNMUTE,
                    0,
                )
                true
            }
        }
    }

    override suspend fun setRingerMode(audioStream: AudioStream, mode: RingerMode) {
+20 −2
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import com.android.settingslib.volume.shared.model.RingerMode
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map

/** Provides audio stream state and an ability to change it */
@@ -46,8 +47,16 @@ class AudioVolumeInteractor(
    val ringerMode: StateFlow<RingerMode>
        get() = audioRepository.ringerMode

    suspend fun setVolume(audioStream: AudioStream, volume: Int) =
    suspend fun setVolume(audioStream: AudioStream, volume: Int) {
        val streamModel = getAudioStream(audioStream).first()
        val oldVolume = streamModel.volume
        audioRepository.setVolume(audioStream, volume)
        when {
            volume == streamModel.minVolume -> setMuted(audioStream, true)
            oldVolume == streamModel.minVolume && volume > streamModel.minVolume ->
                setMuted(audioStream, false)
        }
    }

    suspend fun setMuted(audioStream: AudioStream, isMuted: Boolean) {
        if (audioStream.value == AudioManager.STREAM_RING) {
@@ -55,7 +64,16 @@ class AudioVolumeInteractor(
                if (isMuted) AudioManager.RINGER_MODE_VIBRATE else AudioManager.RINGER_MODE_NORMAL
            audioRepository.setRingerMode(audioStream, RingerMode(mode))
        }
        audioRepository.setMuted(audioStream, isMuted)
        val mutedChanged = audioRepository.setMuted(audioStream, isMuted)
        if (mutedChanged && !isMuted) {
            with(getAudioStream(audioStream).first()) {
                if (volume == minVolume) {
                    // Slightly increase volume when user un-mutes the stream that is lowered
                    // down to its minimum
                    setVolume(audioStream, volume + 1)
                }
            }
        }
    }

    /** Checks if the volume can be changed via the UI. */
+1 −6
Original line number Diff line number Diff line
@@ -78,12 +78,7 @@ fun VolumeSlider(
                    }

                    state.a11yStateDescription?.let { stateDescription = it }
                        ?: run {
                            // provide a not animated value to the a11y because it fails to announce
                            // the settled value when it changes rapidly.
                            progressBarRangeInfo =
                                ProgressBarRangeInfo(state.value, state.valueRange)
                        }
                    progressBarRangeInfo = ProgressBarRangeInfo(state.value, state.valueRange)
                } else {
                    disabled()
                    contentDescription =
+51 −14
Original line number Diff line number Diff line
@@ -36,7 +36,6 @@ import com.google.common.truth.Truth.assertThat
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

@@ -47,19 +46,8 @@ class AudioVolumeInteractorTest : SysuiTestCase() {

    private val kosmos = testKosmos()

    private lateinit var underTest: AudioVolumeInteractor

    @Before
    fun setup() {
        with(kosmos) {
            underTest = AudioVolumeInteractor(audioRepository, notificationsSoundPolicyInteractor)

            audioRepository.setRingerMode(RingerMode(AudioManager.RINGER_MODE_NORMAL))

            notificationsSoundPolicyRepository.updateNotificationPolicy()
            notificationsSoundPolicyRepository.updateZenMode(ZenMode(Settings.Global.ZEN_MODE_OFF))
        }
    }
    private val underTest: AudioVolumeInteractor =
        with(kosmos) { AudioVolumeInteractor(audioRepository, notificationsSoundPolicyInteractor) }

    @Test
    fun setMuted_mutesStream() {
@@ -236,6 +224,55 @@ class AudioVolumeInteractorTest : SysuiTestCase() {
        }
    }

    @Test
    fun testReducingVolumeToMin_mutes() =
        with(kosmos) {
            testScope.runTest {
                val audioStreamModel by
                    collectLastValue(audioRepository.getAudioStream(audioStream))
                runCurrent()

                underTest.setVolume(audioStream, audioStreamModel!!.minVolume)
                runCurrent()

                assertThat(audioStreamModel!!.isMuted).isTrue()
            }
        }

    @Test
    fun testIncreasingVolumeFromMin_unmutes() =
        with(kosmos) {
            testScope.runTest {
                val audioStreamModel by
                    collectLastValue(audioRepository.getAudioStream(audioStream))
                audioRepository.setMuted(audioStream, true)
                audioRepository.setVolume(audioStream, audioStreamModel!!.minVolume)
                runCurrent()

                underTest.setVolume(audioStream, audioStreamModel!!.maxVolume)
                runCurrent()

                assertThat(audioStreamModel!!.isMuted).isFalse()
            }
        }

    @Test
    fun testUnmutingMinVolume_increasesVolume() =
        with(kosmos) {
            testScope.runTest {
                val audioStreamModel by
                    collectLastValue(audioRepository.getAudioStream(audioStream))
                audioRepository.setMuted(audioStream, true)
                audioRepository.setVolume(audioStream, audioStreamModel!!.minVolume)
                runCurrent()

                underTest.setMuted(audioStream, false)
                runCurrent()

                assertThat(audioStreamModel!!.volume).isGreaterThan(audioStreamModel!!.minVolume)
            }
        }

    private companion object {
        val audioStream = AudioStream(AudioManager.STREAM_SYSTEM)
    }
Loading