Skip to content

Commit d5b482e

Browse files
authored
Let TalkBack move through prepared-layout Text by character and word
## Summary: On Android with prepared text layout (`enablePreparedTextLayout`), TalkBack can't move through a `Text` by character or word: those reading controls skip past it, so a screen reader user can't spell out an address or reread a word. Without prepared text layout, `Text` supports this. This lets TalkBack move forward and back through prepared-layout text by character and word, reading each one. When a `Text` has an accessibility label, TalkBack moves through the label instead. Moving by line or paragraph still skips the text. ## Changelog: [ANDROID] [FIXED] - TalkBack can move through Text by character and word with prepared text layout ## Test Plan: A new test in `ReactTextViewAccessibilityDelegateTest`, checked through the traversal events TalkBack reads: word steps go forward and back, skip punctuation and stop at the end; a character step reads one character; TalkBack's selection reset and new text start again from the beginning; and with a content description, `View` steps through the label instead. `./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests "*ReactTextViewAccessibilityDelegateTest*"`: 11 tests, 0 failures. Differential Revision: D135339453 Pull Request resolved: #60327
1 parent c3c3b7b commit d5b482e

3 files changed

Lines changed: 140 additions & 0 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import android.graphics.Rect
1515
import android.os.Build
1616
import android.text.Layout
1717
import android.text.Spanned
18+
import android.text.TextUtils
1819
import android.text.style.ClickableSpan
1920
import android.view.KeyEvent
2021
import android.view.MotionEvent
@@ -75,6 +76,10 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re
7576
}
7677

7778
hasClickableSpansCache = null
79+
if (!TextUtils.equals(field?.sourceText, effectiveValue?.sourceText)) {
80+
(ViewCompat.getAccessibilityDelegate(this) as? ReactTextViewAccessibilityDelegate)
81+
?.resetTextTraversal()
82+
}
7883

7984
field = effectiveValue
8085
invalidate()

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import android.text.Layout
1313
import android.text.Spanned
1414
import android.text.style.ClickableSpan
1515
import android.view.View
16+
import android.view.accessibility.AccessibilityEvent
1617
import android.widget.TextView
1718
import androidx.core.view.ViewCompat
1819
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
@@ -21,6 +22,7 @@ import com.facebook.react.R
2122
import com.facebook.react.common.annotations.UnstableReactNativeAPI
2223
import com.facebook.react.uimanager.ReactAccessibilityDelegate
2324
import com.facebook.react.views.text.internal.span.ReactClickableSpan
25+
import java.text.BreakIterator
2426

2527
@OptIn(UnstableReactNativeAPI::class)
2628
internal class ReactTextViewAccessibilityDelegate(
@@ -29,6 +31,7 @@ internal class ReactTextViewAccessibilityDelegate(
2931
originalImportantForAccessibility: Int,
3032
) : ReactAccessibilityDelegate(view, originalFocus, originalImportantForAccessibility) {
3133
private var accessibilityLinks: AccessibilityLinks? = null
34+
private var textTraversal: TextTraversal? = null
3235

3336
init {
3437
accessibilityLinks = hostView.getTag(R.id.accessibility_links) as AccessibilityLinks?
@@ -221,6 +224,82 @@ internal class ReactTextViewAccessibilityDelegate(
221224
// it is holding so TalkBack knows what to announce when focusing it.
222225
if (host is PreparedLayoutTextView) {
223226
info.text = host.text
227+
if (host.contentDescription.isNullOrEmpty() && !host.text.isNullOrEmpty()) {
228+
info.addAction(AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY)
229+
info.addAction(AccessibilityNodeInfoCompat.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY)
230+
info.movementGranularities =
231+
AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER or
232+
AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD
233+
}
234+
}
235+
}
236+
237+
override fun performAccessibilityAction(host: View, action: Int, args: Bundle?): Boolean {
238+
if (host is PreparedLayoutTextView && host.contentDescription.isNullOrEmpty()) {
239+
when (action) {
240+
AccessibilityNodeInfoCompat.ACTION_SET_SELECTION -> textTraversal?.cursor = -1
241+
AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
242+
AccessibilityNodeInfoCompat.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY ->
243+
return traverseText(
244+
host,
245+
action,
246+
args?.getInt(AccessibilityNodeInfoCompat.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT)
247+
?: 0,
248+
)
249+
}
250+
}
251+
return super.performAccessibilityAction(host, action, args)
252+
}
253+
254+
internal fun resetTextTraversal() {
255+
textTraversal = null
256+
}
257+
258+
private fun traverseText(host: PreparedLayoutTextView, action: Int, granularity: Int): Boolean {
259+
val text = host.text ?: return false
260+
val traversal =
261+
textTraversal?.takeIf { it.granularity == granularity }
262+
?: TextTraversal(
263+
when (granularity) {
264+
AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER ->
265+
BreakIterator.getCharacterInstance()
266+
AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD ->
267+
BreakIterator.getWordInstance()
268+
else -> return false
269+
}.apply { setText(text.toString()) },
270+
granularity,
271+
textTraversal?.cursor ?: -1,
272+
)
273+
.also { textTraversal = it }
274+
val forward = action == AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY
275+
var from = if (traversal.cursor >= 0) traversal.cursor else if (forward) 0 else text.length
276+
while (true) {
277+
val to = if (forward) traversal.breaks.following(from) else traversal.breaks.preceding(from)
278+
if (to == BreakIterator.DONE) {
279+
return false
280+
}
281+
val start = minOf(from, to)
282+
val end = maxOf(from, to)
283+
if (
284+
granularity == AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER ||
285+
(start until end).any { Character.isLetterOrDigit(Character.codePointAt(text, it)) }
286+
) {
287+
traversal.cursor = to
288+
@Suppress("DEPRECATION")
289+
val event =
290+
AccessibilityEvent.obtain(
291+
AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY
292+
)
293+
host.onInitializeAccessibilityEvent(event)
294+
event.text.add(text)
295+
event.fromIndex = start
296+
event.toIndex = end
297+
event.action = action
298+
event.movementGranularity = granularity
299+
host.parent?.requestSendAccessibilityEvent(host, event)
300+
return true
301+
}
302+
from = to
224303
}
225304
}
226305

@@ -322,6 +401,8 @@ internal class ReactTextViewAccessibilityDelegate(
322401
return null
323402
}
324403

404+
private class TextTraversal(val breaks: BreakIterator, val granularity: Int, var cursor: Int)
405+
325406
class AccessibilityLinks(text: Spanned) {
326407
private val links: List<AccessibleLink>
327408

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegateTest.kt‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package com.facebook.react.views.text
99

1010
import android.annotation.SuppressLint
1111
import android.graphics.Color
12+
import android.os.Bundle
1213
import android.text.Layout
1314
import android.text.SpannableString
1415
import android.text.Spanned
@@ -22,6 +23,8 @@ import android.text.style.LocaleSpan
2223
import android.text.style.TtsSpan
2324
import android.util.DisplayMetrics
2425
import android.view.View
26+
import android.view.accessibility.AccessibilityEvent
27+
import android.widget.FrameLayout
2528
import androidx.core.view.AccessibilityDelegateCompat
2629
import androidx.core.view.ViewCompat
2730
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
@@ -192,6 +195,30 @@ class ReactTextViewAccessibilityDelegateTest {
192195
assertThat(nodeText.getSpanEnd(localeSpan)).isEqualTo(2)
193196
}
194197

198+
@Test
199+
fun `prepared layout text steps by character and word`() {
200+
val view = preparedLayoutTextView(SpannableString("Hello, big world"))
201+
val steps = recordSteps(view)
202+
203+
assertThat(createNodeInfo(view).movementGranularities).isNotZero()
204+
repeat(3) { step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD) }
205+
assertThat(step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD)).isFalse()
206+
step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD, forward = false)
207+
step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER)
208+
view.performAccessibilityAction(AccessibilityNodeInfoCompat.ACTION_SET_SELECTION, null)
209+
step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD)
210+
view.preparedLayout = preparedLayoutTextView(SpannableString("Read the docs")).preparedLayout
211+
step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD)
212+
view.recycleView()
213+
view.preparedLayout = preparedLayoutTextView(SpannableString("Read the docs")).preparedLayout
214+
step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD)
215+
view.contentDescription = "Delete listing"
216+
step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD)
217+
218+
assertThat(steps)
219+
.containsExactly("Hello", "big", "world", "world", "w", "Hello", "Read", "Read", "Delete")
220+
}
221+
195222
private fun delegate(): AccessibilityDelegateCompat? = ViewCompat.getAccessibilityDelegate(view)
196223

197224
private fun textWithLink(): Spanned =
@@ -246,6 +273,33 @@ class ReactTextViewAccessibilityDelegateTest {
246273
ReactTextViewAccessibilityDelegate.updateAccessibilityLinks(view, text)
247274
}
248275

276+
private fun step(view: View, granularity: Int, forward: Boolean = true): Boolean =
277+
view.performAccessibilityAction(
278+
if (forward) AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY
279+
else AccessibilityNodeInfoCompat.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
280+
Bundle().apply {
281+
putInt(
282+
AccessibilityNodeInfoCompat.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT,
283+
granularity,
284+
)
285+
},
286+
)
287+
288+
private fun recordSteps(view: View): List<String> {
289+
val steps = mutableListOf<String>()
290+
object : FrameLayout(view.context) {
291+
override fun requestSendAccessibilityEvent(
292+
child: View,
293+
event: AccessibilityEvent,
294+
): Boolean {
295+
val text = event.text.firstOrNull() ?: event.contentDescription
296+
return steps.add(text.toString().substring(event.fromIndex, event.toIndex))
297+
}
298+
}
299+
.addView(view)
300+
return steps
301+
}
302+
249303
private fun createNodeInfo(view: View): AccessibilityNodeInfoCompat =
250304
AccessibilityNodeInfoCompat.obtain().also {
251305
view.onInitializeAccessibilityNodeInfo(it.unwrap())

0 commit comments

Comments
 (0)