From d45142abfdca8b36044058fcc647fd7e478d49bd Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Wed, 25 Jan 2023 19:39:21 -0800 Subject: [PATCH] Mitigation for Samsung TextInput Hangs (#35967) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35967 In https://github.com/facebook/react-native/issues/35936 we observed that the presence of AbsoluteSizeSpan may lead to hangs when using the Grammarly keyboard on Samsung. This mitigation makes it so that we do not emit this span in any case where it is sufficient to rely on already set EditText textSize. In simple cases, tested on two devices, it causes typing into the TextInput to no longer hang. This does not fully resolve the issue for TextInputs which meaningfully use layout-effecting spans (or at least font size), such as non-uniform text size within the input. We instead just try to reduce to minimum AbsoluteSizeSpan possible. Testing the first commit was able to resolve hangs in some simpler inputs tested, by me and cortinico. Changelog: [Android][Fixed] - Mitigation for Samsung TextInput Hangs Reviewed By: cortinico Differential Revision: D42721684 fbshipit-source-id: ec15bf996a6918cadbb53f9d371281e83bb6208e --- .../react/views/textinput/ReactEditText.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) 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 14863e498018c1..d84fa1b7723501 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 @@ -585,6 +585,10 @@ public void maybeSetText(ReactTextUpdate reactTextUpdate) { new SpannableStringBuilder(reactTextUpdate.getText()); manageSpans(spannableStringBuilder, reactTextUpdate.mContainsMultipleFragments); + + // Mitigation for https://github.com/facebook/react-native/issues/35936 (S318090) + stripAbsoluteSizeSpans(spannableStringBuilder); + mContainsImages = reactTextUpdate.containsImages(); // When we update text, we trigger onChangeText code that will @@ -658,6 +662,31 @@ private void manageSpans( } } + private void stripAbsoluteSizeSpans(SpannableStringBuilder sb) { + // We have already set a font size on the EditText itself. We can safely remove sizing spans + // which are the same as the set font size, and not otherwise overlapped. + final int effectiveFontSize = mTextAttributes.getEffectiveFontSize(); + ReactAbsoluteSizeSpan[] spans = sb.getSpans(0, sb.length(), ReactAbsoluteSizeSpan.class); + + outer: + for (ReactAbsoluteSizeSpan span : spans) { + if (span.getSize() != effectiveFontSize) { + continue; + } + + ReactAbsoluteSizeSpan[] overlappingSpans = + sb.getSpans(sb.getSpanStart(span), sb.getSpanEnd(span), ReactAbsoluteSizeSpan.class); + + for (ReactAbsoluteSizeSpan overlappingSpan : overlappingSpans) { + if (span.getSize() != effectiveFontSize) { + continue outer; + } + } + + sb.removeSpan(span); + } + } + private static boolean sameTextForSpan( final Editable oldText, final SpannableStringBuilder newText,