Skip to content

Commit

Permalink
Fix instacrash with RNTester (#39631)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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
  • Loading branch information
Lulu Wu authored and facebook-github-bot committed Sep 25, 2023
1 parent 188ecee commit cee5dce
Showing 1 changed file with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -40,7 +41,7 @@ public static void setDevLoadingEnabled(boolean enabled) {
}

public DefaultDevLoadingViewImplementation(ReactInstanceDevHelper reactInstanceManagerHelper) {
mReactInstanceManagerHelper = reactInstanceManagerHelper;
mReactInstanceDevHelper = reactInstanceManagerHelper;
}

@Override
Expand Down Expand Up @@ -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,
Expand All @@ -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() {
Expand All @@ -141,6 +151,6 @@ private void hideInternal() {
}

private @Nullable Context getContext() {
return mReactInstanceManagerHelper.getCurrentActivity();
return mReactInstanceDevHelper.getCurrentActivity();
}
}

0 comments on commit cee5dce

Please sign in to comment.