From f0734c141f590ab39fad50d5cad2cb3b523ed25a Mon Sep 17 00:00:00 2001 From: Ahmed El-Helw Date: Thu, 16 Jun 2016 13:25:58 -0700 Subject: [PATCH] Fix Nodes crash when text is not spanned Summary: A Layout's text can either be an Ellipsizer or a SpannedEllipsizer. SpannedEllipsizer implements Spannable, but Ellipsizer doesn't. We were casting the Layout's text directly to a Spanned without first checking as to whether or not it was actually a Spanned. Reviewed By: sriramramani Differential Revision: D3435075 --- .../facebook/react/flat/TextNodeRegion.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/flat/TextNodeRegion.java b/ReactAndroid/src/main/java/com/facebook/react/flat/TextNodeRegion.java index 1bfed8e6af0..b556b362f93 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/flat/TextNodeRegion.java +++ b/ReactAndroid/src/main/java/com/facebook/react/flat/TextNodeRegion.java @@ -39,19 +39,22 @@ import android.text.Spanned; /* package */ int getReactTag(float touchX, float touchY) { if (mLayout != null) { - int y = Math.round(touchY - mTop); - if (y >= mLayout.getLineTop(0) && y < mLayout.getLineBottom(mLayout.getLineCount() - 1)) { - float x = Math.round(touchX - mLeft); - int line = mLayout.getLineForVertical(y); + CharSequence text = mLayout.getText(); + if (text instanceof Spanned) { + int y = Math.round(touchY - mTop); + if (y >= mLayout.getLineTop(0) && y < mLayout.getLineBottom(mLayout.getLineCount() - 1)) { + float x = Math.round(touchX - mLeft); + int line = mLayout.getLineForVertical(y); - if (mLayout.getLineLeft(line) <= x && x <= mLayout.getLineRight(line)) { - int off = mLayout.getOffsetForHorizontal(line, x); + if (mLayout.getLineLeft(line) <= x && x <= mLayout.getLineRight(line)) { + int off = mLayout.getOffsetForHorizontal(line, x); - Spanned text = (Spanned) mLayout.getText(); - RCTRawText[] link = text.getSpans(off, off, RCTRawText.class); + Spanned spanned = (Spanned) text; + RCTRawText[] link = spanned.getSpans(off, off, RCTRawText.class); - if (link.length != 0) { - return link[0].getReactTag(); + if (link.length != 0) { + return link[0].getReactTag(); + } } } }