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.

fix: address Copilot review findings from PR #5

Durganshu Mishra78b7a68de06d
diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index bf9e5ae..ffc35d5 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -126,8 +126,11 @@ android {
buildTypes {
release {
isMinifyEnabled = false
- signingConfig = signingConfigs.getByName("debug")
- // Use the release signing config only if it was configured above.
+ // Use the release signing config when one was configured above; fall back
+ // to the debug key only so that a keyless checkout can still build a
+ // release APK locally.
+ signingConfig = signingConfigs.findByName("release")
+ ?: signingConfigs.getByName("debug")
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
diff --git a/app/src/main/java/com/gotcha/service/MediaProjectionService.kt b/app/src/main/java/com/gotcha/service/MediaProjectionService.kt
index 526a3a9..354b085 100644
--- a/app/src/main/java/com/gotcha/service/MediaProjectionService.kt
+++ b/app/src/main/java/com/gotcha/service/MediaProjectionService.kt
@@ -56,7 +56,18 @@ class MediaProjectionService : Service() {
var resultFilePath: String? = null
private set
- fun capture(context: Context, resultIntent: Intent, timeoutMs: Long = 5000): ByteArray? {
+ /**
+ * Serializes captures: [resultData], [resultReady] and [resultFilePath] are
+ * companion-level state, so a second concurrent call would overwrite the
+ * first call's latch and hand it a foreign (or missing) result.
+ */
+ private val captureLock = Any()
+
+ fun capture(
+ context: Context,
+ resultIntent: Intent,
+ timeoutMs: Long = 5000
+ ): ByteArray? = synchronized(captureLock) {
Log.d("ScreenCapture", "MediaProjectionService.capture() called")
resultData = resultIntent
resultReady = CountDownLatch(1)
diff --git a/app/src/main/java/com/gotcha/tools/ScreenPerception.kt b/app/src/main/java/com/gotcha/tools/ScreenPerception.kt
index 1e051e5..1da6f51 100644
--- a/app/src/main/java/com/gotcha/tools/ScreenPerception.kt
+++ b/app/src/main/java/com/gotcha/tools/ScreenPerception.kt
@@ -17,6 +17,7 @@ import kotlinx.coroutines.withContext
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
+import java.util.concurrent.atomic.AtomicReference
object ScreenPerception {
@@ -27,9 +28,25 @@ object ScreenPerception {
* [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.
+ *
+ * Backed by an [AtomicReference] so that concurrent callers of
+ * [compressScreenshot] cannot both claim the same token — see
+ * [consumeMediaProjectionResultData].
*/
- @Volatile
- var mediaProjectionResultData: android.content.Intent? = null
+ private val mediaProjectionToken = AtomicReference<android.content.Intent?>(null)
+
+ var mediaProjectionResultData: android.content.Intent?
+ get() = mediaProjectionToken.get()
+ set(value) {
+ mediaProjectionToken.set(value)
+ }
+
+ /**
+ * Atomically takes the single-use consent token. At most one caller gets a
+ * non-null result; everyone else sees null and falls through to the next path.
+ */
+ private fun consumeMediaProjectionResultData(): android.content.Intent? =
+ mediaProjectionToken.getAndSet(null)
@Volatile
private var lastElementsCache: List<UiElement>? = null
@@ -341,17 +358,17 @@ object ScreenPerception {
Log.w("ScreenCapture", "Path1: both attempts failed")
}
// Path 2: MediaProjection — works on all devices if user granted consent.
- val projectionData = mediaProjectionResultData
val ctx = appContext ?: GotchaAccessibilityService.instance?.applicationContext
+ // The token is spent by this attempt whether or not a frame comes back, so
+ // take it and clear it in one atomic step: the next capture asks for fresh
+ // consent instead of silently retrying with a dead token, and a concurrent
+ // capture cannot claim the same single-use token.
+ val projectionData = if (ctx != null) consumeMediaProjectionResultData() else null
Log.d(
"ScreenCapture",
"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)