From 782e06a059037771c2658c100c06ec452cf3f0b3 Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Wed, 4 Mar 2020 19:35:15 -0800 Subject: [PATCH] LogBox: do less work in production when LogBox shouldn't even be running Summary: Do we need this? Is it possible for any of these codepaths to execute in production? If so, it would be nice to avoid NPEs or even scheduling work on the UI thread. Maybe we want to add soft exception logging for if these paths are called? Changelog: [Internal] make sure LogBox does even less work when it's not hooked up Reviewed By: rickhanlonii Differential Revision: D20156812 fbshipit-source-id: ba9faedcb3b0951e6913724ca99549dc2d16237e --- .../react/devsupport/LogBoxModule.java | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/LogBoxModule.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/LogBoxModule.java index e4e0bd8706d..2c4e4eae532 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/LogBoxModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/LogBoxModule.java @@ -32,11 +32,12 @@ public class LogBoxModule extends NativeLogBoxSpec { super(reactContext); mDevSupportManager = devSupportManager; + UiThreadUtil.runOnUiThread( new Runnable() { @Override public void run() { - if (mReactRootView == null) { + if (mReactRootView == null && mDevSupportManager != null) { mReactRootView = mDevSupportManager.createRootView("LogBox"); if (mReactRootView == null) { FLog.e( @@ -55,25 +56,27 @@ public class LogBoxModule extends NativeLogBoxSpec { @Override public void show() { - UiThreadUtil.runOnUiThread( - new Runnable() { - @Override - public void run() { - if (mLogBoxDialog == null && mReactRootView != null) { - Activity context = getCurrentActivity(); - if (context == null || context.isFinishing()) { - FLog.e( - ReactConstants.TAG, - "Unable to launch logbox because react activity " - + "is not available, here is the error that logbox would've displayed: "); - return; + if (mReactRootView != null) { + UiThreadUtil.runOnUiThread( + new Runnable() { + @Override + public void run() { + if (mLogBoxDialog == null && mReactRootView != null) { + Activity context = getCurrentActivity(); + if (context == null || context.isFinishing()) { + FLog.e( + ReactConstants.TAG, + "Unable to launch logbox because react activity " + + "is not available, here is the error that logbox would've displayed: "); + return; + } + mLogBoxDialog = new LogBoxDialog(context, mReactRootView); + mLogBoxDialog.setCancelable(false); + mLogBoxDialog.show(); } - mLogBoxDialog = new LogBoxDialog(context, mReactRootView); - mLogBoxDialog.setCancelable(false); - mLogBoxDialog.show(); } - } - }); + }); + } } @Override @@ -83,7 +86,7 @@ public class LogBoxModule extends NativeLogBoxSpec { @Override public void run() { if (mLogBoxDialog != null) { - if (mReactRootView.getParent() != null) { + if (mReactRootView != null && mReactRootView.getParent() != null) { ((ViewGroup) mReactRootView.getParent()).removeView(mReactRootView); } mLogBoxDialog.dismiss();