mirror of
https://github.com/PenumbraOS/mabl.git
synced 2026-02-04 02:08:57 -06:00
Working IP address retrieval
This commit is contained in:
parent
b895cff05f
commit
e407dca2fa
@ -2,8 +2,23 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
|
||||
<application>
|
||||
<service
|
||||
android:name=".tool.NetworkService"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="com.penumbraos.mabl.sdk.action.TOOL_SERVICE" />
|
||||
</intent-filter>
|
||||
|
||||
<meta-data
|
||||
android:name="com.penumbraos.mabl.sdk.metadata.DISPLAY_NAME"
|
||||
android:value="Network Tool Service" />
|
||||
<meta-data
|
||||
android:name="com.penumbraos.mabl.sdk.metadata.DESCRIPTION"
|
||||
android:value="Tool that provides access to device network information" />
|
||||
</service>
|
||||
<service
|
||||
android:name=".tool.VolumeService"
|
||||
android:exported="true">
|
||||
|
||||
@ -0,0 +1,79 @@
|
||||
package com.penumbraos.plugins.system.tool
|
||||
|
||||
import android.Manifest
|
||||
import android.net.ConnectivityManager
|
||||
import android.util.Log
|
||||
import androidx.annotation.RequiresPermission
|
||||
import com.penumbraos.mabl.sdk.IToolCallback
|
||||
import com.penumbraos.mabl.sdk.ToolCall
|
||||
import com.penumbraos.mabl.sdk.ToolDefinition
|
||||
import com.penumbraos.mabl.sdk.ToolService
|
||||
import org.json.JSONObject
|
||||
|
||||
|
||||
private const val GET_IP = "get_ip"
|
||||
|
||||
private val IPv4_REGEX = """(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(/\d{1,2})?""".toRegex()
|
||||
|
||||
class NetworkService : ToolService("NetworkService") {
|
||||
@RequiresPermission(Manifest.permission.ACCESS_NETWORK_STATE)
|
||||
override fun executeTool(
|
||||
call: ToolCall,
|
||||
params: JSONObject?,
|
||||
callback: IToolCallback
|
||||
) {
|
||||
when (call.name) {
|
||||
GET_IP -> {
|
||||
val connectivityManager =
|
||||
getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager?
|
||||
|
||||
if (connectivityManager == null) {
|
||||
callback.onError("Failed to get network status")
|
||||
return
|
||||
}
|
||||
|
||||
val linkProperties =
|
||||
connectivityManager.getLinkProperties(connectivityManager.activeNetwork)
|
||||
|
||||
if (linkProperties == null) {
|
||||
callback.onError("Failed to get network status")
|
||||
return
|
||||
}
|
||||
|
||||
Log.d(
|
||||
"NetworkService",
|
||||
"Link properties: ${linkProperties.linkAddresses.map { it.toString() }}"
|
||||
)
|
||||
|
||||
val address =
|
||||
linkProperties.linkAddresses.map {
|
||||
val result = IPv4_REGEX.matchEntire(it.toString())
|
||||
result?.groups[1]?.value
|
||||
}.firstOrNull()
|
||||
|
||||
if (address == null) {
|
||||
callback.onError("Could not identify IP address")
|
||||
return
|
||||
}
|
||||
|
||||
callback.onSuccess("My IP address is $address")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getToolDefinitions(): Array<ToolDefinition> {
|
||||
return arrayOf(
|
||||
ToolDefinition().apply {
|
||||
name = GET_IP
|
||||
description = "Get the IP address of the device"
|
||||
examples = arrayOf(
|
||||
"what is your IP address",
|
||||
"what is your address",
|
||||
"IP address",
|
||||
"internet address",
|
||||
"what is the IP"
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user