From 309396e387a600357a06db3b227e4ec691d5e485 Mon Sep 17 00:00:00 2001 From: OwenMcGirr Date: Wed, 28 Feb 2024 22:11:46 +0000 Subject: [PATCH] Choose number of quadrants based on screen size closes #185 --- .../switchify/service/cursor/CursorManager.kt | 58 ++++++++++++------- .../switchify/service/cursor/CursorUI.kt | 54 ++++++++++++++++- .../switchify/service/cursor/QuadrantInfo.kt | 13 +++++ 3 files changed, 100 insertions(+), 25 deletions(-) create mode 100644 app/src/main/java/com/enaboapps/switchify/service/cursor/QuadrantInfo.kt diff --git a/app/src/main/java/com/enaboapps/switchify/service/cursor/CursorManager.kt b/app/src/main/java/com/enaboapps/switchify/service/cursor/CursorManager.kt index 81cd3c27..5040de3b 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/cursor/CursorManager.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/cursor/CursorManager.kt @@ -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 @@ -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 @@ -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) } } @@ -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) } } @@ -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) } } @@ -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) } } @@ -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 { @@ -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 { @@ -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 @@ -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 \ No newline at end of file +} \ No newline at end of file diff --git a/app/src/main/java/com/enaboapps/switchify/service/cursor/CursorUI.kt b/app/src/main/java/com/enaboapps/switchify/service/cursor/CursorUI.kt index 5a227db1..7765708a 100644 --- a/app/src/main/java/com/enaboapps/switchify/service/cursor/CursorUI.kt +++ b/app/src/main/java/com/enaboapps/switchify/service/cursor/CursorUI.kt @@ -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) { @@ -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) } /** diff --git a/app/src/main/java/com/enaboapps/switchify/service/cursor/QuadrantInfo.kt b/app/src/main/java/com/enaboapps/switchify/service/cursor/QuadrantInfo.kt new file mode 100644 index 00000000..b8421af1 --- /dev/null +++ b/app/src/main/java/com/enaboapps/switchify/service/cursor/QuadrantInfo.kt @@ -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, +) \ No newline at end of file