Skip to content

Commit 0cdb431

Browse files
committed
Actions on output device changed
1 parent 5ef994d commit 0cdb431

8 files changed

Lines changed: 50 additions & 6 deletions

File tree

app/build.gradle.kts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ android {
1414
applicationId = "org.koitharu.volumeicon"
1515
minSdk = 24
1616
targetSdk = 36
17-
versionCode = 3
18-
versionName = "1.2"
17+
versionCode = 4
18+
versionName = "1.3"
1919
}
2020

2121
lint {
@@ -33,6 +33,9 @@ android {
3333
sourceCompatibility = JavaVersion.VERSION_11
3434
targetCompatibility = JavaVersion.VERSION_11
3535
}
36+
dependenciesInfo {
37+
includeInApk = false
38+
}
3639
}
3740

3841
dependencies {

app/src/main/kotlin/org/koitharu/volumeicon/VolumeIconService.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class VolumeIconService : AccessibilityService() {
115115
val outputDevice = audioManager.getOutputDevice()
116116
if (currentDevice != outputDevice) {
117117
if (currentDevice != null) {
118-
toastFactory.createDeviceToast(outputDevice).show()
118+
handleDeviceChanged(outputDevice)
119119
}
120120
currentDevice = outputDevice
121121
}
@@ -133,6 +133,16 @@ class VolumeIconService : AccessibilityService() {
133133
}
134134
}
135135

136+
private fun handleDeviceChanged(outputDevice: OutputDevice) {
137+
val actions = settings.onDeviceChangedActions
138+
if (AppSettings.VALUE_SHOW_UI in actions) {
139+
audioManager.adjustVolume(AudioManager.ADJUST_SAME, AudioManager.FLAG_SHOW_UI)
140+
}
141+
if (AppSettings.VALUE_SHOW_TOAST in actions) {
142+
toastFactory.createDeviceToast(outputDevice).show()
143+
}
144+
}
145+
136146
@SuppressLint("WrongConstant")
137147
private fun handleVolumeButton(action: Int, isUp: Boolean): Boolean {
138148
if (audioManager.mode != AudioManager.MODE_NORMAL) {

app/src/main/kotlin/org/koitharu/volumeicon/config/AppSettings.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class AppSettings(context: Context) {
3636
val hideVolumeUi: Boolean
3737
get() = prefs.getBoolean(KEY_HIDE_UI, false)
3838

39+
val onDeviceChangedActions: Set<String>
40+
get() = prefs.getStringSet(KEY_DEVICE_CHANGE_ACTIONS, emptySet()).orEmpty()
41+
3942
fun doOnSettingsChanged(keys: Set<String>, block: AppSettings.() -> Unit): AutoCloseable {
4043
val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
4144
if (key in keys) {
@@ -56,5 +59,9 @@ class AppSettings(context: Context) {
5659
const val KEY_SYSTEM_NOTIFICATIONS_SETTINGS = "system_notifications_settings"
5760
const val KEY_BEEPS = "beeps"
5861
const val KEY_HIDE_UI = "no_ui"
62+
const val KEY_DEVICE_CHANGE_ACTIONS = "device_change_actions"
63+
64+
const val VALUE_SHOW_TOAST = "toast"
65+
const val VALUE_SHOW_UI = "volume_ui"
5966
}
6067
}

app/src/main/kotlin/org/koitharu/volumeicon/config/SettingsActivity.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.annotation.SuppressLint
66
import android.content.SharedPreferences
77
import android.os.Bundle
88
import android.preference.ListPreference
9+
import android.preference.MultiSelectListPreference
910
import android.preference.Preference
1011
import android.preference.PreferenceActivity
1112
import android.preference.PreferenceGroup
@@ -66,13 +67,20 @@ class SettingsActivity : PreferenceActivity(), SharedPreferences.OnSharedPrefere
6667

6768
private fun Preference.bindSummary() {
6869
when (this) {
70+
is PreferenceGroup -> repeat(preferenceCount) { i ->
71+
getPreference(i).bindSummary()
72+
}
73+
6974
is ListPreference -> {
7075
val valueIndex = entryValues.indexOf(value)
7176
summary = entries.getOrNull(valueIndex)
7277
}
7378

74-
is PreferenceGroup -> repeat(preferenceCount) { i ->
75-
getPreference(i).bindSummary()
79+
is MultiSelectListPreference -> {
80+
summary = values.mapNotNull { value ->
81+
val valueIndex = entryValues.indexOf(value)
82+
entries.getOrNull(valueIndex)
83+
}.joinToString().ifEmpty { getString(R.string.none) }
7684
}
7785
}
7886
}

app/src/main/res/values/arrays.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,12 @@
3232
<item>ENABLED</item>
3333
<item>DISABLED</item>
3434
</string-array>
35+
<string-array name="device_change_actions">
36+
<item>@string/show_popup_message</item>
37+
<item>@string/show_volume_control</item>
38+
</string-array>
39+
<string-array name="device_change_action_values">
40+
<item>toast</item>
41+
<item>volume_ui</item>
42+
</string-array>
3543
</resources>

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@
3131
<string name="headphones">Headphones</string>
3232
<string name="builtin_speaker">Built-in speaker</string>
3333
<string name="other_device">Other device</string>
34-
<string name="output_label_template">%1$s (%2$s)</string>
34+
<string name="output_label_template" translatable="false">%1$s (%2$s)</string>
35+
<string name="show_popup_message">Show pop-up message</string>
36+
<string name="show_volume_control">Show volume slider</string>
37+
<string name="none">None</string>
3538
</resources>

app/src/main/res/xml/pref_root.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,10 @@
3333
android:key="no_ui"
3434
android:summary="@string/hide_volume_slider_description"
3535
android:title="@string/hide_volume_slider" />
36+
<MultiSelectListPreference
37+
android:entries="@array/device_change_actions"
38+
android:entryValues="@array/device_change_action_values"
39+
android:key="device_change_actions"
40+
android:title="When output device is changed" />
3641
</PreferenceCategory>
3742
</PreferenceScreen>
-73 KB
Loading

0 commit comments

Comments
 (0)