Skip to content

Commit

Permalink
Merge pull request #196 from enaboapps/iss193
Browse files Browse the repository at this point in the history
Implement item scan
  • Loading branch information
enaboapps authored Mar 5, 2024
2 parents f807cae + 3a9dfce commit 3bd12db
Show file tree
Hide file tree
Showing 19 changed files with 759 additions and 426 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import android.util.Log
import android.view.KeyEvent
import android.view.accessibility.AccessibilityEvent
import com.enaboapps.switchify.service.gestures.GestureManager
import com.enaboapps.switchify.service.nodes.NodeExaminer
import com.enaboapps.switchify.service.scanning.ScanningManager
import com.enaboapps.switchify.service.selection.AutoSelectionHandler
import com.enaboapps.switchify.service.switches.SwitchListener
import com.enaboapps.switchify.service.utils.KeyboardInfo
import com.enaboapps.switchify.service.utils.NodeExaminer

class SwitchifyAccessibilityService : AccessibilityService() {

Expand All @@ -21,16 +22,9 @@ class SwitchifyAccessibilityService : AccessibilityService() {
override fun onAccessibilityEvent(event: AccessibilityEvent?) {
Log.d(TAG, "onAccessibilityEvent: ${event?.eventType}")

if (event?.eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
val rootNode = rootInActiveWindow
if (rootNode != null) {
NodeExaminer.findRowsOfNodes(rootNode)
for (row in NodeExaminer.currentRows) {
for (node in row) {
Log.d(TAG, "Node: $node")
}
}
}
val rootNode = rootInActiveWindow
if (rootNode != null) {
NodeExaminer.findNodes(rootNode, this)
}

KeyboardInfo.updateKeyboardState(windows)
Expand All @@ -51,6 +45,8 @@ class SwitchifyAccessibilityService : AccessibilityService() {
switchListener = SwitchListener(this, scanningManager!!)

GestureManager.getInstance().setup(this)

AutoSelectionHandler.init(this)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ package com.enaboapps.switchify.service.cursor
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.Log
import com.enaboapps.switchify.service.gestures.GestureManager
import com.enaboapps.switchify.service.menu.MenuManager
import com.enaboapps.switchify.service.gestures.GesturePoint
import com.enaboapps.switchify.service.gestures.GesturePointListener
import com.enaboapps.switchify.service.scanning.ScanDirection
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
import java.util.Timer
import java.util.TimerTask

/**
* This class manages the cursor
* @param context The context
*/
class CursorManager(private val context: Context) : ScanStateInterface, CursorPointListener {
class CursorManager(private val context: Context) : ScanStateInterface, GesturePointListener {

companion object {
private const val TAG = "CursorManager"
Expand All @@ -40,10 +39,6 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo

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

// auto select variables
private var isInAutoSelect = false // If true, we listen for a second event to activate the menu
private var autoSelectTimer: Timer? = null // Timer to wait for the second event


/**
* This function sets up the cursor
Expand All @@ -52,19 +47,19 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo
SwitchifyAccessibilityWindow.instance.setup(context)
SwitchifyAccessibilityWindow.instance.show()

CursorPoint.listener = this
GesturePoint.listener = this
}


/**
* This function is called when the cursor point is reselected
* It sets the quadrant to the last x quadrant
*/
override fun onCursorPointReselect() {
CursorPoint.y = 0
override fun onGesturePointReselect() {
GesturePoint.y = 0
// find the last quadrant
quadrantInfo = CursorPoint.lastXQuadrant
CursorPoint.x = quadrantInfo?.start!!
quadrantInfo = GesturePoint.lastXQuadrant
GesturePoint.x = quadrantInfo?.start!!
isInQuadrant = true
setupXCursorLine()
startAutoScanIfEnabled()
Expand All @@ -82,11 +77,11 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo

when (direction) {
ScanDirection.LEFT, ScanDirection.RIGHT -> {
CursorPoint.lastXQuadrant = quadrantInfo!!
GesturePoint.lastXQuadrant = quadrantInfo!!
}

ScanDirection.UP, ScanDirection.DOWN -> {
CursorPoint.lastYQuadrant = quadrantInfo!!
GesturePoint.lastYQuadrant = quadrantInfo!!
}
}
}
Expand All @@ -96,52 +91,52 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo
* This function sets up the y quadrant
*/
private fun setupYQuadrant() {
CursorPoint.y = CursorBounds.yMin(context)
GesturePoint.y = CursorBounds.yMin(context)
cursorUI.createYQuadrant(0)
setQuadrantInfo(
0,
CursorPoint.y,
CursorPoint.y + cursorUI.getQuadrantHeight()
GesturePoint.y,
GesturePoint.y + cursorUI.getQuadrantHeight()
)
}

/**
* This function updates the y quadrant to the given quadrant index
*/
private fun updateYQuadrant(quadrantIndex: Int) {
CursorPoint.y =
GesturePoint.y =
CursorBounds.yMin(context) + (quadrantIndex * cursorUI.getQuadrantHeight())
cursorUI.updateYQuadrant(quadrantIndex)
setQuadrantInfo(
quadrantIndex,
CursorPoint.y,
CursorPoint.y + cursorUI.getQuadrantHeight()
GesturePoint.y,
GesturePoint.y + cursorUI.getQuadrantHeight()
)
}

/**
* This function sets up the x quadrant
*/
private fun setupXQuadrant() {
CursorPoint.x = CursorBounds.X_MIN
GesturePoint.x = CursorBounds.X_MIN
cursorUI.createXQuadrant(0)
setQuadrantInfo(
0,
CursorPoint.x,
CursorPoint.x + cursorUI.getQuadrantWidth()
GesturePoint.x,
GesturePoint.x + cursorUI.getQuadrantWidth()
)
}

/**
* This function updates the x quadrant to the given quadrant index
*/
private fun updateXQuadrant(quadrantIndex: Int) {
CursorPoint.x = quadrantIndex * cursorUI.getQuadrantWidth()
GesturePoint.x = quadrantIndex * cursorUI.getQuadrantWidth()
cursorUI.updateXQuadrant(quadrantIndex)
setQuadrantInfo(
quadrantIndex,
CursorPoint.x,
CursorPoint.x + cursorUI.getQuadrantWidth()
GesturePoint.x,
GesturePoint.x + cursorUI.getQuadrantWidth()
)
}

Expand All @@ -159,7 +154,7 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo
* This function updates the y cursor line to the current y position
*/
private fun updateYCursorLine() {
cursorUI.updateYCursorLine(CursorPoint.y)
cursorUI.updateYCursorLine(GesturePoint.y)
}

/**
Expand All @@ -175,7 +170,7 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo
* This function updates the x cursor line to the current x position
*/
private fun updateXCursorLine() {
cursorUI.updateXCursorLine(CursorPoint.x)
cursorUI.updateXCursorLine(GesturePoint.x)
}


Expand Down Expand Up @@ -377,35 +372,35 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo
if (quadrantInfo != null) {
when (direction) {
ScanDirection.LEFT ->
if (CursorPoint.x > quadrantInfo?.start!!) {
CursorPoint.x -= cursorLineMovement
if (GesturePoint.x > quadrantInfo?.start!!) {
GesturePoint.x -= cursorLineMovement
updateXCursorLine()
} else {
direction = ScanDirection.RIGHT
moveCursorLine()
}

ScanDirection.RIGHT ->
if (CursorPoint.x < quadrantInfo?.end!!) {
CursorPoint.x += cursorLineMovement
if (GesturePoint.x < quadrantInfo?.end!!) {
GesturePoint.x += cursorLineMovement
updateXCursorLine()
} else {
direction = ScanDirection.LEFT
moveCursorLine()
}

ScanDirection.UP ->
if (CursorPoint.y > quadrantInfo?.start!!) {
CursorPoint.y -= cursorLineMovement
if (GesturePoint.y > quadrantInfo?.start!!) {
GesturePoint.y -= cursorLineMovement
updateYCursorLine()
} else {
direction = ScanDirection.DOWN
moveCursorLine()
}

ScanDirection.DOWN ->
if (CursorPoint.y < quadrantInfo?.end!!) {
CursorPoint.y += cursorLineMovement
if (GesturePoint.y < quadrantInfo?.end!!) {
GesturePoint.y += cursorLineMovement
updateYCursorLine()
} else {
direction = ScanDirection.UP
Expand All @@ -424,10 +419,6 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo
uiHandler.post {
internalReset()

isInAutoSelect = false
autoSelectTimer?.cancel()
autoSelectTimer = null

isInQuadrant = false
quadrantInfo = null
}
Expand Down Expand Up @@ -517,11 +508,6 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo
fun performSelectionAction() {
stopScanning()

// If the event is triggered within the auto select delay, we don't perform the action
if (checkAutoSelectDelay()) {
return
}

if (isReset()) {
setupXQuadrant()
startAutoScanIfEnabled()
Expand Down Expand Up @@ -586,58 +572,17 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo
return
}

// check if auto select is enabled, if so, start the timer
val auto = scanSettings.isAutoSelectEnabled()
if (auto && !isInAutoSelect) {
startAutoSelectTimer()
}

if (!auto) {
// open menu
MenuManager.getInstance().openMainMenu()
}
AutoSelectionHandler.setSelectAction { performTapAction() }
AutoSelectionHandler.performSelectionAction()

internalReset()
}


/**
* This function checks if the auto select delay is triggered
* This function performs the tap action
*/
private fun checkAutoSelectDelay(): Boolean {
if (isInAutoSelect) {
Log.d(Companion.TAG, "checkAutoSelectDelay: true")
isInAutoSelect = false
autoSelectTimer?.cancel()
autoSelectTimer = null
// open menu
MenuManager.getInstance().openMainMenu()
return true
}
return false
private fun performTapAction() {
GestureManager.getInstance().performTap()
}


/**
* This function starts the auto select timer
*/
private fun startAutoSelectTimer() {
val delay = scanSettings.getAutoSelectDelay()
isInAutoSelect = true
if (autoSelectTimer == null) {
autoSelectTimer = Timer()
}
autoSelectTimer?.schedule(object : TimerTask() {
override fun run() {
uiHandler.post {
if (isInAutoSelect) {
isInAutoSelect = false
// tap
GestureManager.getInstance().performTap()
}
}
}
}, delay)
}

}

This file was deleted.

Loading

0 comments on commit 3bd12db

Please sign in to comment.