@@ -571,9 +571,18 @@ protected void onPopulateNodeForVirtualView(
571571 return ;
572572 }
573573
574+ // NOTE: The span may not actually have visible bounds within its parent,
575+ // due to line limits, etc.
576+ final Rect bounds = getBoundsInParent (accessibleTextSpan );
577+ if (bounds == null ) {
578+ node .setContentDescription ("" );
579+ node .setBoundsInParent (new Rect (0 , 0 , 1 , 1 ));
580+ return ;
581+ }
582+
574583 node .setContentDescription (accessibleTextSpan .description );
575584 node .addAction (AccessibilityNodeInfoCompat .ACTION_CLICK );
576- node .setBoundsInParent (getBoundsInParent ( accessibleTextSpan ) );
585+ node .setBoundsInParent (bounds );
577586 node .setRoleDescription (mView .getResources ().getString (R .string .link_description ));
578587 node .setClassName (AccessibilityRole .getValue (AccessibilityRole .BUTTON ));
579588 }
@@ -590,10 +599,19 @@ private Rect getBoundsInParent(AccessibilityLinks.AccessibleLink accessibleLink)
590599 return new Rect (0 , 0 , textView .getWidth (), textView .getHeight ());
591600 }
592601
593- Rect rootRect = new Rect ();
594-
595602 double startOffset = accessibleLink .start ;
596603 double endOffset = accessibleLink .end ;
604+
605+ // Ensure the link hasn't been ellipsized away; in such cases,
606+ // getPrimaryHorizontal will crash (and the link isn't rendered anyway).
607+ int startOffsetLineNumber = textViewLayout .getLineForOffset ((int ) startOffset );
608+ int lineEndOffset = textViewLayout .getLineEnd (startOffsetLineNumber );
609+ if (startOffset > lineEndOffset ) {
610+ return null ;
611+ }
612+
613+ Rect rootRect = new Rect ();
614+
597615 double startXCoordinates = textViewLayout .getPrimaryHorizontal ((int ) startOffset );
598616
599617 final Paint paint = new Paint ();
@@ -603,7 +621,6 @@ private Rect getBoundsInParent(AccessibilityLinks.AccessibleLink accessibleLink)
603621 paint .setTextSize (textSize );
604622 int textWidth = (int ) Math .ceil (paint .measureText (accessibleLink .description ));
605623
606- int startOffsetLineNumber = textViewLayout .getLineForOffset ((int ) startOffset );
607624 int endOffsetLineNumber = textViewLayout .getLineForOffset ((int ) endOffset );
608625 boolean isMultiline = startOffsetLineNumber != endOffsetLineNumber ;
609626 textViewLayout .getLineBounds (startOffsetLineNumber , rootRect );
0 commit comments