Skip to content

Commit e6a22e5

Browse files
committed
Moved AccessHelper to an outer class
1 parent 91852ed commit e6a22e5

File tree

3 files changed

+75
-72
lines changed

3 files changed

+75
-72
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.simplemobiletools.keyboard.helpers
2+
3+
import android.graphics.Rect
4+
import android.os.Bundle
5+
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
6+
import androidx.customview.widget.ExploreByTouchHelper
7+
import com.simplemobiletools.keyboard.views.MyKeyboardView
8+
9+
class AccessHelper(
10+
private val keyboardView: MyKeyboardView,
11+
private val keys: List<MyKeyboard.Key>
12+
) : ExploreByTouchHelper(keyboardView) {
13+
14+
/**
15+
* We need to populate the list with the IDs of all of the visible virtual views (the intervals in the chart).
16+
* In our case, all keys are always visible, so we’ll return a list of all IDs.
17+
*/
18+
override fun getVisibleVirtualViews(virtualViewIds: MutableList<Int>) {
19+
val keysSize = keys.size
20+
for (i in 0 until keysSize) {
21+
virtualViewIds.add(i)
22+
}
23+
}
24+
25+
/**
26+
* For this function, we need to return the ID of the virtual view that’s under the x, y position,
27+
* or ExploreByTouchHelper.HOST_ID if there’s no item at those coordinates.
28+
*/
29+
override fun getVirtualViewAt(x: Float, y: Float): Int {
30+
val rects = keys.map {
31+
Rect(it.x, it.y, it.x + it.width, it.y + it.height)
32+
}
33+
rects.firstOrNull { it.contains(x.toInt(), y.toInt()) }?.let { exactRect ->
34+
return rects.indexOf(exactRect)
35+
} ?: return HOST_ID
36+
}
37+
38+
/**
39+
* This is where we provide all the metadata for our virtual view.
40+
* We need to set the content description (or text, if it’s presented visually) and set the bounds in parent.
41+
*/
42+
override fun onPopulateNodeForVirtualView(virtualViewId: Int, node: AccessibilityNodeInfoCompat) {
43+
node.className = keyboardView::class.simpleName
44+
val key = keys.getOrNull(virtualViewId)
45+
node.contentDescription = key?.getContentDescription(keyboardView.context) ?: ""
46+
val bounds = updateBoundsForInterval(virtualViewId)
47+
node.setBoundsInParent(bounds)
48+
}
49+
50+
/**
51+
* We need to set the content description (or text, if it’s presented visually) and set the bounds in parent.
52+
* The bounds in the parent should match the logic in the onDraw() function.
53+
*/
54+
private fun updateBoundsForInterval(index: Int): Rect {
55+
val keys = keys
56+
val key = keys.getOrNull(index) ?: return Rect()
57+
return Rect().apply {
58+
left = key.x
59+
top = key.y
60+
right = key.x + key.width
61+
bottom = key.y + key.height
62+
}
63+
}
64+
65+
override fun onPerformActionForVirtualView(virtualViewId: Int, action: Int, arguments: Bundle?): Boolean {
66+
return false
67+
}
68+
}

app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/MyKeyboard.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class MyKeyboard {
4343
var mMinWidth = 0
4444

4545
/** List of keys in this keyboard */
46-
var mKeys: MutableList<Key?>? = null
46+
var mKeys: MutableList<Key>? = null
4747

4848
/** Width of the screen available to fit the keyboard */
4949
private var mDisplayWidth = 0

app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt

Lines changed: 6 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import android.content.Intent
1010
import android.graphics.*
1111
import android.graphics.Paint.Align
1212
import android.graphics.drawable.*
13-
import android.os.Bundle
1413
import android.os.Handler
1514
import android.os.Looper
1615
import android.os.Message
@@ -24,8 +23,6 @@ import android.widget.TextView
2423
import androidx.core.animation.doOnEnd
2524
import androidx.core.animation.doOnStart
2625
import androidx.core.view.ViewCompat
27-
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
28-
import androidx.customview.widget.ExploreByTouchHelper
2926
import androidx.emoji2.text.EmojiCompat
3027
import androidx.emoji2.text.EmojiCompat.EMOJI_SUPPORTED
3128
import com.simplemobiletools.commons.extensions.*
@@ -58,80 +55,14 @@ import java.util.*
5855
class 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

Comments
 (0)