From cbe8bc36eb6f180dbdfb764679105612c5d0b363 Mon Sep 17 00:00:00 2001 From: Ahaan Ugale Date: Thu, 12 Aug 2021 14:08:52 -0700 Subject: Fix OOB crash in ContentCapture for translated views When a view is partially visible on the screen, ContentCapture reports only the visible portion (+ a few additional lines). The offsets calculated for this can be out of bounds if the view's displayed text is longer from the original text. Fix: 196414491 Test: manual - translate app to lang with more characters and trigger a relayout (by scrolling for an app with ListView) Test: atest CtsContentCaptureServiceTestCases Change-Id: Iae98133c48cc67a0b00f1b0ab8b93e5adb293423 (cherry picked from commit 273a0eabc8faa46349c63a6836ae4004709aeb0b) --- core/java/android/widget/TextView.java | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'core/java/android/widget/TextView.java') diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index ca6e735f86b4..f5c1bcf2de42 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -11857,6 +11857,15 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener if (text != null) { if (expandedTopChar > 0 || expandedBottomChar < text.length()) { + // Cap the offsets to avoid an OOB exception. That can happen if the + // displayed/layout text, on which these offsets are calculated, is longer + // than the original text (such as when the view is translated by the + // platform intelligence). + // TODO(b/196433694): Figure out how to better handle the offset + // calculations for this case (so we don't unnecessarily cutoff the original + // text, for example). + expandedTopChar = Math.min(expandedTopChar, text.length()); + expandedBottomChar = Math.min(expandedBottomChar, text.length()); text = text.subSequence(expandedTopChar, expandedBottomChar); } -- cgit v1.2.3