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.

feat: the ball breathes when the agent is working (stage 4)

Durganshu Mishra9a3b40567224
diff --git a/app/src/main/java/com/gotcha/service/AssistiveBallService.kt b/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
index ef4d34f..e4ee909 100644
--- a/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
+++ b/app/src/main/java/com/gotcha/service/AssistiveBallService.kt
@@ -213,7 +213,12 @@ class AssistiveBallService : Service() {
scope.launch {
try {
val compressed = if (attachScreenshot) {
- com.gotcha.tools.ScreenPerception.compressScreenshot(maxDimension = 1024, quality = 85)
+ showingActivity(com.gotcha.ui.BallActivity.ACTING) {
+ com.gotcha.tools.ScreenPerception.compressScreenshot(
+ maxDimension = 1024,
+ quality = 85
+ )
+ }
} else {
null
}
@@ -234,7 +239,9 @@ class AssistiveBallService : Service() {
)
history.add(userMsg)
- val response = llmClient.chat(history.toList())
+ val response = showingActivity(com.gotcha.ui.BallActivity.THINKING) {
+ llmClient.chat(history.toList())
+ }
val replyText = response.choices.firstOrNull()?.message?.textContent ?: "No response"
history.add(
com.gotcha.llm.ChatMessage(
@@ -259,7 +266,9 @@ class AssistiveBallService : Service() {
overlay.isPanelOpen = true
screenCompanionPanel.updateResponse("Fetching $url …")
scope.launch {
- val fetched = withContext(Dispatchers.IO) { webFetchTool.fetch(url, "text") }
+ val fetched = showingActivity(com.gotcha.ui.BallActivity.ACTING) {
+ withContext(Dispatchers.IO) { webFetchTool.fetch(url, "text") }
+ }
if (!fetched.success) {
screenCompanionPanel.updateResponse("Couldn't fetch the link: ${fetched.message}")
return@launch
@@ -284,7 +293,9 @@ class AssistiveBallService : Service() {
)
)
)
- val response = llmClient.chat(history.toList())
+ val response = showingActivity(com.gotcha.ui.BallActivity.THINKING) {
+ llmClient.chat(history.toList())
+ }
val replyText = response.choices.firstOrNull()?.message?.textContent ?: "No response"
history.add(
com.gotcha.llm.ChatMessage("assistant", kotlinx.serialization.json.JsonPrimitive(replyText))
@@ -517,7 +528,9 @@ class AssistiveBallService : Service() {
val currentHistory = activeCompanionHistory.toList()
scope.launch {
try {
- val response = llmClient.chat(currentHistory)
+ val response = showingActivity(com.gotcha.ui.BallActivity.THINKING) {
+ llmClient.chat(currentHistory)
+ }
val replyText = response.choices.firstOrNull()?.message?.textContent ?: "No response"
activeCompanionHistory.add(
com.gotcha.llm.ChatMessage("assistant", kotlinx.serialization.json.JsonPrimitive(replyText))
@@ -537,8 +550,10 @@ class AssistiveBallService : Service() {
com.google.mlkit.vision.text.latin.TextRecognizerOptions.DEFAULT_OPTIONS
)
val image = com.google.mlkit.vision.common.InputImage.fromBitmap(bitmap, 0)
- val result = withContext(Dispatchers.Default) {
- com.google.android.gms.tasks.Tasks.await(recognizer.process(image))
+ val result = showingActivity(com.gotcha.ui.BallActivity.ACTING) {
+ withContext(Dispatchers.Default) {
+ com.google.android.gms.tasks.Tasks.await(recognizer.process(image))
+ }
}
val text = result.text.trim()
if (text.isBlank()) {
@@ -556,6 +571,35 @@ class AssistiveBallService : Service() {
}
}
+ /**
+ * Run [block] with the ball showing [state].
+ *
+ * The ball is the only piece of Gotcha on screen while the companion panel
+ * is working, and until now it sat perfectly still through every model call
+ * — the in-app indicator breathed and its counterpart over other apps did
+ * not. This is what tells it when to.
+ *
+ * Ref-counted because these overlap: a Lens crop can be summarising while a
+ * clipboard action fetches. The last one out turns the ring off, so a
+ * finishing call cannot clear a ring that another is still using.
+ */
+ private suspend fun <T> showingActivity(
+ state: com.gotcha.ui.BallActivity,
+ block: suspend () -> T
+ ): T {
+ activityDepth.incrementAndGet()
+ overlay.setActivity(state)
+ try {
+ return block()
+ } finally {
+ if (activityDepth.decrementAndGet() <= 0) {
+ overlay.setActivity(com.gotcha.ui.BallActivity.IDLE)
+ }
+ }
+ }
+
+ private val activityDepth = java.util.concurrent.atomic.AtomicInteger(0)
+
private fun showToast(msg: String) {
android.os.Handler(android.os.Looper.getMainLooper()).post {
android.widget.Toast.makeText(this, msg, android.widget.Toast.LENGTH_SHORT).show()
@@ -595,7 +639,9 @@ class AssistiveBallService : Service() {
updateResponse("Thinking...")
scope.launch {
try {
- val response = llmClient.chat(currentHistory)
+ val response = showingActivity(com.gotcha.ui.BallActivity.THINKING) {
+ llmClient.chat(currentHistory)
+ }
val replyText = response.choices.firstOrNull()?.message?.textContent ?: "No response"
activeCompanionHistory.add(
com.gotcha.llm.ChatMessage(
diff --git a/app/src/main/java/com/gotcha/ui/AssistiveBallOverlay.kt b/app/src/main/java/com/gotcha/ui/AssistiveBallOverlay.kt
index 7f5bcda..c39d399 100644
--- a/app/src/main/java/com/gotcha/ui/AssistiveBallOverlay.kt
+++ b/app/src/main/java/com/gotcha/ui/AssistiveBallOverlay.kt
@@ -27,6 +27,7 @@ import com.gotcha.service.ProactiveSessionItem
import com.gotcha.service.SmartActionDetector
import com.gotcha.ui.theme.OverlaySkin
import com.gotcha.ui.theme.Skins
+import com.gotcha.ui.theme.animationsEnabled
import com.gotcha.ui.theme.overlaySkin
import kotlin.math.abs
import kotlin.math.hypot
@@ -34,6 +35,24 @@ import kotlin.math.hypot
/** Accessibility label for the ball root view — used by UiAutomator to find the overlay. */
const val ASSISTIVE_BALL_CONTENT_DESCRIPTION = "Gotcha assistive ball"
+/**
+ * What the agent is doing, as far as the ball is concerned.
+ *
+ * Deliberately coarser than the call window's [com.gotcha.service.CallState]:
+ * a 56dp disc at the edge of somebody else's screen can carry three states
+ * legibly and no more.
+ */
+enum class BallActivity {
+ /** Nothing running. No ring. */
+ IDLE,
+
+ /** Waiting on a model. */
+ THINKING,
+
+ /** Running a tool — reaching into the device. */
+ ACTING
+}
+
@Suppress("TooManyFunctions", "LargeClass", "MaxLineLength", "ComplexCondition")
class AssistiveBallOverlay(context: Context) {
@@ -71,6 +90,11 @@ class AssistiveBallOverlay(context: Context) {
private var cardView: View? = null
private var dismissTargetView: View? = null
+ private var statusRingView: View? = null
+ private var statusRingDrawable: RingDrawable? = null
+ private var statusRingAnimator: ValueAnimator? = null
+ private var activity: BallActivity = BallActivity.IDLE
+
private var dockSide: Int = DOCK_SIDE_END
private val ballParams: WindowManager.LayoutParams by lazy { ballLayoutParams() }
private val settingsRepository by lazy { SettingsRepository(appContext) }
@@ -100,6 +124,7 @@ class AssistiveBallOverlay(context: Context) {
ballView?.visibility = visibility
if (visible) ballView?.alpha = PEEK_ALPHA
ringView?.visibility = visibility
+ statusRingView?.visibility = visibility
menuView?.visibility = visibility
dismissTargetView?.visibility = visibility
if (includeCard) cardView?.visibility = visibility
@@ -131,6 +156,7 @@ class AssistiveBallOverlay(context: Context) {
try {
windowManager.addView(ball, ballParams)
ballView = ball
+ refreshStatusRing()
} catch (_: Exception) {
ballView = null
}
@@ -140,6 +166,7 @@ class AssistiveBallOverlay(context: Context) {
fun dismiss() {
mainHandler.post {
removeLongPressRing()
+ removeStatusRing()
removeMenu()
removeCard()
removeDismissTarget()
@@ -515,6 +542,120 @@ class AssistiveBallOverlay(context: Context) {
cardView = null
}
+ /**
+ * Tell the ball what the agent is doing.
+ *
+ * The in-app indicator breathes; the ball is the same object in another
+ * place and should breathe with it. Same 900ms reverse, same 0.32→1.0 —
+ * see `ActivityPulse` in ChatComponents.kt. Two indicators pulsing at
+ * different rates would read as two different things happening.
+ */
+ fun setActivity(state: BallActivity) {
+ mainHandler.post {
+ if (state == activity) return@post
+ activity = state
+ refreshStatusRing()
+ }
+ }
+
+ private fun refreshStatusRing() {
+ removeStatusRing()
+ if (activity == BallActivity.IDLE || ballView == null) return
+
+ val density = appContext.resources.displayMetrics.density
+ val ballPx = dp(BALL_SIZE_DP)
+ val viewSize = (BALL_SIZE_DP * STATUS_RING_WINDOW_SCALE * density).toInt()
+ val accent = accentColor()
+ val acting = activity == BallActivity.ACTING
+
+ val drawable = RingDrawable().apply {
+ fillColor = Color.TRANSPARENT
+ strokeColor = accent
+ strokeWidth = STATUS_RING_STROKE_DP * density
+ ringRadius = ballPx * STATUS_RING_RADIUS
+ // Acting reaches into the device, so it carries a halo as well as a
+ // ring. Thinking is only waiting, and gets the ring alone.
+ if (acting) {
+ auraColor = accent
+ auraRadius = ballPx * STATUS_AURA_RADIUS
+ }
+ }
+
+ val view = View(appContext).apply { background = drawable }
+ val params = statusRingLayoutParams(viewSize)
+ try {
+ windowManager.addView(view, params)
+ statusRingView = view
+ statusRingDrawable = drawable
+ } catch (_: Exception) {
+ statusRingView = null
+ statusRingDrawable = null
+ return
+ }
+
+ if (!animationsEnabled(appContext)) {
+ // "Remove animations" is a system-wide preference, and a floating
+ // window over every other app is the last place to ignore it. The
+ // state still shows — it just holds still.
+ drawable.strokeColor = accent
+ if (acting) drawable.auraIntensity = PULSE_MAX * AURA_SHARE
+ return
+ }
+
+ statusRingAnimator = ValueAnimator.ofFloat(PULSE_MIN, PULSE_MAX).apply {
+ duration = PULSE_MS
+ repeatMode = ValueAnimator.REVERSE
+ repeatCount = ValueAnimator.INFINITE
+ interpolator = AccelerateDecelerateInterpolator()
+ addUpdateListener { anim ->
+ val p = anim.animatedValue as Float
+ drawable.strokeColor = ColorUtils.setAlphaComponent(accent, (p * 255).toInt())
+ if (acting) drawable.auraIntensity = p * AURA_SHARE
+ }
+ start()
+ }
+ }
+
+ private fun statusRingLayoutParams(viewSize: Int): WindowManager.LayoutParams =
+ WindowManager.LayoutParams(
+ viewSize,
+ viewSize,
+ overlayType(),
+ WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
+ WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE or
+ WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
+ PixelFormat.TRANSLUCENT
+ ).apply {
+ gravity = Gravity.TOP or Gravity.START
+ val ballPx = dp(BALL_SIZE_DP)
+ x = ballParams.x + ballPx / 2 - viewSize / 2
+ y = ballParams.y + ballPx / 2 - viewSize / 2
+ }
+
+ /**
+ * Keep the ring on the ball. Called from every place the ball moves —
+ * drag, dock, expand, clamp — because the ring is its own window and does
+ * not come along on its own.
+ */
+ private fun followBallWithStatusRing() {
+ val view = statusRingView ?: return
+ val params = view.layoutParams as? WindowManager.LayoutParams ?: return
+ val ballPx = dp(BALL_SIZE_DP)
+ params.x = ballParams.x + ballPx / 2 - params.width / 2
+ params.y = ballParams.y + ballPx / 2 - params.height / 2
+ try {
+ windowManager.updateViewLayout(view, params)
+ } catch (_: Exception) { }
+ }
+
+ private fun removeStatusRing() {
+ statusRingAnimator?.cancel()
+ statusRingAnimator = null
+ statusRingView?.let { safeRemove(it) }
+ statusRingView = null
+ statusRingDrawable = null
+ }
+
private fun showLongPressRing() {
removeLongPressRing()
val density = appContext.resources.displayMetrics.density
@@ -623,6 +764,7 @@ class AssistiveBallOverlay(context: Context) {
windowManager.updateViewLayout(ballView, ballParams)
ballView?.requestFocus()
} catch (_: Exception) { }
+ followBallWithStatusRing()
updateDismissTargetHighlight(isOverDismissTarget())
}
true
@@ -675,6 +817,7 @@ class AssistiveBallOverlay(context: Context) {
try {
windowManager.updateViewLayout(ballView, ballParams)
} catch (_: Exception) { }
+ followBallWithStatusRing()
}
private fun screenWidth(): Int = appContext.resources.displayMetrics.widthPixels
@@ -701,6 +844,7 @@ class AssistiveBallOverlay(context: Context) {
try {
windowManager.updateViewLayout(view, ballParams)
} catch (_: Exception) { }
+ followBallWithStatusRing()
ValueAnimator.ofFloat(view.alpha, 1.0f).apply {
duration = 180L
interpolator = AccelerateDecelerateInterpolator()
@@ -725,6 +869,7 @@ class AssistiveBallOverlay(context: Context) {
try {
windowManager.updateViewLayout(view, ballParams)
} catch (_: Exception) { }
+ followBallWithStatusRing()
view.alpha = startAlpha + (PEEK_ALPHA - startAlpha) * p
}
start()
@@ -877,6 +1022,22 @@ class AssistiveBallOverlay(context: Context) {
const val MENU_ICON_DP = 18
const val MENU_ICON_GAP_DP = 12
+ // The status ring. The pulse is ActivityPulse's, to the millisecond.
+ const val PULSE_MS = 900L
+ const val PULSE_MIN = 0.32f
+ const val PULSE_MAX = 1f
+
+ /** The aura is the quieter half of the pair; the ring carries the read. */
+ const val AURA_SHARE = 0.45f
+
+ /** Just outside the disc, which is inset from the 56dp window. */
+ const val STATUS_RING_RADIUS = 0.54f
+ const val STATUS_RING_STROKE_DP = 2f
+ const val STATUS_AURA_RADIUS = 1.15f
+
+ /** Room for the aura to fade out as a circle inside a square window. */
+ const val STATUS_RING_WINDOW_SCALE = 2.6f
+
const val LONG_PRESS_START_MS = 2000L
const val LONG_PRESS_END_MS = 2000L
const val DISMISS_TARGET_SIZE_DP = 64
diff --git a/app/src/main/java/com/gotcha/ui/theme/Motion.kt b/app/src/main/java/com/gotcha/ui/theme/Motion.kt
index 6ff6b1e..be746f6 100644
--- a/app/src/main/java/com/gotcha/ui/theme/Motion.kt
+++ b/app/src/main/java/com/gotcha/ui/theme/Motion.kt
@@ -1,5 +1,6 @@
package com.gotcha.ui.theme
+import android.content.Context
import android.provider.Settings
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.FiniteAnimationSpec
@@ -26,17 +27,29 @@ const val SKIN_TRANSITION_MS = 320
*/
val LocalAnimationsEnabled = staticCompositionLocalOf { true }
-@Composable
-fun rememberAnimationsEnabled(): Boolean {
- val context = LocalContext.current
- return remember(context) {
- val scale = Settings.Global.getFloat(
+/**
+ * The same question, asked from View code.
+ *
+ * The overlays are not Compose and cannot use [rememberAnimationsEnabled], but
+ * "remove animations" is a system-wide preference and an animation that keeps
+ * running in a floating window over every other app is the worst possible place
+ * to have missed it. One read, one meaning, two callers.
+ */
+fun animationsEnabled(context: Context): Boolean {
+ val scale = runCatching {
+ Settings.Global.getFloat(
context.contentResolver,
Settings.Global.ANIMATOR_DURATION_SCALE,
1f
)
- scale > 0f
- }
+ }.getOrDefault(1f)
+ return scale > 0f
+}
+
+@Composable
+fun rememberAnimationsEnabled(): Boolean {
+ val context = LocalContext.current
+ return remember(context) { animationsEnabled(context) }
}
/** [tween] when animation is wanted, [snap] when it is not. */
diff --git a/app/src/test/java/com/gotcha/ui/theme/MotionTest.kt b/app/src/test/java/com/gotcha/ui/theme/MotionTest.kt
new file mode 100644
index 0000000..4c48ef1
--- /dev/null
+++ b/app/src/test/java/com/gotcha/ui/theme/MotionTest.kt
@@ -0,0 +1,58 @@
+package com.gotcha.ui.theme
+
+import android.content.Context
+import android.provider.Settings
+import androidx.test.core.app.ApplicationProvider
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+
+/**
+ * The accessibility "Remove animations" preference reaches the app as an
+ * animator duration scale of zero. Compose does not consult it for us, and
+ * neither does the View layer, so [animationsEnabled] is the one place that
+ * asks — for the chat screen through [rememberAnimationsEnabled], and for the
+ * overlays directly, because a floating window that keeps pulsing over every
+ * other app is the worst possible place to have missed it.
+ */
+@RunWith(RobolectricTestRunner::class)
+class MotionTest {
+
+ private val context = ApplicationProvider.getApplicationContext<Context>()
+
+ private fun setScale(scale: Float) {
+ Settings.Global.putFloat(
+ context.contentResolver,
+ Settings.Global.ANIMATOR_DURATION_SCALE,
+ scale
+ )
+ }
+
+ @Test
+ fun `a zero duration scale means the user asked for no animation`() {
+ setScale(0f)
+ assertFalse(animationsEnabled(context))
+ }
+
+ @Test
+ fun `any positive scale means animation is wanted`() {
+ setScale(1f)
+ assertTrue(animationsEnabled(context))
+ setScale(0.5f)
+ assertTrue(animationsEnabled(context))
+ setScale(10f)
+ assertTrue(animationsEnabled(context))
+ }
+
+ /**
+ * Unset on plenty of devices. Defaulting to *off* would silently strip the
+ * motion from every one of them, which is the more expensive way to be
+ * wrong than defaulting to on.
+ */
+ @Test
+ fun `an unset scale animates`() {
+ assertTrue(animationsEnabled(context))
+ }
+}