Prevent crash when setting underlineColorAndroid (#24183)

Summary:
Try to prevent the crash described in https://github.com/facebook/react-native/issues/17530

There seems to be a bug in `Drawable.mutate()` in some devices/android os versions even after you check the constant state. This error is hard to reproduce and to fix, so just try to catch the exception to prevent crash.

[Android][Fixed] Prevent random crash when setting underlineColorAndroid
Pull Request resolved: https://github.com/facebook/react-native/pull/24183

Differential Revision: D14710484

Pulled By: cpojer

fbshipit-source-id: 3af20a5cb0ecd40839beaf85118c0f5aa6905414
This commit is contained in:
Sunny Luo
2019-04-02 09:36:30 -07:00
committed by Facebook Github Bot
parent 4478e50520
commit 556aa93ed7
2 changed files with 11 additions and 4 deletions

View File

@@ -13,6 +13,7 @@ rn_android_library(
],
deps = [
YOGA_TARGET,
react_native_dep("libraries/fbcore/src/main/java/com/facebook/common/logging:logging"),
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_target("java/com/facebook/react/bridge:bridge"),

View File

@@ -27,6 +27,7 @@ import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.ReactContext;
@@ -64,7 +65,7 @@ import javax.annotation.Nullable;
*/
@ReactModule(name = ReactTextInputManager.REACT_CLASS)
public class ReactTextInputManager extends BaseViewManager<ReactEditText, LayoutShadowNode> {
public static final String TAG = ReactTextInputManager.class.getSimpleName();
protected static final String REACT_CLASS = "AndroidTextInput";
private static final int[] SPACING_TYPES = {
@@ -464,9 +465,14 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
// Drawable.mutate() can sometimes crash due to an AOSP bug:
// See https://code.google.com/p/android/issues/detail?id=191754 for more info
Drawable background = view.getBackground();
Drawable drawableToMutate = background.getConstantState() != null ?
background.mutate() :
background;
Drawable drawableToMutate = background;
if (background.getConstantState() != null) {
try {
drawableToMutate = background.mutate();
} catch (NullPointerException e) {
FLog.e(TAG, "NullPointerException when setting underlineColorAndroid for TextInput", e);
}
}
if (underlineColor == null) {
drawableToMutate.clearColorFilter();