diff --git a/Libraries/Core/setUpReactDevTools.js b/Libraries/Core/setUpReactDevTools.js index be647e33b4d2ca..30e14b5141205c 100644 --- a/Libraries/Core/setUpReactDevTools.js +++ b/Libraries/Core/setUpReactDevTools.js @@ -62,6 +62,7 @@ if (__DEV__) { }); const ReactNativeStyleAttributes = require('../Components/View/ReactNativeStyleAttributes'); + const devToolsSettingsManager = require('../Settings/DevToolsSettingsManager'); reactDevTools.connectToDevTools({ isAppActive, @@ -70,6 +71,7 @@ if (__DEV__) { ReactNativeStyleAttributes, ), websocket: ws, + devToolsSettingsManager, }); } }; diff --git a/Libraries/Settings/DevToolsSettingsManager.android.js b/Libraries/Settings/DevToolsSettingsManager.android.js new file mode 100644 index 00000000000000..e979cc3760d4ab --- /dev/null +++ b/Libraries/Settings/DevToolsSettingsManager.android.js @@ -0,0 +1,13 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + * @format + */ + +import NativeDevToolsSettingsManager from './NativeDevToolsSettingsManager'; + +module.exports = NativeDevToolsSettingsManager; diff --git a/Libraries/Settings/DevToolsSettingsManager.ios.js b/Libraries/Settings/DevToolsSettingsManager.ios.js new file mode 100644 index 00000000000000..1fe24e9c8f74db --- /dev/null +++ b/Libraries/Settings/DevToolsSettingsManager.ios.js @@ -0,0 +1,26 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + * @format + */ + +import type {Spec} from './NativeDevToolsSettingsManager'; +import Settings from './Settings'; + +const CONSOLE_PATCH_SETTINGS_KEY = 'ReactDevTools::ConsolePatchSettings'; + +const DevToolsSettingsManager = { + setConsolePatchSettings: (newConsolePatchSettings: string) => { + Settings.set({ + [CONSOLE_PATCH_SETTINGS_KEY]: newConsolePatchSettings, + }); + }, + getConsolePatchSettings: () => + ((Settings.get(CONSOLE_PATCH_SETTINGS_KEY): any): string), +}; + +module.exports = (DevToolsSettingsManager: Spec); diff --git a/Libraries/Settings/NativeDevToolsSettingsManager.js b/Libraries/Settings/NativeDevToolsSettingsManager.js new file mode 100644 index 00000000000000..9e67b320e57f1a --- /dev/null +++ b/Libraries/Settings/NativeDevToolsSettingsManager.js @@ -0,0 +1,22 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + * @format + */ + +import type {TurboModule} from '../TurboModule/RCTExport'; + +import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry'; + +export interface Spec extends TurboModule { + +setConsolePatchSettings: (newConsolePatchSettings: string) => void; + +getConsolePatchSettings: () => string; +} + +export default (TurboModuleRegistry.get( + 'DevToolsSettingsManager', +): ?Spec); diff --git a/ReactAndroid/src/main/java/com/facebook/react/BUCK b/ReactAndroid/src/main/java/com/facebook/react/BUCK index 0d19c05abb2f98..d447dfda79c5ce 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/BUCK @@ -42,6 +42,7 @@ rn_android_library( react_native_target("java/com/facebook/react/modules/appearance:appearance"), react_native_target("java/com/facebook/react/modules/bundleloader:bundleloader"), react_native_target("java/com/facebook/react/modules/debug:debug"), + react_native_target("java/com/facebook/react/modules/devtoolssettings:devtoolssettings"), react_native_target("java/com/facebook/react/modules/fabric:fabric"), react_native_target("java/com/facebook/react/modules/debug:interfaces"), react_native_target("java/com/facebook/react/modules/deviceinfo:deviceinfo"), diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/devtoolssettings/BUCK b/ReactAndroid/src/main/java/com/facebook/react/modules/devtoolssettings/BUCK new file mode 100644 index 00000000000000..2990c787bfb7fb --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/devtoolssettings/BUCK @@ -0,0 +1,28 @@ +load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_root_target", "react_native_target", "rn_android_library") + +rn_android_library( + name = "devtoolssettings", + srcs = glob(["**/*.java"]), + autoglob = False, + labels = [ + "pfh:ReactNative_CommonInfrastructurePlaceholder", + "supermodule:xplat/default/public.react_native.infra", + ], + language = "JAVA", + visibility = [ + "PUBLIC", + ], + provided_deps = [ + react_native_dep("third-party/android/androidx:annotation"), + ], + deps = [ + react_native_dep("libraries/fbcore/src/main/java/com/facebook/common/logging:logging"), + react_native_dep("third-party/java/infer-annotations:infer-annotations"), + 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/module/annotations:annotations"), + react_native_target("java/com/facebook/react/modules/core:core"), + ], + exported_deps = [react_native_root_target(":FBReactNativeSpec")], +) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/devtoolssettings/DevToolsSettingsManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/devtoolssettings/DevToolsSettingsManagerModule.java new file mode 100644 index 00000000000000..82452260a8a31e --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/devtoolssettings/DevToolsSettingsManagerModule.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) Meta Platforms, Inc. and 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.devtoolssettings; + +import com.facebook.fbreact.specs.NativeDevToolsSettingsManagerSpec; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.module.annotations.ReactModule; +import android.content.SharedPreferences; +import androidx.annotation.Nullable; +import android.content.SharedPreferences.Editor; +import android.content.Context; + +@ReactModule(name = DevToolsSettingsManagerModule.NAME) +public class DevToolsSettingsManagerModule extends NativeDevToolsSettingsManagerSpec { + public static final String NAME = "DevToolsSettingsManager"; + + private static final String SHARED_PREFERENCES_PREFIX = "ReactNative__DevToolsSettings"; + private static final String KEY_CONSOLE_PATCH_SETTINGS = "ConsolePatchSettings"; + + private final SharedPreferences sharedPreferences; + + public DevToolsSettingsManagerModule(ReactApplicationContext reactContext) { + super(reactContext); + sharedPreferences = reactContext.getSharedPreferences(SHARED_PREFERENCES_PREFIX, Context.MODE_PRIVATE); + } + + @Override + public String getName() { + return NAME; + } + + @Override + public @Nullable String getConsolePatchSettings() { + return sharedPreferences.getString(KEY_CONSOLE_PATCH_SETTINGS, null); + } + + @Override + public void setConsolePatchSettings(String newSettings) { + Editor editor = sharedPreferences.edit(); + editor.putString(KEY_CONSOLE_PATCH_SETTINGS, newSettings); + editor.apply(); + } +} diff --git a/ReactAndroid/src/main/java/com/facebook/react/shell/BUCK b/ReactAndroid/src/main/java/com/facebook/react/shell/BUCK index bb87ef93981cae..535e61e54392d4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/shell/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/shell/BUCK @@ -38,6 +38,7 @@ rn_android_library( react_native_target("java/com/facebook/react/modules/clipboard:clipboard"), react_native_target("java/com/facebook/react/modules/core:core"), react_native_target("java/com/facebook/react/modules/debug:debug"), + react_native_target("java/com/facebook/react/modules/devtoolssettings:devtoolssettings"), react_native_target("java/com/facebook/react/modules/dialog:dialog"), react_native_target("java/com/facebook/react/modules/fresco:fresco"), react_native_target("java/com/facebook/react/modules/i18nmanager:i18nmanager"), diff --git a/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java b/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java index 6fc4a536e2b1f7..bb9848fd54dabd 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java +++ b/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java @@ -30,6 +30,7 @@ import com.facebook.react.modules.intent.IntentModule; import com.facebook.react.modules.network.NetworkingModule; import com.facebook.react.modules.permissions.PermissionsModule; +import com.facebook.react.modules.devtoolssettings.DevToolsSettingsManagerModule; import com.facebook.react.modules.share.ShareModule; import com.facebook.react.modules.sound.SoundManagerModule; import com.facebook.react.modules.statusbar.StatusBarModule; @@ -145,6 +146,8 @@ public MainReactPackage(MainPackageConfig config) { return new VibrationModule(context); case WebSocketModule.NAME: return new WebSocketModule(context); + case DevToolsSettingsManagerModule.NAME: + return new DevToolsSettingsManagerModule(context); default: return null; } @@ -185,7 +188,8 @@ public ReactModuleInfoProvider getReactModuleInfoProvider() { Class.forName("com.facebook.react.shell.MainReactPackage$$ReactModuleInfoProvider"); return (ReactModuleInfoProvider) reactModuleInfoProviderClass.newInstance(); } catch (ClassNotFoundException e) { - // In OSS case, the annotation processor does not run. We fall back on creating this byhand + // In the OSS case, the annotation processor does not run. We fall back to creating this by + // hand Class[] moduleList = new Class[] { AccessibilityInfoModule.class, @@ -204,6 +208,7 @@ public ReactModuleInfoProvider getReactModuleInfoProvider() { NativeAnimatedModule.class, NetworkingModule.class, PermissionsModule.class, + DevToolsSettingsManagerModule.class, ShareModule.class, StatusBarModule.class, SoundManagerModule.class, diff --git a/ReactAndroid/src/test/java/com/facebook/react/modules/BUCK b/ReactAndroid/src/test/java/com/facebook/react/modules/BUCK index 7ab27c360a9714..d8581c32d5dd90 100644 --- a/ReactAndroid/src/test/java/com/facebook/react/modules/BUCK +++ b/ReactAndroid/src/test/java/com/facebook/react/modules/BUCK @@ -34,6 +34,7 @@ rn_robolectric_test( react_native_target("java/com/facebook/react/modules/core:core"), react_native_target("java/com/facebook/react/modules/debug:debug"), react_native_target("java/com/facebook/react/modules/deviceinfo:deviceinfo"), + react_native_target("java/com/facebook/react/modules/devtoolssettings:devtoolssettings"), react_native_target("java/com/facebook/react/modules/dialog:dialog"), react_native_target("java/com/facebook/react/modules/network:network"), react_native_target("java/com/facebook/react/modules/share:share"),