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: enhance ScreenLensController with dynamic chip buttons and improved layout handling
Durganshu Mishra03d271e9f0b0
diff --git a/app/src/main/java/com/gotcha/service/ScreenLensController.kt b/app/src/main/java/com/gotcha/service/ScreenLensController.ktindex 2d0a5e2..5ec32d3 100644--- a/app/src/main/java/com/gotcha/service/ScreenLensController.kt+++ b/app/src/main/java/com/gotcha/service/ScreenLensController.kt@@ -11,6 +11,7 @@ import android.view.Gravityimport android.view.Viewimport android.view.WindowManagerimport android.widget.TextView+import com.gotcha.Rimport com.gotcha.ui.ScreenCropOverlayViewimport com.gotcha.ui.applyOverlayCardimport com.gotcha.ui.theme.OverlaySkin@@ -50,6 +51,9 @@ class ScreenLensController(private var cropOverlay: View? = nullprivate var actionMenu: View? = nullprivate var textChips: View? = null++ /** Where the chip bar ended up, so the action menu can sit under it. */+ private var chipBarBounds: Rect? = nullprivate var pendingCrop: Bitmap? = null/** Begin a Lens capture: add the full-screen crop overlay and auto-annotate UI elements. */@@ -203,7 +207,6 @@ class ScreenLensController(private fun showTextChips(finalRect: Rect, regionText: String?) {removeTextChips()val overlay = cropOverlay ?: return- val density = appContext.resources.displayMetrics.densityval cleanText = regionText?.replace(Regex("\\s+"), " ")?.trim()val colors = currentColors()@@ -215,14 +218,14 @@ class ScreenLensController(if (!cleanText.isNullOrBlank()) {chipsLayout.addView(- chipButton("📝 Select", colors) {+ chipButton("Select", colors, R.drawable.ic_lens_select) {showSelectableTextCard(cleanText)})}chipsLayout.addView(- chipButton("📋 Copy", colors) {+ chipButton("Copy", colors, R.drawable.ic_lens_copy) {val crop = pendingCropif (crop != null && !crop.isRecycled) {val cropCopy = runCatching { crop.copy(crop.config, false) }.getOrNull()@@ -240,7 +243,7 @@ class ScreenLensController(if (!isAlreadyInPreferredLang) {chipsLayout.addView(- chipButton("🌐 Translate", colors) {+ chipButton("Translate", colors, R.drawable.ic_lens_translate) {val prompt = "Extract text from this screenshot, translate it to $preferredLang, " +"and display both original and translated text."encodePendingCrop { base64 -> onImagePrompt(base64, prompt) }@@ -250,7 +253,7 @@ class ScreenLensController(}chipsLayout.addView(- chipButton("💾 Save", colors) {+ chipButton("Save", colors, R.drawable.ic_lens_save) {val crop = pendingCropif (crop != null && !crop.isRecycled) {val cropCopy = runCatching { crop.copy(crop.config, false) }.getOrNull()@@ -263,29 +266,18 @@ class ScreenLensController()chipsLayout.addView(- chipButton("❓ Ask", colors) {+ chipButton("Ask", colors, R.drawable.ic_lens_ask) {encodePendingCrop { base64 -> onAskAboutCrop(base64) }cancel()})- val chipBarHeight = (42 * density).toInt()- val chipBarWidth = (300 * density).toInt()- val screenHeight = overlay.height- val screenWidth = overlay.width-- val targetY = if (finalRect.bottom + chipBarHeight + (12 * density).toInt() <= screenHeight) {- finalRect.bottom + (12 * density).toInt()- } else if (finalRect.top - chipBarHeight - (12 * density).toInt() >= 0) {- finalRect.top - chipBarHeight - (12 * density).toInt()- } else {- (16 * density).toInt()- }-- val targetX = (finalRect.centerX() - chipBarWidth / 2).coerceIn(- (12 * density).toInt(),- (screenWidth - chipBarWidth - (12 * density).toInt()).coerceAtLeast(0)- )+ // Measured, not guessed. This used to assume 300dp × 42dp, which was+ // already approximate and became more so once the bar grew icons and an+ // OverlayCardDrawable gutter — a shadow lives outside the card, so the+ // view is wider than the card you can see.+ val (chipBarWidth, chipBarHeight) = measure(chipsLayout)+ val placed = placeNear(finalRect, chipBarWidth, chipBarHeight, overlay.width, overlay.height)val params = WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,@@ -295,25 +287,75 @@ class ScreenLensController(PixelFormat.TRANSLUCENT).apply {gravity = Gravity.TOP or Gravity.START- x = targetX- y = targetY+ x = placed.left+ y = placed.top}try {windowManager.addView(chipsLayout, params)textChips = chipsLayout+ chipBarBounds = placed} catch (_: Exception) {textChips = null+ chipBarBounds = null}}- private fun chipButton(label: String, colors: OverlaySkin, onClick: () -> Unit): TextView {+ /** [view]'s size when it is allowed to be exactly as big as it wants. */+ private fun measure(view: View): Pair<Int, Int> {+ val spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)+ view.measure(spec, spec)+ return view.measuredWidth to view.measuredHeight+ }++ /**+ * Put a [w]×[h] panel just below [anchor], or just above it when there is no+ * room, or pinned to the top when there is room for neither. Horizontally+ * centred on the anchor and kept on screen.+ */+ private fun placeNear(anchor: Rect, w: Int, h: Int, screenW: Int, screenH: Int): Rect {val density = appContext.resources.displayMetrics.density+ val gap = (GAP_DP * density).toInt()+ val margin = (EDGE_MARGIN_DP * density).toInt()++ val y = when {+ anchor.bottom + gap + h <= screenH -> anchor.bottom + gap+ anchor.top - gap - h >= 0 -> anchor.top - gap - h+ else -> margin+ }+ val x = (anchor.centerX() - w / 2).coerceIn(+ margin,+ (screenW - w - margin).coerceAtLeast(margin)+ )+ return Rect(x, y, x + w, y + h)+ }++ /**+ * 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.+ */+ private fun chipButton(+ label: String,+ colors: OverlaySkin,+ iconRes: Int? = null,+ onClick: () -> Unit+ ): TextView {+ val density = appContext.resources.displayMetrics.density+ val iconPx = (CHIP_ICON_DP * density).toInt()return TextView(appContext).apply {text = labeltextSize = colors.labelSptypeface = colors.sanssetTextColor(colors.buttonText)+ gravity = Gravity.CENTER+ if (iconRes != null) {+ val icon = androidx.core.content.ContextCompat.getDrawable(appContext, iconRes)+ icon?.setBounds(0, 0, iconPx, iconPx)+ icon?.setTint(colors.buttonText)+ setCompoundDrawablesRelative(null, icon, null, null)+ compoundDrawablePadding = (CHIP_ICON_GAP_DP * density).toInt()+ }setPadding((10 * density).toInt(), (6 * density).toInt(), (10 * density).toInt(), (6 * density).toInt())setOnClickListener { onClick() }}@@ -330,7 +372,7 @@ class ScreenLensController(}val titleView = TextView(appContext).apply {- this.text = "📝 Extracted Text"+ this.text = "Extracted Text"textSize = colors.titleSpsetTypeface(colors.sans, android.graphics.Typeface.BOLD)setTextColor(colors.onSurface)@@ -361,7 +403,7 @@ class ScreenLensController(}btnRow.addView(- chipButton("📋 Copy All", colors) {+ chipButton("Copy All", colors, R.drawable.ic_lens_copy) {val clipService = Context.CLIPBOARD_SERVICEval clipManager = appContext.getSystemService(clipService) as? android.content.ClipboardManagerclipManager?.setPrimaryClip(android.content.ClipData.newPlainText("Extracted Text", text))@@ -375,7 +417,7 @@ class ScreenLensController()btnRow.addView(- chipButton("✕ Close", colors) {+ chipButton("Close", colors) {cancel()})@@ -434,14 +476,28 @@ class ScreenLensController(menuLayout.addView(btn)}+ // Anchored under the chip bar rather than at Gravity.CENTER, which put+ // the suggestions squarely on top of the region the user had just drawn+ // a box around.+ val menuWidth = (MENU_WIDTH_DP * density).toInt()+ val overlay = cropOverlay+ val anchor = chipBarBoundsval params = WindowManager.LayoutParams(- (240 * density).toInt(),+ menuWidth,WindowManager.LayoutParams.WRAP_CONTENT,overlayType(),WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,PixelFormat.TRANSLUCENT).apply {- gravity = Gravity.CENTER+ if (anchor != null && overlay != null) {+ val (_, menuHeight) = measure(menuLayout)+ val placed = placeNear(anchor, menuWidth, menuHeight, overlay.width, overlay.height)+ gravity = Gravity.TOP or Gravity.START+ x = placed.left+ y = placed.top+ } else {+ gravity = Gravity.CENTER+ }}try {@@ -465,6 +521,7 @@ class ScreenLensController(private fun removeTextChips() {textChips?.let { safeRemove(it) }textChips = null+ chipBarBounds = null}private fun recyclePendingCrop() {@@ -547,5 +604,10 @@ class ScreenLensController(private companion object {const val MIN_SELECTION_DP = 24+ const val CHIP_ICON_DP = 18+ const val CHIP_ICON_GAP_DP = 3+ const val MENU_WIDTH_DP = 240+ const val GAP_DP = 12+ const val EDGE_MARGIN_DP = 12}}diff --git a/app/src/main/java/com/gotcha/ui/ScreenCropOverlayView.kt b/app/src/main/java/com/gotcha/ui/ScreenCropOverlayView.ktindex 2c40e88..5e667e7 100644--- a/app/src/main/java/com/gotcha/ui/ScreenCropOverlayView.kt+++ b/app/src/main/java/com/gotcha/ui/ScreenCropOverlayView.kt@@ -136,6 +136,9 @@ class ScreenCropOverlayView(private val scratchRect = RectF()private val scratchChipRect = RectF()+ /** Held separately from [scratchRect], which [drawSelectionBox] is using. */+ private val scratchDim = RectF()+/** The bounds [pathPaint]'s gradient was last built for. */private var gradientMinX = Float.NaNprivate var gradientMinY = Float.NaN@@ -253,7 +256,7 @@ class ScreenCropOverlayView(// the view should be getting out of the way. `setCaptureMode(false)`// restarts the loop.if (captureMode) return- canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), dimPaint)+ drawDim(canvas)canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), edgeGlowPaint)stepAndDrawParticles(canvas)@@ -299,6 +302,47 @@ class ScreenCropOverlayView(postInvalidateOnAnimation()}+ /**+ * The dim, with the current selection cut out of it.+ *+ * It used to be one rect over the whole screen, selection included, which+ * meant that after drawing a region nothing on screen was actually+ * highlighted — the thing you had just picked was exactly as dark as the+ * thing you hadn't. Four rects around the hole is also less fill than one+ * rect over everything, so the highlight is cheaper than the flat dim was.+ */+ private fun drawDim(canvas: Canvas) {+ val w = width.toFloat()+ val h = height.toFloat()+ val hole = currentSelection()+ if (hole == null) {+ canvas.drawRect(0f, 0f, w, h, dimPaint)+ return+ }+ val left = hole.left.coerceIn(0f, w)+ val top = hole.top.coerceIn(0f, h)+ val right = hole.right.coerceIn(0f, w)+ val bottom = hole.bottom.coerceIn(0f, h)+ canvas.drawRect(0f, 0f, w, top, dimPaint)+ canvas.drawRect(0f, bottom, w, h, dimPaint)+ canvas.drawRect(0f, top, left, bottom, dimPaint)+ canvas.drawRect(right, top, w, bottom, dimPaint)+ }++ /**+ * The region the user has picked, or null while there is nothing to+ * highlight. Writes into [scratchDim] rather than allocating, because this+ * runs every frame.+ */+ private fun currentSelection(): RectF? {+ frozenRect?.let { return it }+ if (drawing && moved) {+ scratchDim.set(minX, minY, maxX, maxY)+ return scratchDim+ }+ return null+ }+private fun drawAnnotatedEntities(canvas: Canvas) {getLocationOnScreen(scratchLocation)val viewOffsetX = scratchLocation[0].toFloat()diff --git a/app/src/main/res/drawable/ic_lens_ask.xml b/app/src/main/res/drawable/ic_lens_ask.xmlnew file mode 100644index 0000000..a4dd75a--- /dev/null+++ b/app/src/main/res/drawable/ic_lens_ask.xml@@ -0,0 +1,15 @@+<!--+ Material "help_outline", replacing a U+2753 emoji on the Lens chip bar — the same glyph the+ emoji was standing in for, at the app's weight instead of the font vendor's.+ Tinted at runtime from the skin — see ScreenLensController.chipButton.+-->+<vector xmlns:android="http://schemas.android.com/apk/res/android"+ android:width="24dp"+ android:height="24dp"+ android:viewportWidth="24"+ android:viewportHeight="24"+ android:tint="#FFFFFFFF">+ <path+ android:fillColor="@android:color/white"+ android:pathData="M11,18h2v-2h-2V18zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8zM12,6c-2.21,0 -4,1.79 -4,4h2c0,-1.1 0.9,-2 2,-2s2,0.9 2,2c0,2 -3,1.75 -3,5h2c0,-2.25 3,-2.5 3,-5 0,-2.21 -1.79,-4 -4,-4z" />+</vector>diff --git a/app/src/main/res/drawable/ic_lens_copy.xml b/app/src/main/res/drawable/ic_lens_copy.xmlnew file mode 100644index 0000000..43c7272--- /dev/null+++ b/app/src/main/res/drawable/ic_lens_copy.xml@@ -0,0 +1,15 @@+<!--+ Material "content_copy", replacing a U+1F4CB emoji on the Lens chip bar — the same glyph the+ emoji was standing in for, at the app's weight instead of the font vendor's.+ Tinted at runtime from the skin — see ScreenLensController.chipButton.+-->+<vector xmlns:android="http://schemas.android.com/apk/res/android"+ android:width="24dp"+ android:height="24dp"+ android:viewportWidth="24"+ android:viewportHeight="24"+ android:tint="#FFFFFFFF">+ <path+ android:fillColor="@android:color/white"+ android:pathData="M16,1H4C2.9,1 2,1.9 2,3v14h2V3h12V1zM19,5H8C6.9,5 6,5.9 6,7v14c0,1.1 0.9,2 2,2h11c1.1,0 2,-0.9 2,-2V7C21,5.9 20.1,5 19,5zM19,21H8V7h11V21z" />+</vector>diff --git a/app/src/main/res/drawable/ic_lens_save.xml b/app/src/main/res/drawable/ic_lens_save.xmlnew file mode 100644index 0000000..056afa0--- /dev/null+++ b/app/src/main/res/drawable/ic_lens_save.xml@@ -0,0 +1,15 @@+<!--+ Material "download", replacing a U+1F4BE emoji on the Lens chip bar — the same glyph the+ emoji was standing in for, at the app's weight instead of the font vendor's.+ Tinted at runtime from the skin — see ScreenLensController.chipButton.+-->+<vector xmlns:android="http://schemas.android.com/apk/res/android"+ android:width="24dp"+ android:height="24dp"+ android:viewportWidth="24"+ android:viewportHeight="24"+ android:tint="#FFFFFFFF">+ <path+ android:fillColor="@android:color/white"+ android:pathData="M5,20h14v-2H5V20zM19,9h-4V3H9v6H5l7,7L19,9z" />+</vector>diff --git a/app/src/main/res/drawable/ic_lens_select.xml b/app/src/main/res/drawable/ic_lens_select.xmlnew file mode 100644index 0000000..446ef5c--- /dev/null+++ b/app/src/main/res/drawable/ic_lens_select.xml@@ -0,0 +1,15 @@+<!--+ Material "text_fields", replacing a U+1F4DD emoji on the Lens chip bar — the same glyph the+ emoji was standing in for, at the app's weight instead of the font vendor's.+ Tinted at runtime from the skin — see ScreenLensController.chipButton.+-->+<vector xmlns:android="http://schemas.android.com/apk/res/android"+ android:width="24dp"+ android:height="24dp"+ android:viewportWidth="24"+ android:viewportHeight="24"+ android:tint="#FFFFFFFF">+ <path+ android:fillColor="@android:color/white"+ android:pathData="M2.5,4v3h5v12h3V7h5V4h-13zM21.5,9h-9v3h3v7h3v-7h3V9z" />+</vector>diff --git a/app/src/main/res/drawable/ic_lens_translate.xml b/app/src/main/res/drawable/ic_lens_translate.xmlnew file mode 100644index 0000000..2152b90--- /dev/null+++ b/app/src/main/res/drawable/ic_lens_translate.xml@@ -0,0 +1,15 @@+<!--+ Material "translate", replacing a U+1F310 emoji on the Lens chip bar — the same glyph the+ emoji was standing in for, at the app's weight instead of the font vendor's.+ Tinted at runtime from the skin — see ScreenLensController.chipButton.+-->+<vector xmlns:android="http://schemas.android.com/apk/res/android"+ android:width="24dp"+ android:height="24dp"+ android:viewportWidth="24"+ android:viewportHeight="24"+ android:tint="#FFFFFFFF">+ <path+ android:fillColor="@android:color/white"+ android:pathData="M12.87,15.07l-2.54,-2.51 0.03,-0.03c1.74,-1.94 2.98,-4.17 3.71,-6.53H17V4h-7V2H8v2H1v1.99h11.17C11.5,7.92 10.44,9.75 9,11.35 8.07,10.32 7.3,9.19 6.69,8h-2c0.73,1.63 1.73,3.17 2.98,4.56l-5.09,5.02L4,19l5,-5 3.11,3.11 0.76,-2.04zM18.5,10h-2L12,22h2l1.12,-3h4.75L21,22h2l-4.5,-12zM15.88,17l1.62,-4.33L19.12,17H15.88z" />+</vector>