From 7905ab2d91f8e028b17b2df0401708d5a5b9612e Mon Sep 17 00:00:00 2001 From: Ye Park Date: Fri, 27 Nov 2026 14:26:13 +0900 Subject: [PATCH 1/7] Let TalkBack move through prepared-layout Text by character and word --- .../ReactTextViewAccessibilityDelegate.kt | 74 +++++++++++++++++++ .../ReactTextViewAccessibilityDelegateTest.kt | 51 +++++++++++++ 2 files changed, 125 insertions(+) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt index 21c03932d37a..e5366c8f35f2 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt @@ -13,6 +13,7 @@ import android.text.Layout import android.text.Spanned import android.text.style.ClickableSpan import android.view.View +import android.view.accessibility.AccessibilityEvent import android.widget.TextView import androidx.core.view.ViewCompat import androidx.core.view.accessibility.AccessibilityNodeInfoCompat @@ -21,6 +22,7 @@ import com.facebook.react.R import com.facebook.react.common.annotations.UnstableReactNativeAPI import com.facebook.react.uimanager.ReactAccessibilityDelegate import com.facebook.react.views.text.internal.span.ReactClickableSpan +import java.text.BreakIterator @OptIn(UnstableReactNativeAPI::class) internal class ReactTextViewAccessibilityDelegate( @@ -29,6 +31,8 @@ internal class ReactTextViewAccessibilityDelegate( originalImportantForAccessibility: Int, ) : ReactAccessibilityDelegate(view, originalFocus, originalImportantForAccessibility) { private var accessibilityLinks: AccessibilityLinks? = null + private var textCursor = 0 + private var cursorText: String? = null init { accessibilityLinks = hostView.getTag(R.id.accessibility_links) as AccessibilityLinks? @@ -221,6 +225,76 @@ internal class ReactTextViewAccessibilityDelegate( // it is holding so TalkBack knows what to announce when focusing it. if (host is PreparedLayoutTextView) { info.text = host.text + if (host.contentDescription.isNullOrEmpty() && !host.text.isNullOrEmpty()) { + info.addAction(AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY) + info.addAction(AccessibilityNodeInfoCompat.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY) + info.movementGranularities = + AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER or + AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD + } + } + } + + override fun performAccessibilityAction(host: View, action: Int, args: Bundle?): Boolean { + if (host is PreparedLayoutTextView && host.contentDescription.isNullOrEmpty()) { + when (action) { + AccessibilityNodeInfoCompat.ACTION_SET_SELECTION -> cursorText = null + AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY, + AccessibilityNodeInfoCompat.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY -> + return traverseText( + host, + action, + args?.getInt(AccessibilityNodeInfoCompat.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT) + ?: 0, + ) + } + } + return super.performAccessibilityAction(host, action, args) + } + + private fun traverseText(host: PreparedLayoutTextView, action: Int, granularity: Int): Boolean { + val text = host.text ?: return false + val locale = host.resources.configuration.locales[0] + val breaks = + when (granularity) { + AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER -> + BreakIterator.getCharacterInstance(locale) + AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD -> + BreakIterator.getWordInstance(locale) + else -> return false + } + val string = text.toString() + breaks.setText(string) + val forward = action == AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY + var from = if (string == cursorText) textCursor else if (forward) 0 else text.length + while (true) { + val to = if (forward) breaks.following(from) else breaks.preceding(from) + if (to == BreakIterator.DONE) { + return false + } + val start = minOf(from, to) + val end = maxOf(from, to) + if ( + granularity == AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER || + (start until end).any { Character.isLetterOrDigit(Character.codePointAt(text, it)) } + ) { + textCursor = to + cursorText = string + @Suppress("DEPRECATION") + val event = + AccessibilityEvent.obtain( + AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY + ) + host.onInitializeAccessibilityEvent(event) + event.text.add(text) + event.fromIndex = start + event.toIndex = end + event.action = action + event.movementGranularity = granularity + host.parent?.requestSendAccessibilityEvent(host, event) + return true + } + from = to } } diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegateTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegateTest.kt index 2e854b6ee11c..2aa90430ea1f 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegateTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegateTest.kt @@ -9,6 +9,7 @@ package com.facebook.react.views.text import android.annotation.SuppressLint import android.graphics.Color +import android.os.Bundle import android.text.Layout import android.text.SpannableString import android.text.Spanned @@ -22,6 +23,8 @@ import android.text.style.LocaleSpan import android.text.style.TtsSpan import android.util.DisplayMetrics import android.view.View +import android.view.accessibility.AccessibilityEvent +import android.widget.FrameLayout import androidx.core.view.AccessibilityDelegateCompat import androidx.core.view.ViewCompat import androidx.core.view.accessibility.AccessibilityNodeInfoCompat @@ -192,6 +195,27 @@ class ReactTextViewAccessibilityDelegateTest { assertThat(nodeText.getSpanEnd(localeSpan)).isEqualTo(2) } + @Test + fun `prepared layout text steps by character and word`() { + val view = preparedLayoutTextView(SpannableString("Hello, big world")) + val steps = recordSteps(view) + + assertThat(createNodeInfo(view).movementGranularities).isNotZero() + repeat(3) { step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD) } + assertThat(step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD)).isFalse() + step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD, forward = false) + step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER) + view.performAccessibilityAction(AccessibilityNodeInfoCompat.ACTION_SET_SELECTION, null) + step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD) + view.preparedLayout = preparedLayoutTextView(SpannableString("Read the docs")).preparedLayout + step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD) + view.contentDescription = "Delete listing" + step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD) + + assertThat(steps) + .containsExactly("Hello", "big", "world", "world", "w", "Hello", "Read", "Delete") + } + private fun delegate(): AccessibilityDelegateCompat? = ViewCompat.getAccessibilityDelegate(view) private fun textWithLink(): Spanned = @@ -246,6 +270,33 @@ class ReactTextViewAccessibilityDelegateTest { ReactTextViewAccessibilityDelegate.updateAccessibilityLinks(view, text) } + private fun step(view: View, granularity: Int, forward: Boolean = true): Boolean = + view.performAccessibilityAction( + if (forward) AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY + else AccessibilityNodeInfoCompat.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY, + Bundle().apply { + putInt( + AccessibilityNodeInfoCompat.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT, + granularity, + ) + }, + ) + + private fun recordSteps(view: View): List { + val steps = mutableListOf() + object : FrameLayout(view.context) { + override fun requestSendAccessibilityEvent( + child: View, + event: AccessibilityEvent, + ): Boolean { + val text = event.text.firstOrNull() ?: event.contentDescription + return steps.add(text.toString().substring(event.fromIndex, event.toIndex)) + } + } + .addView(view) + return steps + } + private fun createNodeInfo(view: View): AccessibilityNodeInfoCompat = AccessibilityNodeInfoCompat.obtain().also { view.onInitializeAccessibilityNodeInfo(it.unwrap()) From b7c27b9d5f07bf941de28de1ad45be48a6176657 Mon Sep 17 00:00:00 2001 From: Ye Park Date: Fri, 27 Nov 2026 21:48:01 +0900 Subject: [PATCH 2/7] Cache the break iterator and drop traversal state when the view is recycled --- .../views/text/PreparedLayoutTextView.kt | 2 + .../ReactTextViewAccessibilityDelegate.kt | 44 +++++++++++++------ .../ReactTextViewAccessibilityDelegateTest.kt | 5 ++- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt index 14a80d7c9499..c6e6a9a82acb 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt @@ -111,6 +111,8 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re lastFrameTimeNanos = 0L preparedLayout = null hasClickableSpansCache = null + (ViewCompat.getAccessibilityDelegate(this) as? ReactTextViewAccessibilityDelegate) + ?.resetTextTraversal() } override fun onVisibilityChanged(changedView: View, visibility: Int) { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt index e5366c8f35f2..3623da1a878c 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt @@ -11,6 +11,7 @@ import android.graphics.Rect import android.os.Bundle import android.text.Layout import android.text.Spanned +import android.text.TextUtils import android.text.style.ClickableSpan import android.view.View import android.view.accessibility.AccessibilityEvent @@ -31,8 +32,10 @@ internal class ReactTextViewAccessibilityDelegate( originalImportantForAccessibility: Int, ) : ReactAccessibilityDelegate(view, originalFocus, originalImportantForAccessibility) { private var accessibilityLinks: AccessibilityLinks? = null - private var textCursor = 0 - private var cursorText: String? = null + private var traversedText: String? = null + private var textCursor = -1 + private var breaks: BreakIterator? = null + private var breaksGranularity = 0 init { accessibilityLinks = hostView.getTag(R.id.accessibility_links) as AccessibilityLinks? @@ -238,7 +241,7 @@ internal class ReactTextViewAccessibilityDelegate( override fun performAccessibilityAction(host: View, action: Int, args: Bundle?): Boolean { if (host is PreparedLayoutTextView && host.contentDescription.isNullOrEmpty()) { when (action) { - AccessibilityNodeInfoCompat.ACTION_SET_SELECTION -> cursorText = null + AccessibilityNodeInfoCompat.ACTION_SET_SELECTION -> textCursor = -1 AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY, AccessibilityNodeInfoCompat.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY -> return traverseText( @@ -252,21 +255,35 @@ internal class ReactTextViewAccessibilityDelegate( return super.performAccessibilityAction(host, action, args) } + internal fun resetTextTraversal() { + traversedText = null + textCursor = -1 + breaks = null + } + private fun traverseText(host: PreparedLayoutTextView, action: Int, granularity: Int): Boolean { val text = host.text ?: return false + if (!TextUtils.equals(text, traversedText)) { + resetTextTraversal() + traversedText = text.toString() + } + val string = traversedText ?: return false val locale = host.resources.configuration.locales[0] val breaks = - when (granularity) { - AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER -> - BreakIterator.getCharacterInstance(locale) - AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD -> - BreakIterator.getWordInstance(locale) - else -> return false - } - val string = text.toString() - breaks.setText(string) + this.breaks?.takeIf { breaksGranularity == granularity } + ?: when (granularity) { + AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER -> + BreakIterator.getCharacterInstance(locale) + AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD -> + BreakIterator.getWordInstance(locale) + else -> return false + }.also { + it.setText(string) + this.breaks = it + breaksGranularity = granularity + } val forward = action == AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY - var from = if (string == cursorText) textCursor else if (forward) 0 else text.length + var from = if (textCursor >= 0) textCursor else if (forward) 0 else string.length while (true) { val to = if (forward) breaks.following(from) else breaks.preceding(from) if (to == BreakIterator.DONE) { @@ -279,7 +296,6 @@ internal class ReactTextViewAccessibilityDelegate( (start until end).any { Character.isLetterOrDigit(Character.codePointAt(text, it)) } ) { textCursor = to - cursorText = string @Suppress("DEPRECATION") val event = AccessibilityEvent.obtain( diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegateTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegateTest.kt index 2aa90430ea1f..3a74767580e2 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegateTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegateTest.kt @@ -209,11 +209,14 @@ class ReactTextViewAccessibilityDelegateTest { step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD) view.preparedLayout = preparedLayoutTextView(SpannableString("Read the docs")).preparedLayout step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD) + view.recycleView() + view.preparedLayout = preparedLayoutTextView(SpannableString("Read the docs")).preparedLayout + step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD) view.contentDescription = "Delete listing" step(view, AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD) assertThat(steps) - .containsExactly("Hello", "big", "world", "world", "w", "Hello", "Read", "Delete") + .containsExactly("Hello", "big", "world", "world", "w", "Hello", "Read", "Read", "Delete") } private fun delegate(): AccessibilityDelegateCompat? = ViewCompat.getAccessibilityDelegate(view) From f31b5b36b8d8676c66bab191d6d359f97d796a92 Mon Sep 17 00:00:00 2001 From: Ye Park Date: Fri, 27 Nov 2026 21:54:25 +0900 Subject: [PATCH 3/7] Reset text traversal where the text changes instead of copying it --- .../react/views/text/PreparedLayoutTextView.kt | 7 +++++-- .../views/text/ReactTextViewAccessibilityDelegate.kt | 12 ++---------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt index c6e6a9a82acb..68d9754a0b17 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt @@ -15,6 +15,7 @@ import android.graphics.Rect import android.os.Build import android.text.Layout import android.text.Spanned +import android.text.TextUtils import android.text.style.ClickableSpan import android.view.KeyEvent import android.view.MotionEvent @@ -75,6 +76,10 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re } hasClickableSpansCache = null + if (!TextUtils.equals(field?.sourceText, effectiveValue?.sourceText)) { + (ViewCompat.getAccessibilityDelegate(this) as? ReactTextViewAccessibilityDelegate) + ?.resetTextTraversal() + } field = effectiveValue invalidate() @@ -111,8 +116,6 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re lastFrameTimeNanos = 0L preparedLayout = null hasClickableSpansCache = null - (ViewCompat.getAccessibilityDelegate(this) as? ReactTextViewAccessibilityDelegate) - ?.resetTextTraversal() } override fun onVisibilityChanged(changedView: View, visibility: Int) { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt index 3623da1a878c..f039bea9d6ed 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt @@ -11,7 +11,6 @@ import android.graphics.Rect import android.os.Bundle import android.text.Layout import android.text.Spanned -import android.text.TextUtils import android.text.style.ClickableSpan import android.view.View import android.view.accessibility.AccessibilityEvent @@ -32,7 +31,6 @@ internal class ReactTextViewAccessibilityDelegate( originalImportantForAccessibility: Int, ) : ReactAccessibilityDelegate(view, originalFocus, originalImportantForAccessibility) { private var accessibilityLinks: AccessibilityLinks? = null - private var traversedText: String? = null private var textCursor = -1 private var breaks: BreakIterator? = null private var breaksGranularity = 0 @@ -256,18 +254,12 @@ internal class ReactTextViewAccessibilityDelegate( } internal fun resetTextTraversal() { - traversedText = null textCursor = -1 breaks = null } private fun traverseText(host: PreparedLayoutTextView, action: Int, granularity: Int): Boolean { val text = host.text ?: return false - if (!TextUtils.equals(text, traversedText)) { - resetTextTraversal() - traversedText = text.toString() - } - val string = traversedText ?: return false val locale = host.resources.configuration.locales[0] val breaks = this.breaks?.takeIf { breaksGranularity == granularity } @@ -278,12 +270,12 @@ internal class ReactTextViewAccessibilityDelegate( BreakIterator.getWordInstance(locale) else -> return false }.also { - it.setText(string) + it.setText(text.toString()) this.breaks = it breaksGranularity = granularity } val forward = action == AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY - var from = if (textCursor >= 0) textCursor else if (forward) 0 else string.length + var from = if (textCursor >= 0) textCursor else if (forward) 0 else text.length while (true) { val to = if (forward) breaks.following(from) else breaks.preceding(from) if (to == BreakIterator.DONE) { From 1e8127b3698fbbf415fc0a719d04df718f38e62d Mon Sep 17 00:00:00 2001 From: Ye Park Date: Mon, 30 Nov 2026 20:24:53 +0900 Subject: [PATCH 4/7] Hold the text traversal state in one optional nested class --- .../ReactTextViewAccessibilityDelegate.kt | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt index f039bea9d6ed..5280e7293111 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt @@ -31,9 +31,7 @@ internal class ReactTextViewAccessibilityDelegate( originalImportantForAccessibility: Int, ) : ReactAccessibilityDelegate(view, originalFocus, originalImportantForAccessibility) { private var accessibilityLinks: AccessibilityLinks? = null - private var textCursor = -1 - private var breaks: BreakIterator? = null - private var breaksGranularity = 0 + private var textTraversal: TextTraversal? = null init { accessibilityLinks = hostView.getTag(R.id.accessibility_links) as AccessibilityLinks? @@ -239,7 +237,7 @@ internal class ReactTextViewAccessibilityDelegate( override fun performAccessibilityAction(host: View, action: Int, args: Bundle?): Boolean { if (host is PreparedLayoutTextView && host.contentDescription.isNullOrEmpty()) { when (action) { - AccessibilityNodeInfoCompat.ACTION_SET_SELECTION -> textCursor = -1 + AccessibilityNodeInfoCompat.ACTION_SET_SELECTION -> textTraversal?.cursor = -1 AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY, AccessibilityNodeInfoCompat.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY -> return traverseText( @@ -254,28 +252,29 @@ internal class ReactTextViewAccessibilityDelegate( } internal fun resetTextTraversal() { - textCursor = -1 - breaks = null + textTraversal = null } private fun traverseText(host: PreparedLayoutTextView, action: Int, granularity: Int): Boolean { val text = host.text ?: return false - val locale = host.resources.configuration.locales[0] - val breaks = - this.breaks?.takeIf { breaksGranularity == granularity } - ?: when (granularity) { - AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER -> - BreakIterator.getCharacterInstance(locale) - AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD -> - BreakIterator.getWordInstance(locale) - else -> return false - }.also { - it.setText(text.toString()) - this.breaks = it - breaksGranularity = granularity - } + var traversal = textTraversal + if (traversal == null || traversal.granularity != granularity) { + val locale = host.resources.configuration.locales[0] + val breaks = + when (granularity) { + AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER -> + BreakIterator.getCharacterInstance(locale) + AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD -> + BreakIterator.getWordInstance(locale) + else -> return false + } + breaks.setText(text.toString()) + traversal = TextTraversal(breaks, granularity, traversal?.cursor ?: -1) + textTraversal = traversal + } + val breaks = traversal.breaks val forward = action == AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY - var from = if (textCursor >= 0) textCursor else if (forward) 0 else text.length + var from = if (traversal.cursor >= 0) traversal.cursor else if (forward) 0 else text.length while (true) { val to = if (forward) breaks.following(from) else breaks.preceding(from) if (to == BreakIterator.DONE) { @@ -287,7 +286,7 @@ internal class ReactTextViewAccessibilityDelegate( granularity == AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER || (start until end).any { Character.isLetterOrDigit(Character.codePointAt(text, it)) } ) { - textCursor = to + traversal.cursor = to @Suppress("DEPRECATION") val event = AccessibilityEvent.obtain( @@ -404,6 +403,8 @@ internal class ReactTextViewAccessibilityDelegate( return null } + private class TextTraversal(val breaks: BreakIterator, val granularity: Int, var cursor: Int) + class AccessibilityLinks(text: Spanned) { private val links: List From ffbab7b53ac4256637341f63b80daf8680df8a21 Mon Sep 17 00:00:00 2001 From: Ye Park Date: Mon, 30 Nov 2026 21:46:57 +0900 Subject: [PATCH 5/7] Use the default locale for break iterators, as TextTransform does --- .../react/views/text/ReactTextViewAccessibilityDelegate.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt index 5280e7293111..00511ec8a134 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt @@ -259,13 +259,11 @@ internal class ReactTextViewAccessibilityDelegate( val text = host.text ?: return false var traversal = textTraversal if (traversal == null || traversal.granularity != granularity) { - val locale = host.resources.configuration.locales[0] val breaks = when (granularity) { AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER -> - BreakIterator.getCharacterInstance(locale) - AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD -> - BreakIterator.getWordInstance(locale) + BreakIterator.getCharacterInstance() + AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD -> BreakIterator.getWordInstance() else -> return false } breaks.setText(text.toString()) From 29f7c246cf28cbe3b4cf4f6821146b50dc364219 Mon Sep 17 00:00:00 2001 From: Ye Park Date: Mon, 30 Nov 2026 22:45:52 +0900 Subject: [PATCH 6/7] Create the text traversal with the cached-field idiom the text package uses --- .../ReactTextViewAccessibilityDelegate.kt | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt index 00511ec8a134..3fd8295e2b3d 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt @@ -257,19 +257,20 @@ internal class ReactTextViewAccessibilityDelegate( private fun traverseText(host: PreparedLayoutTextView, action: Int, granularity: Int): Boolean { val text = host.text ?: return false - var traversal = textTraversal - if (traversal == null || traversal.granularity != granularity) { - val breaks = - when (granularity) { - AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER -> - BreakIterator.getCharacterInstance() - AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD -> BreakIterator.getWordInstance() - else -> return false - } - breaks.setText(text.toString()) - traversal = TextTraversal(breaks, granularity, traversal?.cursor ?: -1) - textTraversal = traversal - } + val traversal = + textTraversal?.takeIf { it.granularity == granularity } + ?: TextTraversal( + when (granularity) { + AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER -> + BreakIterator.getCharacterInstance() + AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD -> + BreakIterator.getWordInstance() + else -> return false + }.apply { setText(text.toString()) }, + granularity, + textTraversal?.cursor ?: -1, + ) + .also { textTraversal = it } val breaks = traversal.breaks val forward = action == AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY var from = if (traversal.cursor >= 0) traversal.cursor else if (forward) 0 else text.length From 81b226b23e6d986d4de0b3a61cb4abd971f7bffe Mon Sep 17 00:00:00 2001 From: Ye Park Date: Mon, 30 Nov 2026 22:57:36 +0900 Subject: [PATCH 7/7] Drop the break iterator alias --- .../react/views/text/ReactTextViewAccessibilityDelegate.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt index 3fd8295e2b3d..dda5515dc93d 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt @@ -271,11 +271,10 @@ internal class ReactTextViewAccessibilityDelegate( textTraversal?.cursor ?: -1, ) .also { textTraversal = it } - val breaks = traversal.breaks val forward = action == AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY var from = if (traversal.cursor >= 0) traversal.cursor else if (forward) 0 else text.length while (true) { - val to = if (forward) breaks.following(from) else breaks.preceding(from) + val to = if (forward) traversal.breaks.following(from) else traversal.breaks.preceding(from) if (to == BreakIterator.DONE) { return false }