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.

Review fixes: testable overlay params, honest Back comment

Rishabh Bajpai256cf0912707
diff --git a/app/src/main/java/com/gotcha/service/ScreenLensController.kt b/app/src/main/java/com/gotcha/service/ScreenLensController.kt
index 989d0fa..5fae50f 100644
--- a/app/src/main/java/com/gotcha/service/ScreenLensController.kt
+++ b/app/src/main/java/com/gotcha/service/ScreenLensController.kt
@@ -111,27 +111,8 @@ class ScreenLensController(
)
overlay.setAnnotatedEntities(annotatedEntities)
- val params = WindowManager.LayoutParams(
- WindowManager.LayoutParams.MATCH_PARENT,
- WindowManager.LayoutParams.MATCH_PARENT,
- overlayType(),
- // NOT_FOCUSABLE: the overlay must never become the active
- // window. When it steals focus, rootInActiveWindow starts
- // returning our own textless canvas, and on some phones the app
- // underneath drops out of the window list too, so hostRoot()
- // finds nothing and Lens reads an empty screen (issue #149).
- // Touch still works: NOT_TOUCH_MODAL is untouched. Nothing
- // consumes keys here, so no Back handling is lost either.
- WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
- WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or
- WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or
- WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
- PixelFormat.TRANSLUCENT
- ).apply {
- gravity = Gravity.TOP or Gravity.START
- }
try {
- windowManager.addView(overlay, params)
+ windowManager.addView(overlay, cropOverlayParams())
cropOverlay = overlay
rotationWatcher.start()
} catch (_: Exception) {
@@ -141,6 +122,32 @@ class ScreenLensController(
}
}
+ /**
+ * Window params for the crop canvas, kept out of [start] so the focus
+ * invariant is unit-testable (issue #149).
+ */
+ internal fun cropOverlayParams(): WindowManager.LayoutParams =
+ WindowManager.LayoutParams(
+ WindowManager.LayoutParams.MATCH_PARENT,
+ WindowManager.LayoutParams.MATCH_PARENT,
+ overlayType(),
+ // NOT_FOCUSABLE: the overlay must never become the active
+ // window. When it steals focus, rootInActiveWindow starts
+ // returning our own textless canvas, and on some phones the app
+ // underneath drops out of the window list too, so hostRoot()
+ // finds nothing and Lens reads an empty screen (issue #149).
+ // Touch still works: NOT_TOUCH_MODAL is untouched. Back key
+ // events fall through to the underlying app while dismissal
+ // remains tap-to-cancel.
+ WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
+ WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or
+ WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or
+ WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
+ PixelFormat.TRANSLUCENT
+ ).apply {
+ gravity = Gravity.TOP or Gravity.START
+ }
+
fun cancel() {
mainHandler.post {
rotationWatcher.stop()
diff --git a/app/src/test/java/com/gotcha/service/ScreenLensControllerTest.kt b/app/src/test/java/com/gotcha/service/ScreenLensControllerTest.kt
new file mode 100644
index 0000000..83677b0
--- /dev/null
+++ b/app/src/test/java/com/gotcha/service/ScreenLensControllerTest.kt
@@ -0,0 +1,43 @@
+package com.gotcha.service
+
+import android.content.Context
+import android.view.WindowManager
+import androidx.test.core.app.ApplicationProvider
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+
+/**
+ * The Lens crop canvas must never take window focus (issue #149): a focusable
+ * full-screen overlay becomes the active window, and on some phones the app
+ * underneath drops out of the accessibility window list, so hostRoot() finds
+ * nothing and Lens reads an empty screen. The view-level half is pinned in
+ * ScreenCropOverlayViewTest; this pins the load-bearing window flags.
+ */
+@RunWith(RobolectricTestRunner::class)
+class ScreenLensControllerTest {
+
+ private val context = ApplicationProvider.getApplicationContext<Context>()
+
+ private fun controller() = ScreenLensController(
+ context,
+ CoroutineScope(Dispatchers.Unconfined),
+ {},
+ {},
+ { _, _ -> },
+ {},
+ {}
+ )
+
+ @Test
+ fun `the crop canvas never takes window focus`() {
+ val flags = controller().cropOverlayParams().flags
+ assertTrue(
+ "crop overlay must carry FLAG_NOT_FOCUSABLE",
+ flags and WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE != 0
+ )
+ }
+}