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.

feat(podcast): share_podcast, hand a finished episode to any app

Durganshu Mishra9fbd03e877a4
diff --git a/app/src/main/java/com/gotcha/tools/PodcastTool.kt b/app/src/main/java/com/gotcha/tools/PodcastTool.kt
index 6a68bc1..c462631 100644
--- a/app/src/main/java/com/gotcha/tools/PodcastTool.kt
+++ b/app/src/main/java/com/gotcha/tools/PodcastTool.kt
@@ -43,7 +43,9 @@ class PodcastTool(
},
private val mediaExport: MediaExport = MediaExport(context),
private val mediaConvert: MediaConvertTool = MediaConvertTool(context),
- private val termuxUsable: () -> Boolean = { DeviceCapabilities.termuxUsable(context) }
+ private val termuxUsable: () -> Boolean = { DeviceCapabilities.termuxUsable(context) },
+ /** Fires the share sheet. Injectable so tests can capture the intent instead of launching it. */
+ private val startChooser: (android.content.Intent) -> Unit = { context.startActivity(it) }
) {
/** The two calls synthesis needs from the TTS API, extracted for tests. */
@@ -98,6 +100,18 @@ class PodcastTool(
private const val TMP_SUFFIX = ".podcast-tmp.m4a"
private const val ERROR_DETAIL_CHARS = 200
+
+ /** Extension → MIME type for the share intent. Also the shareable-format list. */
+ private val SHARE_MIME_TYPES = mapOf(
+ "m4a" to "audio/mp4",
+ "mp4" to "audio/mp4",
+ "aac" to "audio/aac",
+ "mp3" to "audio/mpeg",
+ "wav" to "audio/wav",
+ "ogg" to "audio/ogg",
+ "opus" to "audio/ogg",
+ "flac" to "audio/flac"
+ )
}
private sealed interface Resolved {
@@ -363,6 +377,58 @@ class PodcastTool(
"media_convert can turn it into an MP3 once that is fixed."
}
+ // ---- sharing ----
+
+ /**
+ * Opens the system share sheet for a finished podcast (or any audio file
+ * under the Gotcha folder). The file is handed over as a `content://` URI
+ * through the app's existing [androidx.core.content.FileProvider] — a
+ * `file://` URI would crash the receiving app on every supported Android
+ * version — and its `external-path path="Gotcha/"` mapping is also the
+ * boundary of what can be shared here.
+ */
+ @Suppress("ReturnCount")
+ fun share(path: String): ToolResult {
+ val file = when (val r = resolver.resolveForRead(path)) {
+ is FileResolver.ResolveResult.PermissionNeeded -> return r.result
+ is FileResolver.ResolveResult.Error -> return ToolResult.error(r.message)
+ is FileResolver.ResolveResult.Ok -> r.file
+ }
+ resolver.checkReadPermission(file)?.let { return it }
+ if (!file.exists() || !file.isFile) {
+ return ToolResult.error(
+ "'$path' does not exist (resolved: ${file.canonicalPath}). You may use list_files to find it."
+ )
+ }
+ val mime = SHARE_MIME_TYPES[file.extension.lowercase()]
+ ?: return ToolResult.error(
+ "'.${file.extension}' is not an audio format this tool shares. Supported: " +
+ SHARE_MIME_TYPES.keys.sorted().joinToString(", ") { ".$it" } + "."
+ )
+ val uri = runCatching {
+ androidx.core.content.FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", file)
+ }.getOrElse {
+ return ToolResult.error(
+ "'${file.canonicalPath}' is outside the folder this app can hand to other apps. Move or " +
+ "copy it under '${GotchaStorage.rootPath}' first, then share that copy."
+ )
+ }
+ val send = android.content.Intent(android.content.Intent.ACTION_SEND).apply {
+ type = mime
+ putExtra(android.content.Intent.EXTRA_STREAM, uri)
+ addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION)
+ }
+ val chooser = android.content.Intent.createChooser(send, "Share ${file.name}").apply {
+ addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)
+ addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION)
+ }
+ startChooser(chooser)
+ return ToolResult.ok(
+ "Opened the share sheet for '${file.name}' (${resolver.formatSize(file.length())}). The user " +
+ "picks the destination app themselves — nothing has been sent yet."
+ )
+ }
+
// ---- validation ----
private fun credentialError(settings: Settings): ToolResult? {
diff --git a/app/src/main/java/com/gotcha/tools/ToolCategories.kt b/app/src/main/java/com/gotcha/tools/ToolCategories.kt
index 4274b4e..44f2af4 100644
--- a/app/src/main/java/com/gotcha/tools/ToolCategories.kt
+++ b/app/src/main/java/com/gotcha/tools/ToolCategories.kt
@@ -14,7 +14,7 @@ object ToolCategories {
"global_action", "open_app", "open_setting",
"read_screen", "read_screen_raw",
"navigate_app", "task",
- "dial_number", "compose_email", "show_alarms"
+ "dial_number", "compose_email", "show_alarms", "share_podcast"
)
private val backgroundTools = setOf(
diff --git a/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt b/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt
index f3268bc..fce96ab 100644
--- a/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt
+++ b/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt
@@ -1638,6 +1638,28 @@ object ToolDefinitions {
}
)
+ val sharePodcast = tool(
+ "share_podcast",
+ "Open the system share sheet for an audio file under the Gotcha folder — a generated podcast, a " +
+ "recording, a converted track — so the user can send it through any app they choose. Nothing is " +
+ "sent by this call itself: it only presents the chooser, and the user picks (or dismisses) the " +
+ "destination. Accepts .m4a, .mp3, .wav, .ogg, .opus, .flac, .aac and .mp4 audio. The file must " +
+ "live under the Gotcha folder on shared storage (where synthesize_podcast writes); files " +
+ "elsewhere must be copied there first.",
+ schema {
+ putJsonObject("properties") {
+ putJsonObject("path") {
+ put("type", "string")
+ put(
+ "description",
+ "Path of the audio file to share, e.g. '/sdcard/Gotcha/Podcasts/morning-digest.m4a'."
+ )
+ }
+ }
+ putJsonArray("required") { add("path") }
+ }
+ )
+
val edit = tool(
"edit",
"Replace exact text in a file, for surgical edits without rewriting it. oldString " +
@@ -2872,7 +2894,7 @@ object ToolDefinitions {
// Audio format conversion via Termux ffmpeg (Operator only)
mediaConvert,
// Script-to-speech podcast synthesis (Operator only)
- synthesizePodcast, synthesizePodcastDialogue,
+ synthesizePodcast, synthesizePodcastDialogue, sharePodcast,
// Audio-file transcription via the STT API (Operator only)
transcribeFile,
// Content search and file discovery
diff --git a/app/src/main/java/com/gotcha/tools/ToolExecutor.kt b/app/src/main/java/com/gotcha/tools/ToolExecutor.kt
index 2bc2328..f18bb5d 100644
--- a/app/src/main/java/com/gotcha/tools/ToolExecutor.kt
+++ b/app/src/main/java/com/gotcha/tools/ToolExecutor.kt
@@ -23,6 +23,8 @@ import kotlinx.serialization.json.jsonPrimitive
* preconditions (executors return permission errors instead of crashing),
* and records every execution in the [ActionLog].
*/
+// One class dispatching the entire fixed tool catalog is the design; size follows from it.
+@Suppress("LargeClass")
class ToolExecutor(
context: Context,
val onTask: (suspend (description: String, prompt: String) -> ToolResult)? = null,
@@ -267,6 +269,9 @@ class ToolExecutor(
format = args.requireString("format"),
overwrite = args.requireBoolean("overwrite") ?: false
)
+ "share_podcast" -> podcastTool.share(
+ path = args.requireString("path") ?: return missing("path")
+ )
"transcribe_file" -> transcribeTool.transcribe(
path = args.requireString("path") ?: return missing("path"),
model = args.requireString("model"),
diff --git a/app/src/test/java/com/gotcha/tools/PodcastToolTest.kt b/app/src/test/java/com/gotcha/tools/PodcastToolTest.kt
index b637eae..8123fdf 100644
--- a/app/src/test/java/com/gotcha/tools/PodcastToolTest.kt
+++ b/app/src/test/java/com/gotcha/tools/PodcastToolTest.kt
@@ -322,6 +322,64 @@ class PodcastToolTest {
assertEquals("The real opener.", backend.synthesizedWith.single().first)
}
+ // ---- sharing ----
+
+ private class ChooserCapture {
+ var intent: android.content.Intent? = null
+ val launcher: (android.content.Intent) -> Unit = { intent = it }
+ }
+
+ private fun shareTool(capture: ChooserCapture): PodcastTool =
+ PodcastTool(context, loadSettings = { apiSettings }, startChooser = capture.launcher)
+
+ @Test
+ fun `sharing a missing file is an error and no chooser appears`() {
+ val capture = ChooserCapture()
+ val result = shareTool(capture).share(File(GotchaStorage.podcastsRoot(), "gone.m4a").path)
+ assertFalse(result.success)
+ assertTrue(capture.intent == null)
+ }
+
+ @Test
+ fun `sharing a non-audio file is refused with the supported list`() {
+ val capture = ChooserCapture()
+ GotchaStorage.podcastsRoot().mkdirs()
+ val file = File(GotchaStorage.podcastsRoot(), "notes.txt").apply { writeText("hi") }
+ val result = shareTool(capture).share(file.path)
+ assertFalse(result.success)
+ assertTrue(result.message.contains(".m4a"))
+ assertTrue(capture.intent == null)
+ }
+
+ @Test
+ @Suppress("DEPRECATION") // getParcelableExtra's typed replacement needs API 33+ at the call site
+ fun `sharing hands over a content URI chooser, never a file URI`() {
+ val capture = ChooserCapture()
+ GotchaStorage.podcastsRoot().mkdirs()
+ val file = File(GotchaStorage.podcastsRoot(), "episode.m4a").apply { writeBytes(ByteArray(64)) }
+
+ val result = shareTool(capture).share(file.path)
+
+ assertTrue(result.message, result.success)
+ val chooser = capture.intent!!
+ assertEquals(android.content.Intent.ACTION_CHOOSER, chooser.action)
+ val send = chooser.getParcelableExtra<android.content.Intent>(android.content.Intent.EXTRA_INTENT)!!
+ assertEquals(android.content.Intent.ACTION_SEND, send.action)
+ assertEquals("audio/mp4", send.type)
+ val uri = send.getParcelableExtra<android.net.Uri>(android.content.Intent.EXTRA_STREAM)!!
+ assertEquals("content", uri.scheme)
+ assertTrue(send.flags and android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION != 0)
+ }
+
+ @Test
+ fun `a file outside the shareable folder is a move-it-first message, not a crash`() {
+ val capture = ChooserCapture()
+ val outside = File(context.cacheDir, "elsewhere.mp3").apply { writeBytes(ByteArray(16)) }
+ val result = shareTool(capture).share(outside.path)
+ assertFalse(result.success)
+ assertTrue(capture.intent == null)
+ }
+
@Test
fun `a long script reaches the API as multiple whole-sentence segments`() {
val backend = FakeBackend(answer = { Result.failure(IOException("stop after capture")) })
diff --git a/app/src/test/resources/feature-test-coverage.json b/app/src/test/resources/feature-test-coverage.json
index 1815048..0909509 100644
--- a/app/src/test/resources/feature-test-coverage.json
+++ b/app/src/test/resources/feature-test-coverage.json
@@ -452,6 +452,14 @@
],
"notes": "shares synthesize_podcast's pipeline and its coverage, plus the dialogue-specific guards and mechanics: unknown-speaker refusal, empty-dialogue refusal, blank turns dropped after sanitising rather than synthesized as dead air, and the three-level host-voice resolution (explicit argument beats configured setting beats the first voice in the model's list that differs from host A). Silence-gap WAV generation is verified on the JVM to match the speech's own sample rate and channel count. The audible result — two distinguishable hosts with a natural pause between turns — is device-only and must be verified by hand"
},
+ {
+ "tool": "share_podcast",
+ "tier": "ROBOLECTRIC",
+ "tests": [
+ "com.gotcha.tools.PodcastToolTest"
+ ],
+ "notes": "the guards and the intent contract: missing files and non-audio extensions refused before any chooser appears, a file outside the FileProvider's Gotcha/ mapping reported with a move-it-first message instead of a crash, and the happy path verified to build an ACTION_SEND chooser carrying a content:// URI (never file://) with the read-grant flag. Actually watching the share sheet appear and land in another app is device-only manual verification"
+ },
{
"tool": "transcribe_file",
"tier": "ROBOLECTRIC",
diff --git a/docs/FEATURE_TEST_COVERAGE.md b/docs/FEATURE_TEST_COVERAGE.md
index f6a28c1..648d6f5 100644
--- a/docs/FEATURE_TEST_COVERAGE.md
+++ b/docs/FEATURE_TEST_COVERAGE.md
@@ -14,12 +14,12 @@ Every tool the assistant can call is listed below, together with how it is verif
| Tier | Tools | What it means |
|---|---:|---|
| `UNIT` | 24 | Plain JVM unit test — no Android framework needed. |
-| `ROBOLECTRIC` | 25 | JVM test against Robolectric's Android framework, often across several API levels. |
+| `ROBOLECTRIC` | 26 | JVM test against Robolectric's Android framework, often across several API levels. |
| `INSTRUMENTED` | 0 | Runs on a real device or emulator (`app/src/androidTest`). |
| `MANUAL_ONLY` | 64 | No automated test — verified by hand, see the checklist below. |
-| **Total** | **113** | |
+| **Total** | **114** | |
-**49 of 113** tools are covered by an automated test; the remaining 64 are manual-QA-only with a recorded reason.
+**50 of 114** tools are covered by an automated test; the remaining 64 are manual-QA-only with a recorded reason.
## Foreground tools (act on the screen)
@@ -37,6 +37,7 @@ Every tool the assistant can call is listed below, together with how it is verif
| `press_key` | `MANUAL_ONLY` | Depends on a live GotchaAccessibilityService bound to a real foreground app; the service cannot reliably self-bind under instrumentation (see AccessibilityServiceTest's @Ignore writeup). |
| `read_screen` | `MANUAL_ONLY` | Depends on a live GotchaAccessibilityService bound to a real foreground app; the service cannot reliably self-bind under instrumentation (see AccessibilityServiceTest's @Ignore writeup). |
| `read_screen_raw` | `MANUAL_ONLY` | Depends on a live GotchaAccessibilityService bound to a real foreground app; the service cannot reliably self-bind under instrumentation (see AccessibilityServiceTest's @Ignore writeup). |
+| `share_podcast` | `ROBOLECTRIC` | `PodcastToolTest` — the guards and the intent contract: missing files and non-audio extensions refused before any chooser appears, a file outside the FileProvider's Gotcha/ mapping reported with a move-it-first message instead of a crash, and the happy path verified to build an ACTION_SEND chooser carrying a content:// URI (never file://) with the read-grant flag. Actually watching the share sheet appear and land in another app is device-only manual verification |
| `show_alarms` | `MANUAL_ONLY` | Needs an Android Context and a system service with real device state; no JVM-tier coverage yet — scheduled for the Robolectric tier. |
| `swipe` | `MANUAL_ONLY` | Depends on a live GotchaAccessibilityService bound to a real foreground app; the service cannot reliably self-bind under instrumentation (see AccessibilityServiceTest's @Ignore writeup). |
| `tap` | `MANUAL_ONLY` | Depends on a live GotchaAccessibilityService bound to a real foreground app; the service cannot reliably self-bind under instrumentation (see AccessibilityServiceTest's @Ignore writeup). |