Skip to content

Commit

Permalink
Merge pull request #208 from enaboapps/iss204
Browse files Browse the repository at this point in the history
Implement scan method in settings
  • Loading branch information
enaboapps authored Mar 9, 2024
2 parents 57cef0f + cf4eb0d commit 07038bf
Show file tree
Hide file tree
Showing 18 changed files with 253 additions and 139 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/com/enaboapps/switchify/nav/NavGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Int>()
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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -44,7 +44,7 @@ object GesturePoint {
*/
fun setReselect(reselect: Boolean) {
if (reselect) {
ScanReceiver.setState(ScanReceiver.ReceiverState.CURSOR)
ScanMethod.setType(ScanMethod.MethodType.CURSOR)
listener?.onGesturePointReselect()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,13 @@ class MenuHierarchy(
tree = mutableListOf()

// reset the scanning manager state
MenuManager.getInstance().resetScanReceiverState()
MenuManager.getInstance().resetScanMethodType()
}

fun getTopMenu(): MenuView? {
return tree.lastOrNull()
}


override fun onMenuViewClosed() {
scanningManager.setCursorState()
}
override fun onMenuViewClosed() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
Loading

0 comments on commit 07038bf

Please sign in to comment.