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: add ScreenCompanionController to trigger context-aware smart actions via system events

Rishabh Bajpai34a602cebb7d
diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index 110465a..ca181af 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -134,6 +134,9 @@ dependencies {
// Markdown rendering
implementation("com.halilibo.compose-richtext:richtext-ui-material3:0.17.0")
implementation("com.halilibo.compose-richtext:richtext-commonmark:0.17.0")
+ implementation("io.noties.markwon:core:4.6.2") {
+ exclude(group = "com.atlassian.commonmark", module = "commonmark")
+ }
// CameraX for automated photo capture
val cameraxVersion = "1.4.0"
diff --git a/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt b/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt
index c59a96e..d65e803 100644
--- a/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt
+++ b/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt
@@ -37,8 +37,14 @@ class GotchaAccessibilityService : AccessibilityService() {
initClipboardListener()
}
- // Passive: we drive the UI on demand from tools rather than reacting to events.
- override fun onAccessibilityEvent(event: AccessibilityEvent?) {}
+ // Passive: we drive the UI on demand from tools rather than reacting to events,
+ // except for broadcasting window state changes to the ScreenCompanionController.
+ override fun onAccessibilityEvent(event: AccessibilityEvent?) {
+ if (event?.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
+ val intent = android.content.Intent("com.gotcha.action.APP_CHANGED")
+ sendBroadcast(intent)
+ }
+ }
override fun onInterrupt() {}
@@ -252,7 +258,10 @@ class GotchaAccessibilityService : AccessibilityService() {
internal fun initClipboardListener() {
val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
cm.addPrimaryClipChangedListener {
- try { lastClipboardData = cm.primaryClip } catch (_: Exception) {}
+ try {
+ val intent = android.content.Intent("com.gotcha.action.CLIPBOARD_CHANGED")
+ sendBroadcast(intent)
+ } catch (_: Exception) {}
}
}
}
diff --git a/app/src/main/java/com/gotcha/service/ScreenCompanionController.kt b/app/src/main/java/com/gotcha/service/ScreenCompanionController.kt
new file mode 100644
index 0000000..5baf015
--- /dev/null
+++ b/app/src/main/java/com/gotcha/service/ScreenCompanionController.kt
@@ -0,0 +1,117 @@
+package com.gotcha.service
+
+import android.content.BroadcastReceiver
+import android.content.Context
+import android.content.Intent
+import android.content.IntentFilter
+import android.database.ContentObserver
+import android.net.Uri
+import android.os.Handler
+import android.os.Looper
+import android.provider.MediaStore
+import android.util.Patterns
+import com.gotcha.agent.ScreenSnapshot
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+
+class ScreenCompanionController(
+ private val context: Context,
+ private val scope: CoroutineScope,
+ private val onSmartActionReady: (label: String, prompt: String) -> Unit,
+ private val onReadClipboardRequest: () -> Unit = {}
+) {
+
+ private val mainHandler = Handler(Looper.getMainLooper())
+
+ private val screenshotObserver = object : ContentObserver(mainHandler) {
+ override fun onChange(selfChange: Boolean, uri: Uri?) {
+ super.onChange(selfChange, uri)
+ performLightweightScan(triggerType = "Screenshot")
+ }
+ }
+
+ private val appChangeReceiver = object : BroadcastReceiver() {
+ override fun onReceive(context: Context?, intent: Intent?) {
+ if (intent?.action == ACTION_APP_CHANGED) {
+ performLightweightScan(triggerType = "AppChange")
+ } else if (intent?.action == ACTION_CLIPBOARD_CHANGED) {
+ performLightweightScan(triggerType = "Clipboard")
+ }
+ }
+ }
+
+ fun start() {
+ context.contentResolver.registerContentObserver(
+ MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
+ true,
+ screenshotObserver
+ )
+
+ val filter = IntentFilter().apply {
+ addAction(ACTION_APP_CHANGED)
+ addAction(ACTION_CLIPBOARD_CHANGED)
+ }
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
+ context.registerReceiver(appChangeReceiver, filter, Context.RECEIVER_NOT_EXPORTED)
+ } else {
+ context.registerReceiver(appChangeReceiver, filter)
+ }
+ }
+
+ fun stop() {
+ context.contentResolver.unregisterContentObserver(screenshotObserver)
+ try {
+ context.unregisterReceiver(appChangeReceiver)
+ } catch (_: Exception) {}
+ }
+
+ private fun performLightweightScan(triggerType: String) {
+ scope.launch {
+ withContext(Dispatchers.Default) {
+ if (triggerType == "Screenshot") {
+ withContext(Dispatchers.Main) {
+ onSmartActionReady(
+ "📸 Screenshot taken. Extract text?",
+ "Extract the text from this screenshot."
+ )
+ }
+ return@withContext
+ }
+
+ if (triggerType == "Clipboard") {
+ // We must read clipboard from Main thread via overlay to avoid SecurityException
+ withContext(Dispatchers.Main) {
+ onReadClipboardRequest()
+ }
+ return@withContext
+ }
+
+ if (triggerType == "AppChange") {
+ val screenText = ScreenSnapshot.captureScreenText(limit = 40) ?: return@withContext
+ if (Patterns.WEB_URL.matcher(screenText).find() || screenText.contains("http", ignoreCase = true)) {
+ withContext(Dispatchers.Main) {
+ onSmartActionReady(
+ "🔗 Link detected. Summarize?",
+ "Summarize the content of the webpage shown on screen."
+ )
+ }
+ } else if (screenText.contains("Exception") || screenText.contains("Error", ignoreCase = true)) {
+ withContext(Dispatchers.Main) {
+ onSmartActionReady(
+ "⚠️ Error detected. Explain?",
+ "Explain the error code or stack trace shown on the screen."
+ )
+ }
+ }
+ }
+ }
+ }
+ }
+
+ companion object {
+ const val ACTION_APP_CHANGED = "com.gotcha.action.APP_CHANGED"
+ const val ACTION_CLIPBOARD_CHANGED = "com.gotcha.action.CLIPBOARD_CHANGED"
+ }
+}