Skip to content

Commit

Permalink
Merge pull request #202 from enaboapps/iss201
Browse files Browse the repository at this point in the history
Improve item scan visuals
  • Loading branch information
enaboapps committed Mar 7, 2024
2 parents 4ce9f9f + 2885e89 commit c232b14
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import android.os.Looper
import android.widget.LinearLayout
import com.enaboapps.switchify.service.gestures.GestureManager
import com.enaboapps.switchify.service.menu.menus.BaseMenu
import com.enaboapps.switchify.service.scanning.ScanTree
import com.enaboapps.switchify.service.scanning.ScanningManager
import com.enaboapps.switchify.service.scanning.tree.ScanTree
import com.enaboapps.switchify.service.utils.ScreenUtils
import com.enaboapps.switchify.service.window.SwitchifyAccessibilityWindow

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Handler
import android.os.Looper
import android.view.accessibility.AccessibilityNodeInfo
import android.widget.RelativeLayout
import com.enaboapps.switchify.R
import com.enaboapps.switchify.service.gestures.GestureManager
import com.enaboapps.switchify.service.gestures.GesturePoint
import com.enaboapps.switchify.service.scanning.ScanNodeInterface
Expand Down Expand Up @@ -133,7 +134,7 @@ class Node : ScanNodeInterface {
}
boundsLayout = RelativeLayout(window.getContext())
boundsLayout?.let {
it.setBackgroundColor(0x55FF0000)
it.background = window.getContext()?.getDrawable(R.drawable.scan_item_border)
window.addView(it, x, y, width, height)
}
}
Expand All @@ -147,7 +148,6 @@ class Node : ScanNodeInterface {
boundsLayout?.let {
window.removeView(it)
}
boundsLayout = null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.enaboapps.switchify.service.nodes

import android.content.Context
import com.enaboapps.switchify.service.scanning.ScanReceiver
import com.enaboapps.switchify.service.scanning.ScanTree
import com.enaboapps.switchify.service.scanning.tree.ScanTree
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand All @@ -18,7 +18,8 @@ class NodeScanner(context: Context) : NodeUpdateDelegate {
private val coroutineScope =
CoroutineScope(Dispatchers.Main + job) // Use Main dispatcher for UI operations.

val scanTree = ScanTree(context, true)
val scanTree =
ScanTree(context, stopScanningOnSelect = true, individualHighlightingItemsInRow = false)

private var nodes: List<Node> = emptyList()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
package com.enaboapps.switchify.service.scanning
package com.enaboapps.switchify.service.scanning.tree

import android.content.Context
import android.util.Log
import com.enaboapps.switchify.service.scanning.ScanDirection
import com.enaboapps.switchify.service.scanning.ScanNodeInterface
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.utils.ScreenUtils
import kotlin.math.abs

/**
* This class represents the scanning tree
* @param context The context
* @param stopScanningOnSelect Whether to stop scanning on select
* @param individualHighlightingItemsInRow Whether to highlight items individually in a row
*/

class ScanTree(
private val context: Context,
private var stopScanningOnSelect: Boolean = false
private var stopScanningOnSelect: Boolean = false,
private var individualHighlightingItemsInRow: Boolean = true
) : ScanStateInterface {
/**
* This property represents the scanning tree
*/
private var tree: MutableList<Row> = mutableListOf()
private var tree: MutableList<ScanTreeRow> = mutableListOf()

/**
* This property indicates the current row of the scanning tree
Expand Down Expand Up @@ -52,14 +65,6 @@ class ScanTree(
private val scanSettings = ScanSettings(context)


/**
* Data class representing a row
* @param nodes The nodes in the row
* @param y The y coordinate of the row
*/
private data class Row(val nodes: List<ScanNodeInterface>, val y: Int)


/**
* This function builds the scanning tree
* by examining the x and y coordinates of the nodes
Expand Down Expand Up @@ -135,7 +140,7 @@ class ScanTree(
private fun addRow(row: List<ScanNodeInterface>) {
if (row.isNotEmpty()) {
val sortedRow = row.sortedBy { it.getX() }
tree.add(Row(sortedRow, sortedRow[0].getY()))
tree.add(ScanTreeRow(sortedRow, sortedRow[0].getY(), individualHighlightingItemsInRow))
}
}

Expand Down Expand Up @@ -239,9 +244,7 @@ class ScanTree(
*/
private fun highlightCurrentRow() {
if (tree.size > currentRow) {
for (node in tree[currentRow].nodes) {
node.highlight()
}
tree[currentRow].highlight()
}
}

Expand All @@ -250,9 +253,7 @@ class ScanTree(
*/
private fun unhighlightCurrentRow() {
if (tree.size > currentRow) {
for (node in tree[currentRow].nodes) {
node.unhighlight()
}
tree[currentRow].unhighlight()
}
}

Expand Down Expand Up @@ -442,6 +443,7 @@ class ScanTree(
fun reset() {
try {
for (row in tree) {
row.unhighlight()
for (node in row.nodes) {
node.unhighlight()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.enaboapps.switchify.service.scanning.tree

import android.os.Handler
import android.os.Looper
import android.widget.RelativeLayout
import com.enaboapps.switchify.service.scanning.ScanNodeInterface
import com.enaboapps.switchify.service.window.SwitchifyAccessibilityWindow

/**
* This class represents a row in the scan tree
* @param nodes The nodes in the row
* @param y The y coordinate of the row
* @param individualHighlighting Whether the nodes should be highlighted individually
*/
class ScanTreeRow(
val nodes: List<ScanNodeInterface>,
val y: Int,
val individualHighlighting: Boolean
) {
private val window = SwitchifyAccessibilityWindow.instance
private var boundsLayout: RelativeLayout? = null

private val handler = Handler(Looper.getMainLooper())

/**
* This function highlights the row
*/
fun highlight() {
if (individualHighlighting || nodes.size == 1) {
nodes.forEach { it.highlight() }
} else {
unhighlight()
boundsLayout = RelativeLayout(window.getContext()).apply {
background = window.getContext()
?.getDrawable(com.enaboapps.switchify.R.drawable.scan_row_border)
}
boundsLayout?.let {
handler.post {
window.addView(it, getX(), y, getWidth(), getHeight())
}
}
}
}

/**
* This function unhighlights the row
*/
fun unhighlight() {
if (individualHighlighting || nodes.size == 1) {
nodes.forEach { it.unhighlight() }
} else {
boundsLayout?.let {
handler.post {
window.removeView(it)
}
}
}
}

/**
* This function gets the x coordinate of the row
* @return The x coordinate of the row
*/
private fun getX(): Int {
var minX = Int.MAX_VALUE
nodes.forEach {
if (it.getX() < minX) {
minX = it.getX()
}
}
return minX
}

/**
* This function gets the width of the row
* @return The width of the row
*/
private fun getWidth(): Int {
val firstX = nodes.minOfOrNull { it.getX() } ?: 0
val lastX = nodes.maxOfOrNull { it.getX() + it.getWidth() } ?: 0
return lastX - firstX
}

/**
* This function gets the height of the row
* @return The height of the row
*/
private fun getHeight(): Int {
val minY = nodes.minOfOrNull { it.getY() } ?: 0
val maxY = nodes.maxOfOrNull { it.getY() + it.getHeight() } ?: 0
return maxY - minY
}
}
13 changes: 13 additions & 0 deletions app/src/main/res/drawable/scan_item_border.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Define the shape as a rectangle -->
<solid android:color="#00FFFFFF"/>

<!-- Specify the border color and width -->
<stroke
android:width="4dp"
android:color="#FF0000"/>

<!-- Specify the corner radius -->
<corners android:radius="16dp"/>
</shape>
13 changes: 13 additions & 0 deletions app/src/main/res/drawable/scan_row_border.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Define the shape as a rectangle -->
<solid android:color="#00FFFFFF"/>

<!-- Specify the border color and width -->
<stroke
android:width="4dp"
android:color="#0000FF"/>

<!-- Specify the corner radius -->
<corners android:radius="16dp"/>
</shape>

0 comments on commit c232b14

Please sign in to comment.