From 67609cc16fbc25bb64d53b6d0a8d1fda842b1569 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Fri, 27 Feb 2026 08:52:31 +0530 Subject: [PATCH] fix(android): parse camera and screen invoke params as JSON --- .../android/node/CameraCaptureManager.kt | 80 +++++++++--------- .../android/node/ScreenRecordManager.kt | 82 ++++++++----------- 2 files changed, 76 insertions(+), 86 deletions(-) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/node/CameraCaptureManager.kt b/apps/android/app/src/main/java/ai/openclaw/android/node/CameraCaptureManager.kt index aa038ad9a94..b3736ce2317 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/node/CameraCaptureManager.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/node/CameraCaptureManager.kt @@ -30,6 +30,10 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.withTimeout import kotlinx.coroutines.withContext +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.JsonPrimitive +import kotlinx.serialization.json.contentOrNull import java.io.ByteArrayOutputStream import java.io.File import java.util.concurrent.Executor @@ -80,9 +84,10 @@ class CameraCaptureManager(private val context: Context) { withContext(Dispatchers.Main) { ensureCameraPermission() val owner = lifecycleOwner ?: throw IllegalStateException("UNAVAILABLE: camera not ready") - val facing = parseFacing(paramsJson) ?: "front" - val quality = (parseQuality(paramsJson) ?: 0.95).coerceIn(0.1, 1.0) - val maxWidth = parseMaxWidth(paramsJson) ?: 1600 + val params = parseParamsObject(paramsJson) + val facing = parseFacing(params) ?: "front" + val quality = (parseQuality(params) ?: 0.95).coerceIn(0.1, 1.0) + val maxWidth = parseMaxWidth(params) ?: 1600 val provider = context.cameraProvider() val capture = ImageCapture.Builder().build() @@ -145,9 +150,10 @@ class CameraCaptureManager(private val context: Context) { withContext(Dispatchers.Main) { ensureCameraPermission() val owner = lifecycleOwner ?: throw IllegalStateException("UNAVAILABLE: camera not ready") - val facing = parseFacing(paramsJson) ?: "front" - val durationMs = (parseDurationMs(paramsJson) ?: 3_000).coerceIn(200, 60_000) - val includeAudio = parseIncludeAudio(paramsJson) ?: true + val params = parseParamsObject(paramsJson) + val facing = parseFacing(params) ?: "front" + val durationMs = (parseDurationMs(params) ?: 3_000).coerceIn(200, 60_000) + val includeAudio = parseIncludeAudio(params) ?: true if (includeAudio) ensureMicPermission() android.util.Log.w("CameraCaptureManager", "clip: start facing=$facing duration=$durationMs audio=$includeAudio") @@ -270,46 +276,42 @@ class CameraCaptureManager(private val context: Context) { return rotated } - private fun parseFacing(paramsJson: String?): String? = - when { - paramsJson?.contains("\"front\"") == true -> "front" - paramsJson?.contains("\"back\"") == true -> "back" - else -> null + private fun parseParamsObject(paramsJson: String?): JsonObject? { + if (paramsJson.isNullOrBlank()) return null + return try { + Json.parseToJsonElement(paramsJson).asObjectOrNull() + } catch (_: Throwable) { + null } + } - private fun parseQuality(paramsJson: String?): Double? = - parseNumber(paramsJson, key = "quality")?.toDoubleOrNull() + private fun readPrimitive(params: JsonObject?, key: String): JsonPrimitive? = + params?.get(key) as? JsonPrimitive - private fun parseMaxWidth(paramsJson: String?): Int? = - parseNumber(paramsJson, key = "maxWidth")?.toIntOrNull() - - private fun parseDurationMs(paramsJson: String?): Int? = - parseNumber(paramsJson, key = "durationMs")?.toIntOrNull() - - private fun parseIncludeAudio(paramsJson: String?): Boolean? { - val raw = paramsJson ?: return null - val key = "\"includeAudio\"" - val idx = raw.indexOf(key) - if (idx < 0) return null - val colon = raw.indexOf(':', idx + key.length) - if (colon < 0) return null - val tail = raw.substring(colon + 1).trimStart() - return when { - tail.startsWith("true") -> true - tail.startsWith("false") -> false + private fun parseFacing(params: JsonObject?): String? { + val value = readPrimitive(params, "facing")?.contentOrNull?.trim()?.lowercase() ?: return null + return when (value) { + "front", "back" -> value else -> null } } - private fun parseNumber(paramsJson: String?, key: String): String? { - val raw = paramsJson ?: return null - val needle = "\"$key\"" - val idx = raw.indexOf(needle) - if (idx < 0) return null - val colon = raw.indexOf(':', idx + needle.length) - if (colon < 0) return null - val tail = raw.substring(colon + 1).trimStart() - return tail.takeWhile { it.isDigit() || it == '.' } + private fun parseQuality(params: JsonObject?): Double? = + readPrimitive(params, "quality")?.contentOrNull?.toDoubleOrNull() + + private fun parseMaxWidth(params: JsonObject?): Int? = + readPrimitive(params, "maxWidth")?.contentOrNull?.toIntOrNull() + + private fun parseDurationMs(params: JsonObject?): Int? = + readPrimitive(params, "durationMs")?.contentOrNull?.toIntOrNull() + + private fun parseIncludeAudio(params: JsonObject?): Boolean? { + val value = readPrimitive(params, "includeAudio")?.contentOrNull?.trim()?.lowercase() + return when (value) { + "true" -> true + "false" -> false + else -> null + } } private fun Context.mainExecutor(): Executor = ContextCompat.getMainExecutor(this) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/node/ScreenRecordManager.kt b/apps/android/app/src/main/java/ai/openclaw/android/node/ScreenRecordManager.kt index 337a953866a..98a3e4d9593 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/node/ScreenRecordManager.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/node/ScreenRecordManager.kt @@ -10,6 +10,10 @@ import ai.openclaw.android.ScreenCaptureRequester import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.withContext +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.JsonPrimitive +import kotlinx.serialization.json.contentOrNull import java.io.File import kotlin.math.roundToInt @@ -35,12 +39,13 @@ class ScreenRecordManager(private val context: Context) { "SCREEN_PERMISSION_REQUIRED: grant Screen Recording permission", ) - val durationMs = (parseDurationMs(paramsJson) ?: 10_000).coerceIn(250, 60_000) - val fps = (parseFps(paramsJson) ?: 10.0).coerceIn(1.0, 60.0) + val params = parseParamsObject(paramsJson) + val durationMs = (parseDurationMs(params) ?: 10_000).coerceIn(250, 60_000) + val fps = (parseFps(params) ?: 10.0).coerceIn(1.0, 60.0) val fpsInt = fps.roundToInt().coerceIn(1, 60) - val screenIndex = parseScreenIndex(paramsJson) - val includeAudio = parseIncludeAudio(paramsJson) ?: true - val format = parseString(paramsJson, key = "format") + val screenIndex = parseScreenIndex(params) + val includeAudio = parseIncludeAudio(params) ?: true + val format = parseString(params, key = "format") if (format != null && format.lowercase() != "mp4") { throw IllegalArgumentException("INVALID_REQUEST: screen format must be mp4") } @@ -141,55 +146,38 @@ class ScreenRecordManager(private val context: Context) { } } - private fun parseDurationMs(paramsJson: String?): Int? = - parseNumber(paramsJson, key = "durationMs")?.toIntOrNull() + private fun parseParamsObject(paramsJson: String?): JsonObject? { + if (paramsJson.isNullOrBlank()) return null + return try { + Json.parseToJsonElement(paramsJson).asObjectOrNull() + } catch (_: Throwable) { + null + } + } - private fun parseFps(paramsJson: String?): Double? = - parseNumber(paramsJson, key = "fps")?.toDoubleOrNull() + private fun readPrimitive(params: JsonObject?, key: String): JsonPrimitive? = + params?.get(key) as? JsonPrimitive - private fun parseScreenIndex(paramsJson: String?): Int? = - parseNumber(paramsJson, key = "screenIndex")?.toIntOrNull() + private fun parseDurationMs(params: JsonObject?): Int? = + readPrimitive(params, "durationMs")?.contentOrNull?.toIntOrNull() - private fun parseIncludeAudio(paramsJson: String?): Boolean? { - val raw = paramsJson ?: return null - val key = "\"includeAudio\"" - val idx = raw.indexOf(key) - if (idx < 0) return null - val colon = raw.indexOf(':', idx + key.length) - if (colon < 0) return null - val tail = raw.substring(colon + 1).trimStart() - return when { - tail.startsWith("true") -> true - tail.startsWith("false") -> false + private fun parseFps(params: JsonObject?): Double? = + readPrimitive(params, "fps")?.contentOrNull?.toDoubleOrNull() + + private fun parseScreenIndex(params: JsonObject?): Int? = + readPrimitive(params, "screenIndex")?.contentOrNull?.toIntOrNull() + + private fun parseIncludeAudio(params: JsonObject?): Boolean? { + val value = readPrimitive(params, "includeAudio")?.contentOrNull?.trim()?.lowercase() + return when (value) { + "true" -> true + "false" -> false else -> null } } - private fun parseNumber(paramsJson: String?, key: String): String? { - val raw = paramsJson ?: return null - val needle = "\"$key\"" - val idx = raw.indexOf(needle) - if (idx < 0) return null - val colon = raw.indexOf(':', idx + needle.length) - if (colon < 0) return null - val tail = raw.substring(colon + 1).trimStart() - return tail.takeWhile { it.isDigit() || it == '.' || it == '-' } - } - - private fun parseString(paramsJson: String?, key: String): String? { - val raw = paramsJson ?: return null - val needle = "\"$key\"" - val idx = raw.indexOf(needle) - if (idx < 0) return null - val colon = raw.indexOf(':', idx + needle.length) - if (colon < 0) return null - val tail = raw.substring(colon + 1).trimStart() - if (!tail.startsWith('\"')) return null - val rest = tail.drop(1) - val end = rest.indexOf('\"') - if (end < 0) return null - return rest.substring(0, end) - } + private fun parseString(params: JsonObject?, key: String): String? = + readPrimitive(params, key)?.contentOrNull private fun estimateBitrate(width: Int, height: Int, fps: Int): Int { val pixels = width.toLong() * height.toLong()