From 07a597ad185c8c31ac38bdd4d022b0b880d02859 Mon Sep 17 00:00:00 2001 From: Lulu Wu Date: Fri, 28 Aug 2020 01:42:18 -0700 Subject: [PATCH] Fix Xiaomi TextInput crash in native Summary: Long term fix in native for Error: android_crash:java.lang.NullPointerException:android.widget.Editor$SelectionModifierCursorController.access$300 For more detail please see T68183343 D23301714 Changelog: [Android][Changed] - Fix Xiaomi TextInput crash in native Reviewed By: mdvacca Differential Revision: D23331828 fbshipit-source-id: 914f2d431772f49711b940d47a2b3ef57ab82cdc --- Libraries/Components/TextInput/TextInput.js | 3 +++ .../react/views/textinput/ReactEditText.java | 20 ++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Libraries/Components/TextInput/TextInput.js b/Libraries/Components/TextInput/TextInput.js index 654765e7c79..b510773d822 100644 --- a/Libraries/Components/TextInput/TextInput.js +++ b/Libraries/Components/TextInput/TextInput.js @@ -486,6 +486,9 @@ export type Props = $ReadOnly<{| * The following values work on Android only: * * - `visible-password` + * + * On Android devices manufactured by Xiaomi with Android Q, 'email-address' + * type will be replaced in native by 'default' to prevent a system related crash. */ keyboardType?: ?KeyboardType, diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java index cbda60c185b..c0cb0df07b8 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java @@ -389,11 +389,25 @@ public class ReactEditText extends AppCompatEditText @Override public void setInputType(int type) { Typeface tf = super.getTypeface(); - super.setInputType(type); - mStagedInputType = type; // Input type password defaults to monospace font, so we need to re-apply the font super.setTypeface(tf); + int inputType = type; + + // Set InputType to TYPE_CLASS_TEXT (the default one for Android) to fix a crash on Xiaomi + // devices with Android Q. This crash happens when focusing on a email EditText within a + // ScrollView, a prompt will be triggered but the system fail to locate it properly. + // Here is an example post discussing about this issue: + // https://github.com/facebook/react-native/issues/27204 + if (inputType == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS + && Build.VERSION.SDK_INT == Build.VERSION_CODES.Q + && Build.MANUFACTURER.startsWith("Xiaomi")) { + inputType = InputType.TYPE_CLASS_TEXT; + } + + super.setInputType(inputType); + mStagedInputType = inputType; + /** * If set forces multiline on input, because of a restriction on Android source that enables * multiline only for inputs of type Text and Multiline on method {@link @@ -407,7 +421,7 @@ public class ReactEditText extends AppCompatEditText // We override the KeyListener so that all keys on the soft input keyboard as well as hardware // keyboards work. Some KeyListeners like DigitsKeyListener will display the keyboard but not // accept all input from it - mKeyListener.setInputType(type); + mKeyListener.setInputType(inputType); setKeyListener(mKeyListener); }