From 987a2b41503c4b186e1a895b5812b3b11003d206 Mon Sep 17 00:00:00 2001 From: Mihir Patel Date: Tue, 22 Jun 2021 11:35:32 -0700 Subject: [PATCH] Guarding against null pointer exception when setting view structure info for content capture Test: Manual, CTS Bug: 189329389 Change-Id: Iaaacd59dc377079847951ab635df9bb602467390 --- core/java/android/widget/TextView.java | 35 +++++++++++++++----------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 3c4fd5e935804..dc16ded089174 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -11848,23 +11848,28 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener // Get the text and trim it to the range we are reporting. CharSequence text = getText(); - if (expandedTopChar > 0 || expandedBottomChar < text.length()) { - text = text.subSequence(expandedTopChar, expandedBottomChar); - } - if (viewFor == VIEW_STRUCTURE_FOR_AUTOFILL) { - structure.setText(text); - } else { - structure.setText(text, selStart - expandedTopChar, selEnd - expandedTopChar); - - final int[] lineOffsets = new int[bottomLine - topLine + 1]; - final int[] lineBaselines = new int[bottomLine - topLine + 1]; - final int baselineOffset = getBaselineOffset(); - for (int i = topLine; i <= bottomLine; i++) { - lineOffsets[i - topLine] = layout.getLineStart(i); - lineBaselines[i - topLine] = layout.getLineBaseline(i) + baselineOffset; + if (text != null) { + if (expandedTopChar > 0 || expandedBottomChar < text.length()) { + text = text.subSequence(expandedTopChar, expandedBottomChar); + } + + if (viewFor == VIEW_STRUCTURE_FOR_AUTOFILL) { + structure.setText(text); + } else { + structure.setText(text, + selStart - expandedTopChar, + selEnd - expandedTopChar); + + final int[] lineOffsets = new int[bottomLine - topLine + 1]; + final int[] lineBaselines = new int[bottomLine - topLine + 1]; + final int baselineOffset = getBaselineOffset(); + for (int i = topLine; i <= bottomLine; i++) { + lineOffsets[i - topLine] = layout.getLineStart(i); + lineBaselines[i - topLine] = layout.getLineBaseline(i) + baselineOffset; + } + structure.setTextLines(lineOffsets, lineBaselines); } - structure.setTextLines(lineOffsets, lineBaselines); } }