Skip to content

Commit

Permalink
Feat: Show system volume panel while adjusting volume with headset on (
Browse files Browse the repository at this point in the history
…#562)

* show volume panel when connected to headset

* feat: add show system volume panel preference

* lint: run KtlintFormat
  • Loading branch information
anilbeesetti authored Sep 4, 2023
1 parent 146a452 commit fb965f5
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ data class PlayerPreferences(
// Audio Preferences
val preferredAudioLanguage: String = "",
val pauseOnHeadsetDisconnect: Boolean = true,
val showSystemVolumePanel: Boolean = true,

// Subtitle Preferences
val preferredSubtitleLanguage: String = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.compose.material.icons.rounded.FolderOff
import androidx.compose.material.icons.rounded.FontDownload
import androidx.compose.material.icons.rounded.FormatBold
import androidx.compose.material.icons.rounded.FormatSize
import androidx.compose.material.icons.rounded.Headset
import androidx.compose.material.icons.rounded.HeadsetOff
import androidx.compose.material.icons.rounded.Info
import androidx.compose.material.icons.rounded.Link
Expand Down Expand Up @@ -74,6 +75,7 @@ object NextIcons {
val FolderOff = Icons.Rounded.FolderOff
val Font = Icons.Rounded.FontDownload
val FontSize = Icons.Rounded.FormatSize
val Headset = Icons.Rounded.Headset
val HeadsetOff = Icons.Rounded.HeadsetOff
val Info = Icons.Rounded.Info
val Language = Icons.Rounded.Translate
Expand Down
4 changes: 3 additions & 1 deletion core/ui/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@
<string name="high_contrast_dark_theme">High contrast dark theme</string>
<string name="high_contrast_dark_theme_desc">Use pure black background for dark theme</string>
<string name="audio_desc">Audio playback options</string>
<string name="pause_on_headset_disconnect">Pause on headset disconnect</string>
<string name="pause_on_headset_disconnect">Pause on disconnect</string>
<string name="pause_on_headset_disconnect_desc">Pause playback when headset disconnected from the device</string>
<string name="system_volume_panel">System volume panel</string>
<string name="system_volume_panel_desc">Show system volume panel while adjusting volume with headset on</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -645,13 +645,13 @@ class PlayerActivity : AppCompatActivity() {
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
when (keyCode) {
KeyEvent.KEYCODE_VOLUME_UP -> {
volumeManager.increaseVolume()
volumeManager.increaseVolume(playerPreferences.showSystemVolumePanel)
showVolumeGestureLayout()
return true
}

KeyEvent.KEYCODE_VOLUME_DOWN -> {
volumeManager.decreaseVolume()
volumeManager.decreaseVolume(playerPreferences.showSystemVolumePanel)
showVolumeGestureLayout()
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class PlayerGestureHelper(

if (firstEvent.x.toInt() > viewCenterX) {
val change = ratioChange * volumeManager.maxStreamVolume
volumeManager.setVolume(volumeManager.currentVolume + change)
volumeManager.setVolume(volumeManager.currentVolume + change, prefs.showSystemVolumePanel)
activity.showVolumeGestureLayout()
} else {
val change = ratioChange * brightnessManager.maxBrightness
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,29 @@ class VolumeManager(private val audioManager: AudioManager) {
val currentLoudnessGain get() = (currentVolume - maxStreamVolume) * (MAX_VOLUME_BOOST / maxStreamVolume)
val volumePercentage get() = (currentVolume / maxStreamVolume.toFloat()).times(100).toInt()

fun setVolume(volume: Float) {
@Suppress("DEPRECATION")
fun setVolume(volume: Float, showVolumePanel: Boolean = false) {
currentVolume = volume.coerceIn(0f, maxVolume.toFloat())

if (currentVolume <= maxStreamVolume) {
loudnessEnhancer?.enabled = false
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume.toInt(), 0)
audioManager.setStreamVolume(
AudioManager.STREAM_MUSIC,
currentVolume.toInt(),
if (showVolumePanel && audioManager.isWiredHeadsetOn) AudioManager.FLAG_SHOW_UI else 0
)
} else {
loudnessEnhancer?.enabled = true
loudnessEnhancer?.setTargetGain(currentLoudnessGain.toInt())
}
}

fun increaseVolume() {
setVolume(currentVolume + 1)
fun increaseVolume(showVolumePanel: Boolean = false) {
setVolume(currentVolume + 1, showVolumePanel)
}

fun decreaseVolume() {
setVolume(currentVolume - 1)
fun decreaseVolume(showVolumePanel: Boolean = false) {
setVolume(currentVolume - 1, showVolumePanel)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ fun AudioPreferencesScreen(
isChecked = preferences.pauseOnHeadsetDisconnect,
onClick = viewModel::togglePauseOnHeadsetDisconnect
)
showSystemVolumePanelSetting(
isChecked = preferences.showSystemVolumePanel,
onClick = viewModel::toggleShowSystemVolumePanel
)
}

uiState.showDialog?.let { showDialog ->
Expand Down Expand Up @@ -121,3 +125,16 @@ fun LazyListScope.pauseOnHeadsetDisconnectSetting(
onClick = onClick
)
}

fun LazyListScope.showSystemVolumePanelSetting(
isChecked: Boolean,
onClick: () -> Unit
) = item {
PreferenceSwitch(
title = stringResource(id = R.string.system_volume_panel),
description = stringResource(id = R.string.system_volume_panel_desc),
icon = NextIcons.Headset,
isChecked = isChecked,
onClick = onClick
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ class AudioPreferencesViewModel @Inject constructor(
}
}
}

fun toggleShowSystemVolumePanel() {
viewModelScope.launch {
preferencesRepository.updatePlayerPreferences {
it.copy(showSystemVolumePanel = !it.showSystemVolumePanel)
}
}
}
}

data class AudioPreferencesUIState(
Expand Down

0 comments on commit fb965f5

Please sign in to comment.