diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 89f47e173af911..2e2abb93f3e2d6 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -3693,13 +3693,13 @@ public abstract class com/facebook/react/packagerconnection/NotificationOnlyHand public final fun onRequest (Ljava/lang/Object;Lcom/facebook/react/packagerconnection/Responder;)V } -public class com/facebook/react/packagerconnection/PackagerConnectionSettings { +public final class com/facebook/react/packagerconnection/PackagerConnectionSettings { public fun (Landroid/content/Context;)V - public fun getAdditionalOptionsForPackager ()Ljava/util/Map; - public fun getDebugServerHost ()Ljava/lang/String; - public fun getPackageName ()Ljava/lang/String; - public fun setAdditionalOptionForPackager (Ljava/lang/String;Ljava/lang/String;)V - public fun setDebugServerHost (Ljava/lang/String;)V + public final fun getAdditionalOptionsForPackager ()Ljava/util/Map; + public final fun getDebugServerHost ()Ljava/lang/String; + public final fun getPackageName ()Ljava/lang/String; + public final fun setAdditionalOptionForPackager (Ljava/lang/String;Ljava/lang/String;)V + public final fun setDebugServerHost (Ljava/lang/String;)V } public final class com/facebook/react/packagerconnection/ReconnectingWebSocket : okhttp3/WebSocketListener { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java deleted file mode 100644 index 50522f8b2c1222..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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.packagerconnection; - -import android.content.Context; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; -import android.text.TextUtils; -import androidx.annotation.Nullable; -import com.facebook.common.logging.FLog; -import com.facebook.infer.annotation.Assertions; -import com.facebook.infer.annotation.Nullsafe; -import com.facebook.react.modules.systeminfo.AndroidInfoHelpers; -import java.util.HashMap; -import java.util.Map; - -@Nullsafe(Nullsafe.Mode.LOCAL) -public class PackagerConnectionSettings { - private static final String TAG = PackagerConnectionSettings.class.getSimpleName(); - private static final String PREFS_DEBUG_SERVER_HOST_KEY = "debug_http_host"; - - private final SharedPreferences mPreferences; - private final String mPackageName; - private final Context mAppContext; - private final Map mAdditionalOptionsForPackager = new HashMap<>(); - - public PackagerConnectionSettings(Context applicationContext) { - mPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext); - mPackageName = applicationContext.getPackageName(); - mAppContext = applicationContext; - } - - public String getDebugServerHost() { - // Check host setting first. If empty try to detect emulator type and use default - // hostname for those - String hostFromSettings = mPreferences.getString(PREFS_DEBUG_SERVER_HOST_KEY, null); - - if (!TextUtils.isEmpty(hostFromSettings)) { - return Assertions.assertNotNull(hostFromSettings); - } - - String host = AndroidInfoHelpers.getServerHost(mAppContext); - - if (host.equals(AndroidInfoHelpers.DEVICE_LOCALHOST)) { - FLog.w( - TAG, - "You seem to be running on device. Run '" - + AndroidInfoHelpers.getAdbReverseTcpCommand(mAppContext) - + "' " - + "to forward the debug server's port to the device."); - } - - return host; - } - - public void setDebugServerHost(String host) { - mPreferences.edit().putString(PREFS_DEBUG_SERVER_HOST_KEY, host).apply(); - } - - public @Nullable String getPackageName() { - return mPackageName; - } - - public void setAdditionalOptionForPackager(String key, String value) { - mAdditionalOptionsForPackager.put(key, value); - } - - public Map getAdditionalOptionsForPackager() { - return mAdditionalOptionsForPackager; - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.kt new file mode 100644 index 00000000000000..490f1bf59ef75a --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/PackagerConnectionSettings.kt @@ -0,0 +1,53 @@ +/* + * 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.packagerconnection + +import android.content.Context +import android.content.SharedPreferences +import com.facebook.common.logging.FLog +import com.facebook.react.modules.systeminfo.AndroidInfoHelpers + +public class PackagerConnectionSettings(private val appContext: Context) { + private val preferences: SharedPreferences = + appContext.applicationContext.getSharedPreferences( + "react_native_packager_connection", Context.MODE_PRIVATE) + public val packageName: String = appContext.packageName + private val _additionalOptionsForPackager: MutableMap = mutableMapOf() + + public var debugServerHost: String + get() { + // Check host setting first. If empty try to detect emulator type and use default + // hostname for those + val hostFromSettings = preferences.getString(PREFS_DEBUG_SERVER_HOST_KEY, null) + if (hostFromSettings?.isNotEmpty() == true) { + return hostFromSettings + } + val host = AndroidInfoHelpers.getServerHost(appContext) + if (host == AndroidInfoHelpers.DEVICE_LOCALHOST) { + FLog.w( + TAG, + "You seem to be running on device. Run '${AndroidInfoHelpers.getAdbReverseTcpCommand(appContext)}' to forward the debug server's port to the device.") + } + return host + } + set(host) { + preferences.edit().putString(PREFS_DEBUG_SERVER_HOST_KEY, host).apply() + } + + public fun setAdditionalOptionForPackager(key: String, value: String) { + _additionalOptionsForPackager[key] = value + } + + public val additionalOptionsForPackager: Map + get() = _additionalOptionsForPackager + + private companion object { + private val TAG = PackagerConnectionSettings::class.java.simpleName + private const val PREFS_DEBUG_SERVER_HOST_KEY = "debug_http_host" + } +}