Moved AccessHelper to an outer class

This commit is contained in:
merkost
2023-06-09 11:40:41 +10:00
parent 91852ed63e
commit e6a22e5920
3 changed files with 75 additions and 72 deletions

View File

@ -0,0 +1,68 @@
package com.simplemobiletools.keyboard.helpers
import android.graphics.Rect
import android.os.Bundle
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
import androidx.customview.widget.ExploreByTouchHelper
import com.simplemobiletools.keyboard.views.MyKeyboardView
class AccessHelper(
private val keyboardView: MyKeyboardView,
private val keys: List<MyKeyboard.Key>
) : ExploreByTouchHelper(keyboardView) {
/**
* We need to populate the list with the IDs of all of the visible virtual views (the intervals in the chart).
* In our case, all keys are always visible, so well return a list of all IDs.
*/
override fun getVisibleVirtualViews(virtualViewIds: MutableList<Int>) {
val keysSize = keys.size
for (i in 0 until keysSize) {
virtualViewIds.add(i)
}
}
/**
* For this function, we need to return the ID of the virtual view thats under the x, y position,
* or ExploreByTouchHelper.HOST_ID if theres no item at those coordinates.
*/
override fun getVirtualViewAt(x: Float, y: Float): Int {
val rects = keys.map {
Rect(it.x, it.y, it.x + it.width, it.y + it.height)
}
rects.firstOrNull { it.contains(x.toInt(), y.toInt()) }?.let { exactRect ->
return rects.indexOf(exactRect)
} ?: return HOST_ID
}
/**
* This is where we provide all the metadata for our virtual view.
* We need to set the content description (or text, if its presented visually) and set the bounds in parent.
*/
override fun onPopulateNodeForVirtualView(virtualViewId: Int, node: AccessibilityNodeInfoCompat) {
node.className = keyboardView::class.simpleName
val key = keys.getOrNull(virtualViewId)
node.contentDescription = key?.getContentDescription(keyboardView.context) ?: ""
val bounds = updateBoundsForInterval(virtualViewId)
node.setBoundsInParent(bounds)
}
/**
* We need to set the content description (or text, if its presented visually) and set the bounds in parent.
* The bounds in the parent should match the logic in the onDraw() function.
*/
private fun updateBoundsForInterval(index: Int): Rect {
val keys = keys
val key = keys.getOrNull(index) ?: return Rect()
return Rect().apply {
left = key.x
top = key.y
right = key.x + key.width
bottom = key.y + key.height
}
}
override fun onPerformActionForVirtualView(virtualViewId: Int, action: Int, arguments: Bundle?): Boolean {
return false
}
}