Skip to content

Commit e76a7df

Browse files
Estevão Lucasfacebook-github-bot
Estevão Lucas
authored andcommitted
- Add DevSetting native module (making it cross-platform) (#24441)
Summary: React Native has a `NativeModule` to manipulate programmatically the dev menu options (live reload, hot reload, remote debugging, etc), called [`DevSettings`](https://github.com/facebook/react-native/blob/master/React/Modules/RCTDevSettings.mm#L120). However this module is only available for iOS. This PR brings the same `DevSettings` for Android, making it a cross-platform NativeModule. Motivation: Right now if your app needs to programmatically reload RN, one option is to install [`react-native-restart`](https://www.npmjs.com/package/react-native-restart). It's a tiny dependency, but it's annoying to have to install it, while the code to do so is inside RN codebase. According to NPM, react-native-restart has ~12k weekly downloads, shows it's a recurring feature for many apps (my case). Thus making `NativeModules.DevSettings` is a small increment in the codebase, just exposing the dev menu methods, to improve the Development Experience [Android] [Added] - Add DevSetting native module (making it cross-platform) With expection of `setIsShakeToShowDevMenuEnabled`, the following methods will be available for both platforms: * reload * setHotLoadingEnabled * setIsDebuggingRemotely * setIsShakeToShowDevMenuEnabled * setLiveReloadEnabled * setProfilingEnabled * toggleElementInspector Pull Request resolved: #24441 Differential Revision: D14932751 Pulled By: cpojer fbshipit-source-id: 465e6a89c3beb5fd1ea22e80ea02e9438f596a09
1 parent 62ff7d6 commit e76a7df

File tree

6 files changed

+193
-1
lines changed

6 files changed

+193
-1
lines changed

ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.facebook.react.modules.core.Timing;
2424
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
2525
import com.facebook.react.modules.core.HeadlessJsTaskSupportModule;
26+
import com.facebook.react.modules.debug.DevSettingsModule;
2627
import com.facebook.react.modules.debug.SourceCodeModule;
2728
import com.facebook.react.modules.deviceinfo.DeviceInfoModule;
2829
import com.facebook.react.modules.systeminfo.AndroidInfoModule;
@@ -49,6 +50,7 @@
4950
AndroidInfoModule.class,
5051
DeviceEventManagerModule.class,
5152
DeviceInfoModule.class,
53+
DevSettingsModule.class,
5254
ExceptionsManagerModule.class,
5355
HeadlessJsTaskSupportModule.class,
5456
SourceCodeModule.class,
@@ -93,6 +95,7 @@ public ReactModuleInfoProvider getReactModuleInfoProvider() {
9395
AndroidInfoModule.class,
9496
DeviceEventManagerModule.class,
9597
DeviceInfoModule.class,
98+
DevSettingsModule.class,
9699
ExceptionsManagerModule.class,
97100
HeadlessJsTaskSupportModule.class,
98101
SourceCodeModule.class,
@@ -138,6 +141,8 @@ public NativeModule getModule(String name, ReactApplicationContext reactContext)
138141
return new AndroidInfoModule(reactContext);
139142
case DeviceEventManagerModule.NAME:
140143
return new DeviceEventManagerModule(reactContext, mHardwareBackBtnHandler);
144+
case DevSettingsModule.NAME:
145+
return new DevSettingsModule(mReactInstanceManager.getDevSupportManager());
141146
case ExceptionsManagerModule.NAME:
142147
return new ExceptionsManagerModule(mReactInstanceManager.getDevSupportManager());
143148
case HeadlessJsTaskSupportModule.NAME:

ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerImpl.java

+84
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,90 @@ public void stopInspector() {
10401040
mDevServerHelper.closeInspectorConnection();
10411041
}
10421042

1043+
@Override
1044+
public void setHotModuleReplacementEnabled(final boolean isHotModuleReplacementEnabled) {
1045+
if (!mIsDevSupportEnabled) {
1046+
return;
1047+
}
1048+
1049+
UiThreadUtil.runOnUiThread(
1050+
new Runnable() {
1051+
@Override
1052+
public void run() {
1053+
mDevSettings.setHotModuleReplacementEnabled(isHotModuleReplacementEnabled);
1054+
handleReloadJS();
1055+
}
1056+
}
1057+
);
1058+
}
1059+
1060+
@Override
1061+
public void setRemoteJSDebugEnabled(final boolean isRemoteJSDebugEnabled) {
1062+
if (!mIsDevSupportEnabled) {
1063+
return;
1064+
}
1065+
1066+
UiThreadUtil.runOnUiThread(
1067+
new Runnable() {
1068+
@Override
1069+
public void run() {
1070+
mDevSettings.setRemoteJSDebugEnabled(isRemoteJSDebugEnabled);
1071+
handleReloadJS();
1072+
}
1073+
}
1074+
);
1075+
}
1076+
1077+
@Override
1078+
public void setReloadOnJSChangeEnabled(final boolean isReloadOnJSChangeEnabled) {
1079+
if (!mIsDevSupportEnabled) {
1080+
return;
1081+
}
1082+
1083+
UiThreadUtil.runOnUiThread(
1084+
new Runnable() {
1085+
@Override
1086+
public void run() {
1087+
mDevSettings.setReloadOnJSChangeEnabled(isReloadOnJSChangeEnabled);
1088+
handleReloadJS();
1089+
}
1090+
}
1091+
);
1092+
}
1093+
1094+
@Override
1095+
public void setFpsDebugEnabled(final boolean isFpsDebugEnabled) {
1096+
if (!mIsDevSupportEnabled) {
1097+
return;
1098+
}
1099+
1100+
UiThreadUtil.runOnUiThread(
1101+
new Runnable() {
1102+
@Override
1103+
public void run() {
1104+
mDevSettings.setFpsDebugEnabled(isFpsDebugEnabled);
1105+
}
1106+
}
1107+
);
1108+
}
1109+
1110+
@Override
1111+
public void toggleElementInspector() {
1112+
if (!mIsDevSupportEnabled) {
1113+
return;
1114+
}
1115+
1116+
UiThreadUtil.runOnUiThread(
1117+
new Runnable() {
1118+
@Override
1119+
public void run() {
1120+
mDevSettings.setElementInspectorEnabled(!mDevSettings.isElementInspectorEnabled());
1121+
mReactInstanceManagerHelper.toggleElementInspector();
1122+
}
1123+
}
1124+
);
1125+
}
1126+
10431127
private void reload() {
10441128
UiThreadUtil.assertOnUiThread();
10451129

ReactAndroid/src/main/java/com/facebook/react/devsupport/DisabledDevSupportManager.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,31 @@ public void stopInspector() {
7878

7979
}
8080

81+
@Override
82+
public void setHotModuleReplacementEnabled(boolean isHotModuleReplacementEnabled) {
83+
84+
}
85+
86+
@Override
87+
public void setRemoteJSDebugEnabled(boolean isRemoteJSDebugEnabled) {
88+
89+
}
90+
91+
@Override
92+
public void setReloadOnJSChangeEnabled(boolean isReloadOnJSChangeEnabled) {
93+
94+
}
95+
96+
@Override
97+
public void setFpsDebugEnabled(boolean isFpsDebugEnabled) {
98+
99+
}
100+
101+
@Override
102+
public void toggleElementInspector() {
103+
104+
}
105+
81106
@Override
82107
public boolean getDevSupportEnabled() {
83108
return false;
@@ -162,7 +187,7 @@ public void isPackagerRunning(PackagerStatusCallback callback) {
162187

163188
@Override
164189
public void registerErrorCustomizer(ErrorCustomizer errorCustomizer) {
165-
190+
166191
}
167192

168193
@Override

ReactAndroid/src/main/java/com/facebook/react/devsupport/interfaces/DevSupportManager.java

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public interface DevSupportManager extends NativeModuleCallExceptionHandler {
4545
void handleReloadJS();
4646
void reloadJSFromServer(final String bundleURL);
4747
void isPackagerRunning(PackagerStatusCallback callback);
48+
void setHotModuleReplacementEnabled(final boolean isHotModuleReplacementEnabled);
49+
void setRemoteJSDebugEnabled(final boolean isRemoteJSDebugEnabled);
50+
void setReloadOnJSChangeEnabled(final boolean isReloadOnJSChangeEnabled);
51+
void setFpsDebugEnabled(final boolean isFpsDebugEnabled);
52+
void toggleElementInspector();
4853
@Nullable File downloadBundleResourceFromUrlSync(
4954
final String resourceURL,
5055
final File outputFile);

ReactAndroid/src/main/java/com/facebook/react/modules/debug/BUCK

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ rn_android_library(
1212
react_native_dep("third-party/java/jsr-305:jsr-305"),
1313
react_native_target("java/com/facebook/react/bridge:bridge"),
1414
react_native_target("java/com/facebook/react/common:common"),
15+
react_native_target("java/com/facebook/react/devsupport:interfaces"),
1516
react_native_target("java/com/facebook/react/module/annotations:annotations"),
1617
react_native_target("java/com/facebook/react/modules/core:core"),
1718
react_native_target("java/com/facebook/react/modules/debug:interfaces"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.modules.debug;
9+
10+
import com.facebook.react.bridge.BaseJavaModule;
11+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
12+
import com.facebook.react.bridge.ReactMethod;
13+
import com.facebook.react.devsupport.interfaces.DevSupportManager;
14+
import com.facebook.react.module.annotations.ReactModule;
15+
import com.facebook.react.bridge.UiThreadUtil;
16+
17+
/**
18+
* Module that exposes the URL to the source code map (used for exception stack trace parsing) to JS
19+
*/
20+
@ReactModule(name = DevSettingsModule.NAME)
21+
public class DevSettingsModule extends BaseJavaModule {
22+
23+
public static final String NAME = "DevSettings";
24+
25+
private final DevSupportManager mDevSupportManager;
26+
27+
public DevSettingsModule(DevSupportManager devSupportManager) {
28+
mDevSupportManager = devSupportManager;
29+
}
30+
31+
@Override
32+
public String getName() {
33+
return NAME;
34+
}
35+
36+
@ReactMethod
37+
public void reload() {
38+
if (mDevSupportManager.getDevSupportEnabled()) {
39+
UiThreadUtil.runOnUiThread(new Runnable() {
40+
@Override
41+
public void run() {
42+
mDevSupportManager.handleReloadJS();
43+
}
44+
});
45+
}
46+
}
47+
48+
@ReactMethod
49+
public void setHotLoadingEnabled(boolean isHotLoadingEnabled) {
50+
mDevSupportManager.setHotModuleReplacementEnabled(isHotLoadingEnabled);
51+
}
52+
53+
@ReactMethod
54+
public void setIsDebuggingRemotely(boolean isDebugginRemotelyEnabled) {
55+
mDevSupportManager.setRemoteJSDebugEnabled(isDebugginRemotelyEnabled);
56+
}
57+
58+
@ReactMethod
59+
public void setLiveReloadEnabled(boolean isLiveReloadEnabled) {
60+
mDevSupportManager.setReloadOnJSChangeEnabled(isLiveReloadEnabled);
61+
}
62+
63+
@ReactMethod
64+
public void setProfilingEnabled(boolean isProfilingEnabled) {
65+
mDevSupportManager.setFpsDebugEnabled(isProfilingEnabled);
66+
}
67+
68+
@ReactMethod
69+
public void toggleElementInspector() {
70+
mDevSupportManager.toggleElementInspector();
71+
}
72+
}

0 commit comments

Comments
 (0)