Skip to content

Commit a89f1a7

Browse files
committed
Add option for key release
1 parent 2eec550 commit a89f1a7

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

app/src/main/java/dev/fabik/bluetoothhid/Scanner.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -527,20 +527,22 @@ private fun BoxScope.BarcodeValue(currentBarcode: String?) {
527527
private fun VolumeKeyHandler(onPress: (Int) -> Boolean) {
528528
val context = LocalContext.current
529529
val sendWithVolume by context.getPreferenceState(PreferenceStore.SEND_WITH_VOLUME)
530+
val triggerOnRelease by context.getPreferenceState(PreferenceStore.VOLUME_ON_RELEASE)
530531

531532
if (sendWithVolume == true) {
532533
val view = LocalView.current
533534

534535
DisposableEffect(context) {
535536
val keyEventDispatcher = ViewCompat.OnUnhandledKeyEventListenerCompat { _, event ->
536537
if (event.keyCode == KeyEvent.KEYCODE_VOLUME_UP || event.keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
537-
if (event.action == KeyEvent.ACTION_UP)
538-
onPress(event.keyCode)
539-
else
540-
true
541-
} else {
542-
false
538+
if (event.repeatCount == 0
539+
&& (event.action == KeyEvent.ACTION_UP && triggerOnRelease == true
540+
|| event.action == KeyEvent.ACTION_DOWN && triggerOnRelease == false)
541+
)
542+
return@OnUnhandledKeyEventListenerCompat onPress(event.keyCode)
543+
return@OnUnhandledKeyEventListenerCompat true
543544
}
545+
false
544546
}
545547

546548
ViewCompat.addOnUnhandledKeyEventListener(view, keyEventDispatcher)

app/src/main/java/dev/fabik/bluetoothhid/ui/VolumeKeyOptions.kt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ import androidx.compose.runtime.saveable.rememberSaveable
2828
import androidx.compose.runtime.setValue
2929
import androidx.compose.ui.Alignment
3030
import androidx.compose.ui.Modifier
31+
import androidx.compose.ui.res.stringArrayResource
32+
import androidx.compose.ui.res.stringResource
3133
import androidx.compose.ui.semantics.semantics
3234
import androidx.compose.ui.semantics.stateDescription
3335
import androidx.compose.ui.tooling.preview.Preview
3436
import androidx.compose.ui.unit.dp
37+
import dev.fabik.bluetoothhid.R
3538
import dev.fabik.bluetoothhid.utils.PreferenceStore
3639
import dev.fabik.bluetoothhid.utils.rememberPreference
3740
import dev.fabik.bluetoothhid.utils.rememberPreferenceNull
@@ -45,8 +48,8 @@ fun VolumeKeyOptionsModal() {
4548
var showSheet by rememberSaveable { mutableStateOf(false) }
4649

4750
ButtonPreference(
48-
title = "Use volume keys",
49-
desc = "Specify action when pressing volume up/down",
51+
title = stringResource(R.string.send_with_volume_keys),
52+
desc = stringResource(R.string.send_vol_keys_desc),
5053
icon = Icons.AutoMirrored.Filled.VolumeMute,
5154
onClick = { showSheet = true },
5255
extra = {
@@ -90,36 +93,33 @@ fun VolumeKeysOptionsContent() {
9093
.padding(horizontal = 16.dp)
9194
) {
9295
Text(
93-
"Volume keys",
96+
stringResource(R.string.send_with_volume_keys),
9497
style = MaterialTheme.typography.titleLarge,
9598
)
9699

97-
val actions = arrayOf(
98-
"Nothing",
99-
"Send value",
100-
"Clear value",
101-
"Run OCR",
102-
"Open manual input",
103-
"Toggle flash",
104-
"Toggle zoom",
105-
"Trigger focus"
106-
)
100+
val actions = stringArrayResource(R.array.volume_keys_actions)
107101

108102
AdvancedEnumSelectionOption(
109-
"Volume up",
103+
stringResource(R.string.volume_up),
110104
actions,
111105
PreferenceStore.VOLUME_ACTION_UP
112106
)
113107

114108
AdvancedEnumSelectionOption(
115-
"Volume down",
116-
actions,
117-
PreferenceStore.VOLUME_ACTION_DOWN
109+
text = stringResource(R.string.volume_down),
110+
values = actions,
111+
preference = PreferenceStore.VOLUME_ACTION_DOWN
118112
)
119113

120-
AdvancedSliderOption("Zoom level (%)", 0f to 100f, PreferenceStore.VOLUME_ZOOM_LEVEL)
121-
122-
//AdvancedToggleOption("Trigger on key release")
114+
AdvancedSliderOption(
115+
stringResource(R.string.zoom_level),
116+
0f to 100f,
117+
PreferenceStore.VOLUME_ZOOM_LEVEL
118+
)
119+
AdvancedToggleOption(
120+
stringResource(R.string.trigger_on_key_release),
121+
PreferenceStore.VOLUME_ON_RELEASE
122+
)
123123

124124
Spacer(Modifier.height(12.dp))
125125
}

app/src/main/java/dev/fabik/bluetoothhid/utils/PreferenceStore.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ open class PreferenceStore {
189189
intPreferencesKey("vol_action_up") enumDefaultsTo VolumeKeyAction::fromIndex
190190
val VOLUME_ACTION_DOWN =
191191
intPreferencesKey("vol_action_down") enumDefaultsTo VolumeKeyAction::fromIndex
192+
val VOLUME_ON_RELEASE = booleanPreferencesKey("vol_on_release") defaultsTo false
192193
val VOLUME_ZOOM_LEVEL = floatPreferencesKey("vol_action_zoom") defaultsTo 1f
193194
val SEND_DELAY = floatPreferencesKey("send_delay") defaultsTo 10f
194195
val KEYBOARD_LAYOUT = intPreferencesKey("keyboard_layout").enumDefaultsTo(KeyboardLayout::fromIndex)

0 commit comments

Comments
 (0)