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
This commit is contained in:
Lulu Wu
2023-09-25 10:51:24 -07:00
committed by Facebook GitHub Bot
parent 188eceec98
commit cee5dceac7
@@ -15,6 +15,7 @@ import android.content.Context;
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 @@ import java.util.Locale;
*/
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 class DefaultDevLoadingViewImplementation implements DevLoadingViewManage
}
public DefaultDevLoadingViewImplementation(ReactInstanceDevHelper reactInstanceManagerHelper) {
mReactInstanceManagerHelper = reactInstanceManagerHelper;
mReactInstanceDevHelper = reactInstanceManagerHelper;
}
@Override
@@ -104,7 +105,7 @@ public class DefaultDevLoadingViewImplementation implements DevLoadingViewManage
return;
}
Activity currentActivity = mReactInstanceManagerHelper.getCurrentActivity();
Activity currentActivity = mReactInstanceDevHelper.getCurrentActivity();
if (currentActivity == null) {
FLog.e(
ReactConstants.TAG,
@@ -115,21 +116,30 @@ public class DefaultDevLoadingViewImplementation implements DevLoadingViewManage
// 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 @@ public class DefaultDevLoadingViewImplementation implements DevLoadingViewManage
}
private @Nullable Context getContext() {
return mReactInstanceManagerHelper.getCurrentActivity();
return mReactInstanceDevHelper.getCurrentActivity();
}
}