mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.droiddeck.launcher.agent
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
import java.io.File
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], manifest = Config.NONE)
|
||||
class AgentCallReceiverTest {
|
||||
private val original = AgentCallReceiver.call
|
||||
|
||||
@After fun restore() {
|
||||
AgentCallReceiver.call = original
|
||||
}
|
||||
|
||||
@Test fun aSlowCallIsAnsweredAfterTheBroadcastReturns() {
|
||||
val context: Context = RuntimeEnvironment.getApplication()
|
||||
val release = CountDownLatch(1)
|
||||
val started = CountDownLatch(1)
|
||||
AgentCallReceiver.call = { _, method, _, _ ->
|
||||
started.countDown()
|
||||
release.await(10, TimeUnit.SECONDS)
|
||||
"""{"ok":true,"method":"$method"}"""
|
||||
}
|
||||
val answer = File(context.getExternalFilesDir(null), "agent-calls/slow1.json").apply { delete() }
|
||||
|
||||
AgentCallReceiver().onReceive(context, Intent().putExtra("method", "guest").putExtra("id", "slow1"))
|
||||
|
||||
assertTrue(started.await(5, TimeUnit.SECONDS))
|
||||
assertFalse(answer.exists())
|
||||
release.countDown()
|
||||
val deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5)
|
||||
while (!answer.exists() && System.nanoTime() < deadline) Thread.sleep(20)
|
||||
assertEquals("""{"ok":true,"method":"guest"}""", answer.readText())
|
||||
}
|
||||
|
||||
@Test fun anUnsafeIdIsIgnored() {
|
||||
val context: Context = RuntimeEnvironment.getApplication()
|
||||
var called = false
|
||||
AgentCallReceiver.call = { _, _, _, _ -> called = true; "{}" }
|
||||
|
||||
AgentCallReceiver().onReceive(context, Intent().putExtra("method", "state").putExtra("id", "../x"))
|
||||
|
||||
Thread.sleep(100)
|
||||
assertFalse(called)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user