|
| 1 | +package ai.openclaw.app |
| 2 | + |
| 3 | +import android.app.Service |
| 4 | +import android.content.BroadcastReceiver |
| 5 | +import android.content.Context |
| 6 | +import android.content.Intent |
| 7 | +import android.os.IBinder |
| 8 | +import android.util.Base64 |
| 9 | +import android.util.Log |
| 10 | +import kotlinx.coroutines.CoroutineScope |
| 11 | +import kotlinx.coroutines.Dispatchers |
| 12 | +import kotlinx.coroutines.SupervisorJob |
| 13 | +import kotlinx.coroutines.cancel |
| 14 | +import kotlinx.coroutines.delay |
| 15 | +import kotlinx.coroutines.launch |
| 16 | +import kotlinx.coroutines.withTimeout |
| 17 | +import kotlinx.serialization.json.JsonNull |
| 18 | +import kotlinx.serialization.json.JsonPrimitive |
| 19 | +import kotlinx.serialization.json.buildJsonObject |
| 20 | +import java.io.File |
| 21 | + |
| 22 | +private const val tag = "VoiceE2E" |
| 23 | +private const val resultFileName = "voice_e2e_result.json" |
| 24 | + |
| 25 | +class VoiceE2eReceiver : BroadcastReceiver() { |
| 26 | + override fun onReceive( |
| 27 | + context: Context, |
| 28 | + intent: Intent, |
| 29 | + ) { |
| 30 | + context.startService( |
| 31 | + Intent(context, VoiceE2eService::class.java) |
| 32 | + .putExtras(intent), |
| 33 | + ) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +class VoiceE2eService : Service() { |
| 38 | + private val serviceScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) |
| 39 | + |
| 40 | + override fun onBind(intent: Intent?): IBinder? = null |
| 41 | + |
| 42 | + override fun onStartCommand( |
| 43 | + intent: Intent?, |
| 44 | + flags: Int, |
| 45 | + startId: Int, |
| 46 | + ): Int { |
| 47 | + val command = intent ?: return START_NOT_STICKY |
| 48 | + serviceScope.launch { |
| 49 | + try { |
| 50 | + runCommand(command) |
| 51 | + } finally { |
| 52 | + stopSelf(startId) |
| 53 | + } |
| 54 | + } |
| 55 | + return START_NOT_STICKY |
| 56 | + } |
| 57 | + |
| 58 | + override fun onDestroy() { |
| 59 | + serviceScope.cancel() |
| 60 | + super.onDestroy() |
| 61 | + } |
| 62 | + |
| 63 | + private suspend fun runCommand(intent: Intent) { |
| 64 | + try { |
| 65 | + val app = applicationContext as NodeApp |
| 66 | + val runtime = app.ensureRuntime() |
| 67 | + val mode = |
| 68 | + intent |
| 69 | + .getDecodedStringExtra("mode") |
| 70 | + ?.trim() |
| 71 | + .orEmpty() |
| 72 | + .ifEmpty { "both" } |
| 73 | + if (mode == "stop") { |
| 74 | + runtime.cancelMicCapture() |
| 75 | + runtime.setTalkModeEnabled(false) |
| 76 | + writeResult("""{"ok":true,"mode":"stop"}""") |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + val connect = !intent.getBooleanExtra("noConnect", false) |
| 81 | + val connectTimeoutMs = intent.getLongExtra("connectTimeoutMs", 20_000L) |
| 82 | + if (connect) { |
| 83 | + configureGateway(runtime = runtime, intent = intent) |
| 84 | + } |
| 85 | + if (connect || !runtime.isConnected.value) { |
| 86 | + awaitGateway(runtime = runtime, timeoutMs = connectTimeoutMs) |
| 87 | + } |
| 88 | + |
| 89 | + startActivity( |
| 90 | + Intent(actionOpenVoiceE2e) |
| 91 | + .setClass(this, MainActivity::class.java) |
| 92 | + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP), |
| 93 | + ) |
| 94 | + |
| 95 | + if (mode == "connect") { |
| 96 | + val resultJson = """{"ok":true,"mode":"connect","connected":true}""" |
| 97 | + writeResult(resultJson) |
| 98 | + Log.i(tag, "PASS $resultJson") |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + val transcript = |
| 103 | + intent |
| 104 | + .getDecodedStringExtra("transcript") |
| 105 | + ?.trim() |
| 106 | + .orEmpty() |
| 107 | + .ifEmpty { "Reply exactly: Android voice e2e normal path ok." } |
| 108 | + val realtimeReply = |
| 109 | + intent |
| 110 | + .getDecodedStringExtra("realtimeAssistant") |
| 111 | + ?.trim() |
| 112 | + .orEmpty() |
| 113 | + .ifEmpty { "Android realtime voice e2e relay path ok." } |
| 114 | + val timeoutMs = intent.getLongExtra("timeoutMs", 60_000L) |
| 115 | + val result = |
| 116 | + runtime.runVoiceE2e( |
| 117 | + mode = mode, |
| 118 | + transcript = transcript, |
| 119 | + realtimeAssistantText = realtimeReply, |
| 120 | + timeoutMs = timeoutMs, |
| 121 | + ) |
| 122 | + val resultJson = encodeResult(result) |
| 123 | + writeResult(resultJson) |
| 124 | + Log.i(tag, "PASS $resultJson") |
| 125 | + } catch (err: Throwable) { |
| 126 | + val resultJson = |
| 127 | + buildJsonObject { |
| 128 | + put("ok", JsonPrimitive(false)) |
| 129 | + put("error", JsonPrimitive(err.message ?: err::class.java.simpleName)) |
| 130 | + }.toString() |
| 131 | + writeResult(resultJson) |
| 132 | + Log.e(tag, "FAIL $resultJson", err) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private fun configureGateway( |
| 137 | + runtime: NodeRuntime, |
| 138 | + intent: Intent, |
| 139 | + ) { |
| 140 | + val host = |
| 141 | + intent |
| 142 | + .getDecodedStringExtra("host") |
| 143 | + ?.trim() |
| 144 | + .orEmpty() |
| 145 | + .ifEmpty { "127.0.0.1" } |
| 146 | + val port = intent.getIntExtra("port", 18789) |
| 147 | + runtime.setManualEnabled(true) |
| 148 | + runtime.setManualHost(host) |
| 149 | + runtime.setManualPort(port) |
| 150 | + runtime.setManualTls(intent.getBooleanExtra("tls", false)) |
| 151 | + runtime.setGatewayToken(intent.getDecodedStringExtra("token").orEmpty()) |
| 152 | + runtime.setGatewayBootstrapToken(intent.getDecodedStringExtra("bootstrapToken").orEmpty()) |
| 153 | + runtime.setGatewayPassword(intent.getDecodedStringExtra("password").orEmpty()) |
| 154 | + runtime.setOnboardingCompleted(true) |
| 155 | + runtime.connectManual() |
| 156 | + } |
| 157 | + |
| 158 | + private suspend fun awaitGateway( |
| 159 | + runtime: NodeRuntime, |
| 160 | + timeoutMs: Long, |
| 161 | + ) { |
| 162 | + withTimeout(timeoutMs) { |
| 163 | + while (!runtime.isConnected.value) { |
| 164 | + delay(100L) |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private fun encodeResult(result: NodeRuntime.VoiceE2eResult): String = |
| 170 | + buildJsonObject { |
| 171 | + put("ok", JsonPrimitive(true)) |
| 172 | + put("normal", result.normal?.let(::encodeSlice) ?: JsonNull) |
| 173 | + put("realtime", result.realtime?.let(::encodeSlice) ?: JsonNull) |
| 174 | + }.toString() |
| 175 | + |
| 176 | + private fun encodeSlice(slice: NodeRuntime.VoiceE2eSliceResult) = |
| 177 | + buildJsonObject { |
| 178 | + put("mode", JsonPrimitive(slice.mode)) |
| 179 | + put("status", JsonPrimitive(slice.status)) |
| 180 | + put("userText", slice.userText?.let(::JsonPrimitive) ?: JsonNull) |
| 181 | + put("assistantText", slice.assistantText?.let(::JsonPrimitive) ?: JsonNull) |
| 182 | + } |
| 183 | + |
| 184 | + private fun writeResult(json: String) { |
| 185 | + File(cacheDir, resultFileName).writeText(json) |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +private fun Intent.getDecodedStringExtra(name: String): String? { |
| 190 | + val encoded = getStringExtra("${name}Base64") |
| 191 | + if (!encoded.isNullOrBlank()) { |
| 192 | + return String(Base64.decode(encoded, Base64.NO_WRAP), Charsets.UTF_8) |
| 193 | + } |
| 194 | + return getStringExtra(name) |
| 195 | +} |
0 commit comments