-
Notifications
You must be signed in to change notification settings - Fork 25k
chore(Android): convert systeminfo module classes to kotlin #47616
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
abe93a9
chore: convert systeminfo module classes to kotlin
oddlyspaced d3a2c74
chore: revert back react native version java class
oddlyspaced de260ae
chore: suppress deprecation for SERIAL param
oddlyspaced a28480c
chore: add JvmStatic annotation to public methods of AndroidInfoHelpers
oddlyspaced 7dc6914
chore: remove INSTANCE for converted system info module
oddlyspaced File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 0 additions & 175 deletions
175
.../ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.java
This file was deleted.
Oops, something went wrong.
123 changes: 123 additions & 0 deletions
123
...ve/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/AndroidInfoHelpers.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
| 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 ?: "" | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.