From cee5dceac7285fbae76139ebd58784009e2da2fb Mon Sep 17 00:00:00 2001 From: Lulu Wu Date: Mon, 25 Sep 2023 10:51:24 -0700 Subject: [PATCH] Fix instacrash with RNTester (#39631) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/39631 Don't display the PopupWindow when current activity is in a bad state, other wise there will be a crash P832378432 Changelog: [Android][Changed] - Don't display the PopupWindow when current activity is in a bad state Reviewed By: mdvacca Differential Revision: D49501328 fbshipit-source-id: 1a51855daa470e8da9399f72ca7211a95388e38f --- .../DefaultDevLoadingViewImplementation.java | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DefaultDevLoadingViewImplementation.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DefaultDevLoadingViewImplementation.java index ef203fbf125899..74052bb2e91876 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DefaultDevLoadingViewImplementation.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DefaultDevLoadingViewImplementation.java @@ -15,6 +15,7 @@ import android.graphics.Rect; import android.view.Gravity; import android.view.LayoutInflater; +import android.view.WindowManager.BadTokenException; import android.widget.PopupWindow; import android.widget.TextView; import androidx.annotation.Nullable; @@ -31,7 +32,7 @@ */ public class DefaultDevLoadingViewImplementation implements DevLoadingViewManager { private static boolean sEnabled = true; - private final ReactInstanceDevHelper mReactInstanceManagerHelper; + private final ReactInstanceDevHelper mReactInstanceDevHelper; private @Nullable TextView mDevLoadingView; private @Nullable PopupWindow mDevLoadingPopup; @@ -40,7 +41,7 @@ public static void setDevLoadingEnabled(boolean enabled) { } public DefaultDevLoadingViewImplementation(ReactInstanceDevHelper reactInstanceManagerHelper) { - mReactInstanceManagerHelper = reactInstanceManagerHelper; + mReactInstanceDevHelper = reactInstanceManagerHelper; } @Override @@ -104,7 +105,7 @@ private void showInternal(String message) { return; } - Activity currentActivity = mReactInstanceManagerHelper.getCurrentActivity(); + Activity currentActivity = mReactInstanceDevHelper.getCurrentActivity(); if (currentActivity == null) { FLog.e( ReactConstants.TAG, @@ -115,21 +116,30 @@ private void showInternal(String message) { // PopupWindow#showAtLocation uses absolute screen position. In order for // loading view to be placed below status bar (if the status bar is present) we need to pass // an appropriate Y offset. - Rect rectangle = new Rect(); - currentActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle); - int topOffset = rectangle.top; + try { + Rect rectangle = new Rect(); + currentActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle); + int topOffset = rectangle.top; - LayoutInflater inflater = - (LayoutInflater) currentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + LayoutInflater inflater = + (LayoutInflater) currentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - mDevLoadingView = (TextView) inflater.inflate(R.layout.dev_loading_view, null); - mDevLoadingView.setText(message); + mDevLoadingView = (TextView) inflater.inflate(R.layout.dev_loading_view, null); + mDevLoadingView.setText(message); - mDevLoadingPopup = new PopupWindow(mDevLoadingView, MATCH_PARENT, WRAP_CONTENT); - mDevLoadingPopup.setTouchable(false); + mDevLoadingPopup = new PopupWindow(mDevLoadingView, MATCH_PARENT, WRAP_CONTENT); + mDevLoadingPopup.setTouchable(false); - mDevLoadingPopup.showAtLocation( - currentActivity.getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, topOffset); + mDevLoadingPopup.showAtLocation( + currentActivity.getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, topOffset); + // TODO T164786028: Find out the root cause of the BadTokenException exception here + } catch (BadTokenException e) { + FLog.e( + ReactConstants.TAG, + "Unable to display loading message because react " + + "activity isn't active, message: " + + message); + } } private void hideInternal() { @@ -141,6 +151,6 @@ private void hideInternal() { } private @Nullable Context getContext() { - return mReactInstanceManagerHelper.getCurrentActivity(); + return mReactInstanceDevHelper.getCurrentActivity(); } }