From 84a1cacfa5cdaf484b93d1d9402c65d0c7084fad Mon Sep 17 00:00:00 2001 From: YuTeh Shen Date: Thu, 4 Apr 2019 14:02:33 -0700 Subject: [PATCH] Fix crash if set text and set selection at the same time. (#22723) Summary: Since text and selection has dependency, handle text selection in updateExtraData as well. The root cause is due to setText is handled on extra data update but setSelection is handled on set property. And extra data update will be handled after all properties are handled. Since selection and text has dependency, move selection to extra data update as well. Changelog: ---------- [Android] [Fixed] - Fix crash when set text and selection on textinput at the same time Pull Request resolved: https://github.com/facebook/react-native/pull/22723 Differential Revision: D14783791 Pulled By: cpojer fbshipit-source-id: a4065f3e151d23f4813d76e91d68362cfd4daaf4 --- .../react/views/text/ReactTextUpdate.java | 43 ++++++++++++++++++- .../textinput/ReactTextInputManager.java | 13 +----- .../textinput/ReactTextInputShadowNode.java | 21 ++++++++- 3 files changed, 64 insertions(+), 13 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextUpdate.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextUpdate.java index fd1344f0fb7..26957a58f19 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextUpdate.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextUpdate.java @@ -26,6 +26,8 @@ public class ReactTextUpdate { private final float mPaddingBottom; private final int mTextAlign; private final int mTextBreakStrategy; + private final int mSelectionStart; + private final int mSelectionEnd; private final int mJustificationMode; /** @@ -51,7 +53,9 @@ public class ReactTextUpdate { paddingBottom, textAlign, Layout.BREAK_STRATEGY_HIGH_QUALITY, - Layout.JUSTIFICATION_MODE_NONE); + Layout.JUSTIFICATION_MODE_NONE, + -1, + -1); } public ReactTextUpdate( @@ -65,6 +69,33 @@ public class ReactTextUpdate { int textAlign, int textBreakStrategy, int justificationMode) { + this(text, + jsEventCounter, + containsImages, + paddingStart, + paddingTop, + paddingEnd, + paddingBottom, + textAlign, + textBreakStrategy, + justificationMode, + -1, + -1); + } + + public ReactTextUpdate( + Spannable text, + int jsEventCounter, + boolean containsImages, + float paddingStart, + float paddingTop, + float paddingEnd, + float paddingBottom, + int textAlign, + int textBreakStrategy, + int justificationMode, + int selectionStart, + int selectionEnd) { mText = text; mJsEventCounter = jsEventCounter; mContainsImages = containsImages; @@ -74,6 +105,8 @@ public class ReactTextUpdate { mPaddingBottom = paddingBottom; mTextAlign = textAlign; mTextBreakStrategy = textBreakStrategy; + mSelectionStart = selectionStart; + mSelectionEnd = selectionEnd; mJustificationMode = justificationMode; } @@ -116,4 +149,12 @@ public class ReactTextUpdate { public int getJustificationMode() { return mJustificationMode; } + + public int getSelectionStart() { + return mSelectionStart; + } + + public int getSelectionEnd() { + return mSelectionEnd; + } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java index 60392088f26..84b63495473 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java @@ -202,6 +202,8 @@ public class ReactTextInputManager extends BaseViewManager