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: update MediaProjection consent handling to request on demand and ensure token is single-use

Durganshu Mishra2dd1b080ef8f
diff --git a/app/src/debug/java/com/gotcha/debug/TestHooksReceiver.kt b/app/src/debug/java/com/gotcha/debug/TestHooksReceiver.kt
index d747418..4d35430 100644
--- a/app/src/debug/java/com/gotcha/debug/TestHooksReceiver.kt
+++ b/app/src/debug/java/com/gotcha/debug/TestHooksReceiver.kt
@@ -68,7 +68,12 @@ class TestHooksReceiver : BroadcastReceiver() {
repository.prefs.edit()
.putBoolean(MainActivity.KEY_FIRST_LAUNCH_DONE, true)
- .putBoolean(MainActivity.KEY_SUPPRESS_MEDIA_PROJECTION_PROMPT, true)
+ // Suppressed by default so automated runs never meet the system dialog;
+ // pass --ez suppress_projection false to exercise the consent flow by hand.
+ .putBoolean(
+ MainActivity.KEY_SUPPRESS_MEDIA_PROJECTION_PROMPT,
+ intent.getBooleanExtra(EXTRA_SUPPRESS_PROJECTION, true)
+ )
.apply()
}
@@ -81,6 +86,7 @@ class TestHooksReceiver : BroadcastReceiver() {
const val EXTRA_API_KEY = "api_key"
const val EXTRA_MODEL = "model"
const val EXTRA_ASSISTIVE_BALL = "assistive_ball"
+ const val EXTRA_SUPPRESS_PROJECTION = "suppress_projection"
}
}
diff --git a/app/src/main/java/com/gotcha/MainActivity.kt b/app/src/main/java/com/gotcha/MainActivity.kt
index 751a858..9cc41ef 100644
--- a/app/src/main/java/com/gotcha/MainActivity.kt
+++ b/app/src/main/java/com/gotcha/MainActivity.kt
@@ -231,16 +231,11 @@ class MainActivity : ComponentActivity() {
requestAllRuntimePermissions()
prefs.edit().putBoolean(KEY_FIRST_LAUNCH_DONE, true).apply()
}
- // Request MediaProjection consent for screenshot capture
- // Always request if not granted in this process session (cleared on process kill)
- if (ScreenPerception.mediaProjectionResultData == null &&
- !prefs.getBoolean(KEY_SUPPRESS_MEDIA_PROJECTION_PROMPT, false)
- ) {
- val mpManager = getSystemService(
- Context.MEDIA_PROJECTION_SERVICE
- ) as android.media.projection.MediaProjectionManager
- mediaProjectionLauncher.launch(mpManager.createScreenCaptureIntent())
- }
+ // MediaProjection consent is deliberately NOT requested here. The token
+ // dies with the process and is single-use on API 34, so prompting at
+ // launch would re-fire the system dialog on every cold start for a
+ // capability the user hasn't asked for yet. It is requested on demand
+ // instead — see the "special:screenshot_consent" branch below.
}
appearance = settingsRepository.load().appearance()
@@ -329,10 +324,14 @@ class MainActivity : ComponentActivity() {
Toast.LENGTH_SHORT
).show()
"special:screenshot_consent" -> {
- val mpManager = getSystemService(
- Context.MEDIA_PROJECTION_SERVICE
- ) as android.media.projection.MediaProjectionManager
- mediaProjectionLauncher.launch(mpManager.createScreenCaptureIntent())
+ val suppressed = settingsRepository.prefs
+ .getBoolean(KEY_SUPPRESS_MEDIA_PROJECTION_PROMPT, false)
+ if (!suppressed) {
+ val mpManager = getSystemService(
+ Context.MEDIA_PROJECTION_SERVICE
+ ) as android.media.projection.MediaProjectionManager
+ mediaProjectionLauncher.launch(mpManager.createScreenCaptureIntent())
+ }
}
ToolResult.HEALTH_CONNECT -> requestHealthConnect()
// Runtime permissions are mapped in Settings → Permissions; skip here.
@@ -926,7 +925,10 @@ class MainActivity : ComponentActivity() {
/** SharedPreferences key to track first-launch permission setup. */
const val KEY_FIRST_LAUNCH_DONE = "first_launch_setup_done"
- /** SharedPreferences key: when true, skip the MediaProjection consent prompt (test-only). */
+ /**
+ * SharedPreferences key: when true, skip the on-demand MediaProjection consent
+ * prompt so instrumentation never faces the system dialog (test-only).
+ */
const val KEY_SUPPRESS_MEDIA_PROJECTION_PROMPT = "suppress_media_projection_prompt"
/** Lifecycle owner for CameraX binding. Set in onCreate, valid while activity lives. */
diff --git a/app/src/main/java/com/gotcha/service/AssistiveBallService.kt b/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
index ec7fb0c..9c808b0 100644
--- a/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
+++ b/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
@@ -213,6 +213,9 @@ class AssistiveBallService : Service() {
overlay.isPanelOpen = true
scope.launch {
try {
+ // The ball has no activity to raise a consent dialog from, so this
+ // relies on the accessibility capture path. Without accessibility the
+ // capture returns null and the prompt is left to the chat flow.
val compressed = if (attachScreenshot) {
showingActivity(com.gotcha.ui.BallActivity.ACTING) {
com.gotcha.tools.ScreenPerception.compressScreenshot(
diff --git a/app/src/main/java/com/gotcha/service/MediaProjectionService.kt b/app/src/main/java/com/gotcha/service/MediaProjectionService.kt
index b9f922e..526a3a9 100644
--- a/app/src/main/java/com/gotcha/service/MediaProjectionService.kt
+++ b/app/src/main/java/com/gotcha/service/MediaProjectionService.kt
@@ -1,5 +1,6 @@
package com.gotcha.service
+import android.app.Activity
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
@@ -10,9 +11,12 @@ import android.graphics.Bitmap
import android.graphics.PixelFormat
import android.hardware.display.DisplayManager
import android.hardware.display.VirtualDisplay
+import android.media.Image
import android.media.ImageReader
import android.media.projection.MediaProjection
import android.media.projection.MediaProjectionManager
+import android.os.Handler
+import android.os.HandlerThread
import android.os.IBinder
import android.util.DisplayMetrics
import android.util.Log
@@ -21,10 +25,16 @@ import java.io.ByteArrayOutputStream
import java.io.File
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
+import java.util.concurrent.atomic.AtomicBoolean
/**
* One-shot foreground service that captures a single screenshot via MediaProjection.
* Writes PNG bytes to a temp file and signals completion via [resultReady].
+ *
+ * One-shot is not a simplification: from API 34 a consent token backs exactly one
+ * capture session, so the projection obtained here cannot be reused for a later
+ * capture. [ScreenPerception][com.gotcha.tools.ScreenPerception] clears the token as
+ * it hands it over, and fresh consent is requested when the next capture needs it.
*/
class MediaProjectionService : Service() {
@@ -33,6 +43,9 @@ class MediaProjectionService : Service() {
private const val NOTIFICATION_ID = 9999
private const val RESULT_FILE = "media_projection_capture.png"
+ /** Give up waiting for a frame slightly before [capture]'s own timeout. */
+ private const val FRAME_TIMEOUT_MS = 4000L
+
/** Set by caller before starting the service. */
var resultData: Intent? = null
@@ -78,6 +91,21 @@ class MediaProjectionService : Service() {
private var virtualDisplay: VirtualDisplay? = null
private var imageReader: ImageReader? = null
+ /** Frame delivery, the projection callback and the timeout all land here. */
+ private var captureThread: HandlerThread? = null
+ private var captureHandler: Handler? = null
+
+ /** Guards against the frame, the timeout and onStop() all racing to finish. */
+ private val finished = AtomicBoolean(false)
+
+ /** API 34 requires a registered callback before createVirtualDisplay(). */
+ private val projectionCallback = object : MediaProjection.Callback() {
+ override fun onStop() {
+ Log.d("ScreenCapture", "MediaProjection.onStop()")
+ finish(null)
+ }
+ }
+
override fun onBind(intent: Intent?): IBinder? = null
override fun onCreate() {
@@ -88,147 +116,180 @@ class MediaProjectionService : Service() {
buildNotification(),
android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
)
+ captureThread = HandlerThread("MediaProjectionCapture").also { it.start() }
+ captureHandler = Handler(captureThread!!.looper)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val data = resultData
+ val handler = captureHandler
Log.d("ScreenCapture", "MediaProjectionService.onStartCommand: data=${data != null}")
- if (data == null) {
- resultFilePath = null
- resultReady.countDown()
- stopSelf()
+ if (data == null || handler == null) {
+ finish(null)
return START_NOT_STICKY
}
- Thread {
+ handler.post {
try {
- captureScreenshot(data)
+ startCapture(data)
} catch (e: Exception) {
- Log.e("ScreenCapture", "MediaProjectionService captureScreenshot exception: ${e.message}")
- resultFilePath = null
- resultReady.countDown()
- stopSelf()
+ Log.e("ScreenCapture", "MediaProjectionService startCapture exception: ${e.message}", e)
+ finish(null)
}
- }.start()
+ }
return START_NOT_STICKY
}
- private fun captureScreenshot(data: Intent) {
- Log.d("ScreenCapture", "captureScreenshot: starting")
+ /**
+ * Sets up the projection and virtual display, then returns — the frame arrives
+ * asynchronously on [captureHandler] via the ImageReader listener.
+ */
+ private fun startCapture(data: Intent) {
+ Log.d("ScreenCapture", "startCapture: starting")
val mpManager = getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
- projection = mpManager.getMediaProjection(-1, data)
- Log.d("ScreenCapture", "captureScreenshot: projection=${projection != null}")
- if (projection == null) {
- Log.e("ScreenCapture", "captureScreenshot: getMediaProjection returned null — consent data may be stale")
- resultFilePath = null
- resultReady.countDown()
- stopSelf()
+ // A spent or stale token throws here rather than returning null on API 34+.
+ val mp = try {
+ mpManager.getMediaProjection(Activity.RESULT_OK, data)
+ } catch (e: Exception) {
+ Log.e("ScreenCapture", "startCapture: getMediaProjection failed — consent is stale: ${e.message}")
+ null
+ }
+ projection = mp
+ if (mp == null) {
+ Log.e("ScreenCapture", "startCapture: no projection — consent data may be stale")
+ finish(null)
return
}
+ mp.registerCallback(projectionCallback, captureHandler)
val wm = getSystemService(Context.WINDOW_SERVICE) as WindowManager
val metrics = DisplayMetrics()
+ @Suppress("DEPRECATION")
wm.defaultDisplay.getRealMetrics(metrics)
val screenWidth = metrics.widthPixels
val screenHeight = metrics.heightPixels
val screenDpi = metrics.densityDpi
- Log.d("ScreenCapture", "captureScreenshot: screen ${screenWidth}x$screenHeight dpi=$screenDpi")
+ Log.d("ScreenCapture", "startCapture: screen ${screenWidth}x$screenHeight dpi=$screenDpi")
- imageReader = ImageReader.newInstance(
- screenWidth, screenHeight,
- PixelFormat.RGBA_8888, 2
- )
+ val reader = ImageReader.newInstance(screenWidth, screenHeight, PixelFormat.RGBA_8888, 2)
+ imageReader = reader
+ reader.setOnImageAvailableListener({ r ->
+ // Only the first frame matters; later ones arrive after we've finished.
+ val image = try {
+ r.acquireLatestImage()
+ } catch (e: IllegalStateException) {
+ Log.e("ScreenCapture", "acquireLatestImage failed: ${e.message}")
+ null
+ }
+ if (image != null) {
+ try {
+ onFrame(image, screenWidth, screenHeight)
+ } finally {
+ image.close()
+ }
+ }
+ }, captureHandler)
- virtualDisplay = projection!!.createVirtualDisplay(
+ virtualDisplay = mp.createVirtualDisplay(
"ScreenCapture",
screenWidth, screenHeight, screenDpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
- imageReader!!.surface,
+ reader.surface,
null, null
)
- Log.d("ScreenCapture", "captureScreenshot: virtualDisplay=${virtualDisplay != null}")
+ Log.d("ScreenCapture", "startCapture: virtualDisplay=${virtualDisplay != null}")
- Thread.sleep(500)
+ // Never leave the caller (and the service) hanging if no frame is produced.
+ captureHandler?.postDelayed({
+ if (!finished.get()) {
+ Log.e("ScreenCapture", "startCapture: timed out waiting for a frame")
+ finish(null)
+ }
+ }, FRAME_TIMEOUT_MS)
+ }
- Log.d("ScreenCapture", "captureScreenshot: acquiring image from ImageReader")
- val image = imageReader!!.acquireLatestImage()
- Log.d("ScreenCapture", "captureScreenshot: image=${image != null}")
- if (image != null) {
- try {
- val bitmap = try {
- val plane = image.planes[0]
- val buffer = plane.buffer
- val pixelStride = plane.pixelStride
- val rowStride = plane.rowStride
- val w = screenWidth
- val h = screenHeight
- val srcBytes = ByteArray(buffer.remaining())
- buffer.get(srcBytes)
- // Copy pixels row-by-row into tightly-packed array (skip row padding)
- // No channel swap — copy bytes as-is; the device's native pixel
- // order already matches ARGB_8888 on most devices.
- val packedBytes = ByteArray(w * h * 4)
- for (row in 0 until h) {
- val srcRow = row * rowStride
- val dstRow = row * w * 4
- for (col in 0 until w) {
- val sp = srcRow + col * pixelStride
- val dp = dstRow + col * 4
- packedBytes[dp + 0] = srcBytes[sp + 0]
- packedBytes[dp + 1] = srcBytes[sp + 1]
- packedBytes[dp + 2] = srcBytes[sp + 2]
- packedBytes[dp + 3] = srcBytes[sp + 3]
- }
- }
- val bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
- bmp.copyPixelsFromBuffer(java.nio.ByteBuffer.wrap(packedBytes))
- Log.d(
- "ScreenCapture",
- "captureScreenshot: bitmap=${bmp.width}x${bmp.height} (stride=$rowStride, pxStride=$pixelStride)"
- )
- bmp
- } catch (t: Throwable) {
- Log.e("ScreenCapture", "captureScreenshot: bitmap creation failed: ${t.message}", t)
- null
- }
+ private fun onFrame(image: Image, screenWidth: Int, screenHeight: Int) {
+ if (finished.get()) return
+ val bitmap = image.toBitmap(screenWidth, screenHeight)
+ if (bitmap == null) {
+ Log.e("ScreenCapture", "onFrame: bitmap was null")
+ finish(null)
+ return
+ }
+ val stream = ByteArrayOutputStream()
+ bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
+ bitmap.recycle()
+ val pngBytes = stream.toByteArray()
+ Log.d("ScreenCapture", "onFrame: PNG compressed ${pngBytes.size} bytes")
+ if (pngBytes.isEmpty()) {
+ Log.e("ScreenCapture", "onFrame: PNG compression produced 0 bytes")
+ finish(null)
+ return
+ }
+ val outPath = File(cacheDir, RESULT_FILE).absolutePath
+ File(outPath).writeBytes(pngBytes)
+ Log.d("ScreenCapture", "onFrame: saved to $outPath")
+ finish(outPath)
+ }
- if (bitmap != null) {
- val stream = ByteArrayOutputStream()
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
- bitmap.recycle()
- val pngBytes = stream.toByteArray()
- Log.d("ScreenCapture", "captureScreenshot: PNG compressed ${pngBytes.size} bytes")
- if (pngBytes.isNotEmpty()) {
- val outPath = File(cacheDir, RESULT_FILE).absolutePath
- File(outPath).writeBytes(pngBytes)
- resultFilePath = outPath
- Log.d("ScreenCapture", "captureScreenshot: saved to $outPath")
- } else {
- Log.e("ScreenCapture", "captureScreenshot: PNG compression produced 0 bytes")
- resultFilePath = null
- }
- } else {
- Log.e("ScreenCapture", "captureScreenshot: bitmap was null")
- resultFilePath = null
- }
- } finally {
- image.close()
+ private fun Image.toBitmap(w: Int, h: Int): Bitmap? = try {
+ val plane = planes[0]
+ val buffer = plane.buffer
+ val pixelStride = plane.pixelStride
+ val rowStride = plane.rowStride
+ val srcBytes = ByteArray(buffer.remaining())
+ buffer.get(srcBytes)
+ // Copy pixels row-by-row into tightly-packed array (skip row padding)
+ // No channel swap — copy bytes as-is; the device's native pixel
+ // order already matches ARGB_8888 on most devices.
+ val packedBytes = ByteArray(w * h * 4)
+ for (row in 0 until h) {
+ val srcRow = row * rowStride
+ val dstRow = row * w * 4
+ for (col in 0 until w) {
+ val sp = srcRow + col * pixelStride
+ val dp = dstRow + col * 4
+ packedBytes[dp + 0] = srcBytes[sp + 0]
+ packedBytes[dp + 1] = srcBytes[sp + 1]
+ packedBytes[dp + 2] = srcBytes[sp + 2]
+ packedBytes[dp + 3] = srcBytes[sp + 3]
}
- } else {
- Log.e("ScreenCapture", "captureScreenshot: acquireLatestImage returned null")
- resultFilePath = null
}
+ val bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
+ bmp.copyPixelsFromBuffer(java.nio.ByteBuffer.wrap(packedBytes))
+ Log.d("ScreenCapture", "toBitmap: ${bmp.width}x${bmp.height} (stride=$rowStride, pxStride=$pixelStride)")
+ bmp
+ } catch (t: Throwable) {
+ Log.e("ScreenCapture", "toBitmap failed: ${t.message}", t)
+ null
+ }
+ /**
+ * Publishes the result, releases the projection and stops the service. Safe to
+ * call from the frame listener, the timeout and [MediaProjection.Callback.onStop]
+ * — only the first caller wins.
+ */
+ private fun finish(filePath: String?) {
+ if (!finished.compareAndSet(false, true)) return
+ resultFilePath = filePath
resultReady.countDown()
-
- virtualDisplay?.release()
- imageReader?.close()
- projection?.stop()
+ releaseCapture()
stopSelf()
}
+ private fun releaseCapture() {
+ runCatching { virtualDisplay?.release() }
+ virtualDisplay = null
+ runCatching { imageReader?.setOnImageAvailableListener(null, null) }
+ runCatching { imageReader?.close() }
+ imageReader = null
+ runCatching { projection?.unregisterCallback(projectionCallback) }
+ runCatching { projection?.stop() }
+ projection = null
+ }
+
private fun createNotificationChannel() {
val channel = NotificationChannel(
CHANNEL_ID,
@@ -248,9 +309,15 @@ class MediaProjectionService : Service() {
}
override fun onDestroy() {
- virtualDisplay?.release()
- imageReader?.close()
- projection?.stop()
+ // Covers a system-initiated stop that never went through finish().
+ if (finished.compareAndSet(false, true)) {
+ resultFilePath = null
+ resultReady.countDown()
+ }
+ releaseCapture()
+ captureThread?.quitSafely()
+ captureThread = null
+ captureHandler = null
super.onDestroy()
}
}
diff --git a/app/src/main/java/com/gotcha/tools/ScreenPerception.kt b/app/src/main/java/com/gotcha/tools/ScreenPerception.kt
index 721f65c..1e051e5 100644
--- a/app/src/main/java/com/gotcha/tools/ScreenPerception.kt
+++ b/app/src/main/java/com/gotcha/tools/ScreenPerception.kt
@@ -20,7 +20,14 @@ import java.io.FileOutputStream
object ScreenPerception {
- /** Set by MainActivity after the user grants MediaProjection consent. */
+ /**
+ * Set by MainActivity after the user grants MediaProjection consent.
+ *
+ * Single-use: on API 34+ a consent token backs exactly one capture session, so
+ * [captureRawBytes] clears this as it consumes it. A null value is what makes
+ * [com.gotcha.agent.AgentEngine.injectReadScreenObservation] ask for consent
+ * again; leaving a spent token here would silently break every later capture.
+ */
@Volatile
var mediaProjectionResultData: android.content.Intent? = null
@@ -341,6 +348,10 @@ object ScreenPerception {
"Path2: projectionData=${projectionData != null}, ctx=${ctx != null}, appContext=${appContext != null}"
)
if (projectionData != null && ctx != null) {
+ // The token is spent by this attempt whether or not a frame comes back,
+ // so drop it here; the next capture asks for fresh consent instead of
+ // silently retrying with a dead token.
+ mediaProjectionResultData = null
Log.d("ScreenCapture", "Path2: calling MediaProjectionService.capture()")
val bytes = withContext(Dispatchers.IO) {
MediaProjectionService.capture(ctx, projectionData)
diff --git a/app/src/test/java/com/gotcha/agent/AgentLoopTest.kt b/app/src/test/java/com/gotcha/agent/AgentLoopTest.kt
index 10dad14..85c6a9d 100644
--- a/app/src/test/java/com/gotcha/agent/AgentLoopTest.kt
+++ b/app/src/test/java/com/gotcha/agent/AgentLoopTest.kt
@@ -10,6 +10,7 @@ import com.gotcha.testsupport.FakeAndroidKeyStore
import com.gotcha.testsupport.ShadowExternalStorageManager
import com.gotcha.tools.AgentMode
import com.gotcha.tools.FileResolver
+import com.gotcha.tools.ScreenPerception
import com.gotcha.tools.ToolResult
import kotlinx.coroutines.test.runTest
import okhttp3.mockwebserver.MockResponse
@@ -557,4 +558,44 @@ class AgentLoopTest {
assertEquals(listOf(true, false), events.screenCaptureChrome)
assertEquals(1, events.screenReadDoneCount)
}
+
+ // ---- MediaProjection consent is requested on demand, never at launch ----
+
+ /**
+ * Consent is no longer requested in `MainActivity.onCreate`, so a failed capture
+ * with no token held is the only thing that can ask for it. If this stops firing,
+ * `read_screen` loses its screenshot for good on devices without accessibility.
+ */
+ @Test
+ fun `a failed capture with no consent token asks for screenshot consent`() = runTest {
+ ScreenPerception.mediaProjectionResultData = null
+
+ engine.injectReadScreenObservation()
+
+ assertTrue(
+ "expected a consent request, got ${events.permissionRequests}",
+ events.permissionRequests.contains("special:screenshot_consent")
+ )
+ }
+
+ /**
+ * From API 34 a consent token backs exactly one capture session. Holding on to a
+ * spent token would keep the request above from ever firing again, so the token
+ * must be dropped as it is consumed. (The capture itself cannot succeed under
+ * Robolectric; only the token bookkeeping is under test.)
+ */
+ @Test
+ fun `a consumed MediaProjection token is not reused for the next capture`() = runTest {
+ val savedContext = ScreenPerception.appContext
+ ScreenPerception.appContext = context
+ ScreenPerception.mediaProjectionResultData = android.content.Intent()
+ try {
+ ScreenPerception.captureRawScreenshotBytes()
+
+ assertEquals(null, ScreenPerception.mediaProjectionResultData)
+ } finally {
+ ScreenPerception.appContext = savedContext
+ ScreenPerception.mediaProjectionResultData = null
+ }
+ }
}