A tiny friend in your Mac's notch and on your iPhone that keeps an eye on your AI coding agents: Claude Code, Codex, Cursor, Gemini CLI, Antigravity and more. Approve from the notch or your Lock Screen.
Swift ai-agentsanthropicantigravityclaudeclaude-codecodexcursordynamic-islandgemini-cliiosiphonlinuxmacosmacos-appmenubar-appnotchopen-sourceswiftswiftuiwindows
Android Widgets: preview every state without a computer
Claude6ba797778a0b
diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.ktsindex b9e0bcfe..aeea3775 100644--- a/android/app/build.gradle.kts+++ b/android/app/build.gradle.kts@@ -57,6 +57,7 @@ android {}kotlinOptions { jvmTarget = "17" }sourceSets["main"].java.srcDirs("src/main/kotlin")+ sourceSets["debug"].java.srcDirs("src/debug/kotlin") // the Widgets preview receiver: debug builds onlysourceSets["test"].java.srcDirs("src/test/kotlin")}diff --git a/android/app/src/debug/AndroidManifest.xml b/android/app/src/debug/AndroidManifest.xmlnew file mode 100644index 00000000..3fd94d58--- /dev/null+++ b/android/app/src/debug/AndroidManifest.xml@@ -0,0 +1,12 @@+<?xml version="1.0" encoding="utf-8"?>+<!-- Debug builds only: this file is merged into the debug variant and into no other, so a release build has no preview receiver. -->+<manifest xmlns:android="http://schemas.android.com/apk/res/android">+ <application>+ <!-- Exported so that `adb shell am broadcast` can reach it. It only redraws the Widgets with a made-up state. -->+ <receiver android:name=".app.WidgetPreviewReceiver" android:exported="true">+ <intent-filter>+ <action android:name="com.coucou.android.debug.WIDGET_STATE" />+ </intent-filter>+ </receiver>+ </application>+</manifest>diff --git a/android/app/src/debug/kotlin/com/coucou/android/app/WidgetPreviewReceiver.kt b/android/app/src/debug/kotlin/com/coucou/android/app/WidgetPreviewReceiver.ktnew file mode 100644index 00000000..96790478--- /dev/null+++ b/android/app/src/debug/kotlin/com/coucou/android/app/WidgetPreviewReceiver.kt@@ -0,0 +1,39 @@+package com.coucou.android.app++import android.content.BroadcastReceiver+import android.content.Context+import android.content.Intent+import android.util.Log+import com.coucou.android.core.WidgetData+import com.coucou.android.core.WidgetSample++/**+ * DEBUG BUILDS ONLY (this file lives in src/debug): look at every Widget in every state without a computer.+ *+ * Put the Widgets on the home screen, then, from a computer with the phone connected:+ *+ * adb shell am broadcast -n com.coucou.android/.app.WidgetPreviewReceiver -a com.coucou.android.debug.WIDGET_STATE --es state disconnected+ * adb shell am broadcast -n com.coucou.android/.app.WidgetPreviewReceiver -a com.coucou.android.debug.WIDGET_STATE --es state empty+ * adb shell am broadcast -n com.coucou.android/.app.WidgetPreviewReceiver -a com.coucou.android.debug.WIDGET_STATE --es state working+ * adb shell am broadcast -n com.coucou.android/.app.WidgetPreviewReceiver -a com.coucou.android.debug.WIDGET_STATE --es state waiting+ * adb shell am broadcast -n com.coucou.android/.app.WidgetPreviewReceiver -a com.coucou.android.debug.WIDGET_STATE --es state done+ * adb shell am broadcast -n com.coucou.android/.app.WidgetPreviewReceiver -a com.coucou.android.debug.WIDGET_STATE --es state live+ *+ * `live` goes back to the real state. A chosen state stays on the Widgets (real changes wait) until `live` or until the app+ * process is killed. The states are made up by [WidgetSample]; nothing is read from, or sent to, a computer.+ */+class WidgetPreviewReceiver : BroadcastReceiver() {+ override fun onReceive(context: Context, intent: Intent) {+ val key = intent.getStringExtra("state")?.trim()?.lowercase()+ if (key == "live") {+ WidgetHub.preview(context, null)+ return+ }+ val sample = WidgetSample.parse(key)+ if (sample == null) {+ Log.w("CoucouWidgets", "unknown state '$key': use disconnected, empty, working, waiting, done or live")+ return+ }+ WidgetHub.preview(context, WidgetData.sample(sample, System.currentTimeMillis()))+ }+}diff --git a/android/app/src/main/kotlin/com/coucou/android/app/WidgetHub.kt b/android/app/src/main/kotlin/com/coucou/android/app/WidgetHub.ktindex 77449422..19e1df4b 100644--- a/android/app/src/main/kotlin/com/coucou/android/app/WidgetHub.kt+++ b/android/app/src/main/kotlin/com/coucou/android/app/WidgetHub.kt@@ -36,9 +36,13 @@ object WidgetHub {private var last: WidgetContent? = null+ /** A sample pinned by the debug build's preview receiver (null in a release build, and in a debug one until asked). */+ private var pinned: WidgetContent? = null+/** Pushes [content] to every Widget of the family on the home screen (none: nothing happens). */@Synchronizedfun update(context: Context, content: WidgetContent) {+ if (pinned != null && content != pinned) return // a preview is on screen: real pushes waitif (content == last) returnrunCatching {val manager = AppWidgetManager.getInstance(context)@@ -93,8 +97,19 @@ object WidgetHub {WidgetTone.TROUBLE -> R.drawable.widget_tint_trouble}- /** What the app says now. */- private fun current(context: Context): WidgetContent = (context.applicationContext as CoucouApp).model.widgetContent()+ /** What the Widgets show now: the pinned preview if there is one, else what the app says. */+ private fun current(context: Context): WidgetContent = pinned ?: (context.applicationContext as CoucouApp).model.widgetContent()++ /**+ * Shows [content] on every Widget and keeps it there (real pushes wait), or with null goes back to the real thing. Only the+ * debug build calls this, from its preview receiver; nothing in a release build does.+ */+ @Synchronized+ fun preview(context: Context, content: WidgetContent?) {+ pinned = content+ last = null+ update(context, current(context))+ }/** Draws every Widget again from what the app says now, even if nothing looks different (a new day, a new time zone). */@Synchronizeddiff --git a/android/app/src/main/kotlin/com/coucou/android/core/WidgetData.kt b/android/app/src/main/kotlin/com/coucou/android/core/WidgetData.ktindex 8a6a5ec1..aed7b499 100644--- a/android/app/src/main/kotlin/com/coucou/android/core/WidgetData.kt+++ b/android/app/src/main/kotlin/com/coucou/android/core/WidgetData.kt@@ -1,5 +1,8 @@package com.coucou.android.core+import com.coucou.android.link.LinkState+import com.coucou.android.link.PlanUsage+import com.coucou.android.link.PlanWindowimport com.coucou.android.link.SessionInfoimport com.coucou.android.link.UsageSnapshotimport com.coucou.android.mochi.BotState@@ -135,6 +138,24 @@ data class WidgetContent(val mood: BotState get() = top?.state ?: if (connected) BotState.IDLE else BotState.SLEEPING}+/**+ * A made-up state to look at every Widget without a computer: the debug build can pin one of these (see the preview receiver in+ * the debug source set). They go through the same [WidgetData.build] as the real thing, so a preview cannot drift from it.+ */+enum class WidgetSample(val key: String) {+ DISCONNECTED("disconnected"),+ EMPTY("empty"),+ WORKING("working"),+ WAITING("waiting"),+ DONE("done"),+ ;++ companion object {+ /** The sample called [key] (any case, spaces around ignored); null for anything else. */+ fun parse(key: String?): WidgetSample? = values().firstOrNull { it.key == key?.trim()?.lowercase() }+ }+}+object WidgetData {/** Rows on the List widget. */const val MAX_ROWS = 5@@ -156,6 +177,47 @@ object WidgetData {return WidgetContent(glance.headline, glance.tone, glance.connected, glance.waiting, tiles, ring, today.coerceAtLeast(0))}+ /** The content a [WidgetSample] stands for, built like a real one. [nowMs] only sets when the sample plan windows reset. */+ fun sample(which: WidgetSample, nowMs: Long = 0L): WidgetContent {+ val link = if (which == WidgetSample.DISCONNECTED) LinkState.DISCONNECTED else LinkState.CONNECTED+ val sessions = when (which) {+ WidgetSample.DISCONNECTED, WidgetSample.EMPTY -> emptyList()+ WidgetSample.WORKING -> listOf(+ SessionInfo("integration_claude", "Claude Code", BotState.WORKING, "", 0, 0, 0L),+ SessionInfo("agent_codex", "Codex", BotState.THINKING, "", 0, 0, 0L),+ SessionInfo("agent_gemini", "Gemini CLI", BotState.IDLE, "", 0, 0, 0L),+ )+ WidgetSample.WAITING -> listOf(+ SessionInfo("integration_claude", "Claude Code", BotState.APPROVAL, "", 0, 0, 0L),+ SessionInfo("agent_codex", "Codex", BotState.WORKING, "", 0, 0, 0L),+ SessionInfo("agent_gemini", "Gemini CLI", BotState.FINISHED, "", 0, 0, 0L),+ )+ WidgetSample.DONE -> listOf(+ SessionInfo("integration_claude", "Claude Code", BotState.FINISHED, "", 0, 0, 0L),+ SessionInfo("agent_codex", "Codex", BotState.FINISHED, "", 0, 0, 0L),+ )+ }+ val approvals = if (which == WidgetSample.WAITING) 1 else 0+ val names = sessions.associate { it.pillId to it.agent }+ val glance = GlanceBuilder.build(true, link, "Your PC", sessions, approvals) { names[it] ?: "An agent" }+ fun plan(five: Int, week: Int) = PlanUsage(PlanWindow(five, nowMs + 2 * HOUR_MS), PlanWindow(week, nowMs + 3 * 24 * HOUR_MS))+ val usage = when (which) {+ WidgetSample.WORKING -> UsageSnapshot(plan(42, 18), null)+ WidgetSample.WAITING -> UsageSnapshot(plan(83, 61), null)+ WidgetSample.DONE -> UsageSnapshot(plan(24, 9), null)+ // No plan usage on these two, so the Usage Ring shows how it asks for the switch.+ WidgetSample.DISCONNECTED, WidgetSample.EMPTY -> null+ }+ val today = when (which) {+ WidgetSample.DISCONNECTED, WidgetSample.EMPTY -> 0+ WidgetSample.WORKING, WidgetSample.WAITING -> 3+ WidgetSample.DONE -> 5+ }+ return build(glance, sessions, { names[it] ?: "An agent" }, usage, today, nowMs)+ }++ private const val HOUR_MS = 3_600_000L+/** Claude's five-hour window first, then its week, then Codex; null when the computer sent none of them. */fun ring(usage: UsageSnapshot?, nowMs: Long): UsageRing? {usage ?: return nulldiff --git a/android/app/src/test/kotlin/com/coucou/android/WidgetSampleTest.kt b/android/app/src/test/kotlin/com/coucou/android/WidgetSampleTest.ktnew file mode 100644index 00000000..28bae42b--- /dev/null+++ b/android/app/src/test/kotlin/com/coucou/android/WidgetSampleTest.kt@@ -0,0 +1,103 @@+package com.coucou.android++import com.coucou.android.core.WidgetData+import com.coucou.android.core.WidgetSample+import com.coucou.android.core.WidgetTone+import com.coucou.android.mochi.BotState+import org.junit.Assert.assertEquals+import org.junit.Assert.assertFalse+import org.junit.Assert.assertNotNull+import org.junit.Assert.assertNull+import org.junit.Assert.assertTrue+import org.junit.Test++/** The made-up states used to look at every Widget without a computer. Pure, so each one is pinned down here. */+class WidgetSampleTest {+ private val now = 5_000_000L+ private fun sample(s: WidgetSample) = WidgetData.sample(s, now)++ @Test fun disconnected() {+ val c = sample(WidgetSample.DISCONNECTED)+ assertFalse(c.connected)+ assertTrue(c.tiles.isEmpty())+ assertEquals(WidgetTone.MUTED, c.look)+ assertEquals("Waiting for your computer", c.emptyLine)+ assertEquals(BotState.SLEEPING, c.mood)+ assertEquals("Not connected", c.alertLine)+ assertNull(c.ring)+ assertEquals("Not connected", c.headline)+ }++ @Test fun empty() {+ val c = sample(WidgetSample.EMPTY)+ assertTrue(c.connected)+ assertTrue(c.tiles.isEmpty())+ assertEquals("No agents running", c.emptyLine)+ assertEquals("No agents running", c.bubble)+ assertEquals(BotState.IDLE, c.mood)+ assertEquals("All calm", c.alertLine)+ assertNull("plan usage is off, so the ring asks for the switch", c.ring)+ }++ @Test fun working() {+ val c = sample(WidgetSample.WORKING)+ assertEquals(WidgetTone.WORKING, c.look)+ assertEquals("Claude Code", c.top!!.name)+ assertEquals("Claude Code is working", c.bubble)+ assertEquals(3, c.tiles.size)+ assertEquals(0, c.waiting)+ assertEquals("Claude", c.ring!!.plan)+ assertEquals(42, c.ring!!.pct)+ assertEquals(WidgetTone.DONE, c.ring!!.tone)+ assertEquals(3, c.today)+ }++ @Test fun waiting() {+ val c = sample(WidgetSample.WAITING)+ assertEquals(WidgetTone.ATTENTION, c.look)+ assertEquals(1, c.waiting)+ assertEquals("1 waiting", c.alertLine)+ assertEquals("Claude Code", c.alertSub)+ assertEquals("Claude Code is waiting for you", c.bubble)+ assertEquals(BotState.APPROVAL, c.mood)+ assertEquals("a request on top, then the others by urgency", listOf("Claude Code", "Codex", "Gemini CLI"), c.tiles.map { it.name })+ assertEquals(WidgetTone.TROUBLE, c.ring!!.tone)+ }++ @Test fun done() {+ val c = sample(WidgetSample.DONE)+ assertEquals(WidgetTone.DONE, c.look)+ assertEquals("Claude Code is done", c.bubble)+ assertEquals(5, c.today)+ assertEquals(BotState.FINISHED, c.todayMood)+ assertEquals(WidgetTone.DONE, c.todayLook)+ assertEquals(24, c.ring!!.pct)+ }++ @Test fun everyStateLooksDifferentAndHasSomethingToShow() {+ val all = WidgetSample.values().map { sample(it) }+ assertEquals(5, all.toSet().size)+ for (c in all) {+ assertTrue(c.headline.isNotBlank())+ assertTrue(c.bubble.isNotBlank())+ assertTrue(c.alertLine.isNotBlank())+ assertTrue(c.alertSub.isNotBlank())+ assertTrue(c.tiles.size <= WidgetData.MAX_ROWS)+ }+ // The four tones that matter are all on show somewhere.+ assertEquals(setOf(WidgetTone.MUTED, WidgetTone.WORKING, WidgetTone.ATTENTION, WidgetTone.DONE), all.map { it.look }.toSet())+ }++ @Test fun theSamePreviewTwiceIsTheSameContentSoAPushCanBeSkipped() {+ for (s in WidgetSample.values()) assertEquals(sample(s), sample(s))+ }++ @Test fun theKeysAreWhatTheBroadcastSays() {+ assertEquals(listOf("disconnected", "empty", "working", "waiting", "done"), WidgetSample.values().map { it.key })+ assertEquals(WidgetSample.WAITING, WidgetSample.parse("waiting"))+ assertEquals(WidgetSample.WAITING, WidgetSample.parse(" Waiting "))+ assertEquals(WidgetSample.DONE, WidgetSample.parse("DONE"))+ for (bad in listOf(null, "", "live", "sleeping", "waiting now")) assertNull(bad, WidgetSample.parse(bad))+ assertNotNull(WidgetSample.parse("empty"))+ }+}diff --git a/android/app/src/test/kotlin/com/coucou/android/WidgetSurfacesTest.kt b/android/app/src/test/kotlin/com/coucou/android/WidgetSurfacesTest.ktindex 24ba427e..4cd8c1f8 100644--- a/android/app/src/test/kotlin/com/coucou/android/WidgetSurfacesTest.kt+++ b/android/app/src/test/kotlin/com/coucou/android/WidgetSurfacesTest.kt@@ -141,4 +141,44 @@ class WidgetSurfacesTest {assertTrue(solo.contains("""android:layout_weight="1""""))assertTrue(read(main + "app/SoloWidget.kt").contains("glow = true"))}++ // ── The debug preview: it must exist in debug builds and nowhere else ──────────────────────────++ @Test fun thePreviewReceiverLivesInTheDebugSourceSetOnly() {+ assertTrue(File("src/debug/kotlin/com/coucou/android/app/WidgetPreviewReceiver.kt").exists())+ val debugManifest = read("src/debug/AndroidManifest.xml")+ assertTrue(debugManifest.contains(".app.WidgetPreviewReceiver"))+ assertTrue(debugManifest.contains("com.coucou.android.debug.WIDGET_STATE"))+ // Not in the main source set, not in the manifest every build gets.+ assertFalse(File(main + "app/WidgetPreviewReceiver.kt").exists())+ assertFalse(manifest.contains("WidgetPreviewReceiver"))+ assertFalse(manifest.contains("com.coucou.android.debug"))+ assertFalse(File("src/release").exists() && File("src/release").walkTopDown().any { it.readText().contains("WidgetPreviewReceiver") })+ // Nothing in the main code calls it, and nothing outside the debug receiver pins a preview.+ for (f in File(main).walkTopDown().filter { it.extension == "kt" }) {+ val text = f.readText()+ assertFalse("${f.name} refers to the preview receiver", text.contains("WidgetPreviewReceiver") && f.name != "WidgetHub.kt")+ if (f.name != "WidgetHub.kt") assertFalse("${f.name} pins a preview", text.contains("WidgetHub.preview("))+ }+ assertTrue(read("build.gradle.kts").contains("""sourceSets["debug"]"""))+ }++ @Test fun thePreviewReceiverDocumentsEveryCommand() {+ val src = read("src/debug/kotlin/com/coucou/android/app/WidgetPreviewReceiver.kt")+ for (state in listOf("disconnected", "empty", "working", "waiting", "done", "live")) {+ assertTrue(state, src.contains("--es state $state"))+ }+ assertTrue(src.contains("adb shell am broadcast -n com.coucou.android/.app.WidgetPreviewReceiver -a com.coucou.android.debug.WIDGET_STATE"))+ assertTrue(src.contains("DEBUG BUILDS ONLY"))+ // It only redraws Widgets with a made-up state.+ assertTrue(src.contains("WidgetSample.parse"))+ for (word in listOf("PendingLaunch", "approve", "AppModel", "SecureStore")) assertFalse(word, src.contains(word))+ }++ @Test fun aPreviewWaitsForNothingElseAndLiveGoesBack() {+ val hub = sources.getValue("app/WidgetHub.kt")+ assertTrue(hub.contains("fun preview(context: Context, content: WidgetContent?)"))+ assertTrue(hub.contains("pinned = content"))+ assertTrue(hub.contains("if (pinned != null && content != pinned) return"))+ }}