From 9e1c67e861302f0df55859c422000b437cef2027 Mon Sep 17 00:00:00 2001 From: Svet Ganov Date: Tue, 14 Oct 2014 08:53:33 -0700 Subject: [PATCH] Optimize text rendering in accessibility mode. In accessibility mode when iterating over the text of a TextView we use the selection to keep track of the current position. Consequentally, if the text of a TextView does not support selection we change the text to Spannable. Doing that has performance cost. While we need selection support before we used to convert the text to Spannable even if we do not need to. Now this transformation happens only when the user decides to traverse the text which is very rare as opposed to doing this for every TextView. bug:17491082 Change-Id: Id7e82e01034e439b5d34133b9350a4efc4d19d4a --- core/java/android/widget/TextView.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 5cdee53abb151..0917b32e89d48 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -8518,6 +8518,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } return false; case AccessibilityNodeInfo.ACTION_SET_SELECTION: { if (isFocused() && canSelectText()) { + ensureIterableTextForAccessibilitySelectable(); CharSequence text = getIterableTextForAccessibility(); if (text == null) { return false; @@ -8543,6 +8544,11 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } } } return false; + case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: + case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: { + ensureIterableTextForAccessibilitySelectable(); + return super.performAccessibilityAction(action, arguments); + } default: { return super.performAccessibilityAction(action, arguments); } @@ -9032,10 +9038,13 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener */ @Override public CharSequence getIterableTextForAccessibility() { + return mText; + } + + private void ensureIterableTextForAccessibilitySelectable() { if (!(mText instanceof Spannable)) { setText(mText, BufferType.SPANNABLE); } - return mText; } /**