Skip to content

Commit

Permalink
Merge pull request #188 from enaboapps/iss187
Browse files Browse the repository at this point in the history
Choose number of quadrants based on screen size
  • Loading branch information
enaboapps committed Feb 28, 2024
2 parents d683255 + 309396e commit 6cdfde7
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import java.util.TimerTask

class CursorManager(private val context: Context) : ScanStateInterface, CursorPointListener {

private val TAG = "CursorManager"
companion object {
private const val TAG = "CursorManager"

private const val MIN_QUADRANT_INDEX = 0
}

private val cursorLineMovement = 40

Expand Down Expand Up @@ -148,6 +152,25 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo
}


/**
* This function determines the max quadrant index
* It uses the direction to determine the max quadrant index
* The smaller the width or height of the screen, the smaller the max quadrant index
* @return The max quadrant index
*/
private fun getMaxQuadrantIndex(): Int {
return when (direction) {
ScanDirection.LEFT, ScanDirection.RIGHT -> {
CursorUI.getNumberOfQuadrantsHorizontally(context) - 1
}

ScanDirection.UP, ScanDirection.DOWN -> {
CursorUI.getNumberOfQuadrantsVertically(context) - 1
}
}
}


// Function to swap the direction
fun swapDirection() {
// Here we swap the direction
Expand All @@ -158,8 +181,8 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo
if (!isInQuadrant) {
quadrantInfo?.let {
if (it.quadrantIndex == MIN_QUADRANT_INDEX) {
updateXQuadrant(MAX_QUADRANT_INDEX)
} else if (it.quadrantIndex == MAX_QUADRANT_INDEX) {
updateXQuadrant(getMaxQuadrantIndex())
} else if (it.quadrantIndex == getMaxQuadrantIndex()) {
updateXQuadrant(MIN_QUADRANT_INDEX)
}
}
Expand All @@ -171,8 +194,8 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo
if (!isInQuadrant) {
quadrantInfo?.let {
if (it.quadrantIndex == MIN_QUADRANT_INDEX) {
updateXQuadrant(MAX_QUADRANT_INDEX)
} else if (it.quadrantIndex == MAX_QUADRANT_INDEX) {
updateXQuadrant(getMaxQuadrantIndex())
} else if (it.quadrantIndex == getMaxQuadrantIndex()) {
updateXQuadrant(MIN_QUADRANT_INDEX)
}
}
Expand All @@ -184,8 +207,8 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo
if (!isInQuadrant) {
quadrantInfo?.let {
if (it.quadrantIndex == MIN_QUADRANT_INDEX) {
updateYQuadrant(MAX_QUADRANT_INDEX)
} else if (it.quadrantIndex == MAX_QUADRANT_INDEX) {
updateYQuadrant(getMaxQuadrantIndex())
} else if (it.quadrantIndex == getMaxQuadrantIndex()) {
updateYQuadrant(MIN_QUADRANT_INDEX)
}
}
Expand All @@ -197,8 +220,8 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo
if (!isInQuadrant) {
quadrantInfo?.let {
if (it.quadrantIndex == MIN_QUADRANT_INDEX) {
updateYQuadrant(MAX_QUADRANT_INDEX)
} else if (it.quadrantIndex == MAX_QUADRANT_INDEX) {
updateYQuadrant(getMaxQuadrantIndex())
} else if (it.quadrantIndex == getMaxQuadrantIndex()) {
updateYQuadrant(MIN_QUADRANT_INDEX)
}
}
Expand Down Expand Up @@ -255,7 +278,7 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo

ScanDirection.RIGHT -> {
quadrantInfo?.let {
if (it.quadrantIndex < MAX_QUADRANT_INDEX) {
if (it.quadrantIndex < getMaxQuadrantIndex()) {
val quadrantIndex = it.quadrantIndex + 1
updateXQuadrant(quadrantIndex)
} else {
Expand All @@ -279,7 +302,7 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo

ScanDirection.DOWN -> {
quadrantInfo?.let {
if (it.quadrantIndex < MAX_QUADRANT_INDEX) {
if (it.quadrantIndex < getMaxQuadrantIndex()) {
val quadrantIndex = it.quadrantIndex + 1
updateYQuadrant(quadrantIndex)
} else {
Expand Down Expand Up @@ -499,7 +522,7 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo
// Function to check if the event is triggered within the auto select delay
private fun checkAutoSelectDelay(): Boolean {
if (isInAutoSelect) {
Log.d(TAG, "checkAutoSelectDelay: true")
Log.d(Companion.TAG, "checkAutoSelectDelay: true")
isInAutoSelect = false
autoSelectTimer?.cancel()
autoSelectTimer = null
Expand Down Expand Up @@ -531,13 +554,4 @@ class CursorManager(private val context: Context) : ScanStateInterface, CursorPo
}, delay)
}

}

data class QuadrantInfo(
val quadrantIndex: Int,
val start: Int,
val end: Int,
)

val MIN_QUADRANT_INDEX = 0
val MAX_QUADRANT_INDEX = 3
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.graphics.Color
import android.os.Handler
import android.widget.RelativeLayout
import com.enaboapps.switchify.service.utils.KeyboardInfo
import com.enaboapps.switchify.service.window.SwitchifyAccessibilityWindow

class CursorUI(private val context: Context, private val handler: Handler) {
Expand All @@ -20,21 +21,68 @@ class CursorUI(private val context: Context, private val handler: Handler) {
private const val CURSOR_LINE_COLOR = Color.RED
private const val QUADRANT_ALPHA = 0.5f
private const val QUADRANT_COLOR = Color.BLUE
private const val NUMBER_OF_QUADRANTS = 4

/**
* This function determines the number of quadrants horizontally
* It uses the width of the cursor bounds to determine the number of quadrants
* Or it uses the keyboard visibility to determine the number of quadrants
* @param context The context
* @return The number of quadrants horizontally
*/
fun getNumberOfQuadrantsHorizontally(context: Context): Int {
return if (KeyboardInfo.isKeyboardVisible) {
7 // The average keyboard has 7 columns of keys (each row can differ)
} else {
val cursorBounds = CursorBounds.width(context)
getNumberOfQuadrants(cursorBounds)
}
}

/**
* This function determines the number of quadrants vertically
* It uses the height of the cursor bounds to determine the number of quadrants
* Or it uses the keyboard visibility to determine the number of quadrants
* @param context The context
* @return The number of quadrants vertically
*/
fun getNumberOfQuadrantsVertically(context: Context): Int {
return if (KeyboardInfo.isKeyboardVisible) {
3 // The average keyboard has 3 rows of keys
} else {
val cursorBounds = CursorBounds.height(context)
getNumberOfQuadrants(cursorBounds)
}
}

/**
* This function determines the number of quadrants
* It uses the size of the cursor bounds to determine the number of quadrants
* @param size The size of the cursor bounds
* @return The number of quadrants
*/
private fun getNumberOfQuadrants(size: Int): Int {
val minThreshold = 500
val quarterBounds = size / 4
return if (quarterBounds < minThreshold) {
2
} else {
4
}
}
}

/**
* Get the width of a quadrant
*/
fun getQuadrantWidth(): Int {
return CursorBounds.width(context) / NUMBER_OF_QUADRANTS
return CursorBounds.width(context) / getNumberOfQuadrantsHorizontally(context)
}

/**
* Get the height of a quadrant
*/
fun getQuadrantHeight(): Int {
return CursorBounds.height(context) / NUMBER_OF_QUADRANTS
return CursorBounds.height(context) / getNumberOfQuadrantsVertically(context)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.enaboapps.switchify.service.cursor

/**
* This data class represents the quadrant information
* @param quadrantIndex The index of the quadrant
* @param start The start index of the quadrant
* @param end The end index of the quadrant
*/
data class QuadrantInfo(
val quadrantIndex: Int,
val start: Int,
val end: Int,
)

0 comments on commit 6cdfde7

Please sign in to comment.