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(media): enhance audio recording handling with last saved details and improved error reporting

Durganshu Mishra8a39c29dc82c
diff --git a/app/src/main/java/com/gotcha/service/AssistiveBallService.kt b/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
index e6092eb..43f7ab5 100644
--- a/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
+++ b/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
@@ -65,15 +65,40 @@ class AssistiveBallService : Service() {
private lateinit var screenCompanionPanel: com.gotcha.ui.ScreenCompanionPanelOverlay
private lateinit var screenLensController: ScreenLensController
private val webFetchTool by lazy { com.gotcha.tools.WebFetchTool() }
- private val llmClient by lazy {
- val s = settingsRepository.load()
- com.gotcha.llm.LLMClient(
- apiKey = s.effectiveApiKey,
- baseUrl = s.effectiveBaseUrl,
- model = s.model,
- context = this
- )
- }
+ private var cachedLlmClient: com.gotcha.llm.LLMClient? = null
+ private var cachedLlmKey: String? = null
+
+ /**
+ * The companion panel's LLM client. Rebuilt whenever the settings it depends
+ * on change, so edits in Settings take effect without restarting the service
+ * (a plain `by lazy` pinned the very first values for the service's lifetime).
+ *
+ * [Settings.apiTimeoutSeconds] must be passed through: omitting it left the
+ * client on LLMClient's `0L` default, which OkHttp reads as *no timeout*, so
+ * a stalled request hung the panel on "Thinking..." forever — it has no stop
+ * button, and the `catch` that would show an error never runs.
+ */
+ private val llmClient: com.gotcha.llm.LLMClient
+ get() {
+ val s = settingsRepository.load()
+ val key = listOf(
+ s.effectiveApiKey.hashCode(),
+ s.effectiveBaseUrl,
+ s.model,
+ s.apiTimeoutSeconds
+ ).joinToString("|")
+ cachedLlmClient?.let { if (cachedLlmKey == key) return it }
+ return com.gotcha.llm.LLMClient(
+ apiKey = s.effectiveApiKey,
+ baseUrl = s.effectiveBaseUrl,
+ model = s.model,
+ context = this,
+ apiTimeoutSeconds = s.apiTimeoutSeconds
+ ).also {
+ cachedLlmClient = it
+ cachedLlmKey = key
+ }
+ }
val proactiveSessionManager = ProactiveSessionManager()
diff --git a/app/src/main/java/com/gotcha/tools/MediaCaptureTool.kt b/app/src/main/java/com/gotcha/tools/MediaCaptureTool.kt
index 4fd8158..45e29af 100644
--- a/app/src/main/java/com/gotcha/tools/MediaCaptureTool.kt
+++ b/app/src/main/java/com/gotcha/tools/MediaCaptureTool.kt
@@ -31,6 +31,15 @@ class MediaCaptureTool(private val context: Context) {
private var recordingStartTime: Long = 0L
private var recordingPaused: Boolean = false
+ /**
+ * Where the last completed recording was saved, and how long it ran.
+ * Kept so a repeated [stopAudioRecording] can report the already-saved file
+ * instead of a bare "no recording in progress" error — models routinely
+ * issue a second, redundant stop, and an error there reads as a failure.
+ */
+ private var lastSavedPath: String? = null
+ private var lastSavedDurationSeconds: Long = 0L
+
suspend fun takePhoto(camera: String?): ToolResult {
Log.d(TAG, "takePhoto: camera=$camera")
if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
@@ -117,6 +126,10 @@ class MediaCaptureTool(private val context: Context) {
"status with get_audio_recording_status."
)
}
+ // Drop the previous recording's details up front: if this start fails, a
+ // following stop must report the real error, not a stale saved file.
+ lastSavedPath = null
+ lastSavedDurationSeconds = 0L
return try {
val file = if (!outputPath.isNullOrBlank()) {
val resolved = FileResolver(context).resolveForWrite(outputPath)
@@ -189,10 +202,7 @@ class MediaCaptureTool(private val context: Context) {
}
fun stopAudioRecording(): ToolResult {
- val rec = recorder ?: return ToolResult.error(
- "No recording is in progress. Use start_audio_recording to begin one, or check " +
- "status with get_audio_recording_status."
- )
+ val rec = recorder ?: return alreadyStoppedResult()
return try {
rec.stop()
val file = recordingFile
@@ -204,13 +214,33 @@ class MediaCaptureTool(private val context: Context) {
com.gotcha.data.GotchaStorage.publishToGallery(context, file)
}
releaseRecorder()
- ToolResult.ok("Recording saved to $path (${dur / 60}m ${dur % 60}s).")
+ lastSavedPath = path
+ lastSavedDurationSeconds = dur
+ ToolResult.ok("Recording saved to $path (${formatDuration(dur)}).")
} catch (e: Exception) {
releaseRecorder()
ToolResult.error("Could not stop the recording cleanly: ${e.message}")
}
}
+ /**
+ * Nothing is recording. If a recording was already stopped and saved, that is
+ * the state the caller wanted, so report success with the saved file rather
+ * than an error — otherwise a redundant second stop looks like a failure.
+ */
+ private fun alreadyStoppedResult(): ToolResult {
+ val path = lastSavedPath ?: return ToolResult.error(
+ "No recording is in progress. Use start_audio_recording to begin one, or check " +
+ "status with get_audio_recording_status."
+ )
+ return ToolResult.ok(
+ "No recording is currently running — it was already stopped and saved to $path " +
+ "(${formatDuration(lastSavedDurationSeconds)}). Nothing further to do."
+ )
+ }
+
+ private fun formatDuration(seconds: Long): String = "${seconds / 60}m ${seconds % 60}s"
+
fun getAudioRecordingStatus(): ToolResult {
val rec = recorder
if (rec == null) return ToolResult.ok("No recording is active.")