Skip to content

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.

Android: Solo and List widgets

Claudec2e611989616
diff --git a/android/README.md b/android/README.md
index 4c35c8f5..4472b0fb 100644
--- a/android/README.md
+++ b/android/README.md
@@ -58,6 +58,7 @@ The desktop side is in `windows/src-tauri/src/phone_link`; the protocol is `docs
- **Away from home Wi-Fi** (optional): through your own Cloudflare relay, end-to-end encrypted; the direct link is always tried first.
- **Keeping the link alive with the screen off**: an alarm every few minutes checks that the computer still answers and reconnects if not (Doze allows nothing more); Home says when and why the link was lost; Settings > Computer > *Can't connect?* checks the network, address, discovery, certificate, pairing code and relay; a calm hint suggests the battery setting ("Unrestricted", with the Samsung steps) only if it would help. Details and limits: `docs/ANDROID_LINK.md`, "Keeping the link alive".
- **Widget, quick-settings tile, ongoing notification**: agent names and state words only. Updated when something changes, never on a timer.
+- **Widgets**: three to pick from on the home screen. *Coucou for Android* (the summary), *Coucou Solo* (Mochi in the state of your most urgent agent, with its name and a state word) and *Coucou List* (one row per agent, up to five, with a badge for what waits for you). A tap on Solo or on a row opens that agent in the app; a request is still decided in the app, behind the fingerprint, never from a widget. They show agent names, state words and colours only, are redrawn when something changes (`updatePeriodMillis` stays 0) and a redraw that would look the same is skipped. Code: `core/WidgetData.kt` (pure, tested), `app/WidgetHub.kt`, `app/SoloWidget.kt`, `app/ListWidget.kt`, `app/MochiBitmap.kt`.
- Mochi and his 11 outfits, sounds, wardrobe (Settings), a tap on Mochi slaps him, a long press pets him.
## Getting the phone link running on Windows
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index 4b4a69b1..b57d633c 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -74,6 +74,20 @@
<meta-data android:name="android.appwidget.provider" android:resource="@xml/coucou_widget_info" />
</receiver>
+ <!-- The Solo and List widgets: Mochi and the agents, pushed by the app when something changes (no timer). -->
+ <receiver android:name=".app.SoloWidget" android:exported="true" android:label="@string/widget_solo_label">
+ <intent-filter>
+ <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
+ </intent-filter>
+ <meta-data android:name="android.appwidget.provider" android:resource="@xml/coucou_solo_widget_info" />
+ </receiver>
+ <receiver android:name=".app.ListWidget" android:exported="true" android:label="@string/widget_list_label">
+ <intent-filter>
+ <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
+ </intent-filter>
+ <meta-data android:name="android.appwidget.provider" android:resource="@xml/coucou_list_widget_info" />
+ </receiver>
+
<!-- The quick-settings tile: on while the computer is connected; a tap opens Coucou. -->
<service
android:name=".app.GlanceTile"
diff --git a/android/app/src/main/kotlin/com/coucou/android/MainActivity.kt b/android/app/src/main/kotlin/com/coucou/android/MainActivity.kt
index 3570b032..0b76ded4 100644
--- a/android/app/src/main/kotlin/com/coucou/android/MainActivity.kt
+++ b/android/app/src/main/kotlin/com/coucou/android/MainActivity.kt
@@ -257,8 +257,12 @@ class MainActivity : ComponentActivity() {
i.data = null // handled once: a rotation must not ask again
}
}
- // Never read from the intent: this activity is exported, any app could send these extras. The request comes
+ // Never read from the intent: this activity is exported, any app could send these extras. The requests come
// from LaunchActivity (ours, not exported) through the model, once.
+ model.launch.takeSession()?.let { pill ->
+ // A tap on a widget row. The session screen says so itself if it has ended, and fills in when the link is back.
+ if (model.isPaired || model.mode != Mode.NONE) { detailPill = pill; screen = Screen.SESSION }
+ }
val launch = model.launch.take() ?: return
val fp = launch.fingerprint
if (launch.allow) {
diff --git a/android/app/src/main/kotlin/com/coucou/android/app/AppModel.kt b/android/app/src/main/kotlin/com/coucou/android/app/AppModel.kt
index f0529542..ede089e2 100644
--- a/android/app/src/main/kotlin/com/coucou/android/app/AppModel.kt
+++ b/android/app/src/main/kotlin/com/coucou/android/app/AppModel.kt
@@ -335,6 +335,7 @@ class AppModel(private val context: Context) : LinkListener {
val g = glance()
notifier.updateOngoing(this, g)
GlanceWidget.update(context, g)
+ WidgetHub.update(context, com.coucou.android.core.WidgetData.build(g, sessions, this::agentName))
GlanceTile.requestUpdate(context)
}
diff --git a/android/app/src/main/kotlin/com/coucou/android/app/LaunchActivity.kt b/android/app/src/main/kotlin/com/coucou/android/app/LaunchActivity.kt
index 57c29f01..43c81990 100644
--- a/android/app/src/main/kotlin/com/coucou/android/app/LaunchActivity.kt
+++ b/android/app/src/main/kotlin/com/coucou/android/app/LaunchActivity.kt
@@ -6,18 +6,27 @@ import android.os.Bundle
import com.coucou.android.MainActivity
/**
- * Where our own notification actions and the island's Allow land. It is not exported, so no other app can start it;
+ * Where our own notification actions, the island's Allow and the widget rows land. It is not exported, so no other app can start it;
* it passes the request to the app inside the process ([PendingLaunch]) and opens MainActivity with a plain intent.
*/
class LaunchActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val model = (application as CoucouApp).model
- model.launch.request(intent.getStringExtra(Notifications.EXTRA_FP), intent.getBooleanExtra(Notifications.EXTRA_ALLOW, false))
+ val session = intent.getStringExtra(EXTRA_SESSION)
+ // A widget row asks for a session to open; a notification asks for an Allow. Each has its own slot, and one
+ // never clears the other.
+ if (session != null) model.launch.requestSession(session)
+ else model.launch.request(intent.getStringExtra(Notifications.EXTRA_FP), intent.getBooleanExtra(Notifications.EXTRA_ALLOW, false))
startActivity(
Intent(this, MainActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP),
)
finish()
}
+
+ companion object {
+ /** The pill id of the session a widget row opens. */
+ const val EXTRA_SESSION = "session"
+ }
}
diff --git a/android/app/src/main/kotlin/com/coucou/android/app/ListWidget.kt b/android/app/src/main/kotlin/com/coucou/android/app/ListWidget.kt
new file mode 100644
index 00000000..1631a981
--- /dev/null
+++ b/android/app/src/main/kotlin/com/coucou/android/app/ListWidget.kt
@@ -0,0 +1,58 @@
+package com.coucou.android.app
+
+import android.appwidget.AppWidgetManager
+import android.appwidget.AppWidgetProvider
+import android.content.Context
+import android.view.View
+import android.widget.RemoteViews
+import com.coucou.android.R
+import com.coucou.android.core.WidgetContent
+
+/**
+ * The List widget: the headline, then one row per agent (a dot in its colour, its name, a state word). Drawn by
+ * [WidgetHub] when something changes (updatePeriodMillis = 0). A tap on a row opens that agent; a tap on the headline
+ * opens the app. It only opens things: a request waits for you in the app, behind the fingerprint.
+ */
+class ListWidget : AppWidgetProvider() {
+ override fun onUpdate(context: Context, manager: AppWidgetManager, ids: IntArray) =
+ WidgetHub.refresh(context, manager, ids) { c, w -> views(c, w) }
+
+ companion object {
+ private val ROWS = listOf(
+ Row(R.id.list_row0, R.id.list_dot0, R.id.list_name0, R.id.list_state0),
+ Row(R.id.list_row1, R.id.list_dot1, R.id.list_name1, R.id.list_state1),
+ Row(R.id.list_row2, R.id.list_dot2, R.id.list_name2, R.id.list_state2),
+ Row(R.id.list_row3, R.id.list_dot3, R.id.list_name3, R.id.list_state3),
+ Row(R.id.list_row4, R.id.list_dot4, R.id.list_name4, R.id.list_state4),
+ )
+
+ private class Row(val row: Int, val dot: Int, val name: Int, val state: Int)
+
+ fun views(context: Context, c: WidgetContent): RemoteViews {
+ val v = RemoteViews(context.packageName, R.layout.widget_list)
+ v.setTextViewText(R.id.list_headline, c.headline)
+ v.setTextColor(R.id.list_headline, c.tone.argb)
+ v.setOnClickPendingIntent(R.id.list_header, WidgetHub.openApp(context, WidgetHub.CODE_LIST_HEADER))
+ if (c.waiting > 0) {
+ v.setTextViewText(R.id.list_badge, c.waiting.toString())
+ v.setContentDescription(R.id.list_badge, context.getString(R.string.widget_badge_description, c.waiting))
+ v.setViewVisibility(R.id.list_badge, View.VISIBLE)
+ } else {
+ v.setViewVisibility(R.id.list_badge, View.GONE)
+ }
+ ROWS.forEachIndexed { i, r ->
+ val tile = c.tiles.getOrNull(i)
+ if (tile == null) {
+ v.setViewVisibility(r.row, View.GONE)
+ return@forEachIndexed
+ }
+ v.setViewVisibility(r.row, View.VISIBLE)
+ v.setTextColor(r.dot, tile.argb)
+ v.setTextViewText(r.name, tile.name)
+ v.setTextViewText(r.state, tile.stateWord)
+ v.setOnClickPendingIntent(r.row, WidgetHub.openSession(context, WidgetHub.CODE_LIST_ROW + i, tile.pillId))
+ }
+ return v
+ }
+ }
+}
diff --git a/android/app/src/main/kotlin/com/coucou/android/app/MochiBitmap.kt b/android/app/src/main/kotlin/com/coucou/android/app/MochiBitmap.kt
new file mode 100644
index 00000000..8f0b3953
--- /dev/null
+++ b/android/app/src/main/kotlin/com/coucou/android/app/MochiBitmap.kt
@@ -0,0 +1,53 @@
+package com.coucou.android.app
+
+import android.graphics.Bitmap
+import androidx.compose.ui.geometry.Size
+import androidx.compose.ui.graphics.Canvas
+import androidx.compose.ui.graphics.ImageBitmap
+import androidx.compose.ui.graphics.asAndroidBitmap
+import androidx.compose.ui.graphics.drawscope.CanvasDrawScope
+import androidx.compose.ui.unit.Density
+import androidx.compose.ui.unit.LayoutDirection
+import com.coucou.android.mochi.BotState
+import com.coucou.android.mochi.MochiEngine
+import com.coucou.android.mochi.MochiPainter.drawMochi
+import kotlin.random.Random
+
+/**
+ * One still picture of Mochi for a widget (a widget cannot animate): the engine is settled for a second of fake time
+ * with a fixed seed, so the same state always gives the same picture, then drawn once with the app's own painter into an
+ * [ImageBitmap]. No timer and no frame loop; a few pictures are kept so a push that changes only the text redraws nothing.
+ */
+object MochiBitmap {
+ private const val SEED = 7
+ private const val SETTLE_FRAMES = 60
+ private const val FRAME_SEC = 1.0 / 60
+ private const val KEEP = 6
+
+ private data class Key(val state: BotState, val size: Int)
+
+ private val cache = object : LinkedHashMap<Key, Bitmap>(8, 0.75f, true) {
+ override fun removeEldestEntry(eldest: MutableMap.MutableEntry<Key, Bitmap>) = size > KEEP
+ }
+
+ /** Mochi in [state], [sizePx] pixels square. The same instance comes back while it is kept. */
+ @Synchronized
+ fun of(state: BotState, sizePx: Int): Bitmap {
+ val key = Key(state, sizePx)
+ return cache.getOrPut(key) { draw(state, sizePx) }
+ }
+
+ private fun draw(state: BotState, sizePx: Int): Bitmap {
+ var ms = 0.0
+ val engine = MochiEngine({ ms }, Random(SEED)).apply { setState(state, force = true) }
+ repeat(SETTLE_FRAMES) {
+ ms += FRAME_SEC * 1000
+ engine.update(FRAME_SEC)
+ }
+ val image = ImageBitmap(sizePx, sizePx)
+ CanvasDrawScope().draw(Density(1f), LayoutDirection.Ltr, Canvas(image), Size(sizePx.toFloat(), sizePx.toFloat())) {
+ drawMochi(engine, ms / 1000)
+ }
+ return image.asAndroidBitmap()
+ }
+}
diff --git a/android/app/src/main/kotlin/com/coucou/android/app/SoloWidget.kt b/android/app/src/main/kotlin/com/coucou/android/app/SoloWidget.kt
new file mode 100644
index 00000000..c2e01379
--- /dev/null
+++ b/android/app/src/main/kotlin/com/coucou/android/app/SoloWidget.kt
@@ -0,0 +1,48 @@
+package com.coucou.android.app
+
+import android.appwidget.AppWidgetManager
+import android.appwidget.AppWidgetProvider
+import android.content.Context
+import android.view.View
+import android.widget.RemoteViews
+import com.coucou.android.R
+import com.coucou.android.core.WidgetContent
+
+/**
+ * The Solo widget: Mochi in the state of the most urgent agent, its name and one state word. Drawn by [WidgetHub] when
+ * something changes (updatePeriodMillis = 0). A tap opens that agent, or the app when none is running. It only opens things: nothing is changed from here.
+ */
+class SoloWidget : AppWidgetProvider() {
+ override fun onUpdate(context: Context, manager: AppWidgetManager, ids: IntArray) =
+ WidgetHub.refresh(context, manager, ids) { c, w -> views(c, w) }
+
+ companion object {
+ /** Mochi's picture is drawn this many dp wide; the system scales it to the widget. */
+ private const val MOCHI_DP = 96
+
+ fun views(context: Context, c: WidgetContent): RemoteViews {
+ val v = RemoteViews(context.packageName, R.layout.widget_solo)
+ val top = c.top
+ val px = (MOCHI_DP * context.resources.displayMetrics.density).toInt().coerceAtLeast(1)
+ v.setImageViewBitmap(R.id.solo_mochi, MochiBitmap.of(c.mood, px))
+ val word = top?.stateWord ?: c.headline
+ v.setContentDescription(R.id.solo_mochi, context.getString(R.string.widget_mochi_description, word.lowercase()))
+ v.setTextViewText(R.id.solo_name, top?.name ?: c.headline)
+ v.setTextColor(R.id.solo_name, top?.argb ?: c.tone.argb)
+ v.setTextViewText(R.id.solo_state, top?.stateWord.orEmpty())
+ v.setViewVisibility(R.id.solo_state, if (top == null) View.GONE else View.VISIBLE)
+ if (c.waiting > 0) {
+ v.setTextViewText(R.id.solo_badge, c.waiting.toString())
+ v.setContentDescription(R.id.solo_badge, context.getString(R.string.widget_badge_description, c.waiting))
+ v.setViewVisibility(R.id.solo_badge, View.VISIBLE)
+ } else {
+ v.setViewVisibility(R.id.solo_badge, View.GONE)
+ }
+ v.setOnClickPendingIntent(
+ R.id.solo_root,
+ if (top != null) WidgetHub.openSession(context, WidgetHub.CODE_SOLO, top.pillId) else WidgetHub.openApp(context, WidgetHub.CODE_SOLO),
+ )
+ return v
+ }
+ }
+}
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.kt
new file mode 100644
index 00000000..e2e12b79
--- /dev/null
+++ b/android/app/src/main/kotlin/com/coucou/android/app/WidgetHub.kt
@@ -0,0 +1,78 @@
+package com.coucou.android.app
+
+import android.app.PendingIntent
+import android.appwidget.AppWidgetManager
+import android.appwidget.AppWidgetProvider
+import android.content.ComponentName
+import android.content.Context
+import android.content.Intent
+import android.widget.RemoteViews
+import com.coucou.android.MainActivity
+import com.coucou.android.core.WidgetContent
+import com.coucou.android.core.WidgetData
+
+/**
+ * The one place that pushes the Solo and List widgets. The app calls [update] on every change of sessions, requests and
+ * link (from AppModel.pushGlance); a push whose content is the same as the last one is dropped, so a change that does not
+ * show on a widget costs nothing. There is no timer: a widget is drawn when something changes or when it is added.
+ */
+object WidgetHub {
+ private class Kind(val provider: Class<out AppWidgetProvider>, val views: (Context, WidgetContent) -> RemoteViews)
+
+ private val widgets = listOf(
+ Kind(SoloWidget::class.java) { c, w -> SoloWidget.views(c, w) },
+ Kind(ListWidget::class.java) { c, w -> ListWidget.views(c, w) },
+ )
+
+ private var last: WidgetContent? = null
+
+ /** Pushes [content] to every Solo and List widget on the home screen (none: nothing happens). */
+ @Synchronized
+ fun update(context: Context, content: WidgetContent) {
+ if (content == last) return
+ runCatching {
+ val manager = AppWidgetManager.getInstance(context)
+ var pushed = false
+ for (kind in widgets) {
+ val ids = manager.getAppWidgetIds(ComponentName(context, kind.provider))
+ if (ids.isEmpty()) continue
+ val rendered = kind.views(context, content)
+ for (id in ids) manager.updateAppWidget(id, rendered)
+ pushed = true
+ }
+ // With no widget on screen nothing is remembered, so the first one added is drawn.
+ if (pushed) last = content
+ }
+ }
+
+ /** Draws the widgets the system just created or resized, from what the app says now. */
+ fun refresh(context: Context, manager: AppWidgetManager, ids: IntArray, views: (Context, WidgetContent) -> RemoteViews) {
+ val model = (context.applicationContext as CoucouApp).model
+ val content = WidgetData.build(model.glance(), model.sessions, model::agentName)
+ val rendered = views(context, content)
+ for (id in ids) runCatching { manager.updateAppWidget(id, rendered) }
+ }
+
+ /** Opens the app on Home. */
+ fun openApp(context: Context, code: Int): PendingIntent {
+ val open = Intent(context, MainActivity::class.java)
+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)
+ return PendingIntent.getActivity(context, code, open, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
+ }
+
+ /**
+ * Opens one session. It goes through LaunchActivity (our own, not exported) with an explicit component, never through
+ * MainActivity's extras. Each call needs its own [code]: PendingIntents are told apart by code, not by extras.
+ */
+ fun openSession(context: Context, code: Int, pillId: String): PendingIntent {
+ val open = Intent(context, LaunchActivity::class.java)
+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+ .putExtra(LaunchActivity.EXTRA_SESSION, pillId)
+ return PendingIntent.getActivity(context, code, open, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
+ }
+
+ /** Request codes: one range per widget, one code per row, so no two PendingIntents are the same. */
+ const val CODE_SOLO = 0x5100
+ const val CODE_LIST_HEADER = 0x5200
+ const val CODE_LIST_ROW = 0x5300
+}
diff --git a/android/app/src/main/kotlin/com/coucou/android/core/Glance.kt b/android/app/src/main/kotlin/com/coucou/android/core/Glance.kt
index db020362..70389082 100644
--- a/android/app/src/main/kotlin/com/coucou/android/core/Glance.kt
+++ b/android/app/src/main/kotlin/com/coucou/android/core/Glance.kt
@@ -43,6 +43,9 @@ object GlanceBuilder {
BotState.WORKING, BotState.SEARCHING, BotState.THINKING, BotState.FINISHED, BotState.SLEEPING, BotState.IDLE,
)
+ /** Where a state sits in the list, most urgent first; unknown states go last. */
+ fun rank(state: BotState): Int = ORDER.indexOf(state).let { i -> if (i < 0) ORDER.size else i }
+
fun build(
paired: Boolean,
link: LinkState,
@@ -96,7 +99,7 @@ object GlanceBuilder {
}
/** The plain line of a session: its state in a few words, never its step text or answer (those stay in the app). */
- private fun lineOf(s: SessionInfo): String = when (s.state) {
+ fun lineOf(s: SessionInfo): String = when (s.state) {
BotState.APPROVAL -> "Waiting for your approval"
BotState.QUESTION -> "Has a question"
BotState.ERROR -> "Stopped on an error"
diff --git a/android/app/src/main/kotlin/com/coucou/android/core/PendingLaunch.kt b/android/app/src/main/kotlin/com/coucou/android/core/PendingLaunch.kt
index d5048327..7c7ec5eb 100644
--- a/android/app/src/main/kotlin/com/coucou/android/core/PendingLaunch.kt
+++ b/android/app/src/main/kotlin/com/coucou/android/core/PendingLaunch.kt
@@ -13,6 +13,9 @@ class PendingLaunch(private val clock: () -> Long = System::currentTimeMillis) {
private var allow = false
private var at = 0L
+ private var session: String? = null
+ private var sessionAt = 0L
+
@Synchronized
fun request(fp: String?, allow: Boolean) {
val clean = fp?.takeIf { it.isNotBlank() && it.length <= MAX_FP }
@@ -32,7 +35,27 @@ class PendingLaunch(private val clock: () -> Long = System::currentTimeMillis) {
return if (fresh) Request(fp, a) else null
}
+ /**
+ * "Open this session", from a tap on a widget row. A slot of its own: it never carries an Allow, and an Allow
+ * request never opens a session. Only the non-exported LaunchActivity calls it.
+ */
+ @Synchronized
+ fun requestSession(pillId: String?) {
+ session = pillId?.takeIf { it.isNotBlank() && it.length <= MAX_PILL }
+ sessionAt = clock()
+ }
+
+ /** The pill id of the session to open, once, if the tap is fresh; null for everything else. */
+ @Synchronized
+ fun takeSession(): String? {
+ val id = session ?: return null
+ val fresh = clock() - sessionAt <= TTL_MS
+ session = null
+ return if (fresh) id else null
+ }
+
companion object {
+ const val MAX_PILL = 64
/** The tap and the screen are in the same second; a longer wait means something else is going on. */
const val TTL_MS = 15_000L
const val MAX_FP = 128
diff --git a/android/app/src/main/kotlin/com/coucou/android/core/WidgetData.kt b/android/app/src/main/kotlin/com/coucou/android/core/WidgetData.kt
new file mode 100644
index 00000000..93fed543
--- /dev/null
+++ b/android/app/src/main/kotlin/com/coucou/android/core/WidgetData.kt
@@ -0,0 +1,56 @@
+package com.coucou.android.core
+
+import com.coucou.android.link.SessionInfo
+import com.coucou.android.mochi.BotState
+
+/** One agent on a widget: who it is, a plain state word and its colour. Only names, state words and colours. */
+data class WidgetTile(val pillId: String, val name: String, val state: BotState, val stateWord: String, val argb: Int)
+
+/**
+ * Everything the Solo and List widgets draw, as plain data so two pushes can be compared and the identical one skipped.
+ * Built from the same sessions, order and wording as [GlanceBuilder], so the widgets never say anything the notification,
+ * the tile and the first widget do not.
+ */
+data class WidgetContent(
+ val headline: String,
+ val tone: GlanceTone,
+ val connected: Boolean,
+ /** Requests and questions waiting for the user. */
+ val waiting: Int,
+ /** Most urgent first. Empty while the computer is not connected. */
+ val tiles: List<WidgetTile>,
+) {
+ /** The agent Solo shows. */
+ val top: WidgetTile? get() = tiles.firstOrNull()
+
+ /** The state Mochi is drawn in: the top agent's, else calm when connected and asleep when not. */
+ val mood: BotState get() = top?.state ?: if (connected) BotState.IDLE else BotState.SLEEPING
+}
+
+object WidgetData {
+ /** Rows on the List widget. */
+ const val MAX_ROWS = 5
+ private val FALLBACK = GlanceTone.CALM.argb
+
+ fun build(glance: Glance, sessions: List<SessionInfo>, nameOf: (String) -> String): WidgetContent {
+ val tiles = if (!glance.connected) emptyList() else sessions
+ .sortedBy { GlanceBuilder.rank(it.state) }
+ .take(MAX_ROWS)
+ .map { WidgetTile(it.pillId, nameOf(it.pillId), it.state, GlanceBuilder.lineOf(it), colourOf(it)) }
+ return WidgetContent(glance.headline, glance.tone, glance.connected, glance.waiting, tiles)
+ }
+
+ /** The session's own colour, else its pill's, else grey. */
+ private fun colourOf(s: SessionInfo): Int =
+ parse(s.color) ?: parse(Pills.byId(s.pillId)?.colorHex) ?: FALLBACK
+
+ /** "#RRGGBB" to opaque ARGB; null for anything else. */
+ fun parse(hex: String?): Int? {
+ val h = hex?.trim() ?: return null
+ if (h.length != 7 || h[0] != '#') return null
+ val digits = h.substring(1)
+ // toIntOrNull accepts a sign, which is not a colour.
+ if (!digits.all { it in '0'..'9' || it in 'a'..'f' || it in 'A'..'F' }) return null
+ return 0xFF000000.toInt() or digits.toInt(16)
+ }
+}
diff --git a/android/app/src/main/res/drawable/widget_badge_bg.xml b/android/app/src/main/res/drawable/widget_badge_bg.xml
new file mode 100644
index 00000000..d4dfdefb
--- /dev/null
+++ b/android/app/src/main/res/drawable/widget_badge_bg.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
+ <solid android:color="#FFFF9F0A" />
+ <corners android:radius="10dp" />
+</shape>
diff --git a/android/app/src/main/res/layout/widget_list.xml b/android/app/src/main/res/layout/widget_list.xml
new file mode 100644
index 00000000..71ecb340
--- /dev/null
+++ b/android/app/src/main/res/layout/widget_list.xml
@@ -0,0 +1,245 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- The List widget: the headline with a badge for what waits for you, then one row per agent (dot, name, state word). Plain RemoteViews, no library. -->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:id="@+id/list_root"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:background="@drawable/widget_bg"
+ android:orientation="vertical"
+ android:padding="14dp">
+
+ <LinearLayout
+ android:id="@+id/list_header"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:gravity="center_vertical"
+ android:orientation="horizontal"
+ android:paddingBottom="4dp">
+
+ <TextView
+ android:id="@+id/list_headline"
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
+ android:layout_weight="1"
+ android:ellipsize="end"
+ android:maxLines="1"
+ android:textColor="#FFFFFFFF"
+ android:textSize="15sp"
+ android:textStyle="bold" />
+
+ <TextView
+ android:id="@+id/list_badge"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="8dp"
+ android:background="@drawable/widget_badge_bg"
+ android:gravity="center"
+ android:minWidth="20dp"
+ android:paddingStart="6dp"
+ android:paddingEnd="6dp"
+ android:textColor="#FF1C1C1E"
+ android:textSize="12sp"
+ android:textStyle="bold"
+ android:visibility="gone" />
+ </LinearLayout>
+
+ <LinearLayout
+ android:id="@+id/list_row0"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:gravity="center_vertical"
+ android:orientation="horizontal"
+ android:paddingTop="3dp"
+ android:paddingBottom="3dp">
+
+ <TextView
+ android:id="@+id/list_dot0"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:importantForAccessibility="no"
+ android:text="●"
+ android:textSize="12sp" />
+
+ <TextView
+ android:id="@+id/list_name0"
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="8dp"
+ android:layout_weight="1"
+ android:ellipsize="end"
+ android:maxLines="1"
+ android:textColor="#FFFFFFFF"
+ android:textSize="13sp"
+ android:textStyle="bold" />
+
+ <TextView
+ android:id="@+id/list_state0"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="8dp"
+ android:ellipsize="end"
+ android:maxLines="1"
+ android:textColor="#FFAEAEB2"
+ android:textSize="12sp" />
+ </LinearLayout>
+
+ <LinearLayout
+ android:id="@+id/list_row1"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:gravity="center_vertical"
+ android:orientation="horizontal"
+ android:paddingTop="3dp"
+ android:paddingBottom="3dp">
+
+ <TextView
+ android:id="@+id/list_dot1"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:importantForAccessibility="no"
+ android:text="●"
+ android:textSize="12sp" />
+
+ <TextView
+ android:id="@+id/list_name1"
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="8dp"
+ android:layout_weight="1"
+ android:ellipsize="end"
+ android:maxLines="1"
+ android:textColor="#FFFFFFFF"
+ android:textSize="13sp"
+ android:textStyle="bold" />
+
+ <TextView
+ android:id="@+id/list_state1"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="8dp"
+ android:ellipsize="end"
+ android:maxLines="1"
+ android:textColor="#FFAEAEB2"
+ android:textSize="12sp" />
+ </LinearLayout>
+
+ <LinearLayout
+ android:id="@+id/list_row2"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:gravity="center_vertical"
+ android:orientation="horizontal"
+ android:paddingTop="3dp"
+ android:paddingBottom="3dp">
+
+ <TextView
+ android:id="@+id/list_dot2"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:importantForAccessibility="no"
+ android:text="●"
+ android:textSize="12sp" />
+
+ <TextView
+ android:id="@+id/list_name2"
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="8dp"
+ android:layout_weight="1"
+ android:ellipsize="end"
+ android:maxLines="1"
+ android:textColor="#FFFFFFFF"
+ android:textSize="13sp"
+ android:textStyle="bold" />
+
+ <TextView
+ android:id="@+id/list_state2"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="8dp"
+ android:ellipsize="end"
+ android:maxLines="1"
+ android:textColor="#FFAEAEB2"
+ android:textSize="12sp" />
+ </LinearLayout>
+
+ <LinearLayout
+ android:id="@+id/list_row3"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:gravity="center_vertical"
+ android:orientation="horizontal"
+ android:paddingTop="3dp"
+ android:paddingBottom="3dp">
+
+ <TextView
+ android:id="@+id/list_dot3"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:importantForAccessibility="no"
+ android:text="●"
+ android:textSize="12sp" />
+
+ <TextView
+ android:id="@+id/list_name3"
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="8dp"
+ android:layout_weight="1"
+ android:ellipsize="end"
+ android:maxLines="1"
+ android:textColor="#FFFFFFFF"
+ android:textSize="13sp"
+ android:textStyle="bold" />
+
+ <TextView
+ android:id="@+id/list_state3"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="8dp"
+ android:ellipsize="end"
+ android:maxLines="1"
+ android:textColor="#FFAEAEB2"
+ android:textSize="12sp" />
+ </LinearLayout>
+
+ <LinearLayout
+ android:id="@+id/list_row4"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:gravity="center_vertical"
+ android:orientation="horizontal"
+ android:paddingTop="3dp"
+ android:paddingBottom="3dp">
+
+ <TextView
+ android:id="@+id/list_dot4"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:importantForAccessibility="no"
+ android:text="●"
+ android:textSize="12sp" />
+
+ <TextView
+ android:id="@+id/list_name4"
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="8dp"
+ android:layout_weight="1"
+ android:ellipsize="end"
+ android:maxLines="1"
+ android:textColor="#FFFFFFFF"
+ android:textSize="13sp"
+ android:textStyle="bold" />
+
+ <TextView
+ android:id="@+id/list_state4"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="8dp"
+ android:ellipsize="end"
+ android:maxLines="1"
+ android:textColor="#FFAEAEB2"
+ android:textSize="12sp" />
+ </LinearLayout>
+</LinearLayout>
diff --git a/android/app/src/main/res/layout/widget_solo.xml b/android/app/src/main/res/layout/widget_solo.xml
new file mode 100644
index 00000000..04031ce6
--- /dev/null
+++ b/android/app/src/main/res/layout/widget_solo.xml
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- The Solo widget: Mochi in the state of the most urgent agent, its name and a state word. Plain RemoteViews, no library. -->
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:id="@+id/solo_root"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:background="@drawable/widget_bg">
+
+ <LinearLayout
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:gravity="center_horizontal"
+ android:orientation="vertical"
+ android:padding="10dp">
+
+ <ImageView
+ android:id="@+id/solo_mochi"
+ android:contentDescription="@string/widget_solo_label"
+ android:layout_width="match_parent"
+ android:layout_height="0dp"
+ android:layout_weight="1"
+ android:scaleType="fitCenter" />
+
+ <TextView
+ android:id="@+id/solo_name"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:ellipsize="end"
+ android:gravity="center"
+ android:maxLines="1"
+ android:textColor="#FFFFFFFF"
+ android:textSize="14sp"
+ android:textStyle="bold" />
+
+ <TextView
+ android:id="@+id/solo_state"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:ellipsize="end"
+ android:gravity="center"
+ android:maxLines="1"
+ android:textColor="#FFAEAEB2"
+ android:textSize="12sp" />
+ </LinearLayout>
+
+ <TextView
+ android:id="@+id/solo_badge"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="top|end"
+ android:layout_margin="8dp"
+ android:background="@drawable/widget_badge_bg"
+ android:gravity="center"
+ android:minWidth="20dp"
+ android:paddingStart="6dp"
+ android:paddingEnd="6dp"
+ android:textColor="#FF1C1C1E"
+ android:textSize="12sp"
+ android:textStyle="bold"
+ android:visibility="gone" />
+</FrameLayout>
diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml
index 5a87c237..6b117617 100644
--- a/android/app/src/main/res/values/strings.xml
+++ b/android/app/src/main/res/values/strings.xml
@@ -11,6 +11,12 @@
<string name="action_deny">Deny</string>
<string name="action_close">Close</string>
<string name="widget_description">Shows whether your agents are working, without opening Coucou.</string>
+ <string name="widget_solo_label">Coucou Solo</string>
+ <string name="widget_list_label">Coucou List</string>
+ <string name="widget_solo_description">Mochi and the state of your most urgent agent, without opening Coucou.</string>
+ <string name="widget_list_description">Your agents and what each one is doing, without opening Coucou.</string>
+ <string name="widget_mochi_description">Mochi, %1$s</string>
+ <string name="widget_badge_description">%1$d waiting for you</string>
<string name="status_connected">Connected</string>
<string name="status_connecting">Connecting…</string>
<string name="status_not_connected">Not connected</string>
diff --git a/android/app/src/main/res/xml/coucou_list_widget_info.xml b/android/app/src/main/res/xml/coucou_list_widget_info.xml
new file mode 100644
index 00000000..66ef6863
--- /dev/null
+++ b/android/app/src/main/res/xml/coucou_list_widget_info.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
+ android:minWidth="250dp"
+ android:minHeight="180dp"
+ android:targetCellWidth="4"
+ android:targetCellHeight="3"
+ android:minResizeWidth="180dp"
+ android:minResizeHeight="110dp"
+ android:resizeMode="horizontal|vertical"
+ android:updatePeriodMillis="0"
+ android:widgetCategory="home_screen"
+ android:initialLayout="@layout/widget_list"
+ android:description="@string/widget_list_description" />
diff --git a/android/app/src/main/res/xml/coucou_solo_widget_info.xml b/android/app/src/main/res/xml/coucou_solo_widget_info.xml
new file mode 100644
index 00000000..5ad1e2b6
--- /dev/null
+++ b/android/app/src/main/res/xml/coucou_solo_widget_info.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
+ android:minWidth="110dp"
+ android:minHeight="110dp"
+ android:targetCellWidth="2"
+ android:targetCellHeight="2"
+ android:minResizeWidth="110dp"
+ android:minResizeHeight="110dp"
+ android:resizeMode="horizontal|vertical"
+ android:updatePeriodMillis="0"
+ android:widgetCategory="home_screen"
+ android:initialLayout="@layout/widget_solo"
+ android:description="@string/widget_solo_description" />
diff --git a/android/app/src/test/kotlin/com/coucou/android/PendingLaunchTest.kt b/android/app/src/test/kotlin/com/coucou/android/PendingLaunchTest.kt
index 1a4bbf78..1e8718b3 100644
--- a/android/app/src/test/kotlin/com/coucou/android/PendingLaunchTest.kt
+++ b/android/app/src/test/kotlin/com/coucou/android/PendingLaunchTest.kt
@@ -79,6 +79,43 @@ class PendingLaunchTest {
@Test fun everyOtherComponentWithAnIntentFilterIsTheLauncher() {
val m = manifest()
val exported = Regex("""android:name="([^"]+)"[^>]*android:exported="true"""").findAll(m).map { it.groupValues[1] }.toSet()
- assertEquals(setOf(".MainActivity", ".app.GlanceWidget", ".app.GlanceTile"), exported)
+ assertEquals(setOf(".MainActivity", ".app.GlanceWidget", ".app.GlanceTile", ".app.SoloWidget", ".app.ListWidget"), exported)
+ }
+
+ @Test fun aWidgetRowAsksForASessionOnce() {
+ launch.requestSession("agent_codex")
+ assertEquals("agent_codex", launch.takeSession())
+ assertNull("only once", launch.takeSession())
+ }
+
+ @Test fun aStaleOrBadSessionRequestIsDropped() {
+ launch.requestSession("agent_codex")
+ now += PendingLaunch.TTL_MS + 1
+ assertNull(launch.takeSession())
+ launch.requestSession("")
+ assertNull(launch.takeSession())
+ launch.requestSession(null)
+ assertNull(launch.takeSession())
+ launch.requestSession("x".repeat(PendingLaunch.MAX_PILL + 1))
+ assertNull(launch.takeSession())
+ }
+
+ @Test fun theSessionSlotAndTheAllowSlotNeverMeet() {
+ launch.request("fp-1", allow = true)
+ launch.requestSession("agent_codex")
+ assertEquals("agent_codex", launch.takeSession())
+ assertEquals("the Allow is still there", PendingLaunch.Request("fp-1", true), launch.take())
+ launch.requestSession("agent_codex")
+ assertNull("a session request is never an Allow", launch.take())
+ assertEquals("agent_codex", launch.takeSession())
+ }
+
+ @Test fun theExportedActivityOnlyAsksTheModelForTheSessionToOpen() {
+ val src = File(main + "MainActivity.kt").readText()
+ assertTrue(src.contains("model.launch.takeSession()"))
+ assertFalse(src.contains("EXTRA_SESSION"))
+ val callers = File(main).walkTopDown().filter { it.extension == "kt" }
+ .filter { it.readText().contains("launch.requestSession(") }.map { it.name }.toList()
+ assertEquals(listOf("LaunchActivity.kt"), callers)
}
}
diff --git a/android/app/src/test/kotlin/com/coucou/android/WidgetDataTest.kt b/android/app/src/test/kotlin/com/coucou/android/WidgetDataTest.kt
new file mode 100644
index 00000000..3a890ca5
--- /dev/null
+++ b/android/app/src/test/kotlin/com/coucou/android/WidgetDataTest.kt
@@ -0,0 +1,99 @@
+package com.coucou.android
+
+import com.coucou.android.core.Glance
+import com.coucou.android.core.GlanceBuilder
+import com.coucou.android.core.GlanceTone
+import com.coucou.android.core.WidgetData
+import com.coucou.android.link.LinkState
+import com.coucou.android.link.SessionInfo
+import com.coucou.android.mochi.BotState
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertNull
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+class WidgetDataTest {
+ private fun s(id: String, state: BotState, color: String? = null, status: String = "") =
+ SessionInfo(id, "", state, status, 0, 0, 0L, project = "secret-folder", color = color)
+
+ private val names = mapOf("agent_codex" to "Codex", "agent_gemini" to "Gemini CLI", "agent_amp" to "Amp")
+ private fun glance(sessions: List<SessionInfo>, approvals: Int = 0, link: LinkState = LinkState.CONNECTED): Glance =
+ GlanceBuilder.build(true, link, "PC", sessions, approvals) { names[it] ?: "An agent" }
+
+ private fun build(sessions: List<SessionInfo>, approvals: Int = 0, link: LinkState = LinkState.CONNECTED) =
+ WidgetData.build(glance(sessions, approvals, link), sessions) { names[it] ?: "An agent" }
+
+ @Test fun theMostUrgentAgentComesFirstLikeOnTheOtherSurfaces() {
+ val sessions = listOf(s("agent_amp", BotState.IDLE), s("agent_codex", BotState.WORKING), s("agent_gemini", BotState.APPROVAL))
+ val c = build(sessions, approvals = 1)
+ assertEquals(listOf("agent_gemini", "agent_codex", "agent_amp"), c.tiles.map { it.pillId })
+ assertEquals("agent_gemini", c.top!!.pillId)
+ // The same order as the notification and the first widget.
+ assertEquals(c.tiles.map { it.name }, glance(sessions, 1).lines.map { it.agent })
+ }
+
+ @Test fun theStateWordIsTheOneOfTheGlance() {
+ val sessions = listOf(s("agent_codex", BotState.QUESTION))
+ assertEquals("Has a question", build(sessions).top!!.stateWord)
+ assertEquals("Codex", build(sessions).top!!.name)
+ assertEquals(glance(sessions).lines.single().text, build(sessions).top!!.stateWord)
+ }
+
+ @Test fun theListIsCapped() {
+ val many = (1..9).map { s("p$it", BotState.WORKING) }
+ assertEquals(WidgetData.MAX_ROWS, build(many).tiles.size)
+ }
+
+ @Test fun theColourIsTheSessionsOwnThenThePillsThenGrey() {
+ assertEquals(0xFF112233.toInt(), build(listOf(s("agent_codex", BotState.IDLE, color = "#112233"))).top!!.argb)
+ assertEquals(0xFF2DD4BF.toInt(), build(listOf(s("agent_codex", BotState.IDLE))).top!!.argb)
+ assertEquals(0xFF2DD4BF.toInt(), build(listOf(s("agent_codex", BotState.IDLE, color = "teal"))).top!!.argb)
+ assertEquals(GlanceTone.CALM.argb, build(listOf(s("nope", BotState.IDLE))).top!!.argb)
+ }
+
+ @Test fun parseOnlyTakesSixDigitHex() {
+ assertEquals(0xFFFFFFFF.toInt(), WidgetData.parse("#FFFFFF"))
+ for (bad in listOf(null, "", "FFFFFF", "#FFF", "#GGGGGG", "#FFFFFFFF", "#+12345")) assertNull(bad, WidgetData.parse(bad))
+ }
+
+ @Test fun nothingToShowWhileNotConnected() {
+ val c = build(listOf(s("agent_codex", BotState.WORKING)), link = LinkState.DISCONNECTED)
+ assertTrue(c.tiles.isEmpty())
+ assertNull(c.top)
+ assertFalse(c.connected)
+ assertEquals(BotState.SLEEPING, c.mood)
+ assertEquals("Not connected", c.headline)
+ }
+
+ @Test fun connectedWithNoAgentIsCalm() {
+ val c = build(emptyList())
+ assertEquals(BotState.IDLE, c.mood)
+ assertEquals("Connected", c.headline)
+ }
+
+ @Test fun moodIsTheTopAgents() {
+ assertEquals(BotState.ERROR, build(listOf(s("agent_codex", BotState.WORKING), s("agent_amp", BotState.ERROR))).mood)
+ }
+
+ @Test fun waitingCountsRequestsAndQuestions() {
+ val c = build(listOf(s("agent_codex", BotState.QUESTION)), approvals = 2)
+ assertEquals(3, c.waiting)
+ }
+
+ @Test fun sameContentIsEqualSoThePushCanBeSkipped() {
+ val a = build(listOf(s("agent_codex", BotState.WORKING)))
+ val b = build(listOf(s("agent_codex", BotState.WORKING)))
+ assertEquals(a, b)
+ // A change the widget does not show (step text, project) does not change the content.
+ assertEquals(a, build(listOf(s("agent_codex", BotState.WORKING, status = "editing a.kt"))))
+ assertFalse(a == build(listOf(s("agent_codex", BotState.THINKING))))
+ }
+
+ @Test fun nothingPrivateReachesTheTiles() {
+ val c = build(listOf(s("agent_codex", BotState.WORKING, status = "rm -rf /home/me/secret.txt")), approvals = 1)
+ val text = c.toString()
+ assertFalse(text.contains("secret"))
+ assertFalse(text.contains("rm -rf"))
+ }
+}
diff --git a/android/app/src/test/kotlin/com/coucou/android/WidgetSurfacesTest.kt b/android/app/src/test/kotlin/com/coucou/android/WidgetSurfacesTest.kt
new file mode 100644
index 00000000..617bc921
--- /dev/null
+++ b/android/app/src/test/kotlin/com/coucou/android/WidgetSurfacesTest.kt
@@ -0,0 +1,92 @@
+package com.coucou.android
+
+import java.io.File
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+/** Source guards for the Solo and List widgets (Compose and Android classes only compile in CI), in the style of [GlanceSurfacesTest]. */
+class WidgetSurfacesTest {
+ private val main = "src/main/kotlin/com/coucou/android/"
+ private fun read(path: String) = File(path).readText()
+ private val manifest get() = read("src/main/AndroidManifest.xml")
+
+ private val sources = listOf("app/SoloWidget.kt", "app/ListWidget.kt", "app/WidgetHub.kt", "app/MochiBitmap.kt", "core/WidgetData.kt")
+ .associateWith { read(main + it) }
+
+ @Test fun theWidgetsAreRegisteredAndNeverWakeOnATimer() {
+ for ((cls, info) in listOf("SoloWidget" to "coucou_solo_widget_info", "ListWidget" to "coucou_list_widget_info")) {
+ assertTrue(manifest.contains("""android:name=".app.$cls""""))
+ assertTrue(manifest.contains("@xml/$info"))
+ val xml = read("src/main/res/xml/$info.xml")
+ assertTrue(xml.contains("""android:updatePeriodMillis="0""""))
+ assertTrue(xml.contains("""android:widgetCategory="home_screen""""))
+ }
+ }
+
+ @Test fun noWidgetSourceTouchesAnythingBeyondTheGlance() {
+ // Agent names, state words and colours only: no step text, no command, no file, no answer.
+ for ((name, src) in sources) {
+ for (word in listOf("statusText", "finalLine", "steps", ".command", "files", "project")) assertFalse("$name uses $word", src.contains(word))
+ }
+ }
+
+ @Test fun noWidgetCanDecideAnything() {
+ for ((name, src) in sources) {
+ for (word in listOf("decide", "allow", "deny", "approve", "answer", "unpair")) assertFalse("$name uses $word", src.contains(word, ignoreCase = true))
+ }
+ for (layout in listOf("widget_solo", "widget_list")) assertFalse(read("src/main/res/layout/$layout.xml").contains("Button"))
+ }
+
+ @Test fun noTimerAndNoLibrary() {
+ for ((name, src) in sources) {
+ for (word in listOf("AlarmManager", "WorkManager", "postDelayed", "Handler(", "Timer", "androidx.glance")) assertFalse("$name uses $word", src.contains(word))
+ }
+ assertFalse(read("build.gradle.kts").contains("androidx.glance"))
+ }
+
+ @Test fun everyPendingIntentIsImmutableAndExplicit() {
+ val hub = sources.getValue("app/WidgetHub.kt")
+ assertEquals(2, Regex("PendingIntent.getActivity").findAll(hub).count())
+ assertEquals(2, Regex("FLAG_IMMUTABLE").findAll(hub).count())
+ assertTrue(hub.contains("Intent(context, LaunchActivity::class.java)"))
+ assertTrue(hub.contains("Intent(context, MainActivity::class.java)"))
+ for (src in listOf(sources.getValue("app/SoloWidget.kt"), sources.getValue("app/ListWidget.kt"))) {
+ assertFalse(src.contains("PendingIntent.get"))
+ assertFalse(src.contains("Intent(\""))
+ }
+ }
+
+ @Test fun everyRowAndEveryWidgetHasItsOwnRequestCode() {
+ val hub = sources.getValue("app/WidgetHub.kt")
+ val codes = Regex("""const val CODE_\w+ = (0x[0-9A-Fa-f]+)""").findAll(hub).map { it.groupValues[1] }.toList()
+ assertEquals(3, codes.size)
+ assertEquals(codes.size, codes.toSet().size)
+ // The rows use CODE_LIST_ROW + index: the ranges must not run into the next code.
+ val values = codes.map { it.removePrefix("0x").toInt(16) }.sorted()
+ assertTrue(values.zipWithNext().all { (a, b) -> b - a >= 16 })
+ assertTrue(sources.getValue("app/ListWidget.kt").contains("WidgetHub.CODE_LIST_ROW + i"))
+ }
+
+ @Test fun theImageAndTheBadgeAreDescribed() {
+ for (cls in listOf("app/SoloWidget.kt")) assertTrue(sources.getValue(cls).contains("setContentDescription(R.id.solo_mochi"))
+ assertTrue(sources.getValue("app/SoloWidget.kt").contains("setContentDescription(R.id.solo_badge"))
+ assertTrue(sources.getValue("app/ListWidget.kt").contains("setContentDescription(R.id.list_badge"))
+ }
+
+ @Test fun everyChangeOfThePictureReachesTheHub() {
+ val model = read(main + "app/AppModel.kt")
+ val push = model.substringAfter("private fun pushGlance()").substringBefore("private fun refreshIsland()")
+ assertTrue(push.contains("WidgetHub.update"))
+ assertTrue("the first widget stays", push.contains("GlanceWidget.update"))
+ assertTrue(sources.getValue("app/WidgetHub.kt").contains("if (content == last) return"))
+ }
+
+ @Test fun theFirstWidgetIsUntouched() {
+ // GlanceSurfacesTest guards its registration; this guards that the new widgets did not take its names.
+ assertTrue(manifest.contains("@xml/coucou_widget_info"))
+ assertTrue(File("src/main/res/layout/widget_glance.xml").exists())
+ assertTrue(read("src/main/res/xml/coucou_widget_info.xml").contains("""android:targetCellWidth="3""""))
+ }
+}
diff --git a/android/i18n/app-strings.json b/android/i18n/app-strings.json
index d28c0f48..68abd230 100644
--- a/android/i18n/app-strings.json
+++ b/android/i18n/app-strings.json
@@ -11,6 +11,12 @@
"action_deny": "Deny",
"action_close": "Close",
"widget_description": "Shows whether your agents are working, without opening Coucou.",
+ "widget_solo_label": "Coucou Solo",
+ "widget_list_label": "Coucou List",
+ "widget_solo_description": "Mochi and the state of your most urgent agent, without opening Coucou.",
+ "widget_list_description": "Your agents and what each one is doing, without opening Coucou.",
+ "widget_mochi_description": "Mochi, %1$s",
+ "widget_badge_description": "%1$d waiting for you",
"status_connected": "Connected",
"status_connecting": "Connecting…",
"status_not_connected": "Not connected",