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

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

Merge "Conflate volume changes to reduce pressure on the AudioManager" into main

parents 3368734b 82138cb4
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterIsInstance
@@ -161,6 +162,7 @@ class AudioRepositoryImpl(
                },
                volumeSettingChanges(audioStream),
            )
            .conflate()
            .map { getCurrentAudioStream(audioStream) }
            .onStart { emit(getCurrentAudioStream(audioStream)) }
            .flowOn(backgroundCoroutineContext)
@@ -184,10 +186,11 @@ class AudioRepositoryImpl(
        }
    }

    override suspend fun setVolume(audioStream: AudioStream, volume: Int) =
    override suspend fun setVolume(audioStream: AudioStream, volume: Int) {
        withContext(backgroundCoroutineContext) {
            audioManager.setStreamVolume(audioStream.value, volume, 0)
        }
    }

    override suspend fun setMuted(audioStream: AudioStream, isMuted: Boolean): Boolean {
        return withContext(backgroundCoroutineContext) {
+13 −3
Original line number Diff line number Diff line
@@ -32,9 +32,13 @@ import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import kotlin.math.roundToInt
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch

@@ -49,6 +53,7 @@ constructor(
    private val uiEventLogger: UiEventLogger,
) : SliderViewModel {

    private val volumeChanges = MutableStateFlow<Int?>(null)
    private val streamsAffectedByRing =
        setOf(
            AudioManager.STREAM_RING,
@@ -104,12 +109,17 @@ constructor(
            }
            .stateIn(coroutineScope, SharingStarted.Eagerly, SliderState.Empty)

    init {
        volumeChanges
            .filterNotNull()
            .onEach { audioVolumeInteractor.setVolume(audioStream, it) }
            .launchIn(coroutineScope)
    }

    override fun onValueChanged(state: SliderState, newValue: Float) {
        val audioViewModel = state as? State
        audioViewModel ?: return
        coroutineScope.launch {
            audioVolumeInteractor.setVolume(audioStream, newValue.roundToInt())
        }
        volumeChanges.tryEmit(newValue.roundToInt())
    }

    override fun onValueChangeFinished() {