mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 14:26:00 +00:00
fix(agent): connect adb only for commands that use it and run setup checks one at a time
This commit is contained in:
1 parent
3f5e3bb19d
commit
77e30e4b4e
2 files changed
+19
-17
No files matched your search
@@ -131,7 +131,7 @@ object AgentHost {
|
||||
* proot over the installed rootfs, without a session. Loopback adb is connected first so
|
||||
* droiddeckctl reaches the app.
|
||||
*/
|
||||
fun guest(app: Context, command: List<String>, workdir: String = "/root"): ProcessBuilder {
|
||||
fun guest(app: Context, command: List<String>, workdir: String = "/root", connectAdb: Boolean = true): ProcessBuilder {
|
||||
val adb = AgentAdb.prepare(app)
|
||||
val env = listOf(
|
||||
"/usr/bin/env", "HOME=/root", "PATH=/root/.local/bin:/usr/local/bin:/usr/bin:/bin", "LANG=C.UTF-8",
|
||||
@@ -140,7 +140,9 @@ object AgentHost {
|
||||
)
|
||||
val shell = listOf(
|
||||
"/bin/sh", "-c",
|
||||
"adb connect \"\$ADB_SERIAL\" >/dev/null 2>&1; cd \"$1\" 2>/dev/null; shift; exec \"$@\"", "sh", workdir,
|
||||
// proot ends the adb server with the command, so each command that needs adb connects it.
|
||||
(if (connectAdb) "adb connect \"\$ADB_SERIAL\" >/dev/null 2>&1; " else "") +
|
||||
"cd \"$1\" 2>/dev/null; shift; exec \"$@\"", "sh", workdir,
|
||||
)
|
||||
val proot = LinuxRuntime.command(app, null, null, Environment.getExternalStorageDirectory(), env + shell + command)
|
||||
return ProcessBuilder(proot).redirectErrorStream(true).apply {
|
||||
|
||||
@@ -44,16 +44,18 @@ object AgentSetup {
|
||||
if (!runtimeInstalled) return
|
||||
AgentHost.refresh(app)
|
||||
val harness = AgentHost.harness
|
||||
val probes = mutableListOf<Pair<Step, List<String>>>()
|
||||
for (step in Step.entries) if (status[step] != Status.WORKING) {
|
||||
when (step) {
|
||||
Step.INSTALL -> set(step, if (AgentHost.guestFileExists(app, harness.binary)) Status.DONE else Status.TODO)
|
||||
Step.BUILD_TOOLS -> set(step, if (AgentHost.guestFileExists(app, "/root/aapt2/aapt2") &&
|
||||
AgentHost.guestFileExists(app, "$CHECKOUT/.git")) Status.DONE else Status.TODO)
|
||||
Step.CONNECTION -> probe(app, step, listOf("sh", "-c", "adb -s \"\$ADB_SERIAL\" get-state | grep -q device"))
|
||||
Step.SIGN_IN -> if (AgentHost.guestFileExists(app, harness.binary)) probe(app, step, harness.signedIn) else set(step, Status.TODO)
|
||||
Step.GITHUB -> probe(app, step, listOf("gh", "auth", "status"))
|
||||
Step.CONNECTION -> probes += step to listOf("sh", "-c", "adb -s \"\$ADB_SERIAL\" get-state | grep -q device")
|
||||
Step.SIGN_IN -> if (AgentHost.guestFileExists(app, harness.binary)) probes += step to harness.signedIn else set(step, Status.TODO)
|
||||
Step.GITHUB -> probes += step to listOf("gh", "auth", "status")
|
||||
}
|
||||
}
|
||||
Thread({ for ((step, command) in probes) probe(app, step, command) }, "agent-checks").start()
|
||||
}
|
||||
|
||||
/** Runs [step]'s action. [CONNECTION] is the app's own pairing flow, so the caller opens it. */
|
||||
@@ -98,18 +100,16 @@ object AgentSetup {
|
||||
}
|
||||
|
||||
private fun probe(app: Context, step: Step, command: List<String>) {
|
||||
Thread({
|
||||
val ok = runCatching {
|
||||
val child = AgentHost.guest(app, command).start()
|
||||
child.outputStream.close()
|
||||
child.inputStream.readBytes()
|
||||
child.waitFor(60, TimeUnit.SECONDS) && child.exitValue() == 0
|
||||
}.getOrDefault(false)
|
||||
main.post {
|
||||
if (status[step] != Status.WORKING) status[step] = if (ok) Status.DONE else Status.TODO
|
||||
if (ready) AgentHost.prefs(app).edit().putBoolean(COMPLETED_KEY, true).apply()
|
||||
}
|
||||
}, "agent-check-${step.name.lowercase()}").start()
|
||||
val ok = runCatching {
|
||||
val child = AgentHost.guest(app, command, connectAdb = step == Step.CONNECTION).start()
|
||||
child.outputStream.close()
|
||||
child.inputStream.readBytes()
|
||||
child.waitFor(60, TimeUnit.SECONDS) && child.exitValue() == 0
|
||||
}.getOrDefault(false)
|
||||
main.post {
|
||||
if (status[step] != Status.WORKING) status[step] = if (ok) Status.DONE else Status.TODO
|
||||
if (ready) AgentHost.prefs(app).edit().putBoolean(COMPLETED_KEY, true).apply()
|
||||
}
|
||||
}
|
||||
|
||||
private fun set(step: Step, value: Status) {
|
||||
|
||||
Reference in new issue
Block a user