-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: This diff adds a NativeLogBox module implementation on Android to manage rendering LogBox the way we render RedBox, except rendering a React Native component instead of a native view. The strategy here is: - initialize: will create a React rootview and render it. - show: will add the rootview to a dialog and display the dialog. - hide: will remove the rootview from it's parent, dismiss the dialog, and release the reference to the activity to prevent leaks. Most of this is copied from the way RedBox works, the difference here is that we eagerly initialize the rootview with the `initialize` function so that it's warm by the time the dialog needs to render. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D18768517 fbshipit-source-id: 2510d6c186ccf73153ef9372c736c9e0c71bbc7d
- Loading branch information
1 parent
6b22a4e
commit e272089
Showing
10 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
ReactAndroid/src/main/java/com/facebook/react/devsupport/LogBoxDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.devsupport; | ||
|
||
import android.app.Activity; | ||
import android.app.Dialog; | ||
import android.view.View; | ||
import android.view.Window; | ||
import com.facebook.react.R; | ||
|
||
/** Dialog for displaying JS errors in LogBox. */ | ||
public class LogBoxDialog extends Dialog { | ||
public LogBoxDialog(Activity context, View reactRootView) { | ||
super(context, R.style.Theme_Catalyst_LogBox); | ||
|
||
requestWindowFeature(Window.FEATURE_NO_TITLE); | ||
setContentView(reactRootView); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
ReactAndroid/src/main/java/com/facebook/react/devsupport/LogBoxModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.devsupport; | ||
|
||
import android.app.Activity; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import androidx.annotation.Nullable; | ||
import com.facebook.common.logging.FLog; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
import com.facebook.react.bridge.ReactMethod; | ||
import com.facebook.react.bridge.UiThreadUtil; | ||
import com.facebook.react.common.ReactConstants; | ||
import com.facebook.react.devsupport.interfaces.DevSupportManager; | ||
import com.facebook.react.module.annotations.ReactModule; | ||
|
||
@ReactModule(name = LogBoxModule.NAME) | ||
public class LogBoxModule extends ReactContextBaseJavaModule { | ||
|
||
public static final String NAME = "LogBox"; | ||
|
||
private final DevSupportManager mDevSupportManager; | ||
private @Nullable View mReactRootView; | ||
private @Nullable LogBoxDialog mLogBoxDialog; | ||
|
||
public LogBoxModule(ReactApplicationContext reactContext, DevSupportManager devSupportManager) { | ||
super(reactContext); | ||
|
||
mDevSupportManager = devSupportManager; | ||
UiThreadUtil.runOnUiThread( | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
if (mReactRootView == null) { | ||
mReactRootView = mDevSupportManager.createRootView("LogBox"); | ||
if (mReactRootView == null) { | ||
FLog.e( | ||
ReactConstants.TAG, | ||
"Unable to launch logbox because react was unable to create the root view"); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
@ReactMethod | ||
public void show() { | ||
UiThreadUtil.runOnUiThread( | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
if (mLogBoxDialog == 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(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@ReactMethod | ||
public void hide() { | ||
UiThreadUtil.runOnUiThread( | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
if (mLogBoxDialog != null) { | ||
if (mReactRootView.getParent() != null) { | ||
((ViewGroup) mReactRootView.getParent()).removeView(mReactRootView); | ||
} | ||
mLogBoxDialog.dismiss(); | ||
mLogBoxDialog = null; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void onCatalystInstanceDestroy() { | ||
UiThreadUtil.runOnUiThread( | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
if (mReactRootView != null) { | ||
mDevSupportManager.destroyRootView(mReactRootView); | ||
mReactRootView = null; | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<color name="catalyst_redbox_background">#eecc0000</color> | ||
<color name="catalyst_logbox_background">#ffffffff</color> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters