From efc0dabf20eef79975bdb1daffb9bb0f156e2e3f Mon Sep 17 00:00:00 2001 From: OwenMcGirr Date: Fri, 8 Mar 2024 21:56:14 +0000 Subject: [PATCH 1/2] #204 Rename scan receiver to scan method --- .../preferences/PreferenceManager.kt | 2 +- .../service/SwitchifyAccessibilityService.kt | 4 +- .../service/gestures/GestureManager.kt | 4 +- .../service/gestures/GesturePoint.kt | 4 +- .../switchify/service/menu/MenuHierarchy.kt | 6 +- .../switchify/service/menu/MenuManager.kt | 29 +++---- .../switchify/service/menu/MenuView.kt | 4 +- .../service/menu/menus/main/MainMenu.kt | 2 +- .../switchify/service/nodes/NodeScanner.kt | 8 +- .../switchify/service/scanning/ScanMethod.kt | 56 +++++++++++++ .../service/scanning/ScanReceiver.kt | 56 ------------- .../service/scanning/ScanningManager.kt | 78 +++++++++---------- .../service/selection/AutoSelectionHandler.kt | 5 +- .../switchify/service/utils/KeyboardInfo.kt | 18 ++--- 14 files changed, 137 insertions(+), 139 deletions(-) create mode 100644 app/src/main/java/com/enaboapps/switchify/service/scanning/ScanMethod.kt delete mode 100644 app/src/main/java/com/enaboapps/switchify/service/scanning/ScanReceiver.kt 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 80d5e529..09a7d829 100644 --- a/app/src/main/java/com/enaboapps/switchify/preferences/PreferenceManager.kt +++ b/app/src/main/java/com/enaboapps/switchify/preferences/PreferenceManager.kt @@ -9,6 +9,7 @@ class PreferenceManager(context: Context) { const val PREFERENCE_KEY_SETUP_COMPLETE = "setup_complete" const val PREFERENCE_KEY_SCAN_MODE = "scan_mode" const val PREFERENCE_KEY_SCAN_RATE = "scan_rate" + const val PREFERENCE_KEY_SCAN_METHOD = "scan_method" const val PREFERENCE_KEY_REFINE_SCAN_RATE = "refine_scan_rate" const val PREFERENCE_KEY_SWITCH_HOLD_TIME = "switch_hold_time" const val PREFERENCE_KEY_PAUSE_ON_FIRST_ITEM = "pause_on_first_item" @@ -20,7 +21,6 @@ class PreferenceManager(context: Context) { const val PREFERENCE_KEY_PAUSE_SCAN_ON_SWITCH_HOLD = "pause_scan_on_switch_hold" const val PREFERENCE_KEY_SWITCH_IGNORE_REPEAT = "switch_ignore_repeat" const val PREFERENCE_KEY_SWITCH_IGNORE_REPEAT_DELAY = "switch_ignore_repeat_delay" - const val PREFERENCE_KEY_SCAN_RECEIVER = "scan_receiver" private const val PREFERENCE_FILE_NAME = "switchify_preferences" } diff --git a/app/src/main/java/com/enaboapps/switchify/service/SwitchifyAccessibilityService.kt b/app/src/main/java/com/enaboapps/switchify/service/SwitchifyAccessibilityService.kt index 70885225..fc12fd1a 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/SwitchifyAccessibilityService.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/SwitchifyAccessibilityService.kt @@ -6,7 +6,7 @@ import android.view.accessibility.AccessibilityEvent import com.enaboapps.switchify.preferences.PreferenceManager import com.enaboapps.switchify.service.gestures.GestureManager import com.enaboapps.switchify.service.nodes.NodeExaminer -import com.enaboapps.switchify.service.scanning.ScanReceiver +import com.enaboapps.switchify.service.scanning.ScanMethod import com.enaboapps.switchify.service.scanning.ScanningManager import com.enaboapps.switchify.service.selection.AutoSelectionHandler import com.enaboapps.switchify.service.switches.SwitchListener @@ -58,7 +58,7 @@ class SwitchifyAccessibilityService : AccessibilityService() { override fun onServiceConnected() { super.onServiceConnected() - ScanReceiver.preferenceManager = PreferenceManager(this) + ScanMethod.preferenceManager = PreferenceManager(this.applicationContext) scanningManager = ScanningManager(this, this) scanningManager.setup() diff --git a/app/src/main/java/com/enaboapps/switchify/service/gestures/GestureManager.kt b/app/src/main/java/com/enaboapps/switchify/service/gestures/GestureManager.kt index f9955ce2..d2b7ba3f 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/gestures/GestureManager.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/gestures/GestureManager.kt @@ -8,7 +8,7 @@ import com.enaboapps.switchify.service.SwitchifyAccessibilityService import com.enaboapps.switchify.service.gestures.utils.GestureUtils.getInBoundsCoordinate import com.enaboapps.switchify.service.gestures.visuals.GestureDrawing import com.enaboapps.switchify.service.nodes.NodeExaminer -import com.enaboapps.switchify.service.scanning.ScanReceiver +import com.enaboapps.switchify.service.scanning.ScanMethod import com.enaboapps.switchify.service.utils.ScreenUtils import com.enaboapps.switchify.service.window.ServiceMessageHUD import kotlin.math.pow @@ -316,7 +316,7 @@ class GestureManager { dragStartPoint = GesturePoint.getPoint() isDragging = true - ScanReceiver.setState(ScanReceiver.ReceiverState.CURSOR) + ScanMethod.setType(ScanMethod.MethodType.CURSOR) ServiceMessageHUD.instance.showMessage( "Select where to drag to", diff --git a/app/src/main/java/com/enaboapps/switchify/service/gestures/GesturePoint.kt b/app/src/main/java/com/enaboapps/switchify/service/gestures/GesturePoint.kt index 508794b5..55c16d57 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/gestures/GesturePoint.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/gestures/GesturePoint.kt @@ -2,7 +2,7 @@ package com.enaboapps.switchify.service.gestures import android.graphics.PointF import com.enaboapps.switchify.service.cursor.QuadrantInfo -import com.enaboapps.switchify.service.scanning.ScanReceiver +import com.enaboapps.switchify.service.scanning.ScanMethod /** * This interface represents the gesture point listener @@ -44,7 +44,7 @@ object GesturePoint { */ fun setReselect(reselect: Boolean) { if (reselect) { - ScanReceiver.setState(ScanReceiver.ReceiverState.CURSOR) + ScanMethod.setType(ScanMethod.MethodType.CURSOR) listener?.onGesturePointReselect() } } diff --git a/app/src/main/java/com/enaboapps/switchify/service/menu/MenuHierarchy.kt b/app/src/main/java/com/enaboapps/switchify/service/menu/MenuHierarchy.kt index ee17b24c..a44d8309 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/menu/MenuHierarchy.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/menu/MenuHierarchy.kt @@ -49,7 +49,7 @@ class MenuHierarchy( tree = mutableListOf() // reset the scanning manager state - MenuManager.getInstance().resetScanReceiverState() + MenuManager.getInstance().resetScanMethodType() } fun getTopMenu(): MenuView? { @@ -57,7 +57,5 @@ class MenuHierarchy( } - override fun onMenuViewClosed() { - scanningManager.setCursorState() - } + override fun onMenuViewClosed() {} } \ No newline at end of file diff --git a/app/src/main/java/com/enaboapps/switchify/service/menu/MenuManager.kt b/app/src/main/java/com/enaboapps/switchify/service/menu/MenuManager.kt index 8f76e059..672ffb5f 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/menu/MenuManager.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/menu/MenuManager.kt @@ -8,7 +8,7 @@ import com.enaboapps.switchify.service.menu.menus.gestures.ZoomGesturesMenu import com.enaboapps.switchify.service.menu.menus.main.MainMenu import com.enaboapps.switchify.service.menu.menus.system.SystemControlMenu import com.enaboapps.switchify.service.menu.menus.system.VolumeControlMenu -import com.enaboapps.switchify.service.scanning.ScanReceiver +import com.enaboapps.switchify.service.scanning.ScanMethod import com.enaboapps.switchify.service.scanning.ScanningManager /** @@ -40,9 +40,9 @@ class MenuManager { private var accessibilityService: SwitchifyAccessibilityService? = null /** - * The state of the scan receiver when the menu was activated + * The scan method to revert to when the menu is closed */ - var scanReceiverState: Int = ScanReceiver.ReceiverState.CURSOR + var scanMethodToRevertTo: Int = ScanMethod.MethodType.CURSOR /** * The menu hierarchy @@ -64,29 +64,30 @@ class MenuManager { } /** - * This function sets the scan receiver state back to the state that activated the menu + * This function resets the scan method type to the original type */ - fun resetScanReceiverState() { - ScanReceiver.setState(scanReceiverState) + fun resetScanMethodType() { + ScanMethod.isInMenu = false + ScanMethod.setType(scanMethodToRevertTo) } /** - * This function changes between cursor and item scan based on the current state + * This function changes between cursor and item scan based on the current type */ fun changeBetweenCursorAndItemScan() { - if (scanReceiverState == ScanReceiver.ReceiverState.CURSOR) { - scanningManager?.setItemScanState() + if (scanMethodToRevertTo == ScanMethod.MethodType.CURSOR) { + scanningManager?.setItemScanType() } else { - scanningManager?.setCursorState() + scanningManager?.setCursorType() } } /** - * This function gets the name of the state to switch to (cursor or item scan) - * @return The name of the state to switch to + * This function gets the name of the type to switch to (cursor or item scan) + * @return The name of the type to switch to */ - fun getStateToSwitchTo(): String { - return if (scanReceiverState == ScanReceiver.ReceiverState.CURSOR) { + fun getTypeToSwitchTo(): String { + return if (scanMethodToRevertTo == ScanMethod.MethodType.CURSOR) { "Item Scan" } else { "Cursor" diff --git a/app/src/main/java/com/enaboapps/switchify/service/menu/MenuView.kt b/app/src/main/java/com/enaboapps/switchify/service/menu/MenuView.kt index 41186880..e3714a68 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/menu/MenuView.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/menu/MenuView.kt @@ -125,8 +125,8 @@ class MenuView( addToWindow() // Inflate the menu inflateMenu() - // Set the menu state - scanningManager.setMenuState() + // Set the menu type + scanningManager.setMenuType() } // This function is called when the menu is closed diff --git a/app/src/main/java/com/enaboapps/switchify/service/menu/menus/main/MainMenu.kt b/app/src/main/java/com/enaboapps/switchify/service/menu/menus/main/MainMenu.kt index e80782d7..7bf4dcd7 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/menu/menus/main/MainMenu.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/menu/menus/main/MainMenu.kt @@ -25,7 +25,7 @@ class MainMenu(accessibilityService: SwitchifyAccessibilityService) : MenuItem("System Control", isLinkToMenu = true) { MenuManager.getInstance().openSystemControlMenu() }, - MenuItem(MenuManager.getInstance().getStateToSwitchTo()) { + MenuItem(MenuManager.getInstance().getTypeToSwitchTo()) { MenuManager.getInstance().changeBetweenCursorAndItemScan() } ) diff --git a/app/src/main/java/com/enaboapps/switchify/service/nodes/NodeScanner.kt b/app/src/main/java/com/enaboapps/switchify/service/nodes/NodeScanner.kt index 9f21c31f..43d0c842 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/nodes/NodeScanner.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/nodes/NodeScanner.kt @@ -1,7 +1,7 @@ package com.enaboapps.switchify.service.nodes import android.content.Context -import com.enaboapps.switchify.service.scanning.ScanReceiver +import com.enaboapps.switchify.service.scanning.ScanMethod import com.enaboapps.switchify.service.scanning.tree.ScanTree import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -39,15 +39,15 @@ class NodeScanner private constructor(context: Context) : NodeUpdateDelegate { } /** - * Starts a timeout that resets the scanTree and changes the state of the ScanReceiver + * 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. */ fun startTimeoutToRevertToCursor() { coroutineScope.launch { delay(5000) - if (ScanReceiver.getState() == ScanReceiver.ReceiverState.ITEM_SCAN && this@NodeScanner.nodes.isEmpty()) { + if (ScanMethod.getType() == ScanMethod.MethodType.ITEM_SCAN && this@NodeScanner.nodes.isEmpty()) { scanTree.reset() - ScanReceiver.setState(ScanReceiver.ReceiverState.CURSOR) + ScanMethod.setType(ScanMethod.MethodType.CURSOR) } } } diff --git a/app/src/main/java/com/enaboapps/switchify/service/scanning/ScanMethod.kt b/app/src/main/java/com/enaboapps/switchify/service/scanning/ScanMethod.kt new file mode 100644 index 00000000..66351621 --- /dev/null +++ b/app/src/main/java/com/enaboapps/switchify/service/scanning/ScanMethod.kt @@ -0,0 +1,56 @@ +package com.enaboapps.switchify.service.scanning + +import com.enaboapps.switchify.preferences.PreferenceManager + +object ScanMethod { + var preferenceManager: PreferenceManager? = null + + /** + * This variable is used to determine if the scanning is in the menu + */ + var isInMenu = false + + /** + * This enum represents the type of the scanning method + */ + object MethodType { + /** + * This type represents the cursor + */ + const val CURSOR = 0 + + /** + * This type represents the item scan + * Sequentially scanning the items on the screen + */ + const val ITEM_SCAN = 1 + } + + /** + * This function is used to get the type of the scanning method + */ + fun getType(): Int { + preferenceManager?.let { preferenceManager -> + val storedType = preferenceManager.getIntegerValue( + PreferenceManager.PREFERENCE_KEY_SCAN_METHOD + ) + println("Stored type: $storedType") + return if (storedType == MethodType.CURSOR || storedType == MethodType.ITEM_SCAN) { + storedType + } else { + MethodType.CURSOR + } + } + return MethodType.CURSOR + } + + /** + * This function is used to set the type of the scanning method + */ + fun setType(value: Int) { + preferenceManager?.setIntegerValue( + PreferenceManager.PREFERENCE_KEY_SCAN_METHOD, + value + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/enaboapps/switchify/service/scanning/ScanReceiver.kt b/app/src/main/java/com/enaboapps/switchify/service/scanning/ScanReceiver.kt deleted file mode 100644 index 8c492d56..00000000 --- a/app/src/main/java/com/enaboapps/switchify/service/scanning/ScanReceiver.kt +++ /dev/null @@ -1,56 +0,0 @@ -package com.enaboapps.switchify.service.scanning - -import com.enaboapps.switchify.preferences.PreferenceManager - -object ScanReceiver { - var preferenceManager: PreferenceManager? = null - - /** - * This variable is used to determine if the scanning is in the menu - */ - var isInMenu = false - - /** - * This enum represents the state of the scanning receiver - */ - object ReceiverState { - /** - * This state represents the cursor - */ - const val CURSOR = 0 - - /** - * This state represents the item scan - * Sequentially scanning the items on the screen - */ - const val ITEM_SCAN = 1 - } - - /** - * This function is used to get the state of the scanning receiver - */ - fun getState(): Int { - preferenceManager?.let { preferenceManager -> - val storedState = preferenceManager.getIntegerValue( - PreferenceManager.PREFERENCE_KEY_SCAN_RECEIVER - ) - println("Stored state: $storedState") - return if (storedState == ReceiverState.CURSOR || storedState == ReceiverState.ITEM_SCAN) { - storedState - } else { - ReceiverState.CURSOR - } - } - return ReceiverState.CURSOR - } - - /** - * This function is used to set the state of the scanning receiver - */ - fun setState(value: Int) { - preferenceManager?.setIntegerValue( - PreferenceManager.PREFERENCE_KEY_SCAN_RECEIVER, - value - ) - } -} \ No newline at end of file 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 b6f2ecd1..7e65505e 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 @@ -28,42 +28,42 @@ class ScanningManager( } - // This function explicitly sets the state of the scanning manager to cursor - fun setCursorState() { - ScanReceiver.setState(ScanReceiver.ReceiverState.CURSOR) - ScanReceiver.isInMenu = false + // 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 state of the scanning manager to item scan - fun setItemScanState() { - ScanReceiver.setState(ScanReceiver.ReceiverState.ITEM_SCAN) - ScanReceiver.isInMenu = false + // This function explicitly sets the type of the scanning manager to item scan + fun setItemScanType() { + ScanMethod.setType(ScanMethod.MethodType.ITEM_SCAN) + ScanMethod.isInMenu = false // Start the NodeScanner timeout nodeScanner.startTimeoutToRevertToCursor() } - // This function explicitly sets the state of the scanning manager to menu - fun setMenuState() { - ScanReceiver.isInMenu = true + // This function explicitly sets the type of the scanning manager to menu + fun setMenuType() { + ScanMethod.isInMenu = true } // This function makes a selection fun select() { - if (ScanReceiver.isInMenu) { + if (ScanMethod.isInMenu) { // Select the menu item MenuManager.getInstance().menuHierarchy?.getTopMenu()?.scanTree?.performSelection() return } - when (ScanReceiver.getState()) { - ScanReceiver.ReceiverState.CURSOR -> { + when (ScanMethod.getType()) { + ScanMethod.MethodType.CURSOR -> { // Perform the cursor action cursorManager.performSelectionAction() } - ScanReceiver.ReceiverState.ITEM_SCAN -> { + ScanMethod.MethodType.ITEM_SCAN -> { // Perform the item scan action nodeScanner.scanTree.performSelection() } @@ -90,19 +90,19 @@ class ScanningManager( } SwitchAction.Actions.ACTION_STOP_SCANNING -> { - if (ScanReceiver.isInMenu) { + if (ScanMethod.isInMenu) { // Stop the menu scanning MenuManager.getInstance().menuHierarchy?.getTopMenu()?.scanTree?.stopScanning() return } - when (ScanReceiver.getState()) { - ScanReceiver.ReceiverState.CURSOR -> { + when (ScanMethod.getType()) { + ScanMethod.MethodType.CURSOR -> { // reset the cursor cursorManager.externalReset() } - ScanReceiver.ReceiverState.ITEM_SCAN -> { + ScanMethod.MethodType.ITEM_SCAN -> { // Stop item scanning nodeScanner.scanTree.stopScanning() } @@ -110,19 +110,19 @@ class ScanningManager( } SwitchAction.Actions.ACTION_CHANGE_SCANNING_DIRECTION -> { - if (ScanReceiver.isInMenu) { + if (ScanMethod.isInMenu) { // Change the menu scanning direction MenuManager.getInstance().menuHierarchy?.getTopMenu()?.scanTree?.swapScanDirection() return } - when (ScanReceiver.getState()) { - ScanReceiver.ReceiverState.CURSOR -> { + when (ScanMethod.getType()) { + ScanMethod.MethodType.CURSOR -> { // Change the cursor direction cursorManager.swapDirection() } - ScanReceiver.ReceiverState.ITEM_SCAN -> { + ScanMethod.MethodType.ITEM_SCAN -> { // Change the item scan direction nodeScanner.scanTree.swapScanDirection() } @@ -130,19 +130,19 @@ class ScanningManager( } SwitchAction.Actions.ACTION_MOVE_TO_NEXT_ITEM -> { - if (ScanReceiver.isInMenu) { + if (ScanMethod.isInMenu) { // Move the menu to the next item MenuManager.getInstance().menuHierarchy?.getTopMenu()?.scanTree?.stepForward() return } - when (ScanReceiver.getState()) { - ScanReceiver.ReceiverState.CURSOR -> { + when (ScanMethod.getType()) { + ScanMethod.MethodType.CURSOR -> { // Move the cursor to the next item cursorManager.moveToNextItem() } - ScanReceiver.ReceiverState.ITEM_SCAN -> { + ScanMethod.MethodType.ITEM_SCAN -> { // Move to the next item nodeScanner.scanTree.stepForward() } @@ -150,19 +150,19 @@ class ScanningManager( } SwitchAction.Actions.ACTION_MOVE_TO_PREVIOUS_ITEM -> { - if (ScanReceiver.isInMenu) { + if (ScanMethod.isInMenu) { // Move the menu to the previous item MenuManager.getInstance().menuHierarchy?.getTopMenu()?.scanTree?.stepBackward() return } - when (ScanReceiver.getState()) { - ScanReceiver.ReceiverState.CURSOR -> { + when (ScanMethod.getType()) { + ScanMethod.MethodType.CURSOR -> { // Move the cursor to the previous item cursorManager.moveToPreviousItem() } - ScanReceiver.ReceiverState.ITEM_SCAN -> { + ScanMethod.MethodType.ITEM_SCAN -> { // Move to the previous item nodeScanner.scanTree.stepBackward() } @@ -177,19 +177,19 @@ class ScanningManager( } fun pauseScanning() { - if (ScanReceiver.isInMenu) { + if (ScanMethod.isInMenu) { // Pause the menu MenuManager.getInstance().menuHierarchy?.getTopMenu()?.scanTree?.pauseScanning() return } - when (ScanReceiver.getState()) { - ScanReceiver.ReceiverState.CURSOR -> { + when (ScanMethod.getType()) { + ScanMethod.MethodType.CURSOR -> { // Pause the cursor cursorManager.pauseScanning() } - ScanReceiver.ReceiverState.ITEM_SCAN -> { + ScanMethod.MethodType.ITEM_SCAN -> { // Pause the item scan nodeScanner.scanTree.pauseScanning() } @@ -197,19 +197,19 @@ class ScanningManager( } fun resumeScanning() { - if (ScanReceiver.isInMenu) { + if (ScanMethod.isInMenu) { // Resume the menu MenuManager.getInstance().menuHierarchy?.getTopMenu()?.scanTree?.resumeScanning() return } - when (ScanReceiver.getState()) { - ScanReceiver.ReceiverState.CURSOR -> { + when (ScanMethod.getType()) { + ScanMethod.MethodType.CURSOR -> { // Resume the cursor cursorManager.resumeScanning() } - ScanReceiver.ReceiverState.ITEM_SCAN -> { + ScanMethod.MethodType.ITEM_SCAN -> { // Resume the item scan nodeScanner.scanTree.resumeScanning() } diff --git a/app/src/main/java/com/enaboapps/switchify/service/selection/AutoSelectionHandler.kt b/app/src/main/java/com/enaboapps/switchify/service/selection/AutoSelectionHandler.kt index 099ecac4..de6a0b50 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/selection/AutoSelectionHandler.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/selection/AutoSelectionHandler.kt @@ -3,7 +3,7 @@ package com.enaboapps.switchify.service.selection import android.content.Context import com.enaboapps.switchify.preferences.PreferenceManager import com.enaboapps.switchify.service.menu.MenuManager -import com.enaboapps.switchify.service.scanning.ScanReceiver +import com.enaboapps.switchify.service.scanning.ScanMethod import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay @@ -42,8 +42,7 @@ object AutoSelectionHandler { * Performs the selection action based on the current settings and state. */ fun performSelectionAction() { - // Set the scan receiver to go back to after the menu is closed - MenuManager.getInstance().scanReceiverState = ScanReceiver.getState() + MenuManager.getInstance().scanMethodToRevertTo = ScanMethod.getType() // If auto-select is in progress, cancel it and open the main menu if (autoSelectInProgress) { diff --git a/app/src/main/java/com/enaboapps/switchify/service/utils/KeyboardInfo.kt b/app/src/main/java/com/enaboapps/switchify/service/utils/KeyboardInfo.kt index 8ad72455..64925004 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/utils/KeyboardInfo.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/utils/KeyboardInfo.kt @@ -3,14 +3,14 @@ package com.enaboapps.switchify.service.utils import android.graphics.Rect import android.util.Log import android.view.accessibility.AccessibilityWindowInfo -import com.enaboapps.switchify.service.scanning.ScanReceiver +import com.enaboapps.switchify.service.scanning.ScanMethod object KeyboardInfo { var isKeyboardVisible = false var keyboardHeight = 0 - // Track last scan state to go back to it after keyboard is dismissed - private var lastScanState: Int = ScanReceiver.getState() + // Track last scan type to go back to it after keyboard is dismissed + private var lastScanType: Int = ScanMethod.getType() // Track last update time to prevent multiple updates in a short time private var lastUpdateTime: Long = 0 @@ -26,11 +26,11 @@ object KeyboardInfo { if (keyboardWindow != null) { if (!isKeyboardVisible) { // Go to cursor as keyboard keys don't report AccessibilityNodeInfo - if (ScanReceiver.getState() == ScanReceiver.ReceiverState.ITEM_SCAN) { - lastScanState = ScanReceiver.ReceiverState.ITEM_SCAN - ScanReceiver.setState(ScanReceiver.ReceiverState.CURSOR) + if (ScanMethod.getType() == ScanMethod.MethodType.ITEM_SCAN) { + lastScanType = ScanMethod.MethodType.ITEM_SCAN + ScanMethod.setType(ScanMethod.MethodType.CURSOR) } else { - lastScanState = ScanReceiver.getState() + lastScanType = ScanMethod.getType() } } @@ -40,8 +40,8 @@ object KeyboardInfo { keyboardHeight = rect.height() } else { if (isKeyboardVisible) { - // Go back to last scan state - ScanReceiver.setState(lastScanState) + // Go back to last scan type + ScanMethod.setType(lastScanType) } isKeyboardVisible = false From cf4eb0d0c606d5a449536867032ab7fcfc9d1664 Mon Sep 17 00:00:00 2001 From: OwenMcGirr Date: Sat, 9 Mar 2024 09:17:19 +0000 Subject: [PATCH 2/2] Implement scan method in settings closes #204 --- .../com/enaboapps/switchify/nav/NavGraph.kt | 4 + .../switchify/nav/NavigationRoute.kt | 1 + .../screens/settings/SettingsScreen.kt | 7 ++ .../scanning/ScanMethodSelectionScreen.kt | 78 +++++++++++++++++++ .../switchify/service/scanning/ScanMethod.kt | 26 +++++++ 5 files changed, 116 insertions(+) create mode 100644 app/src/main/java/com/enaboapps/switchify/screens/settings/scanning/ScanMethodSelectionScreen.kt diff --git a/app/src/main/java/com/enaboapps/switchify/nav/NavGraph.kt b/app/src/main/java/com/enaboapps/switchify/nav/NavGraph.kt index b91626d4..7af334aa 100644 --- a/app/src/main/java/com/enaboapps/switchify/nav/NavGraph.kt +++ b/app/src/main/java/com/enaboapps/switchify/nav/NavGraph.kt @@ -13,6 +13,7 @@ import com.enaboapps.switchify.screens.account.SignInScreen import com.enaboapps.switchify.screens.account.SignUpScreen import com.enaboapps.switchify.screens.settings.AboutScreen import com.enaboapps.switchify.screens.settings.SettingsScreen +import com.enaboapps.switchify.screens.settings.scanning.ScanMethodSelectionScreen import com.enaboapps.switchify.screens.settings.scanning.ScanModeSelectionScreen import com.enaboapps.switchify.screens.settings.switches.AddNewSwitchScreen import com.enaboapps.switchify.screens.settings.switches.EditSwitchScreen @@ -56,6 +57,9 @@ fun NavGraph(navController: NavHostController) { composable(NavigationRoute.ScanMode.name) { ScanModeSelectionScreen(navController) } + composable(NavigationRoute.ScanMethod.name) { + ScanMethodSelectionScreen(navController) + } composable(NavigationRoute.Switches.name) { SwitchesScreen(navController) } diff --git a/app/src/main/java/com/enaboapps/switchify/nav/NavigationRoute.kt b/app/src/main/java/com/enaboapps/switchify/nav/NavigationRoute.kt index 56066d6d..3a81145b 100644 --- a/app/src/main/java/com/enaboapps/switchify/nav/NavigationRoute.kt +++ b/app/src/main/java/com/enaboapps/switchify/nav/NavigationRoute.kt @@ -13,6 +13,7 @@ sealed class NavigationRoute(val name: String) { data object SwitchStability : NavigationRoute("SwitchStability") data object About : NavigationRoute("About") data object ScanMode : NavigationRoute("ScanMode") + data object ScanMethod : NavigationRoute("ScanMethod") data object Switches : NavigationRoute("Switches") data object AddNewSwitch : NavigationRoute("AddNewSwitch") data object EditSwitch : NavigationRoute("EditSwitch") 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 9a03873a..1943970f 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 @@ -51,6 +51,13 @@ fun SettingsScreen(navController: NavController) { navController = navController, route = NavigationRoute.ScanMode.name ) + Spacer(modifier = Modifier.padding(top = 16.dp)) + PreferenceLink( + title = "Scan Method", + summary = "Configure the scan method", + navController = navController, + route = NavigationRoute.ScanMethod.name + ) } if (mode.id == ScanMode.Modes.MODE_AUTO) { TimingSection(settingsScreenModel) diff --git a/app/src/main/java/com/enaboapps/switchify/screens/settings/scanning/ScanMethodSelectionScreen.kt b/app/src/main/java/com/enaboapps/switchify/screens/settings/scanning/ScanMethodSelectionScreen.kt new file mode 100644 index 00000000..7bba1829 --- /dev/null +++ b/app/src/main/java/com/enaboapps/switchify/screens/settings/scanning/ScanMethodSelectionScreen.kt @@ -0,0 +1,78 @@ +package com.enaboapps.switchify.screens.settings.scanning + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.material.RadioButton +import androidx.compose.material.Scaffold +import androidx.compose.material.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.livedata.observeAsState +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.unit.dp +import androidx.lifecycle.MutableLiveData +import androidx.navigation.NavController +import com.enaboapps.switchify.preferences.PreferenceManager +import com.enaboapps.switchify.service.scanning.ScanMethod +import com.enaboapps.switchify.widgets.NavBar + +@Composable +fun ScanMethodSelectionScreen(navController: NavController) { + val methods = listOf(ScanMethod.MethodType.CURSOR, ScanMethod.MethodType.ITEM_SCAN) + val preferenceManager = PreferenceManager(LocalContext.current) + val currentMethod = MutableLiveData() + currentMethod.value = + preferenceManager.getIntegerValue(PreferenceManager.Keys.PREFERENCE_KEY_SCAN_METHOD) + val currentMethodState = currentMethod.observeAsState() + val setScanMethod = { method: Int -> + preferenceManager.setIntegerValue(PreferenceManager.Keys.PREFERENCE_KEY_SCAN_METHOD, method) + currentMethod.value = method + } + Scaffold( + topBar = { + NavBar(title = "Scan Method", navController = navController) + } + ) { + Column( + modifier = Modifier + .fillMaxWidth() + .padding(it) + .padding(all = 16.dp), + ) { + // radio buttons for each method + methods.forEach { method -> + Row( + modifier = Modifier + .fillMaxWidth(), + verticalAlignment = Alignment.CenterVertically + ) { + RadioButton( + selected = currentMethodState.value == method, + onClick = { + setScanMethod(method) + } + ) + Text(text = ScanMethod.getName(method)) + } + } + + // show the current method info + ScanMethodInfo(method = currentMethodState.value ?: ScanMethod.MethodType.CURSOR) + } + } +} + +@Composable +fun ScanMethodInfo(method: Int) { + Column( + modifier = Modifier + .fillMaxWidth() + .padding(all = 16.dp) + ) { + Text(text = ScanMethod.getName(method)) + Text(text = ScanMethod.getDescription(method)) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/enaboapps/switchify/service/scanning/ScanMethod.kt b/app/src/main/java/com/enaboapps/switchify/service/scanning/ScanMethod.kt index 66351621..9d844cd6 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/scanning/ScanMethod.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/scanning/ScanMethod.kt @@ -44,6 +44,32 @@ object ScanMethod { return MethodType.CURSOR } + /** + * This function gets the name of the scanning method + * @param type The type of the scanning method + * @return The name of the scanning method + */ + fun getName(type: Int): String { + return when (type) { + MethodType.CURSOR -> "Cursor" + MethodType.ITEM_SCAN -> "Item Scan" + else -> "Unknown" + } + } + + /** + * This function gets the description of the scanning method + * @param type The type of the scanning method + * @return The description of the scanning method + */ + fun getDescription(type: Int): String { + return when (type) { + MethodType.CURSOR -> "Cursor allows you to select items by moving a set of crosshairs over the screen." + MethodType.ITEM_SCAN -> "Item Scan allows you to select items by scanning through them sequentially." + else -> "Unknown" + } + } + /** * This function is used to set the type of the scanning method */