Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

fix(termux): start RUN_COMMAND with startForegroundService so it works when Termux is closed

Durganshu3db6cdae77ea
diff --git a/app/src/main/java/com/gotcha/tools/TermuxMessages.kt b/app/src/main/java/com/gotcha/tools/TermuxMessages.kt
index c6820c0..b169ad7 100644
--- a/app/src/main/java/com/gotcha/tools/TermuxMessages.kt
+++ b/app/src/main/java/com/gotcha/tools/TermuxMessages.kt
@@ -48,10 +48,12 @@ internal object TermuxMessages {
"When you tell the user, give them exactly this fenced block, not the commands inline."
/**
- * Seen on-device as `Not allowed to start service … app is in background`: from Android 12,
- * an app in the background cannot start another app's foreground service, and Termux's
- * RunCommandService is one. Gotcha's assistive ball normally holds a foreground service that
- * exempts us, so this shows up when it is switched off and the chat UI has been backgrounded.
+ * The start itself was refused. The one expected cause: from Android 12 an app in the
+ * background may not start a foreground service, and Termux's RunCommandService is one
+ * (ForegroundServiceStartNotAllowedException). Gotcha's assistive ball holds a foreground
+ * service that exempts us, so this shows up when it is switched off and the chat UI has been
+ * backgrounded. The older "app is in background uid null" came from starting it with
+ * startService while Termux was not running, whatever Gotcha's state; see [TermuxTool.runCommand].
*/
fun startFailed(cause: Throwable) = ToolResult.error(
"Could not reach Termux's RUN_COMMAND service: ${cause.message}. If that says Gotcha is in the " +
diff --git a/app/src/main/java/com/gotcha/tools/TermuxTool.kt b/app/src/main/java/com/gotcha/tools/TermuxTool.kt
index 22fb02c..76da438 100644
--- a/app/src/main/java/com/gotcha/tools/TermuxTool.kt
+++ b/app/src/main/java/com/gotcha/tools/TermuxTool.kt
@@ -228,11 +228,18 @@ class TermuxTool(
TermuxResultReceiver.resultIntent(context, requestCode),
pendingIntentFlags()
)
+ // startForegroundService, not startService. Termux targets SDK 28, so a plain
+ // startService into it is refused ("app is in background uid null") whenever Termux
+ // has no process — after it is swiped away, say — however much Gotcha is on screen:
+ // the start rules look at the target's state, and only apps targeting below 26 get the
+ // foreground-caller pass. RunCommandService calls startForeground() as it starts, which
+ // is what this call needs; Gotcha may make it while visible or while the assistive
+ // ball's foreground service runs.
val started = runCatching {
- context.startService(commandIntent(commandToRun(trimmed), workingDir, stdin, pendingIntent))
+ context.startForegroundService(commandIntent(commandToRun(trimmed), workingDir, stdin, pendingIntent))
}
started.exceptionOrNull()?.let { return TermuxMessages.startFailed(it) }
- // startService reports "no such service" by returning null, not by throwing. Android
+ // A start reports "no such service" by returning null, not by throwing. Android
// also excludes force-stopped packages from intent resolution, so a Termux the user
// (or an OEM battery manager) has stopped lands here. Waiting on the deferred would
// burn the whole timeout and then claim the command was accepted and still running.
diff --git a/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt b/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt
index 65c9346..46ee589 100644
--- a/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt
+++ b/app/src/test/java/com/gotcha/tools/TermuxToolTest.kt
@@ -178,14 +178,15 @@ class TermuxToolTest {
@Test
fun `a service that does not start is reported at once, not as a timeout`() = runTest {
- // startService signals "no such service" by returning null rather than throwing, which
+ // A start signals "no such service" by returning null rather than throwing, which
// is what a force-stopped Termux produces: Android excludes stopped packages from intent
// resolution, while the package and its service still resolve for the availability
// probe. Robolectric always returns a component, so the null is injected here.
installTermux()
grantRunCommand()
val nullStartingContext = object : android.content.ContextWrapper(context) {
- override fun startService(service: android.content.Intent?): android.content.ComponentName? = null
+ override fun startForegroundService(service: android.content.Intent?): android.content.ComponentName? =
+ null
}
val result = TermuxTool(nullStartingContext).runCommand("echo hello", timeoutSeconds = 600)
@@ -195,6 +196,29 @@ class TermuxToolTest {
assertTrue("must say nothing is pending", result.message.contains("nothing is pending"))
}
+ @Test
+ fun `the command goes to Termux through startForegroundService`() = runTest {
+ // A plain startService into Termux (targetSdk 28) is refused while Termux has no process,
+ // "app is in background uid null", even with Gotcha on screen: seen on a Nothing Phone
+ // 3a on Android 16 after Termux was swiped away. startForegroundService is allowed.
+ installTermux()
+ grantRunCommand()
+ var viaForeground: android.content.Intent? = null
+ val recordingContext = object : android.content.ContextWrapper(context) {
+ override fun startService(service: android.content.Intent?): android.content.ComponentName? =
+ error("Not allowed to start service: app is in background uid null")
+
+ override fun startForegroundService(service: android.content.Intent?): android.content.ComponentName? {
+ viaForeground = service
+ return null
+ }
+ }
+
+ TermuxTool(recordingContext).runCommand("echo hello")
+
+ assertEquals("com.termux.RUN_COMMAND", viaForeground?.action)
+ }
+
@Test
fun `command and stdin share the size budget`() = runTest {
installTermux()