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: add floating text chips for copy/translate and prevent spurious STT error toasts during panel dismissal
Rishabh Bajpai4534b9dc70f1
diff --git a/app/src/main/java/com/gotcha/service/AssistiveBallService.kt b/app/src/main/java/com/gotcha/service/AssistiveBallService.ktindex 9c8babc..f1f0d4f 100644--- a/app/src/main/java/com/gotcha/service/AssistiveBallService.kt+++ b/app/src/main/java/com/gotcha/service/AssistiveBallService.kt@@ -55,6 +55,9 @@ class AssistiveBallService : Service() {* message. Set when the user picks "Ask about this"; cleared once consumed.*/private var pendingCropImage: String? = null++ /** True while the panel mic is actively recording, so dismiss won't try to transcribe silence. */+ private var panelVoiceActive = falseprivate lateinit var overlay: com.gotcha.ui.AssistiveBallOverlayprivate lateinit var chatWindow: CallChatWindowprivate lateinit var callController: CallSessionController@@ -404,8 +407,13 @@ class AssistiveBallService : Service() {overlay.isPanelOpen = falseactiveCompanionHistory.clear()pendingCropImage = null- // Stop any in-flight voice I/O so nothing keeps the mic/TTS alive.- stopPanelVoiceInput()+ // Only stop/transcribe if the mic was actually recording; otherwise+ // a no-op stop would surface a spurious "Transcription failed" toast.+ if (panelVoiceActive) {+ stopPanelVoiceInput()+ } else {+ sttEngine.cancelListening(settingsRepository.load().sttProvider)+ }ttsEngine.stop()}onStartVoiceInput = { startPanelVoiceInput() }@@ -455,7 +463,9 @@ class AssistiveBallService : Service() {screenCompanionPanel.setListening(false)return}- if (!sttEngine.startAndroidListening()) {+ if (sttEngine.startAndroidListening()) {+ panelVoiceActive = true+ } else {overlay.showError("Failed to start speech recognition.")screenCompanionPanel.setListening(false)}@@ -471,7 +481,9 @@ class AssistiveBallService : Service() {screenCompanionPanel.setListening(false)return}- if (!sttEngine.startRecording()) {+ if (sttEngine.startRecording()) {+ panelVoiceActive = true+ } else {overlay.showError("Failed to start recording.")screenCompanionPanel.setListening(false)}@@ -485,6 +497,12 @@ class AssistiveBallService : Service() {/** Stop voice typing, transcribe, and append the result to the panel input. */private fun stopPanelVoiceInput() {+ // Nothing was recording — don't run a transcribe that would fail on silence.+ if (!panelVoiceActive) {+ screenCompanionPanel.setListening(false)+ return+ }+ panelVoiceActive = falseval s = settingsRepository.load()val provider = s.sttProviderif (provider == com.gotcha.audio.AudioProvider.NONE) {diff --git a/app/src/main/java/com/gotcha/service/ScreenLensController.kt b/app/src/main/java/com/gotcha/service/ScreenLensController.ktindex 12bec8a..ee07932 100644--- a/app/src/main/java/com/gotcha/service/ScreenLensController.kt+++ b/app/src/main/java/com/gotcha/service/ScreenLensController.kt@@ -60,6 +60,9 @@ class ScreenLensController(private var cropOverlay: View? = nullprivate var actionMenu: View? = null+ /** Small floating "Copy text" / "Translate" chips anchored above the rectangle. */+ private var textChips: View? = null+/** The current cropped bitmap; owned and recycled only by this controller. */private var pendingCrop: Bitmap? = null@@ -164,7 +167,8 @@ class ScreenLensController(}withContext(Dispatchers.Main) {pendingCrop = cropped- showActionMenu(contextualActions, regionText)+ showTextChips(finalRect, regionText)+ showActionMenu(contextualActions)}}}@@ -207,7 +211,94 @@ class ScreenLensController(}}- private fun showActionMenu(contextualActions: List<SmartAction>, regionText: String?) {+ /**+ * Float small "Copy text" / "Translate" chips just above the selection+ * rectangle, so the user can act on the selection's text directly without+ * opening the full menu. Only shown when the selection contains text.+ */+ private fun showTextChips(rectInView: Rect, regionText: String?) {+ removeTextChips()+ val text = regionText?.trim()+ if (text.isNullOrBlank()) return++ val row = LinearLayout(appContext).apply {+ orientation = LinearLayout.HORIZONTAL+ setPadding(dp(4), dp(4), dp(4), dp(4))+ background = GradientDrawable().apply {+ cornerRadius = dp(18).toFloat()+ setColor(Color.parseColor("#F21E1E1E"))+ setStroke(dp(1), Color.parseColor("#66568CD8"))+ }+ }+ row.addView(+ smallChip("📝 Copy text") {+ copyTextToClipboard(text)+ finishMenu(recycle = true)+ }+ )+ row.addView(+ smallChip("🌐 Translate") {+ finishMenu(recycle = true)+ onContextualAction(+ "Translate the following text to English (or the user's system " ++ "language if it is already English). Show only the translation:\n\n$text"+ )+ }+ )++ // Measure so we can centre the row over the rectangle and place it above it.+ row.measure(+ View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),+ View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)+ )+ val rowW = row.measuredWidth.coerceAtLeast(dp(160))+ val rowH = row.measuredHeight.coerceAtLeast(dp(40))+ val metrics = appContext.resources.displayMetrics++ var x = rectInView.centerX() - rowW / 2+ x = x.coerceIn(dp(8), (metrics.widthPixels - rowW - dp(8)).coerceAtLeast(dp(8)))+ // Prefer just above the rectangle; if there's no room, place just below it.+ var y = rectInView.top - rowH - dp(8)+ if (y < dp(8)) y = (rectInView.bottom + dp(8)).coerceAtMost(metrics.heightPixels - rowH - dp(8))++ val params = WindowManager.LayoutParams(+ WindowManager.LayoutParams.WRAP_CONTENT,+ WindowManager.LayoutParams.WRAP_CONTENT,+ overlayType(),+ WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,+ PixelFormat.TRANSLUCENT+ ).apply {+ gravity = Gravity.TOP or Gravity.START+ this.x = x+ this.y = y+ }+ try {+ windowManager.addView(row, params)+ textChips = row+ } catch (_: Exception) {+ textChips = null+ }+ }++ private fun smallChip(label: String, onClick: () -> Unit): TextView = TextView(appContext).apply {+ text = label+ setTextColor(Color.WHITE)+ textSize = 13f+ gravity = Gravity.CENTER+ setPadding(dp(14), dp(8), dp(14), dp(8))+ background = GradientDrawable().apply {+ cornerRadius = dp(14).toFloat()+ setColor(Color.parseColor("#33568CD8"))+ setStroke(dp(1), Color.parseColor("#66B06FD0"))+ }+ layoutParams = LinearLayout.LayoutParams(+ LinearLayout.LayoutParams.WRAP_CONTENT,+ LinearLayout.LayoutParams.WRAP_CONTENT+ ).apply { setMargins(dp(3), dp(3), dp(3), dp(3)) }+ setOnClickListener { onClick() }+ }++ private fun showActionMenu(contextualActions: List<SmartAction>) {removeActionMenu()val container = LinearLayout(appContext).apply {orientation = LinearLayout.VERTICAL@@ -231,16 +322,7 @@ class ScreenLensController()}- // "Copy text" when the selection contains readable text.- val selectedText = regionText?.trim()- if (!selectedText.isNullOrBlank()) {- container.addView(- menuButton("📝 Copy text") {- copyTextToClipboard(selectedText)- finishMenu(recycle = true)- }- )- }+ // (Text ops — Copy text / Translate — live as inline chips on the rectangle.)container.addView(menuButton("🔍 Ask about this") {@@ -346,6 +428,7 @@ class ScreenLensController(*/private fun finishMenu(recycle: Boolean) {removeActionMenu()+ removeTextChips()removeCropOverlay()if (recycle) recyclePendingCrop()}@@ -492,10 +575,16 @@ class ScreenLensController(}private fun removeCropOverlay() {+ removeTextChips()cropOverlay?.let { safeRemove(it) }cropOverlay = null}+ private fun removeTextChips() {+ textChips?.let { safeRemove(it) }+ textChips = null+ }+private fun removeActionMenu() {actionMenu?.let { safeRemove(it) }actionMenu = null