Skip to content

Commit

Permalink
Migrate DevSettings related code to Kotlin (#43731)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #43731

# Changelog:
[Internal] -

Converts code related to DevSettings Module/Activity to Kotlin.

Reviewed By: cortinico

Differential Revision: D55574529

fbshipit-source-id: 41057307d0a12685a937a232e02a1a7a15736abe
  • Loading branch information
rshest authored and facebook-github-bot committed Apr 8, 2024
1 parent 88b6e11 commit a977b2e
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 278 deletions.
4 changes: 2 additions & 2 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -2091,7 +2091,7 @@ public abstract interface class com/facebook/react/devsupport/DevServerHelper$Pa
public abstract fun onPackagerReloadCommand ()V
}

public class com/facebook/react/devsupport/DevSettingsActivity : android/preference/PreferenceActivity {
public final class com/facebook/react/devsupport/DevSettingsActivity : android/preference/PreferenceActivity {
public fun <init> ()V
public fun onCreate (Landroid/os/Bundle;)V
}
Expand Down Expand Up @@ -3165,7 +3165,7 @@ public final class com/facebook/react/modules/core/TimingModule : com/facebook/f
public fun setSendIdleEvents (Z)V
}

public class com/facebook/react/modules/debug/DevSettingsModule : com/facebook/fbreact/specs/NativeDevSettingsSpec {
public final class com/facebook/react/modules/debug/DevSettingsModule : com/facebook/fbreact/specs/NativeDevSettingsSpec {
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;Lcom/facebook/react/devsupport/interfaces/DevSupportManager;)V
public fun addListener (Ljava/lang/String;)V
public fun addMenuItem (Ljava/lang/String;)V
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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.
*/

@file:Suppress("DEPRECATION")

package com.facebook.react.devsupport

import android.content.Context
import android.content.SharedPreferences
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.preference.PreferenceManager
import com.facebook.react.common.build.ReactBuildConfig
import com.facebook.react.modules.debug.interfaces.DeveloperSettings
import com.facebook.react.packagerconnection.PackagerConnectionSettings

/**
* Helper class for accessing developers settings that can not be accessed outside of the package
* [com.facebook.react.devsupport]. For accessing some of the settings by external modules this
* class implements an external interface [DeveloperSettings].
*/
internal class DevInternalSettings(applicationContext: Context, private val listener: Listener?) :
DeveloperSettings, OnSharedPreferenceChangeListener {
private val preferences: SharedPreferences =
PreferenceManager.getDefaultSharedPreferences(applicationContext)
val packagerConnectionSettings: PackagerConnectionSettings

init {
preferences.registerOnSharedPreferenceChangeListener(this)
packagerConnectionSettings = PackagerConnectionSettings(applicationContext)
}

override fun isFpsDebugEnabled(): Boolean = preferences.getBoolean(PREFS_FPS_DEBUG_KEY, false)

override fun isAnimationFpsDebugEnabled(): Boolean =
preferences.getBoolean(PREFS_ANIMATIONS_DEBUG_KEY, false)

override fun isJSDevModeEnabled(): Boolean =
preferences.getBoolean(PREFS_JS_DEV_MODE_DEBUG_KEY, true)

override fun isJSMinifyEnabled(): Boolean =
preferences.getBoolean(PREFS_JS_MINIFY_DEBUG_KEY, false)

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String?) {
if (listener != null) {
if (PREFS_FPS_DEBUG_KEY == key ||
PREFS_JS_DEV_MODE_DEBUG_KEY == key ||
PREFS_START_SAMPLING_PROFILER_ON_INIT == key ||
PREFS_JS_MINIFY_DEBUG_KEY == key) {
listener.onInternalSettingsChanged()
}
}
}

override fun isElementInspectorEnabled(): Boolean =
preferences.getBoolean(PREFS_INSPECTOR_DEBUG_KEY, false)

override fun isDeviceDebugEnabled(): Boolean = ReactBuildConfig.DEBUG

override fun isRemoteJSDebugEnabled(): Boolean =
preferences.getBoolean(PREFS_REMOTE_JS_DEBUG_KEY, false)

override fun setRemoteJSDebugEnabled(remoteJSDebugEnabled: Boolean) {
preferences.edit().putBoolean(PREFS_REMOTE_JS_DEBUG_KEY, remoteJSDebugEnabled).apply()
}

override fun isStartSamplingProfilerOnInit(): Boolean =
preferences.getBoolean(PREFS_START_SAMPLING_PROFILER_ON_INIT, false)

// Not supported.
override fun addMenuItem(title: String) = Unit

fun setElementInspectorEnabled(enabled: Boolean) {
preferences.edit().putBoolean(PREFS_INSPECTOR_DEBUG_KEY, enabled).apply()
}

var isHotModuleReplacementEnabled: Boolean
get() = preferences.getBoolean(PREFS_HOT_MODULE_REPLACEMENT_KEY, true)
set(enabled) {
preferences.edit().putBoolean(PREFS_HOT_MODULE_REPLACEMENT_KEY, enabled).apply()
}

fun setJSDevModeEnabled(value: Boolean) {
preferences.edit().putBoolean(PREFS_JS_DEV_MODE_DEBUG_KEY, value).apply()
}

fun setFpsDebugEnabled(enabled: Boolean) {
preferences.edit().putBoolean(PREFS_FPS_DEBUG_KEY, enabled).apply()
}

interface Listener {
fun onInternalSettingsChanged()
}

companion object {
private const val PREFS_FPS_DEBUG_KEY = "fps_debug"
private const val PREFS_JS_DEV_MODE_DEBUG_KEY = "js_dev_mode_debug"
private const val PREFS_JS_MINIFY_DEBUG_KEY = "js_minify_debug"
private const val PREFS_ANIMATIONS_DEBUG_KEY = "animations_debug"
private const val PREFS_INSPECTOR_DEBUG_KEY = "inspector_debug"
private const val PREFS_HOT_MODULE_REPLACEMENT_KEY = "hot_module_replacement"
private const val PREFS_REMOTE_JS_DEBUG_KEY = "remote_js_debug"
private const val PREFS_START_SAMPLING_PROFILER_ON_INIT = "start_sampling_profiler_on_init"
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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.
*/

@file:Suppress("DEPRECATION")

package com.facebook.react.devsupport

import android.os.Bundle
import android.preference.PreferenceActivity
import com.facebook.react.R

/**
* Activity that display developers settings. Should be added to the debug manifest of the app. Can
* be triggered through the developers option menu displayed by [DevSupportManager].
*/
public class DevSettingsActivity : PreferenceActivity() {
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
title = application.resources.getString(R.string.catalyst_settings_title)
addPreferencesFromResource(R.xml.rn_dev_preferences)
}
}
Loading

0 comments on commit a977b2e

Please sign in to comment.