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: implement screenshot capture and expand AssistiveBall UI menu actions
Rishabh Bajpaib1528c9cbaa2
diff --git a/app/src/main/java/com/gotcha/service/AssistiveBallService.kt b/app/src/main/java/com/gotcha/service/AssistiveBallService.ktindex c2e31ec..d3db518 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.CoroutineScopeimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.SupervisorJobimport kotlinx.coroutines.cancel+import kotlinx.coroutines.delayimport kotlinx.coroutines.flow.MutableStateFlowimport kotlinx.coroutines.flow.StateFlowimport kotlinx.coroutines.flow.asStateFlow@@ -74,8 +75,17 @@ class AssistiveBallService : Service() {// Assistive ball (shown when idle, hidden during a call)overlay = AssistiveBallOverlay(this).apply {onDismiss = { stopBall() }+ onOpenApp = {+ val intent = Intent(this@AssistiveBallService, MainActivity::class.java)+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)+ startActivity(intent)+ }+ onHideBall = {+ callController.endCall()+ stopBall()+ }+ onTakeScreenshot = { takeScreenshot() }onStartCall = { callController.startCall() }- onPauseCall = { callController.stopAgent() }onEndCall = { callController.endCall() }onToggleChatWindow = { } // no-op during calls (ball is hidden)isCallActive = { callController.isActive() }@@ -152,6 +162,61 @@ class AssistiveBallService : Service() {}}+ private fun takeScreenshot() {+ scope.launch(Dispatchers.IO) {+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {+ overlay.showError("Screenshot requires Android 11+")+ return@launch+ }+ try {+ val service = GotchaAccessibilityService.instance+ if (service == null) {+ overlay.showError("Accessibility service not available")+ return@launch+ }+ var bitmap = service.takeScreenshotBitmap()+ if (bitmap == null) {+ delay(1200L)+ bitmap = service.takeScreenshotBitmap()+ }+ if (bitmap == null) {+ overlay.showError("Screenshot failed — try again")+ return@launch+ }+ val timestamp = java.text.SimpleDateFormat(+ "yyyyMMdd_HHmmss",+ java.util.Locale.US+ ).format(java.util.Date())+ val fileName = "Screenshot_$timestamp.png"+ val contentValues = android.content.ContentValues().apply {+ put(android.provider.MediaStore.Images.Media.DISPLAY_NAME, fileName)+ put(android.provider.MediaStore.Images.Media.MIME_TYPE, "image/png")+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {+ put(+ android.provider.MediaStore.Images.Media.RELATIVE_PATH,+ android.os.Environment.DIRECTORY_PICTURES + "/Gotcha"+ )+ }+ }+ val uri = contentResolver.insert(+ android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,+ contentValues+ )+ if (uri != null) {+ contentResolver.openOutputStream(uri)?.use { out ->+ bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, out)+ }+ overlay.showError("Screenshot saved: $fileName")+ } else {+ overlay.showError("Screenshot save failed")+ }+ bitmap.recycle()+ } catch (e: Throwable) {+ overlay.showError("Screenshot error: ${e.message}")+ }+ }+ }+// ---- Lifecycle helpers ----private fun stopBall() {diff --git a/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt b/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.ktindex 85aca71..c59a96e 100644--- a/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt+++ b/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt@@ -61,31 +61,34 @@ class GotchaAccessibilityService : AccessibilityService() {* flow to give the LLM vision context alongside [dumpScreenText].*/@RequiresApi(Build.VERSION_CODES.R)- suspend fun takeScreenshotBitmap(): Bitmap? = suspendCancellableCoroutine { cont ->- try {- takeScreenshot(- Display.DEFAULT_DISPLAY,- mainExecutor,- object : AccessibilityService.TakeScreenshotCallback {- override fun onSuccess(screenshot: AccessibilityService.ScreenshotResult) {- val bitmap = try {- Bitmap.wrapHardwareBuffer(screenshot.hardwareBuffer, screenshot.colorSpace)- ?.copy(Bitmap.Config.ARGB_8888, false)- } catch (_: Exception) {- null- } finally {- screenshot.hardwareBuffer.close()+ suspend fun takeScreenshotBitmap(): Bitmap? {+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) return null+ return suspendCancellableCoroutine { cont ->+ try {+ takeScreenshot(+ Display.DEFAULT_DISPLAY,+ mainExecutor,+ object : AccessibilityService.TakeScreenshotCallback {+ override fun onSuccess(screenshot: AccessibilityService.ScreenshotResult) {+ val bitmap = try {+ Bitmap.wrapHardwareBuffer(screenshot.hardwareBuffer, screenshot.colorSpace)+ ?.copy(Bitmap.Config.ARGB_8888, false)+ } catch (_: Throwable) {+ null+ } finally {+ screenshot.hardwareBuffer.close()+ }+ if (cont.isActive) cont.resume(bitmap)}- if (cont.isActive) cont.resume(bitmap)- }- override fun onFailure(errorCode: Int) {- if (cont.isActive) cont.resume(null)+ override fun onFailure(errorCode: Int) {+ if (cont.isActive) cont.resume(null)+ }}- }- )- } catch (_: Exception) {- if (cont.isActive) cont.resume(null)+ )+ } catch (_: Throwable) {+ if (cont.isActive) cont.resume(null)+ }}}diff --git a/app/src/main/java/com/gotcha/ui/AssistiveBallOverlay.kt b/app/src/main/java/com/gotcha/ui/AssistiveBallOverlay.ktindex 8d4f21b..1681e26 100644--- a/app/src/main/java/com/gotcha/ui/AssistiveBallOverlay.kt+++ b/app/src/main/java/com/gotcha/ui/AssistiveBallOverlay.kt@@ -54,8 +54,10 @@ class AssistiveBallOverlay(context: Context) {// Callbacks — set by the host service before [show].var onDismiss: () -> Unit = {}+ var onOpenApp: () -> Unit = {}+ var onHideBall: () -> Unit = {}+ var onTakeScreenshot: () -> Unit = {}var onStartCall: () -> Unit = {}- var onPauseCall: () -> Unit = {}var onEndCall: () -> Unit = {}var onToggleChatWindow: () -> Unit = {}@@ -400,25 +402,24 @@ class AssistiveBallOverlay(context: Context) {}}- val active = isCallActive()- container.addView(menuTitle("Gotcha Call"))+ container.addView(menuTitle("Gotcha"))container.addView(- tapButton("▶ Start") {+ tapButton("\uD83D\uDCF1 Open App") {removeMenu()- onStartCall()- }.apply { isEnabled = !active }+ onOpenApp()+ })container.addView(- tapButton("⏸ Pause") {+ tapButton("\u2716 Hide Ball") {removeMenu()- onPauseCall()- }.apply { isEnabled = active }+ onHideBall()+ })container.addView(- tapButton("⏹ End") {+ tapButton("\uD83D\uDCF7 Screenshot") {removeMenu()- onEndCall()- }.apply { isEnabled = active }+ onTakeScreenshot()+ })val params = menuLayoutParams()