mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
fix(ctl): long agent calls over the broadcast bridge no longer make Android report DroidDeck as not responding (#2363)
This commit is contained in:
1 parent
db933e4232
commit
2426ea793a
2 files changed
+74
-6
No files matched your search
@@ -4,6 +4,7 @@ import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
@@ -17,9 +18,17 @@ class AgentCallReceiver : BroadcastReceiver() {
|
||||
val id = intent.getStringExtra("id")?.takeIf { it.matches(Regex("[A-Za-z0-9_-]{1,64}")) } ?: return
|
||||
val arg = intent.getStringExtra("arg")
|
||||
val request = intent.getStringExtra(AgentBridgeProvider.EXTRA_REQUEST)
|
||||
val pending = goAsync()
|
||||
Thread({
|
||||
val json = try {
|
||||
val app = context.applicationContext
|
||||
// The caller waits for the answer file, not for the broadcast. Holding the broadcast open
|
||||
// with goAsync() for a call that outlasts the broadcast timeout (10 s in the foreground)
|
||||
// makes Android report the app as not responding.
|
||||
Thread({ answer(app, method, id, call(app, method, arg, request)) }, "agent-call-$id").start()
|
||||
}
|
||||
|
||||
companion object {
|
||||
@VisibleForTesting
|
||||
internal var call: (Context, String, String?, String?) -> String = { context, method, arg, request ->
|
||||
try {
|
||||
val extras = Bundle().apply { putString(AgentBridgeProvider.EXTRA_REQUEST, request) }
|
||||
context.contentResolver.call("${context.packageName}.agent", method, arg, extras)
|
||||
?.getString(AgentBridgeProvider.RESULT_JSON) ?: """{"ok":false,"error":{"code":"NO_RESPONSE","message":"no response"}}"""
|
||||
@@ -27,15 +36,16 @@ class AgentCallReceiver : BroadcastReceiver() {
|
||||
org.json.JSONObject().put("ok", false)
|
||||
.put("error", org.json.JSONObject().put("code", "COMMAND_FAILED").put("message", e.message ?: e.javaClass.simpleName)).toString()
|
||||
}
|
||||
}
|
||||
|
||||
private fun answer(context: Context, method: String, id: String, json: String) {
|
||||
try {
|
||||
val dir = File(context.getExternalFilesDir(null), "agent-calls").apply { mkdirs() }
|
||||
File(dir, "$id.json.part").writeText(json)
|
||||
File(dir, "$id.json.part").renameTo(File(dir, "$id.json"))
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.w("AgentCallReceiver", "could not write the answer to $method: ${e.message}")
|
||||
} finally {
|
||||
pending.finish()
|
||||
}
|
||||
}, "agent-call-$id").start()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user