Check for null layout in LinkAccessibilityHelper

Return an empty rect for getBoundsForSpan if the text layout is null.

Bug: 30554641
Change-Id: Ia5affea014bf994170291978f10690083d786577
(cherry picked from commit 07df2a743e3708ba57e601258053e1d52daac82e)
diff --git a/library/eclair-mr1/src/com/android/setupwizardlib/util/LinkAccessibilityHelper.java b/library/eclair-mr1/src/com/android/setupwizardlib/util/LinkAccessibilityHelper.java
index 2c53ee7..3e4b7e2 100644
--- a/library/eclair-mr1/src/com/android/setupwizardlib/util/LinkAccessibilityHelper.java
+++ b/library/eclair-mr1/src/com/android/setupwizardlib/util/LinkAccessibilityHelper.java
@@ -118,13 +118,11 @@
         info.setFocusable(true);
         info.setClickable(true);
         getBoundsForSpan(span, mTempRect);
-        if (!mTempRect.isEmpty()) {
-            info.setBoundsInParent(getBoundsForSpan(span, mTempRect));
-        } else {
+        if (mTempRect.isEmpty()) {
             Log.e(TAG, "LinkSpan bounds is empty for: " + virtualViewId);
             mTempRect.set(0, 0, 1, 1);
-            info.setBoundsInParent(mTempRect);
         }
+        info.setBoundsInParent(mTempRect);
         info.addAction(AccessibilityNodeInfoCompat.ACTION_CLICK);
     }
 
@@ -171,22 +169,24 @@
         CharSequence text = mView.getText();
         outRect.setEmpty();
         if (text instanceof Spanned) {
-            Spanned spannedText = (Spanned) text;
-            final int spanStart = spannedText.getSpanStart(span);
-            final int spanEnd = spannedText.getSpanEnd(span);
             final Layout layout = mView.getLayout();
-            final float xStart = layout.getPrimaryHorizontal(spanStart);
-            final float xEnd = layout.getPrimaryHorizontal(spanEnd);
-            final int lineStart = layout.getLineForOffset(spanStart);
-            final int lineEnd = layout.getLineForOffset(spanEnd);
-            layout.getLineBounds(lineStart, outRect);
-            outRect.left = (int) xStart;
-            if (lineEnd == lineStart) {
-                outRect.right = (int) xEnd;
-            } // otherwise just leave it at the end of the start line
+            if (layout != null) {
+                Spanned spannedText = (Spanned) text;
+                final int spanStart = spannedText.getSpanStart(span);
+                final int spanEnd = spannedText.getSpanEnd(span);
+                final float xStart = layout.getPrimaryHorizontal(spanStart);
+                final float xEnd = layout.getPrimaryHorizontal(spanEnd);
+                final int lineStart = layout.getLineForOffset(spanStart);
+                final int lineEnd = layout.getLineForOffset(spanEnd);
+                layout.getLineBounds(lineStart, outRect);
+                outRect.left = (int) xStart;
+                if (lineEnd == lineStart) {
+                    outRect.right = (int) xEnd;
+                } // otherwise just leave it at the end of the start line
 
-            // Offset for padding
-            outRect.offset(mView.getTotalPaddingLeft(), mView.getTotalPaddingTop());
+                // Offset for padding
+                outRect.offset(mView.getTotalPaddingLeft(), mView.getTotalPaddingTop());
+            }
         }
         return outRect;
     }
diff --git a/library/eclair-mr1/test/src/com/android/setupwizardlib/test/LinkAccessibilityHelperTest.java b/library/eclair-mr1/test/src/com/android/setupwizardlib/test/LinkAccessibilityHelperTest.java
index 8d42fa3..a129f65 100644
--- a/library/eclair-mr1/test/src/com/android/setupwizardlib/test/LinkAccessibilityHelperTest.java
+++ b/library/eclair-mr1/test/src/com/android/setupwizardlib/test/LinkAccessibilityHelperTest.java
@@ -121,6 +121,22 @@
         info.recycle();
     }
 
+    @SmallTest
+    public void testNullLayout() {
+        // Setting the padding will cause the layout to be null-ed out.
+        mTextView.setPadding(1, 1, 1, 1);
+
+        AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain();
+        mHelper.onPopulateNodeForVirtualView(0, info);
+
+        Rect bounds = new Rect();
+        info.getBoundsInParent(bounds);
+        assertEquals("LinkSpan bounds should be (0, 0, 1, 1)",
+                new Rect(0, 0, 1, 1), bounds);
+
+        info.recycle();
+    }
+
     private int dp2Px(float dp) {
         if (mDisplayMetrics == null) {
             mDisplayMetrics = getContext().getResources().getDisplayMetrics();