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: Enhance screen sharing functionality with troubleshooting guidance and improved accessibility checks

Durganshu Mishra864fba7c1082
diff --git a/app/src/main/java/com/gotcha/agent/QuickAskEngine.kt b/app/src/main/java/com/gotcha/agent/QuickAskEngine.kt
index c5dc7cd..b7de5b6 100644
--- a/app/src/main/java/com/gotcha/agent/QuickAskEngine.kt
+++ b/app/src/main/java/com/gotcha/agent/QuickAskEngine.kt
@@ -11,6 +11,7 @@ import com.gotcha.llm.LLMClient
import com.gotcha.llm.visionUserMessage
import com.gotcha.service.GotchaAccessibilityService
import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.JsonPrimitive
import java.io.ByteArrayOutputStream
@@ -68,15 +69,26 @@ class QuickAskEngine(
}
}
+ /** True when the Gotcha accessibility service is enabled and bound. */
+ fun isAccessibilityAvailable(): Boolean = GotchaAccessibilityService.instance != null
+
/**
* Capture the current screen as a downscaled JPEG base64 string via the
* accessibility screenshot API (API 30+). Returns null if unavailable
* (older API, service not bound, or capture failed).
+ *
+ * The accessibility screenshot API is rate-limited (roughly one call per second),
+ * so a single failure is retried once after a short delay.
*/
suspend fun captureScreenBase64(): String? {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) return null
val service = GotchaAccessibilityService.instance ?: return null
- val bitmap = service.takeScreenshotBitmap() ?: return null
+ var bitmap = service.takeScreenshotBitmap()
+ if (bitmap == null) {
+ delay(1100) // clear the screenshot rate-limit window, then retry once
+ bitmap = service.takeScreenshotBitmap()
+ }
+ if (bitmap == null) return null
return withContext(Dispatchers.Default) {
try {
val maxDim = 1024
@@ -106,10 +118,27 @@ class QuickAskEngine(
/**
* Run a single LLM turn for [question], optionally grounded in a screenshot and/or
* on-screen text. Returns the assistant's answer text, or throws on network/API error.
+ *
+ * When [screenRequested] is true but neither a screenshot nor screen text was captured,
+ * the model is told it could not read the screen — so it says so plainly instead of
+ * hallucinating a generic screen.
*/
- suspend fun ask(question: String, screenshotBase64: String?, screenText: String?): String {
+ suspend fun ask(
+ question: String,
+ screenshotBase64: String?,
+ screenText: String?,
+ screenRequested: Boolean = false
+ ): String {
val llm = llmProvider() ?: throw IllegalStateException("Not configured")
- val system = ChatMessage(role = "system", content = JsonPrimitive(SYSTEM_PROMPT))
+
+ val noScreenCaptured = screenshotBase64 == null && screenText.isNullOrBlank()
+ val systemText = if (screenRequested && noScreenCaptured) {
+ SYSTEM_PROMPT + "\n\nNOTE: The user asked about their screen, but no screenshot or " +
+ "on-screen text could be captured this time. Tell them briefly that you " +
+ "couldn't read the screen right now, then help as best you can. Do NOT invent or " +
+ "guess what is on the screen."
+ } else SYSTEM_PROMPT
+ val system = ChatMessage(role = "system", content = JsonPrimitive(systemText))
val userText = buildString {
if (!screenText.isNullOrBlank()) {
diff --git a/app/src/main/java/com/gotcha/service/AssistiveBallService.kt b/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
index 6ded73f..ae100cb 100644
--- a/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
+++ b/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
@@ -21,6 +21,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
+import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
/**
@@ -138,18 +139,31 @@ class AssistiveBallService : Service() {
overlay.showError("I didn't catch that. Try again.")
return@launch
}
- overlay.showThinking()
var screenshot: String? = null
var screenText: String? = null
if (withScreen) {
- // If neither a screenshot (API 30+) nor on-screen text (accessibility)
- // is available, we degrade gracefully to a plain question.
+ // Screen questions require the accessibility service (it provides both
+ // the screenshot and the on-screen text). Guide the user if it's off.
+ if (!quickAsk.isAccessibilityAvailable()) {
+ overlay.showError(
+ "I can't see your screen yet. Turn on Gotcha under " +
+ "Settings ▸ Accessibility, then use Screen Share again. " +
+ "(For a screen-free question, use the Talk option.)"
+ )
+ return@launch
+ }
+ // Hide our own overlays so they aren't captured, and let the compositor
+ // paint a clean frame of the underlying app before we grab it.
+ overlay.hideChromeForCapture()
+ delay(350)
screenshot = quickAsk.captureScreenBase64()
screenText = quickAsk.captureScreenText()
+ overlay.showChromeAfterCapture()
}
- val answer = quickAsk.ask(question, screenshot, screenText)
+ overlay.showThinking()
+ val answer = quickAsk.ask(question, screenshot, screenText, screenRequested = withScreen)
overlay.showAnswer(answer)
quickAsk.speak(answer)
} catch (e: Exception) {
diff --git a/app/src/main/java/com/gotcha/ui/AssistiveBallOverlay.kt b/app/src/main/java/com/gotcha/ui/AssistiveBallOverlay.kt
index 644b5f4..d8becb3 100644
--- a/app/src/main/java/com/gotcha/ui/AssistiveBallOverlay.kt
+++ b/app/src/main/java/com/gotcha/ui/AssistiveBallOverlay.kt
@@ -81,6 +81,28 @@ class AssistiveBallOverlay(context: Context) {
}
}
+ // ---- Capture chrome ----
+
+ /**
+ * Hide all of our own overlay windows (menu, status card, and the ball) so they are
+ * not baked into a screen capture taken for the "screen share" flow. Call
+ * [showChromeAfterCapture] once the screenshot has been taken.
+ */
+ fun hideChromeForCapture() {
+ mainHandler.post {
+ removeMenu()
+ removeCard()
+ ballView?.visibility = View.GONE
+ }
+ }
+
+ /** Restore the ball after a capture (the status card is shown separately). */
+ fun showChromeAfterCapture() {
+ mainHandler.post {
+ ballView?.visibility = View.VISIBLE
+ }
+ }
+
// ---- Status / answer card ----
fun showListening() = showCard("Listening… release to send", showClose = false)