Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(Android): convert systeminfo module classes to kotlin #47616

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1596,7 +1596,7 @@ public InspectorTargetDelegateImpl(ReactInstanceManager inspectorTarget) {
public Map<String, String> getMetadata() {
ReactInstanceManager reactInstanceManager = mReactInstanceManagerWeak.get();

return AndroidInfoHelpers.getInspectorHostMetadata(
return AndroidInfoHelpers.INSTANCE.getInspectorHostMetadata(
reactInstanceManager != null ? reactInstanceManager.mApplicationContext : null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package com.facebook.react;

import static com.facebook.react.ReactInstanceManager.initializeSoLoaderIfNecessary;
import static com.facebook.react.modules.systeminfo.AndroidInfoHelpers.getFriendlyDeviceName;

import android.app.Activity;
import android.app.Application;
Expand Down Expand Up @@ -37,6 +36,7 @@
import com.facebook.react.jscexecutor.JSCExecutor;
import com.facebook.react.jscexecutor.JSCExecutorFactory;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.modules.systeminfo.AndroidInfoHelpers;
import com.facebook.react.packagerconnection.RequestHandler;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -339,7 +339,7 @@ public ReactInstanceManager build() {
// We use the name of the device and the app for debugging & metrics
//noinspection ConstantConditions
String appName = mApplication.getPackageName();
String deviceName = getFriendlyDeviceName();
String deviceName = AndroidInfoHelpers.INSTANCE.getFriendlyDeviceName();

return new ReactInstanceManager(
mApplication,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public void openInspectorConnection() {
protected Void doInBackground(Void... params) {
if (InspectorFlags.getFuseboxEnabled()) {
Map<String, String> metadata =
AndroidInfoHelpers.getInspectorHostMetadata(mApplicationContext);
AndroidInfoHelpers.INSTANCE.getInspectorHostMetadata(mApplicationContext);

mInspectorPackagerConnection =
new CxxInspectorPackagerConnection(
Expand Down Expand Up @@ -327,7 +327,7 @@ private String getInspectorDeviceUrl() {
Locale.US,
"http://%s/inspector/device?name=%s&app=%s&device=%s",
mPackagerConnectionSettings.getDebugServerHost(),
Uri.encode(AndroidInfoHelpers.getFriendlyDeviceName()),
Uri.encode(AndroidInfoHelpers.INSTANCE.getFriendlyDeviceName()),
Uri.encode(mPackageName),
Uri.encode(getInspectorDeviceId()));
}
Expand Down Expand Up @@ -360,7 +360,7 @@ private String getHostForJSProxy() {
if (portOffset > -1) {
return "localhost" + host.substring(portOffset);
} else {
return AndroidInfoHelpers.DEVICE_LOCALHOST;
return AndroidInfoHelpers.INSTANCE.DEVICE_LOCALHOST;
}
}

Expand Down

This file was deleted.

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

import android.content.Context
import android.os.Build
import com.facebook.common.logging.FLog
import com.facebook.react.R
import java.io.BufferedReader
import java.io.InputStreamReader
import java.nio.charset.Charset
import java.util.Locale

public object AndroidInfoHelpers {

public const val EMULATOR_LOCALHOST: String = "10.0.2.2"
public const val GENYMOTION_LOCALHOST: String = "10.0.3.2"
public const val DEVICE_LOCALHOST: String = "localhost"
public const val METRO_HOST_PROP_NAME: String = "metro.host"
private val TAG = AndroidInfoHelpers::class.java.simpleName
private var metroHostPropValue: String? = null

private fun isRunningOnGenymotion(): Boolean = Build.FINGERPRINT.contains("vbox")

private fun isRunningOnStockEmulator(): Boolean = Build.FINGERPRINT.contains("generic") or Build.FINGERPRINT.startsWith("google/sdk_gphone")

@JvmStatic
public fun getServerHost(port: Int): String = getServerIpAddress(port)

@JvmStatic
public fun getServerHost(context: Context): String = getServerIpAddress(getDevServerPort(context))

@JvmStatic
public fun getAdbReverseTcpCommand(port: Int): String = "adb reverse tcp:$port tcp:$port"

@JvmStatic
public fun getAdbReverseTcpCommand(context: Context): String = getAdbReverseTcpCommand(getDevServerPort(context))

@JvmStatic
public fun getFriendlyDeviceName(): String {
oddlyspaced marked this conversation as resolved.
Show resolved Hide resolved
return if (isRunningOnGenymotion()) {
Build.MODEL
} else {
"${Build.MODEL} - ${Build.VERSION.RELEASE} - API ${Build.VERSION.SDK_INT}"
}
}

@JvmStatic
public fun getInspectorHostMetadata(applicationContext: Context?): Map<String, String?> {
var appIdentifier: String? = null
var appDisplayName: String? = null

applicationContext?.let {
val applicationInfo = it.applicationInfo
val labelResourceId = applicationInfo.labelRes

appIdentifier = it.packageName
appDisplayName = if (labelResourceId == 0) {
applicationInfo.nonLocalizedLabel.toString()
} else {
it.getString(labelResourceId)
}
}

return mapOf(
"appDisplayName" to appDisplayName,
"appIdentifier" to appIdentifier,
"platform" to "android",
"deviceName" to Build.MODEL,
"reactNativeVersion" to getReactNativeVersionString()
)
}

private fun getReactNativeVersionString(): String {
val version = ReactNativeVersion.VERSION
return "${version["major"]}.${version["minor"]}.${version["patch"]}" +
(version["prerelease"]?.let { "-$it" } ?: "")
}

private fun getDevServerPort(context: Context): Int = context.resources.getInteger(R.integer.react_native_dev_server_port)

private fun getServerIpAddress(port: Int): String {
val ipAddress: String = when {
getMetroHostPropValue().isNotEmpty() -> getMetroHostPropValue()
isRunningOnGenymotion() -> GENYMOTION_LOCALHOST
isRunningOnStockEmulator() -> EMULATOR_LOCALHOST
else -> DEVICE_LOCALHOST
}
return String.format(Locale.US, "%s:%d", ipAddress, port)
}

@Synchronized
private fun getMetroHostPropValue(): String {
if (metroHostPropValue != null) {
return metroHostPropValue!!
}
var process: Process? = null
var reader: BufferedReader? = null
try {
process = Runtime.getRuntime().exec(arrayOf("/system/bin/getprop", METRO_HOST_PROP_NAME))
reader = BufferedReader(InputStreamReader(process.inputStream, Charset.forName("UTF-8")))

var lastLine = ""
var line: String?
while (reader.readLine().also { line = it } != null) {
lastLine = line ?: ""
}
metroHostPropValue = lastLine
} catch (e: Exception) {
FLog.w(TAG, "Failed to query for metro.host prop:", e)
metroHostPropValue = ""
} finally {
reader?.close()
process?.destroy()
}
return metroHostPropValue ?: ""
}
}
Loading
Loading