@@ -10,7 +10,6 @@ import android.content.Intent
1010import android.graphics.*
1111import android.graphics.Paint.Align
1212import android.graphics.drawable.*
13- import android.os.Bundle
1413import android.os.Handler
1514import android.os.Looper
1615import android.os.Message
@@ -24,8 +23,6 @@ import android.widget.TextView
2423import androidx.core.animation.doOnEnd
2524import androidx.core.animation.doOnStart
2625import androidx.core.view.ViewCompat
27- import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
28- import androidx.customview.widget.ExploreByTouchHelper
2926import androidx.emoji2.text.EmojiCompat
3027import androidx.emoji2.text.EmojiCompat.EMOJI_SUPPORTED
3128import com.simplemobiletools.commons.extensions.*
@@ -58,80 +55,14 @@ import java.util.*
5855class MyKeyboardView @JvmOverloads constructor(context : Context , attrs : AttributeSet ? , defStyleRes : Int = 0 ) : View(context, attrs, defStyleRes) {
5956
6057 override fun dispatchHoverEvent (event : MotionEvent ): Boolean {
61- return if (accessHelper.dispatchHoverEvent(event)) {
58+ return if (accessHelper? .dispatchHoverEvent(event) == true ) {
6259 true
6360 } else {
6461 super .dispatchHoverEvent(event)
6562 }
6663 }
6764
68- private val accessHelper = AccessHelper ()
69-
70- inner class AccessHelper () : ExploreByTouchHelper(this) {
71-
72- /* *
73- * We need to populate the list with the IDs of all of the visible virtual views (the intervals in the chart).
74- * In our case, all keys are always visible, so we’ll return a list of all IDs.
75- */
76- override fun getVisibleVirtualViews (virtualViewIds : MutableList <Int >) {
77- val keysSize = mKeyboard?.mKeys?.size ? : 0
78- for (i in 0 until keysSize) {
79- virtualViewIds.add(i)
80- }
81- }
82-
83- /* *
84- * For this function, we need to return the ID of the virtual view that’s under the x, y position,
85- * or ExploreByTouchHelper.HOST_ID if there’s no item at those coordinates.
86- */
87- override fun getVirtualViewAt (x : Float , y : Float ): Int {
88- mKeyboard?.mKeys?.filterNotNull()?.let { keyList ->
89- val rects = keyList.map {
90- Rect (it.x, it.y, it.x + it.width, it.y + it.height)
91- }
92- rects.firstOrNull { it.contains(x.toInt(), y.toInt()) }?.let { exactRect ->
93- val exactIndexKey = rects.indexOf(exactRect)
94- return exactIndexKey
95- } ? : return HOST_ID
96- }
97- return HOST_ID
98- }
99-
100- /* *
101- * This is where we provide all the metadata for our virtual view.
102- * We need to set the content description (or text, if it’s presented visually) and set the bounds in parent.
103- */
104- override fun onPopulateNodeForVirtualView (virtualViewId : Int , node : AccessibilityNodeInfoCompat ) {
105- node.className = MyKeyboardView ::class .simpleName
106- val key = mKeyboard?.mKeys?.get(virtualViewId)
107- node.contentDescription = key?.getContentDescription(context)
108- val bounds = updateBoundsForInterval(virtualViewId)
109- node.setBoundsInParent(bounds)
110- }
111-
112- /* *
113- * We need to set the content description (or text, if it’s presented visually) and set the bounds in parent.
114- * The bounds in the parent should match the logic in the onDraw() function.
115- */
116- private fun updateBoundsForInterval (index : Int ): Rect {
117- val keys = mKeyboard?.mKeys ? : return Rect ()
118- val key = keys[index]!!
119- return Rect ().apply {
120- left = key.x
121- top = key.y
122- right = key.x + key.width
123- bottom = key.y + key.height
124- }
125- }
126-
127- override fun onPerformActionForVirtualView (virtualViewId : Int , action : Int , arguments : Bundle ? ): Boolean {
128- return false
129- }
130- }
131-
132- init {
133- ViewCompat .setAccessibilityDelegate(this , accessHelper)
134- }
65+ private var accessHelper: AccessHelper ? = null
13566
13667 private var mKeyboard: MyKeyboard ? = null
13768 private var mCurrentKeyIndex: Int = NOT_A_KEY
@@ -335,6 +266,10 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
335266 invalidateAllKeys()
336267 computeProximityThreshold(keyboard)
337268 mMiniKeyboardCache.clear()
269+
270+ accessHelper = AccessHelper (this , mKeyboard?.mKeys.orEmpty())
271+ ViewCompat .setAccessibilityDelegate(this , accessHelper)
272+
338273 // Not really necessary to do every time, but will free up views
339274 // Switching to a different keyboard should abort any pending keys so that the key up
340275 // doesn't get delivered to the old or new keyboard
0 commit comments