From f3a37177d3769a28acf15d17e584fd342b88ff91 Mon Sep 17 00:00:00 2001 From: OwenMcGirr Date: Wed, 3 Jan 2024 13:17:59 +0000 Subject: [PATCH] Implement press, and hold switch actions --- .../preferences/PreferenceManager.kt | 1 + .../screens/settings/SettingsScreen.kt | 10 ++++++ .../settings/models/SettlingsScreenModel.kt | 8 +++++ .../switchify/service/cursor/CursorManager.kt | 2 +- .../service/scanning/ScanningManager.kt | 29 +++++++++++++++ .../service/scanning/SwitchListener.kt | 36 +++++++++++++++---- .../switchify/switches/SwitchAction.kt | 7 ++-- .../switchify/switches/SwitchEvent.kt | 21 +++++------ .../widgets/PreferenceTimeStepper.kt | 2 +- 9 files changed, 93 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/enaboapps/switchify/preferences/PreferenceManager.kt b/app/src/main/java/com/enaboapps/switchify/preferences/PreferenceManager.kt index c6c1fa3b..f681392e 100644 --- a/app/src/main/java/com/enaboapps/switchify/preferences/PreferenceManager.kt +++ b/app/src/main/java/com/enaboapps/switchify/preferences/PreferenceManager.kt @@ -7,6 +7,7 @@ class PreferenceManager(context: Context) { companion object Keys { const val PREFERENCE_KEY_SCAN_RATE = "scan_rate" + const val PREFERENCE_KEY_SWITCH_HOLD_TIME = "switch_hold_time" const val PREFERENCE_KEY_AUTO_SELECT = "auto_select" const val PREFERENCE_KEY_AUTO_SELECT_DELAY = "auto_select_delay" private const val PREFERENCE_FILE_NAME = "switchify_preferences" diff --git a/app/src/main/java/com/enaboapps/switchify/screens/settings/SettingsScreen.kt b/app/src/main/java/com/enaboapps/switchify/screens/settings/SettingsScreen.kt index 22c59ccc..99b83133 100644 --- a/app/src/main/java/com/enaboapps/switchify/screens/settings/SettingsScreen.kt +++ b/app/src/main/java/com/enaboapps/switchify/screens/settings/SettingsScreen.kt @@ -67,6 +67,16 @@ private fun TimingSection(settingsScreenModel: SettingsScreenModel) { settingsScreenModel.setScanRate(it) } ) + PreferenceTimeStepper( + value = settingsScreenModel.getSwitchHoldTime(), + title = "Switch hold time", + summary = "The time to hold the switch before the long pressed action is triggered", + min = 100, + max = 100000, + onValueChanged = { + settingsScreenModel.setSwitchHoldTime(it) + } + ) } } diff --git a/app/src/main/java/com/enaboapps/switchify/screens/settings/models/SettlingsScreenModel.kt b/app/src/main/java/com/enaboapps/switchify/screens/settings/models/SettlingsScreenModel.kt index 75753a2b..ab780043 100644 --- a/app/src/main/java/com/enaboapps/switchify/screens/settings/models/SettlingsScreenModel.kt +++ b/app/src/main/java/com/enaboapps/switchify/screens/settings/models/SettlingsScreenModel.kt @@ -15,6 +15,14 @@ class SettingsScreenModel(context: Context) : ViewModel() { preferenceManager.setIntegerValue(PreferenceManager.Keys.PREFERENCE_KEY_SCAN_RATE, rate) } + fun getSwitchHoldTime(): Int { + return preferenceManager.getIntegerValue(PreferenceManager.Keys.PREFERENCE_KEY_SWITCH_HOLD_TIME) + } + + fun setSwitchHoldTime(time: Int) { + preferenceManager.setIntegerValue(PreferenceManager.Keys.PREFERENCE_KEY_SWITCH_HOLD_TIME, time) + } + fun getAutoSelect(): Boolean { return preferenceManager.getBooleanValue(PreferenceManager.Keys.PREFERENCE_KEY_AUTO_SELECT) } diff --git a/app/src/main/java/com/enaboapps/switchify/service/cursor/CursorManager.kt b/app/src/main/java/com/enaboapps/switchify/service/cursor/CursorManager.kt index a9a32ea1..17086b3d 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/cursor/CursorManager.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/cursor/CursorManager.kt @@ -159,7 +159,7 @@ class CursorManager(private val context: Context) { // Function to stop the timer - private fun stop() { + fun stop() { movingTimer?.cancel() movingTimer = null } diff --git a/app/src/main/java/com/enaboapps/switchify/service/scanning/ScanningManager.kt b/app/src/main/java/com/enaboapps/switchify/service/scanning/ScanningManager.kt index 9bbe1ddd..eb0633c3 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/scanning/ScanningManager.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/scanning/ScanningManager.kt @@ -8,6 +8,7 @@ import com.enaboapps.switchify.service.menu.MenuView import com.enaboapps.switchify.service.menu.MenuViewListener import com.enaboapps.switchify.service.menu.menus.MainMenu import com.enaboapps.switchify.service.menu.menus.SystemControlMenu +import com.enaboapps.switchify.switches.SwitchAction class ScanningManager( private val accessibilityService: SwitchifyAccessibilityService, @@ -60,4 +61,32 @@ class ScanningManager( } } } + + + // This function performs an action + fun performAction(action: SwitchAction) { + when (action.id) { + SwitchAction.Actions.ACTION_NONE -> { + // do nothing + } + + SwitchAction.Actions.ACTION_SELECT -> { + select() + } + + SwitchAction.Actions.ACTION_STOP_SCANNING -> { + when (state) { + State.CURSOR -> { + // Stop scanning + cursorManager.stop() + } + + State.MENU -> { + // Stop scanning + MenuManager.getInstance().currentMenu?.close() + } + } + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/enaboapps/switchify/service/scanning/SwitchListener.kt b/app/src/main/java/com/enaboapps/switchify/service/scanning/SwitchListener.kt index e4372176..db909e34 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/scanning/SwitchListener.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/scanning/SwitchListener.kt @@ -3,6 +3,8 @@ package com.enaboapps.switchify.service.scanning import android.content.Context import android.util.Log import android.widget.Toast +import com.enaboapps.switchify.preferences.PreferenceManager +import com.enaboapps.switchify.switches.SwitchAction import com.enaboapps.switchify.switches.SwitchEvent import com.enaboapps.switchify.switches.SwitchEventStore @@ -11,18 +13,26 @@ class SwitchListener( private val scanningManager: ScanningManager ) { + private val preferenceManager = PreferenceManager(context) + private val switchEventStore = SwitchEventStore(context) // Variable to track the latest action - private var latestAction: SwitchAction? = null + private var latestAction: AbsorbedSwitchAction? = null // This function is called when the switch is pressed // It takes in the key code, checks if it is a switch event, and if it is, it updates the latest action + // If there is no long press action, it calls the press action on the scanning manager fun onSwitchPressed(keyCode: Int): Boolean { val switchEvent = switchEventStore.find(keyCode.toString()) Log.d("SwitchListener", "onSwitchPressed: $keyCode") if (switchEvent != null) { - latestAction = SwitchAction(switchEvent, System.currentTimeMillis()) + latestAction = AbsorbedSwitchAction(switchEvent, System.currentTimeMillis()) + + // If there is no long press action, it calls the press action on the scanning manager + if (switchEvent.longPressAction.id == SwitchAction.Actions.ACTION_NONE) { + scanningManager.performAction(switchEvent.pressAction) + } } else { Log.d("SwitchListener", "No switch event found for key code $keyCode") } @@ -31,16 +41,28 @@ class SwitchListener( // This function is called when the switch is released // It takes in the key code, checks if it is a switch event, and if it is, it checks if the latest action is the same as the switch event - // If it is, it calls the select function on the scanning manager + // If it is, it checks if there is a long press action, and if there is, it checks if the time between the press and release is greater than the switch hold time + // If it is, it calls the long press action on the scanning manager + // If it isn't, it calls the press action on the scanning manager fun onSwitchReleased(keyCode: Int): Boolean { val switchEvent = switchEventStore.find(keyCode.toString()) Log.d("SwitchListener", "onSwitchReleased: $keyCode") if (switchEvent != null) { if (latestAction?.switchEvent == switchEvent) { - scanningManager.select() + if (switchEvent.longPressAction.id != SwitchAction.Actions.ACTION_NONE) { + val time = System.currentTimeMillis() - latestAction!!.time + val switchHoldTime = preferenceManager.getIntegerValue(PreferenceManager.PREFERENCE_KEY_SWITCH_HOLD_TIME) + if (time > switchHoldTime) { + scanningManager.performAction(switchEvent.longPressAction) + } else { + scanningManager.performAction(switchEvent.pressAction) + } + } else { + // do nothing + } + } else { + Log.d("SwitchListener", "Switch event does not match latest action") } - } else { - Toast.makeText(context, "No switch event found for key code $keyCode", Toast.LENGTH_SHORT).show() } return switchEvent == null } @@ -49,4 +71,4 @@ class SwitchListener( // This class represents a switch action heard by the switch listener // It contains the switch event and the time it was heard -data class SwitchAction(val switchEvent: SwitchEvent, val time: Long) \ No newline at end of file +data class AbsorbedSwitchAction(val switchEvent: SwitchEvent, val time: Long) \ No newline at end of file diff --git a/app/src/main/java/com/enaboapps/switchify/switches/SwitchAction.kt b/app/src/main/java/com/enaboapps/switchify/switches/SwitchAction.kt index a4ea7266..26839233 100644 --- a/app/src/main/java/com/enaboapps/switchify/switches/SwitchAction.kt +++ b/app/src/main/java/com/enaboapps/switchify/switches/SwitchAction.kt @@ -2,13 +2,15 @@ package com.enaboapps.switchify.switches class SwitchAction(val id: Int) { object Actions { - const val ACTION_SELECT = 0 - const val ACTION_STOP_SCANNING = 1 + const val ACTION_NONE = 0 + const val ACTION_SELECT = 1 + const val ACTION_STOP_SCANNING = 2 } // static array of actions companion object { val actions = arrayOf( + SwitchAction(Actions.ACTION_NONE), SwitchAction(Actions.ACTION_SELECT), SwitchAction(Actions.ACTION_STOP_SCANNING) ) @@ -16,6 +18,7 @@ class SwitchAction(val id: Int) { fun getActionName(): String { return when (id) { + Actions.ACTION_NONE -> "None" Actions.ACTION_SELECT -> "Select" Actions.ACTION_STOP_SCANNING -> "Stop Scanning" else -> "Unknown" diff --git a/app/src/main/java/com/enaboapps/switchify/switches/SwitchEvent.kt b/app/src/main/java/com/enaboapps/switchify/switches/SwitchEvent.kt index 783fb781..8820361b 100644 --- a/app/src/main/java/com/enaboapps/switchify/switches/SwitchEvent.kt +++ b/app/src/main/java/com/enaboapps/switchify/switches/SwitchEvent.kt @@ -4,14 +4,10 @@ data class SwitchEvent( val name: String, val code: String, val pressAction: SwitchAction, - val longPressAction: SwitchAction? = null, + val longPressAction: SwitchAction ) { override fun toString(): String { - return if (longPressAction == null) { - "$name, $code, ${pressAction.id}" - } else { - "$name, $code, ${pressAction.id}, ${longPressAction.id}" - } + return "$name, $code, $pressAction, $longPressAction" } override fun equals(other: Any?): Boolean { @@ -25,18 +21,19 @@ data class SwitchEvent( var result = name.hashCode() result = 31 * result + code.hashCode() result = 31 * result + pressAction.hashCode() - result = 31 * result + (longPressAction?.hashCode() ?: 0) + result = 31 * result + longPressAction.hashCode() return result } companion object { fun fromString(string: String): SwitchEvent { val parts = string.split(", ") - return if (parts.size == 3) { - SwitchEvent(parts[0], parts[1], SwitchAction(parts[2].toInt())) - } else { - SwitchEvent(parts[0], parts[1], SwitchAction(parts[2].toInt()), SwitchAction(parts[3].toInt())) - } + return SwitchEvent( + name = parts[0], + code = parts[1], + pressAction = SwitchAction(parts[2].toInt()), + longPressAction = SwitchAction(parts[3].toInt()) + ) } } } diff --git a/app/src/main/java/com/enaboapps/switchify/widgets/PreferenceTimeStepper.kt b/app/src/main/java/com/enaboapps/switchify/widgets/PreferenceTimeStepper.kt index dd6b3219..168395ac 100644 --- a/app/src/main/java/com/enaboapps/switchify/widgets/PreferenceTimeStepper.kt +++ b/app/src/main/java/com/enaboapps/switchify/widgets/PreferenceTimeStepper.kt @@ -36,7 +36,7 @@ fun PreferenceTimeStepper( Card( modifier = Modifier .fillMaxWidth() - .padding(horizontal = 20.dp), + .padding(horizontal = 20.dp, vertical = 8.dp), elevation = 8.dp, backgroundColor = MaterialTheme.colors.surface ) {