- 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: https://github.com/facebook/react-native/pull/24441

Differential Revision: D14932751

Pulled By: cpojer

fbshipit-source-id: 465e6a89c3beb5fd1ea22e80ea02e9438f596a09
This commit is contained in:
Estevão Lucas
2019-04-17 11:00:17 -07:00
committed by Facebook Github Bot
parent 62ff7d6863
commit e76a7df5b5
6 changed files with 193 additions and 1 deletions
@@ -12,6 +12,7 @@ rn_android_library(
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_target("java/com/facebook/react/bridge:bridge"),
react_native_target("java/com/facebook/react/common:common"),
react_native_target("java/com/facebook/react/devsupport:interfaces"),
react_native_target("java/com/facebook/react/module/annotations:annotations"),
react_native_target("java/com/facebook/react/modules/core:core"),
react_native_target("java/com/facebook/react/modules/debug:interfaces"),
@@ -0,0 +1,72 @@
/**
* 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.modules.debug;
import com.facebook.react.bridge.BaseJavaModule;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.devsupport.interfaces.DevSupportManager;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.bridge.UiThreadUtil;
/**
* Module that exposes the URL to the source code map (used for exception stack trace parsing) to JS
*/
@ReactModule(name = DevSettingsModule.NAME)
public class DevSettingsModule extends BaseJavaModule {
public static final String NAME = "DevSettings";
private final DevSupportManager mDevSupportManager;
public DevSettingsModule(DevSupportManager devSupportManager) {
mDevSupportManager = devSupportManager;
}
@Override
public String getName() {
return NAME;
}
@ReactMethod
public void reload() {
if (mDevSupportManager.getDevSupportEnabled()) {
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
mDevSupportManager.handleReloadJS();
}
});
}
}
@ReactMethod
public void setHotLoadingEnabled(boolean isHotLoadingEnabled) {
mDevSupportManager.setHotModuleReplacementEnabled(isHotLoadingEnabled);
}
@ReactMethod
public void setIsDebuggingRemotely(boolean isDebugginRemotelyEnabled) {
mDevSupportManager.setRemoteJSDebugEnabled(isDebugginRemotelyEnabled);
}
@ReactMethod
public void setLiveReloadEnabled(boolean isLiveReloadEnabled) {
mDevSupportManager.setReloadOnJSChangeEnabled(isLiveReloadEnabled);
}
@ReactMethod
public void setProfilingEnabled(boolean isProfilingEnabled) {
mDevSupportManager.setFpsDebugEnabled(isProfilingEnabled);
}
@ReactMethod
public void toggleElementInspector() {
mDevSupportManager.toggleElementInspector();
}
}