Added inner AccessHelper class for talkback functional

This commit is contained in:
merkost 2023-06-07 13:10:33 +10:00
parent 763ea43c61
commit 9901cfbc10
1 changed files with 81 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import android.content.Intent
import android.graphics.* import android.graphics.*
import android.graphics.Paint.Align import android.graphics.Paint.Align
import android.graphics.drawable.* import android.graphics.drawable.*
import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.os.Message import android.os.Message
@ -24,6 +25,9 @@ import android.widget.PopupWindow
import android.widget.TextView import android.widget.TextView
import androidx.core.animation.doOnEnd import androidx.core.animation.doOnEnd
import androidx.core.animation.doOnStart import androidx.core.animation.doOnStart
import androidx.core.view.ViewCompat
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
import androidx.customview.widget.ExploreByTouchHelper
import androidx.emoji2.text.EmojiCompat import androidx.emoji2.text.EmojiCompat
import androidx.emoji2.text.EmojiCompat.EMOJI_SUPPORTED import androidx.emoji2.text.EmojiCompat.EMOJI_SUPPORTED
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
@ -95,6 +99,83 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
fun reloadKeyboard() fun reloadKeyboard()
} }
override fun dispatchHoverEvent(event: MotionEvent): Boolean {
return if (accessHelper.dispatchHoverEvent(event)) {
true
} else {
super.dispatchHoverEvent(event)
}
}
private val accessHelper = AccessHelper()
inner class AccessHelper() : ExploreByTouchHelper(this) {
/**
* 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 = mKeyboard?.mKeys?.size ?: 0
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 {
mKeyboard?.mKeys?.filterNotNull()?.let { keyList ->
val rects = keyList.map {
Rect(it.x, it.y, it.x + it.width, it.y + it.height)
}
rects.firstOrNull { it.contains(x.toInt(), y.toInt()) }?.let { exactRect ->
val exactIndexKey = rects.indexOf(exactRect)
return exactIndexKey
} ?: return HOST_ID
}
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 = MyKeyboardView::class.simpleName
val key = mKeyboard?.mKeys?.get(virtualViewId)
node.contentDescription = key?.label ?: "keyboard key"
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 = mKeyboard?.mKeys ?: return Rect()
val key = keys[index]!!
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
}
}
init {
ViewCompat.setAccessibilityDelegate(this, accessHelper)
}
private var mKeyboard: MyKeyboard? = null private var mKeyboard: MyKeyboard? = null
private var mCurrentKeyIndex: Int = NOT_A_KEY private var mCurrentKeyIndex: Int = NOT_A_KEY