Skip to content

Commit

Permalink
Add KDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenMcGirr committed Mar 10, 2024
1 parent 5dabf83 commit 7864011
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
package com.enaboapps.switchify.service.scanning

/**
* This class represents a scanning mode.
* @property id The unique identifier of the scanning mode.
*/
class ScanMode(val id: Int) {

/**
* This object holds the constants for the different scanning modes.
*/
object Modes {
const val MODE_AUTO = 0
const val MODE_MANUAL = 1
}

// static array of modes
/**
* This companion object holds an array of the different scanning modes and a function to get a scanning mode from its id.
*/
companion object {
// An array of the different scanning modes.
val modes = arrayOf(
ScanMode(Modes.MODE_AUTO),
ScanMode(Modes.MODE_MANUAL)
)

/**
* This function returns a scanning mode from its id.
* @param id The id of the scanning mode.
* @return The scanning mode with the given id.
*/
fun fromId(id: Int): ScanMode {
return when (id) {
Modes.MODE_AUTO -> ScanMode(Modes.MODE_AUTO)
Expand All @@ -22,6 +38,10 @@ class ScanMode(val id: Int) {
}
}

/**
* This function returns the name of the scanning mode.
* @return The name of the scanning mode.
*/
fun getModeName(): String {
return when (id) {
Modes.MODE_AUTO -> "Auto"
Expand All @@ -30,6 +50,10 @@ class ScanMode(val id: Int) {
}
}

/**
* This function returns the description of the scanning mode.
* @return The description of the scanning mode.
*/
fun getModeDescription(): String {
return when (id) {
Modes.MODE_AUTO -> "Automatically scan and use a single switch to select"
Expand All @@ -38,10 +62,23 @@ class ScanMode(val id: Int) {
}
}

/**
* This function checks if this scanning mode is equal to another object.
* @param other The object to compare with this scanning mode.
* @return True if the other object is a scanning mode with the same id, false otherwise.
*/
override fun equals(other: Any?): Boolean {
if (other is ScanMode) {
return other.id == id
}
return false
}

/**
* This function returns the hash code of this scanning mode.
* @return The hash code of this scanning mode.
*/
override fun hashCode(): Int {
return id
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import com.enaboapps.switchify.service.nodes.NodeScanner
import com.enaboapps.switchify.service.window.SwitchifyAccessibilityWindow
import com.enaboapps.switchify.switches.SwitchAction

/**
* ScanningManager is responsible for managing the scanning process in the application.
* It sets up and controls the scanning methods, performs actions and manages the scanning state.
*
* @property accessibilityService the accessibility service instance.
* @property context the application context.
*/
class ScanningManager(
private val accessibilityService: SwitchifyAccessibilityService,
val context: Context
Expand All @@ -21,8 +28,10 @@ class ScanningManager(
// node scanner
private val nodeScanner = NodeScanner.getInstance(context)


// This function sets up the scanning manager
/**
* This function sets up the scanning manager.
* It initializes the accessibility window, menu manager and scanning methods.
*/
fun setup() {
SwitchifyAccessibilityWindow.instance.setup(context)
SwitchifyAccessibilityWindow.instance.show()
Expand All @@ -34,8 +43,10 @@ class ScanningManager(
setupScanningMethods()
}


// This function sets up the respective scanning methods
/**
* This function sets up the respective scanning methods.
* It checks the current scanning method type and sets up the corresponding manager.
*/
private fun setupScanningMethods() {
when (ScanMethod.getType()) {
ScanMethod.MethodType.CURSOR -> {
Expand All @@ -48,14 +59,18 @@ class ScanningManager(
}
}


// This function explicitly sets the type of the scanning manager to cursor
/**
* This function explicitly sets the type of the scanning manager to cursor.
*/
fun setCursorType() {
ScanMethod.setType(ScanMethod.MethodType.CURSOR)
ScanMethod.isInMenu = false
}

// This function explicitly sets the type of the scanning manager to item scan
/**
* This function explicitly sets the type of the scanning manager to item scan.
* It also starts the NodeScanner timeout.
*/
fun setItemScanType() {
ScanMethod.setType(ScanMethod.MethodType.ITEM_SCAN)
ScanMethod.isInMenu = false
Expand All @@ -64,13 +79,17 @@ class ScanningManager(
nodeScanner.startTimeoutToRevertToCursor()
}

// This function explicitly sets the type of the scanning manager to menu
/**
* This function explicitly sets the type of the scanning manager to menu.
*/
fun setMenuType() {
ScanMethod.isInMenu = true
}


// This function makes a selection
/**
* This function makes a selection.
* It checks the current scanning method type and performs the corresponding selection action.
*/
fun select() {
if (ScanMethod.isInMenu) {
// Select the menu item
Expand All @@ -91,15 +110,18 @@ class ScanningManager(
}
}


// This function performs an action
/**
* This function performs an action.
* It checks the action id and performs the corresponding action.
*
* @param action the action to be performed.
*/
fun performAction(action: SwitchAction) {
// If swipe lock is enabled, swipe and return
if (GestureManager.getInstance().performSwipeLock()) {
return
}


// Perform the action based on the action id
when (action.id) {
SwitchAction.Actions.ACTION_NONE -> {
Expand Down Expand Up @@ -197,6 +219,10 @@ class ScanningManager(
}
}

/**
* This function pauses the scanning.
* It checks the current scanning method type and pauses the corresponding scanning process.
*/
fun pauseScanning() {
if (ScanMethod.isInMenu) {
// Pause the menu
Expand All @@ -217,6 +243,10 @@ class ScanningManager(
}
}

/**
* This function resumes the scanning.
* It checks the current scanning method type and resumes the corresponding scanning process.
*/
fun resumeScanning() {
if (ScanMethod.isInMenu) {
// Resume the menu
Expand All @@ -237,7 +267,12 @@ class ScanningManager(
}
}

// This function is called when the scanning method is changed
/**
* This function is called when the scanning method is changed.
* It cleans up the previous scanning method and sets up the new one.
*
* @param scanMethod the new scanning method.
*/
override fun onScanMethodChanged(scanMethod: Int) {
when (scanMethod) {
ScanMethod.MethodType.CURSOR -> {
Expand All @@ -252,6 +287,10 @@ class ScanningManager(
setupScanningMethods()
}

/**
* This function shuts down the scanning manager.
* It stops the scanning and cleans up the resources.
*/
fun shutdown() {
// Stop scanning
pauseScanning()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,85 @@ import java.util.UUID
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicReference

/**
* ScanningScheduler is a class that manages the scheduling of scanning tasks.
* It uses coroutines to run the scanning tasks asynchronously.
*
* @property context The application context.
* @property onScan A suspend function that gets executed during each scan.
*/
class ScanningScheduler(context: Context, private val onScan: suspend () -> Unit) {

/**
* A unique identifier for this instance of ScanningScheduler.
*/
private val uniqueId = UUID.randomUUID().toString()

/**
* The CoroutineScope in which the scanning tasks are launched.
*/
private val coroutineScope = CoroutineScope(Dispatchers.IO + CoroutineName(uniqueId))

/**
* The Job representing the currently running scanning task.
*/
private var scanningJob: Job? = null

/**
* A flag indicating whether a scanning task is currently executing.
*/
private val isExecuting = AtomicBoolean(false)

/**
* The initial delay before the first scanning task is launched.
*/
private var initialDelay: Long = 0L

/**
* The period between successive scanning tasks.
*/
private var period: Long = 1000L // Default period of 1 second

/**
* The current state of the scanner.
*/
private var scanState = AtomicReference(ScanState.STOPPED)

/**
* The settings for the scanning tasks.
*/
private val scanSettings = ScanSettings(context)

/**
* Starts the scanning tasks.
*
* @param initialDelay The initial delay before the first scanning task is launched.
* @param period The period between successive scanning tasks.
*/
fun startScanning(
initialDelay: Long = scanSettings.getScanRate(),
period: Long = scanSettings.getScanRate()
) {
// If the scanner is already scanning, print a message and return
if (scanState.get() == ScanState.SCANNING) {
println("[$uniqueId] Already scanning")
return
}

// Set the scanner state to SCANNING
scanState.set(ScanState.SCANNING)

// Calculate the initial delay plus the pause on the first item
val initialDelayPlusPause = initialDelay + scanSettings.getPauseOnFirstItemDelay()

// Set the initial delay and period
this.initialDelay = initialDelay
this.period = period
scanningJob?.cancel() // Ensure no previous job is running

// Cancel any previous scanning job
scanningJob?.cancel()

// Start a new scanning job
scanningJob = coroutineScope.launch {
println("[$uniqueId] Starting scanning job")
delay(initialDelayPlusPause)
Expand All @@ -56,10 +110,30 @@ class ScanningScheduler(context: Context, private val onScan: suspend () -> Unit
}
}

/**
* Checks if the scanner is currently scanning.
*
* @return True if the scanner is scanning, false otherwise.
*/
fun isScanning(): Boolean = scanState.get() == ScanState.SCANNING

/**
* Checks if the scanner is currently paused.
*
* @return True if the scanner is paused, false otherwise.
*/
fun isPaused(): Boolean = scanState.get() == ScanState.PAUSED

/**
* Checks if the scanner is currently stopped.
*
* @return True if the scanner is stopped, false otherwise.
*/
fun isStopped(): Boolean = scanState.get() == ScanState.STOPPED

/**
* Stops the scanning tasks.
*/
fun stopScanning() {
println("Attempting to stop scanning... $scanState")
if (scanState.get() == ScanState.SCANNING) {
Expand All @@ -68,20 +142,29 @@ class ScanningScheduler(context: Context, private val onScan: suspend () -> Unit
}
}

/**
* Pauses the scanning tasks.
*/
fun pauseScanning() {
println("Attempting to pause scanning... $scanState")
if (scanState.compareAndSet(ScanState.SCANNING, ScanState.PAUSED)) {
scanningJob?.cancel()
}
}

/**
* Resumes the scanning tasks.
*/
fun resumeScanning() {
println("Attempting to resume scanning... $scanState")
if (scanState.get() == ScanState.PAUSED) {
startScanning(initialDelay, period)
}
}

/**
* Shuts down the CoroutineScope, cancelling all active coroutines.
*/
fun shutdown() {
println("[$uniqueId] Shutting down scope")
coroutineScope.cancel() // Cancel all coroutines started by this scope
Expand Down

0 comments on commit 7864011

Please sign in to comment.