Skip to content

Commit

Permalink
Merge pull request #212 from enaboapps/iss206
Browse files Browse the repository at this point in the history
Ensure that cursor and Item scan don't overlap
  • Loading branch information
enaboapps authored Mar 9, 2024
2 parents d5275ff + 5e9a28e commit bb0eb81
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.enaboapps.switchify.service.scanning.ScanSettings
import com.enaboapps.switchify.service.scanning.ScanStateInterface
import com.enaboapps.switchify.service.scanning.ScanningScheduler
import com.enaboapps.switchify.service.selection.AutoSelectionHandler
import com.enaboapps.switchify.service.window.SwitchifyAccessibilityWindow

/**
* This class manages the cursor
Expand All @@ -37,17 +36,14 @@ class CursorManager(private val context: Context) : ScanStateInterface, GestureP

private var direction: ScanDirection = ScanDirection.RIGHT

private val scanningScheduler = ScanningScheduler(context) { move() }

private var scanningScheduler: ScanningScheduler? = null

/**
* This function sets up the cursor
*/
fun setup() {
SwitchifyAccessibilityWindow.instance.setup(context)
SwitchifyAccessibilityWindow.instance.show()

GesturePoint.listener = this
scanningScheduler = ScanningScheduler(context) { move() }
}


Expand Down Expand Up @@ -184,7 +180,7 @@ class CursorManager(private val context: Context) : ScanStateInterface, GestureP
} else {
scanSettings.getScanRate()
}
scanningScheduler.startScanning(rate, rate)
scanningScheduler?.startScanning(rate, rate)
}
}

Expand Down Expand Up @@ -277,23 +273,23 @@ class CursorManager(private val context: Context) : ScanStateInterface, GestureP
* This function stops the scanning
*/
override fun stopScanning() {
scanningScheduler.stopScanning()
scanningScheduler?.stopScanning()
}


/**
* This function pauses the scanning
*/
override fun pauseScanning() {
scanningScheduler.pauseScanning()
scanningScheduler?.pauseScanning()
}


/**
* This function resumes the scanning
*/
override fun resumeScanning() {
scanningScheduler.resumeScanning()
scanningScheduler?.resumeScanning()
}


Expand Down Expand Up @@ -588,6 +584,6 @@ class CursorManager(private val context: Context) : ScanStateInterface, GestureP

fun cleanup() {
cursorUI.reset()
scanningScheduler.shutdown()
scanningScheduler?.shutdown()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class MenuView(
baseLayout.addView(menuPages[currentPage].getMenuLayout())
// Build the scan tree after half a second
Handler(Looper.getMainLooper()).postDelayed({
scanTree.setup()
scanTree.buildTree(menuPages[currentPage].getMenuItems(), 0)
}, 500)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class NodeScanner private constructor(context: Context) : NodeUpdateDelegate {
startTimeoutToRevertToCursor()
}

/**
* Sets up the scanTree.
*/
fun setup() {
scanTree.setup()
}

/**
* Starts a timeout that resets the scanTree and changes the state of the ScanMethod
* if the state is ITEM_SCAN and there are no nodes after 5 seconds.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@ package com.enaboapps.switchify.service.scanning

import com.enaboapps.switchify.preferences.PreferenceManager

/**
* This interface is used to observe the changes in the scanning method
*/
interface ScanMethodObserver {
fun onScanMethodChanged(scanMethod: Int)
}

/**
* This object is used to manage the scanning method
*/
object ScanMethod {
var preferenceManager: PreferenceManager? = null

/**
* This is the observer of the scanning method
*/
var observer: ScanMethodObserver? = null

/**
* This variable is used to determine if the scanning is in the menu
*/
Expand Down Expand Up @@ -78,5 +93,6 @@ object ScanMethod {
PreferenceManager.PREFERENCE_KEY_SCAN_METHOD,
value
)
observer?.onScanMethodChanged(value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import com.enaboapps.switchify.service.cursor.CursorManager
import com.enaboapps.switchify.service.gestures.GestureManager
import com.enaboapps.switchify.service.menu.MenuManager
import com.enaboapps.switchify.service.nodes.NodeScanner
import com.enaboapps.switchify.service.window.SwitchifyAccessibilityWindow
import com.enaboapps.switchify.switches.SwitchAction

class ScanningManager(
private val accessibilityService: SwitchifyAccessibilityService,
val context: Context
) {
) : ScanMethodObserver {

// cursor manager
private val cursorManager = CursorManager(context)
Expand All @@ -23,8 +24,28 @@ class ScanningManager(

// This function sets up the scanning manager
fun setup() {
cursorManager.setup()
SwitchifyAccessibilityWindow.instance.setup(context)
SwitchifyAccessibilityWindow.instance.show()

MenuManager.getInstance().setup(this, accessibilityService)

ScanMethod.observer = this

setupScanningMethods()
}


// This function sets up the respective scanning methods
private fun setupScanningMethods() {
when (ScanMethod.getType()) {
ScanMethod.MethodType.CURSOR -> {
cursorManager.setup()
}

ScanMethod.MethodType.ITEM_SCAN -> {
nodeScanner.setup()
}
}
}


Expand Down Expand Up @@ -216,6 +237,21 @@ class ScanningManager(
}
}

// This function is called when the scanning method is changed
override fun onScanMethodChanged(scanMethod: Int) {
when (scanMethod) {
ScanMethod.MethodType.CURSOR -> {
nodeScanner.cleanup()
}

ScanMethod.MethodType.ITEM_SCAN -> {
cursorManager.cleanup()
}
}

setupScanningMethods()
}

fun shutdown() {
// Stop scanning
pauseScanning()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ class ScanTree(
addRow(currentRow)
}
}

setupScanningScheduler()
}

/**
Expand All @@ -155,11 +153,14 @@ class ScanTree(
/**
* This function sets the scanning scheduler
*/
private fun setupScanningScheduler() {
fun setup() {
reset()
shutdown()

scanningScheduler = ScanningScheduler(context) { stepAutoScanning() }
if (scanningScheduler == null) {
scanningScheduler = ScanningScheduler(context) {
stepAutoScanning()
}
}
}

/**
Expand Down

0 comments on commit bb0eb81

Please sign in to comment.