From fb965f53764a79fe5dd87f97388d8695ca1f751e Mon Sep 17 00:00:00 2001
From: Anil Kumar Beesetti <66936649+anilbeesetti@users.noreply.github.com>
Date: Mon, 4 Sep 2023 13:29:29 +0530
Subject: [PATCH] Feat: Show system volume panel while adjusting volume with
headset on (#562)
* show volume panel when connected to headset
* feat: add show system volume panel preference
* lint: run KtlintFormat
---
.../nextplayer/core/model/PlayerPreferences.kt | 1 +
.../core/ui/designsystem/NextIcons.kt | 2 ++
core/ui/src/main/res/values/strings.xml | 4 +++-
.../nextplayer/feature/player/PlayerActivity.kt | 4 ++--
.../feature/player/utils/PlayerGestureHelper.kt | 2 +-
.../feature/player/utils/VolumeManager.kt | 17 +++++++++++------
.../screens/audio/AudioPreferencesScreen.kt | 17 +++++++++++++++++
.../screens/audio/AudioPreferencesViewModel.kt | 8 ++++++++
8 files changed, 45 insertions(+), 10 deletions(-)
diff --git a/core/model/src/main/java/dev/anilbeesetti/nextplayer/core/model/PlayerPreferences.kt b/core/model/src/main/java/dev/anilbeesetti/nextplayer/core/model/PlayerPreferences.kt
index dd7d379bd..04a95cfed 100644
--- a/core/model/src/main/java/dev/anilbeesetti/nextplayer/core/model/PlayerPreferences.kt
+++ b/core/model/src/main/java/dev/anilbeesetti/nextplayer/core/model/PlayerPreferences.kt
@@ -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 = "",
diff --git a/core/ui/src/main/java/dev/anilbeesetti/nextplayer/core/ui/designsystem/NextIcons.kt b/core/ui/src/main/java/dev/anilbeesetti/nextplayer/core/ui/designsystem/NextIcons.kt
index c609851ae..6d0f04ce5 100644
--- a/core/ui/src/main/java/dev/anilbeesetti/nextplayer/core/ui/designsystem/NextIcons.kt
+++ b/core/ui/src/main/java/dev/anilbeesetti/nextplayer/core/ui/designsystem/NextIcons.kt
@@ -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
@@ -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
diff --git a/core/ui/src/main/res/values/strings.xml b/core/ui/src/main/res/values/strings.xml
index b461ca644..b34dfe658 100644
--- a/core/ui/src/main/res/values/strings.xml
+++ b/core/ui/src/main/res/values/strings.xml
@@ -152,6 +152,8 @@
High contrast dark theme
Use pure black background for dark theme
Audio playback options
- Pause on headset disconnect
+ Pause on disconnect
Pause playback when headset disconnected from the device
+ System volume panel
+ Show system volume panel while adjusting volume with headset on
\ No newline at end of file
diff --git a/feature/player/src/main/java/dev/anilbeesetti/nextplayer/feature/player/PlayerActivity.kt b/feature/player/src/main/java/dev/anilbeesetti/nextplayer/feature/player/PlayerActivity.kt
index cb3b3e1ca..17dec1be4 100644
--- a/feature/player/src/main/java/dev/anilbeesetti/nextplayer/feature/player/PlayerActivity.kt
+++ b/feature/player/src/main/java/dev/anilbeesetti/nextplayer/feature/player/PlayerActivity.kt
@@ -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
}
diff --git a/feature/player/src/main/java/dev/anilbeesetti/nextplayer/feature/player/utils/PlayerGestureHelper.kt b/feature/player/src/main/java/dev/anilbeesetti/nextplayer/feature/player/utils/PlayerGestureHelper.kt
index abf9c14b9..835e93bfa 100644
--- a/feature/player/src/main/java/dev/anilbeesetti/nextplayer/feature/player/utils/PlayerGestureHelper.kt
+++ b/feature/player/src/main/java/dev/anilbeesetti/nextplayer/feature/player/utils/PlayerGestureHelper.kt
@@ -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
diff --git a/feature/player/src/main/java/dev/anilbeesetti/nextplayer/feature/player/utils/VolumeManager.kt b/feature/player/src/main/java/dev/anilbeesetti/nextplayer/feature/player/utils/VolumeManager.kt
index eae3af9f0..876db8909 100644
--- a/feature/player/src/main/java/dev/anilbeesetti/nextplayer/feature/player/utils/VolumeManager.kt
+++ b/feature/player/src/main/java/dev/anilbeesetti/nextplayer/feature/player/utils/VolumeManager.kt
@@ -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 {
diff --git a/feature/settings/src/main/java/dev/anilbeesetti/nextplayer/settings/screens/audio/AudioPreferencesScreen.kt b/feature/settings/src/main/java/dev/anilbeesetti/nextplayer/settings/screens/audio/AudioPreferencesScreen.kt
index 2fd689eff..f2627696b 100644
--- a/feature/settings/src/main/java/dev/anilbeesetti/nextplayer/settings/screens/audio/AudioPreferencesScreen.kt
+++ b/feature/settings/src/main/java/dev/anilbeesetti/nextplayer/settings/screens/audio/AudioPreferencesScreen.kt
@@ -69,6 +69,10 @@ fun AudioPreferencesScreen(
isChecked = preferences.pauseOnHeadsetDisconnect,
onClick = viewModel::togglePauseOnHeadsetDisconnect
)
+ showSystemVolumePanelSetting(
+ isChecked = preferences.showSystemVolumePanel,
+ onClick = viewModel::toggleShowSystemVolumePanel
+ )
}
uiState.showDialog?.let { showDialog ->
@@ -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
+ )
+}
diff --git a/feature/settings/src/main/java/dev/anilbeesetti/nextplayer/settings/screens/audio/AudioPreferencesViewModel.kt b/feature/settings/src/main/java/dev/anilbeesetti/nextplayer/settings/screens/audio/AudioPreferencesViewModel.kt
index 7560501df..1b5eb27a7 100644
--- a/feature/settings/src/main/java/dev/anilbeesetti/nextplayer/settings/screens/audio/AudioPreferencesViewModel.kt
+++ b/feature/settings/src/main/java/dev/anilbeesetti/nextplayer/settings/screens/audio/AudioPreferencesViewModel.kt
@@ -50,6 +50,14 @@ class AudioPreferencesViewModel @Inject constructor(
}
}
}
+
+ fun toggleShowSystemVolumePanel() {
+ viewModelScope.launch {
+ preferencesRepository.updatePlayerPreferences {
+ it.copy(showSystemVolumePanel = !it.showSystemVolumePanel)
+ }
+ }
+ }
}
data class AudioPreferencesUIState(