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.

refactor: restrict text extraction to substantial area intersections and update Lens UI with styled selection-aware copy actions.

Rishabh Bajpai4391bb207703
diff --git a/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt b/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt
index e80416f..b297e46 100644
--- a/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt
+++ b/app/src/main/java/com/gotcha/service/GotchaAccessibilityService.kt
@@ -268,7 +268,12 @@ class GotchaAccessibilityService : AccessibilityService() {
if (node == null || out.size >= limit) return
val bounds = android.graphics.Rect()
node.getBoundsInScreen(bounds)
- if (android.graphics.Rect.intersects(bounds, region)) {
+ // Only text that actually lives inside the selection: a node must be
+ // substantially within the region, not merely touching it. A bare
+ // Rect.intersects would pull in the full text of any wide block or
+ // container that overlaps the selection by a single pixel, which made
+ // the extracted-text card read like the whole screen.
+ if (substantiallyInside(bounds, region)) {
val text = node.text?.toString()?.trim()
val desc = node.contentDescription?.toString()?.trim()
if (!text.isNullOrEmpty()) {
@@ -280,6 +285,20 @@ class GotchaAccessibilityService : AccessibilityService() {
}
}
+ /**
+ * True when [bounds] is mostly inside [region]: the intersection must cover
+ * at least [REGION_TEXT_COVERAGE] of the node's own area. This keeps the
+ * "Extracted Text" card scoped to the selected area.
+ */
+ private fun substantiallyInside(bounds: android.graphics.Rect, region: android.graphics.Rect): Boolean {
+ if (bounds.isEmpty || bounds.width() <= 0 || bounds.height() <= 0) return false
+ val inter = android.graphics.Rect(bounds)
+ if (!inter.intersect(region)) return false
+ val nodeArea = bounds.width().toLong() * bounds.height().toLong()
+ val interArea = inter.width().toLong() * inter.height().toLong()
+ return interArea >= nodeArea * REGION_TEXT_COVERAGE
+ }
+
/**
* Grow [region] (screen pixels) to include any UI element it substantially
* overlaps, so a selection drawn around ~most of a control snaps to the whole
@@ -615,6 +634,13 @@ class GotchaAccessibilityService : AccessibilityService() {
@Volatile
var lastClipboardData: ClipData? = null
internal set
+
+ /**
+ * Fraction of a node's own area that must fall inside the Lens selection
+ * for its text to be included (see [substantiallyInside]). Mirrors the
+ * snap-to-element coverage so the extracted text matches the selection.
+ */
+ private const val REGION_TEXT_COVERAGE = 0.5f
}
/** Register a clipboard listener to cache clipboard content. */
diff --git a/app/src/main/java/com/gotcha/service/ScreenLensController.kt b/app/src/main/java/com/gotcha/service/ScreenLensController.kt
index bb2e862..75ebde9 100644
--- a/app/src/main/java/com/gotcha/service/ScreenLensController.kt
+++ b/app/src/main/java/com/gotcha/service/ScreenLensController.kt
@@ -366,11 +366,17 @@ class ScreenLensController(
* One chip on the Lens bar. The icon sits *above* the label rather than
* beside it, unlike the ball's menu rows: five of these have to fit across a
* phone, and side-by-side icons would push the bar wider than the screen.
+ *
+ * When [filled] is set the chip gets a [OverlaySkin.buttonBg] rounded
+ * background (the same treatment the contextual-action menu buttons use), so
+ * it stays legible on every skin instead of relying on `buttonText` against
+ * the card surface.
*/
private fun chipButton(
label: String,
colors: OverlaySkin,
iconRes: Int? = null,
+ filled: Boolean = false,
onClick: () -> Unit
): TextView {
val density = appContext.resources.displayMetrics.density
@@ -381,6 +387,12 @@ class ScreenLensController(
typeface = colors.sans
setTextColor(colors.buttonText)
gravity = Gravity.CENTER
+ if (filled) {
+ background = android.graphics.drawable.GradientDrawable().apply {
+ cornerRadius = colors.buttonRadiusDp * density
+ setColor(colors.buttonBg)
+ }
+ }
if (iconRes != null) {
val icon = androidx.core.content.ContextCompat.getDrawable(appContext, iconRes)
icon?.setBounds(0, 0, iconPx, iconPx)
@@ -444,28 +456,26 @@ class ScreenLensController(
}
btnRow.addView(
- chipButton("Copy All", colors, R.drawable.ic_lens_copy) {
- val clipService = Context.CLIPBOARD_SERVICE
- val clipManager = appContext.getSystemService(clipService) as? android.content.ClipboardManager
- clipManager?.setPrimaryClip(android.content.ClipData.newPlainText("Extracted Text", text))
- android.widget.Toast.makeText(
- appContext,
- "Copied to clipboard",
- android.widget.Toast.LENGTH_SHORT
- ).show()
- cancel()
+ chipButton("Copy Selected", colors, filled = true) {
+ copyTextSelection(textView, text)
+ }
+ )
+
+ btnRow.addView(
+ chipButton("Copy All", colors, R.drawable.ic_lens_copy, filled = true) {
+ copyTextToClipboard(text)
}
)
btnRow.addView(
- chipButton("Close", colors) {
+ chipButton("Close", colors, filled = true) {
cancel()
}
)
cardLayout.addView(btnRow)
val params = WindowManager.LayoutParams(
- (300 * density).toInt(),
+ (TEXT_CARD_WIDTH_DP * density).toInt(),
WindowManager.LayoutParams.WRAP_CONTENT,
overlayType(),
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
@@ -482,6 +492,38 @@ class ScreenLensController(
}
}
+ /**
+ * Copy the user's highlighted range from [textView] to the clipboard. When
+ * nothing is selected (no handles dragged), fall back to a hint instead of
+ * silently copying everything.
+ */
+ private fun copyTextSelection(textView: TextView, fullText: String) {
+ val start = textView.selectionStart
+ val end = textView.selectionEnd
+ if (start >= 0 && end > start && end <= fullText.length) {
+ copyTextToClipboard(fullText.substring(start, end))
+ } else {
+ android.widget.Toast.makeText(
+ appContext,
+ "Select some text first, then tap Copy Selected",
+ android.widget.Toast.LENGTH_SHORT
+ ).show()
+ }
+ }
+
+ /** Copy [content] to the clipboard and confirm with a toast. */
+ private fun copyTextToClipboard(content: String) {
+ val clipService = Context.CLIPBOARD_SERVICE
+ val clipManager = appContext.getSystemService(clipService) as? android.content.ClipboardManager
+ clipManager?.setPrimaryClip(android.content.ClipData.newPlainText("Extracted Text", content))
+ android.widget.Toast.makeText(
+ appContext,
+ "Copied to clipboard",
+ android.widget.Toast.LENGTH_SHORT
+ ).show()
+ cancel()
+ }
+
private fun showActionMenu(actions: List<SmartAction>) {
removeActionMenu()
if (actions.isEmpty()) return
@@ -653,5 +695,11 @@ class ScreenLensController(
const val MAX_GROUP_MENU_ITEMS = 8
const val GAP_DP = 12
const val EDGE_MARGIN_DP = 12
+
+ /**
+ * Width of the extracted-text card. Wide enough for the three-button
+ * row (Copy Selected / Copy All / Close) to fit without wrapping.
+ */
+ const val TEXT_CARD_WIDTH_DP = 340
}
}