Skip to content

Commit

Permalink
Improve long press management
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenMcGirr committed Feb 29, 2024
1 parent 193d878 commit 5939811
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.view.KeyEvent
import android.view.accessibility.AccessibilityEvent
import com.enaboapps.switchify.service.gestures.GestureManager
import com.enaboapps.switchify.service.scanning.ScanningManager
import com.enaboapps.switchify.service.scanning.SwitchListener
import com.enaboapps.switchify.service.switches.SwitchListener
import com.enaboapps.switchify.service.utils.KeyboardInfo
import com.enaboapps.switchify.service.utils.NodeExaminer

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.enaboapps.switchify.service.scanning
package com.enaboapps.switchify.service.switches

import android.content.Context
import android.util.Log
import com.enaboapps.switchify.preferences.PreferenceManager
import com.enaboapps.switchify.service.gestures.GestureManager
import com.enaboapps.switchify.service.scanning.ScanningManager
import com.enaboapps.switchify.switches.SwitchAction
import com.enaboapps.switchify.switches.SwitchEvent
import com.enaboapps.switchify.switches.SwitchEventStore
import java.util.Timer
import java.util.TimerTask

// SwitchListener class to handle switch events
class SwitchListener(
Expand All @@ -25,38 +24,11 @@ class SwitchListener(
// Variable to track the latest absorbed switch action
private var latestAction: AbsorbedSwitchAction? = null

// Timer for tracking switch hold duration
private var switchHoldTimer: Timer? = null

// Variable for tracking if long press action is performed
private var longPressPerformed = false

// Variables for ignoring switch repeat
private var lastSwitchPressedTime: Long = 0
private var lastSwitchPressedCode: Int = 0


// Function to start the switch hold timer
private fun startSwitchHoldTimer() {
switchHoldTimer = Timer()
switchHoldTimer?.schedule(
object : TimerTask() {
override fun run() {
// Cancel the timer when it runs out
switchHoldTimer?.cancel()
switchHoldTimer = null

// Perform long press action if available
latestAction?.let {
scanningManager.performAction(it.switchEvent.longPressAction)
longPressPerformed = true
}
}
},
preferenceManager.getLongValue(PreferenceManager.PREFERENCE_KEY_SWITCH_HOLD_TIME)
)
}

/**
* Called when a switch is pressed
* @param keyCode the key code of the switch event
Expand All @@ -76,7 +48,7 @@ class SwitchListener(
if (it.longPressAction.id == SwitchAction.Actions.ACTION_NONE) {
scanningManager.performAction(it.pressAction)
} else {
startSwitchHoldTimer()
SwitchLongPressHandler.startLongPress(context, it.longPressAction, scanningManager)
// Pause scanning if the setting is enabled
if (preferenceManager.getBooleanValue(PreferenceManager.PREFERENCE_KEY_PAUSE_SCAN_ON_SWITCH_HOLD)) {
scanningManager.pauseScanning()
Expand All @@ -96,9 +68,10 @@ class SwitchListener(
Log.d("SwitchListener", "onSwitchReleased: $keyCode")
return switchEvent?.let { event ->
latestAction?.takeIf { it.switchEvent == event }?.let {
SwitchLongPressHandler.stopLongPress()

// Check ignore repeat setting
if (shouldIgnoreSwitchRepeat(keyCode)) {
switchHoldTimer?.cancel() // Cancel any running timer
return false // Absorb the event, but don't perform any action
}

Expand All @@ -111,7 +84,6 @@ class SwitchListener(
.isSwipeLockEnabled()
) {
GestureManager.getInstance().toggleSwipeLock()
longPressPerformed = false
return true
}

Expand All @@ -120,21 +92,12 @@ class SwitchListener(
scanningManager.resumeScanning()
}

// Check if long press action is performed
if (longPressPerformed) {
longPressPerformed = false
return true // Absorb the event
}

if (event.longPressAction.id != SwitchAction.Actions.ACTION_NONE) {
// Perform press action if time elapsed is less than hold time
if (timeElapsed < switchHoldTime) {
scanningManager.performAction(event.pressAction)
}
}

// Cancel any running timer
switchHoldTimer?.cancel()
}
false
} ?: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.enaboapps.switchify.service.switches

import android.content.Context
import com.enaboapps.switchify.preferences.PreferenceManager
import com.enaboapps.switchify.service.scanning.ScanningManager
import com.enaboapps.switchify.switches.SwitchAction
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
* This object manages the long press action on a switch.
*/
object SwitchLongPressHandler {
private var longPressJob: Job? = null
private var longPressAction: SwitchAction? = null

/**
* Initiates the long press action.
* @param context The context.
* @param action The action to perform on long press.
* @param scanningManager The manager responsible for scanning actions.
*/
fun startLongPress(context: Context, action: SwitchAction, scanningManager: ScanningManager) {
longPressAction = action
val holdTime =
PreferenceManager(context).getLongValue(PreferenceManager.PREFERENCE_KEY_SWITCH_HOLD_TIME)
longPressJob = CoroutineScope(Dispatchers.Main).launch {
delay(holdTime)
longPressAction?.let {
scanningManager.performAction(it)
}
}
}

/**
* Cancels the ongoing long press action.
*/
fun stopLongPress() {
longPressJob?.cancel()
longPressJob = null
}
}

0 comments on commit 5939811

Please sign in to comment.