Skip to content

Commit

Permalink
Prevent crash when setting underlineColorAndroid (#24183)
Browse files Browse the repository at this point in the history
Summary:
Try to prevent the crash described in #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: #24183

Differential Revision: D14710484

Pulled By: cpojer

fbshipit-source-id: 3af20a5cb0ecd40839beaf85118c0f5aa6905414
  • Loading branch information
sunnylqm authored and facebook-github-bot committed Apr 2, 2019
1 parent 4478e50 commit 556aa93
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;
Expand Down Expand Up @@ -64,7 +65,7 @@
*/
@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 = {
Expand Down Expand Up @@ -464,9 +465,14 @@ public void setUnderlineColor(ReactEditText view, @Nullable Integer underlineCol
// 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();
Expand Down

0 comments on commit 556aa93

Please sign in to comment.