Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Setting to Toggle Volume Sliders #17

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright 2022-2023 mkckr0 <https://github.com/mkckr0>
* Copyright 2023 CamarataM <https://github.com/CamarataM>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -78,14 +79,14 @@ class HomeViewModel(private val application: Application) : AndroidViewModel(app

fun onWorkVolumeChange(value: Int) {
workVolume.value = value
if (isPlaying.value!!) {
if (sharedPreferences.getBoolean("audio_use_volume_sliders", true) && isPlaying.value!!) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, value, 0)
}
}

fun onIdleVolumeChange(value: Int) {
idleVolume.value = value
if (!isPlaying.value!!) {
if (sharedPreferences.getBoolean("audio_use_volume_sliders", true) && !isPlaying.value!!) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, value, 0)
}
}
Expand Down Expand Up @@ -136,12 +137,16 @@ class HomeViewModel(private val application: Application) : AndroidViewModel(app
override fun onAudioStop() {
isPlaying.value = false
info.value = application.getString(R.string.audio_stopped)
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, idleVolume.value!!, 0)
if (sharedPreferences.getBoolean("audio_use_volume_sliders", true)) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, idleVolume.value!!, 0)
}
}

override fun onAudioStart() {
info.value = application.getString(R.string.audio_started)
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, workVolume.value!!, 0)
if (sharedPreferences.getBoolean("audio_use_volume_sliders", true)) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, workVolume.value!!, 0)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright 2022-2023 mkckr0 <https://github.com/mkckr0>
* Copyright 2023 CamarataM <https://github.com/CamarataM>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -69,14 +70,18 @@ class HomeFragment : Fragment() {
binding.viewModel = viewModel
binding.lifecycleOwner = viewLifecycleOwner

val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())

binding.sliderWorkVolume.addOnSliderTouchListener(VolumeSliderTouchListener())
binding.sliderWorkVolume.addOnChangeListener { _, value, _ ->
viewModel.onWorkVolumeChange(value.toInt())
}
binding.sliderWorkVolume.isEnabled = sharedPreferences.getBoolean("audio_use_volume_sliders", true)
binding.sliderIdleVolume.addOnSliderTouchListener(VolumeSliderTouchListener())
binding.sliderIdleVolume.addOnChangeListener { _, value, _ ->
viewModel.onIdleVolumeChange(value.toInt())
}
binding.sliderIdleVolume.isEnabled = sharedPreferences.getBoolean("audio_use_volume_sliders", true)

viewModel.hostError.observe(viewLifecycleOwner) { binding.textFieldHostLayout.error = it }
viewModel.portError.observe(viewLifecycleOwner) { binding.textFieldPortLayout.error = it }
Expand Down Expand Up @@ -118,4 +123,4 @@ class HomeFragment : Fragment() {
override fun onStartTrackingTouch(slider: Slider) {}
override fun onStopTrackingTouch(slider: Slider) { viewModel.saveVolume() }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright 2022-2023 mkckr0 <https://github.com/mkckr0>
* Copyright 2023 CamarataM <https://github.com/CamarataM>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,6 +58,9 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
setSummaryProvider { "${(it as EditTextPreference).text}ms" }
}
findPreference<SwitchPreference>("audio_use_volume_sliders")!!.apply {
isEnabled = true
}

updateRequestIgnoreBatteryOptimizations()

Expand Down Expand Up @@ -118,4 +122,4 @@ class SettingsFragment : PreferenceFragmentCompat() {
preference.summary = newSummary
return changed
}
}
}
5 changes: 5 additions & 0 deletions android-app/app/src/main/res/drawable/baseline_volume_up.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:autoMirrored="true" android:height="36dp"
android:tint="#000000" android:viewportHeight="24"
android:viewportWidth="24" android:width="36dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M3,9v6h4l5,5L12,4L7,9L3,9zM16.5,12c0,-1.77 -1.02,-3.29 -2.5,-4.03v8.05c1.48,-0.73 2.5,-2.25 2.5,-4.02zM14,3.23v2.06c2.89,0.86 5,3.54 5,6.71s-2.11,5.85 -5,6.71v2.06c4.01,-0.91 7,-4.49 7,-8.77s-2.99,-7.86 -7,-8.77z"/>
</vector>
10 changes: 9 additions & 1 deletion android-app/app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<!--
Copyright 2022-2023 mkckr0 <https://github.com/mkckr0>
Copyright 2023 CamarataM <https://github.com/CamarataM>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,6 +32,13 @@
app:icon="@drawable/baseline_timer_3"
app:defaultValue="@string/audio_tcp_connect_timeout"
tools:summary="3000ms" />
<SwitchPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:defaultValue="true"
android:title="Use Volume Sliders"
app:icon="@drawable/baseline_volume_up"
app:key="audio_use_volume_sliders" />
</PreferenceCategory>

<PreferenceCategory app:title="Battery Optimization">
Expand Down Expand Up @@ -94,4 +102,4 @@
app:summary="0.0.1"
app:icon="@drawable/baseline_info"/>
</PreferenceCategory>
</PreferenceScreen>
</PreferenceScreen>