Skip to content
Merged
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
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class CursorManager(private val context: Context) {


// Function to stop the timer
private fun stop() {
fun stop() {
movingTimer?.cancel()
movingTimer = null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
}
Expand All @@ -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
}
Expand All @@ -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)
data class AbsorbedSwitchAction(val switchEvent: SwitchEvent, val time: Long)
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ 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)
)
}

fun getActionName(): String {
return when (id) {
Actions.ACTION_NONE -> "None"
Actions.ACTION_SELECT -> "Select"
Actions.ACTION_STOP_SCANNING -> "Stop Scanning"
else -> "Unknown"
Expand Down
21 changes: 9 additions & 12 deletions app/src/main/java/com/enaboapps/switchify/switches/SwitchEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand Down