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.

Keep Lens overlay off focus so host reads survive (#149)

Rishabh Bajpaiafbd5491661e
diff --git a/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt b/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt
index e61ba5f..8c4b70b 100644
--- a/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt
+++ b/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt
@@ -191,13 +191,16 @@ class GotchaAccessibilityService : AccessibilityService() {
* The node tree of the app we are looking *at*, which is not always
* [rootInActiveWindow].
*
- * Lens adds a full-screen overlay and only then asks what is on screen. That
- * window is focusable, so it becomes the active one, and
- * `rootInActiveWindow` starts returning our own canvas — a bare custom View
- * with no text anywhere in it. Everything downstream then behaves as though
- * the screen were empty: no Select chip, no contextual actions, no action
- * menu. It reproduced identically on two emulators and a Nothing Phone 3a,
- * and looked for a long time like a bug in the text extraction itself.
+ * Lens used to add a focusable full-screen overlay and only then ask what
+ * is on screen. That window became the active one, and `rootInActiveWindow`
+ * started returning our own canvas — a bare custom View with no text
+ * anywhere in it. Everything downstream then behaved as though the screen
+ * were empty: no Select chip, no contextual actions, no action menu. It
+ * reproduced identically on two emulators and a Nothing Phone 3a, and
+ * looked for a long time like a bug in the text extraction itself. The
+ * overlay has since been made non-focusable (issue #149), so it no longer
+ * steals the active window; the type filter below stays as the backstop
+ * for any other focusable window of ours.
*
* Callers own the returned node and must recycle it, as before.
*/
diff --git a/app/src/main/java/com/gotcha/service/ScreenLensController.kt b/app/src/main/java/com/gotcha/service/ScreenLensController.kt
index 6f8ba52..989d0fa 100644
--- a/app/src/main/java/com/gotcha/service/ScreenLensController.kt
+++ b/app/src/main/java/com/gotcha/service/ScreenLensController.kt
@@ -115,7 +115,15 @@ class ScreenLensController(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
overlayType(),
- WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or
+ // 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
@@ -481,6 +489,10 @@ class ScreenLensController(
(TEXT_CARD_WIDTH_DP * density).toInt(),
WindowManager.LayoutParams.WRAP_CONTENT,
overlayType(),
+ // Deliberately still focusable (unlike the crop canvas): selectable
+ // text needs it. Do fresh accessibility reads from this card and
+ // hostRoot() will hand back our own window instead of the app
+ // underneath (issue #149); pass extracted text in instead.
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT
).apply {
diff --git a/app/src/main/java/com/gotcha/ui/ScreenCropOverlayView.kt b/app/src/main/java/com/gotcha/ui/ScreenCropOverlayView.kt
index 2f0d7e7..8828e8c 100644
--- a/app/src/main/java/com/gotcha/ui/ScreenCropOverlayView.kt
+++ b/app/src/main/java/com/gotcha/ui/ScreenCropOverlayView.kt
@@ -186,7 +186,12 @@ class ScreenCropOverlayView(
private var lastFrameNs = 0L
init {
- isFocusableInTouchMode = true
+ // Deliberately not focusable (in touch mode or otherwise): the Lens
+ // overlay must never become the active window, or accessibility reads
+ // start returning our own canvas instead of the app underneath
+ // (issue #149). See also FLAG_NOT_FOCUSABLE where this view is added.
+ isFocusable = false
+ isFocusableInTouchMode = false
setLayerType(LAYER_TYPE_SOFTWARE, null)
}
diff --git a/app/src/test/java/com/gotcha/ui/ScreenCropOverlayViewTest.kt b/app/src/test/java/com/gotcha/ui/ScreenCropOverlayViewTest.kt
index 7f396d2..8772471 100644
--- a/app/src/test/java/com/gotcha/ui/ScreenCropOverlayViewTest.kt
+++ b/app/src/test/java/com/gotcha/ui/ScreenCropOverlayViewTest.kt
@@ -66,6 +66,16 @@ class ScreenCropOverlayViewTest {
}
}
+ @Test
+ fun `the canvas never takes focus`() {
+ // The overlay must not become the active window, or accessibility
+ // reads start returning our own canvas instead of the app underneath
+ // (issue #149). See FLAG_NOT_FOCUSABLE where it is added.
+ val view = viewFor(Skins.DeepSpaceDark.id)
+ assertFalse(view.isFocusable)
+ assertFalse(view.isFocusableInTouchMode)
+ }
+
@Test
fun `the hint stays light on a light skin`() {
// It is drawn onto the dim over somebody else's app, not onto a surface