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: plan usage on the right of Mochi Big
Claudee8a1db7f9de3
diff --git a/android/app/src/main/kotlin/com/coucou/android/app/BarBitmap.kt b/android/app/src/main/kotlin/com/coucou/android/app/BarBitmap.ktnew file mode 100644index 00000000..8fa5a0a0--- /dev/null+++ b/android/app/src/main/kotlin/com/coucou/android/app/BarBitmap.kt@@ -0,0 +1,52 @@+package com.coucou.android.app++import android.graphics.Bitmap+import androidx.compose.ui.geometry.CornerRadius+import androidx.compose.ui.geometry.Offset+import androidx.compose.ui.geometry.Size+import androidx.compose.ui.graphics.Canvas+import androidx.compose.ui.graphics.Color+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.core.UsageRing+import kotlin.math.max++/**+ * The thin bar of a plan-usage row on Mochi Big, drawn once into a bitmap like [RingBitmap] draws the Usage Ring's ring: a faint+ * track and a rounded fill in the colour of the usage (green, orange above 75%, red above 90%). A few are kept so a push that changes+ * nothing about a bar draws nothing. No timer, no frame loop.+ */+object BarBitmap {+ private const val KEEP = 8+ private const val TRACK = 0x40808080++ private data class Key(val pct: Int, val tone: Int, val width: Int, val height: Int)++ private val cache = object : LinkedHashMap<Key, Bitmap>(8, 0.75f, true) {+ override fun removeEldestEntry(eldest: MutableMap.MutableEntry<Key, Bitmap>) = size > KEEP+ }++ /** The bar for [row], [widthPx] by [heightPx]. */+ @Synchronized+ fun of(row: UsageRing, widthPx: Int, heightPx: Int): Bitmap {+ val key = Key(row.pct.coerceIn(0, 100), row.barTone.argb, widthPx, heightPx)+ return cache.getOrPut(key) { draw(row, widthPx, heightPx) }+ }++ private fun draw(row: UsageRing, widthPx: Int, heightPx: Int): Bitmap {+ val image = ImageBitmap(widthPx, heightPx)+ val round = CornerRadius(heightPx / 2f, heightPx / 2f)+ CanvasDrawScope().draw(Density(1f), LayoutDirection.Ltr, Canvas(image), Size(widthPx.toFloat(), heightPx.toFloat())) {+ drawRoundRect(Color(TRACK), Offset.Zero, Size(widthPx.toFloat(), heightPx.toFloat()), round)+ if (row.fraction > 0f) {+ // Never narrower than its own height, so a small value still shows a round dot.+ val fill = max(heightPx.toFloat(), widthPx * row.fraction)+ drawRoundRect(Color(row.barTone.argb), Offset.Zero, Size(fill, heightPx.toFloat()), round)+ }+ }+ return image.asAndroidBitmap()+ }+}diff --git a/android/app/src/main/kotlin/com/coucou/android/app/MochiBigWidget.kt b/android/app/src/main/kotlin/com/coucou/android/app/MochiBigWidget.ktindex 7c2f0614..79cfdb0a 100644--- a/android/app/src/main/kotlin/com/coucou/android/app/MochiBigWidget.kt+++ b/android/app/src/main/kotlin/com/coucou/android/app/MochiBigWidget.kt@@ -9,13 +9,15 @@ import android.view.Viewimport android.widget.RemoteViewsimport com.coucou.android.Rimport com.coucou.android.core.LaunchScreen+import com.coucou.android.core.UsageRing+import com.coucou.android.core.UsageSideimport com.coucou.android.core.WidgetContentimport com.coucou.android.core.WidgetTone/*** The Mochi Big widget (4x4). The top is in two sides. On the left, Mochi with his glow and his badge, then the agent's name and- * state word, so the state reads without a sentence. On the right, the plan usage, or one calm message when it is off or the computer- * is not there. Under them, rounded buttons that only open a screen of the app (Home, Chat, Settings, Recent, the top agent, Pair+ * state word, so the state reads without a sentence. On the right, the plan usage (up to two rows: Claude and Codex, each with a big percentage and a thin bar that is green, orange above+ * 75% and red above 90%), or one calm message when it is off or the computer is not there. Under them, rounded buttons that only open a screen of the app (Home, Chat, Settings, Recent, the top agent, Pair* again): none when the card is small, one row of four from the medium size, two rows of three when large. They are tinted orange* while something waits. Drawn by [WidgetHub] when something changes (updatePeriodMillis = 0). A tap on the card opens the top agent,* or the app. Nothing here changes anything.@@ -62,7 +64,7 @@ class MochiBigWidget : AppWidgetProvider() {}v.setViewVisibility(R.id.big_state, View.VISIBLE)- bindUsage(v, context, c)+ bindUsage(v, context, c, small)// Buttons: none when small, one row of four by default, two rows of three when large. They only open things.v.setViewVisibility(R.id.big_buttons, if (small) View.GONE else View.VISIBLE)@@ -101,20 +103,44 @@ class MochiBigWidget : AppWidgetProvider() {}}- /** The right side. A calm message, never an empty area: the computer is not there, or plan usage is off. */- private fun bindUsage(v: RemoteViews, context: Context, c: WidgetContent) {- v.setViewVisibility(R.id.big_usage_a, View.GONE)- v.setViewVisibility(R.id.big_usage_b, View.GONE)- v.setViewVisibility(R.id.big_off, View.VISIBLE)- if (!c.connected) {- v.setTextViewText(R.id.big_off_title, context.getString(R.string.widget_big_not_connected))- v.setTextViewText(R.id.big_off_line, c.emptyLine.orEmpty())- v.setOnClickPendingIntent(R.id.big_right, WidgetHub.openApp(context, WidgetHub.CODE_BIG_USAGE_APP))- } else {- v.setTextViewText(R.id.big_off_title, context.getString(R.string.widget_big_usage_off))- v.setTextViewText(R.id.big_off_line, context.getString(R.string.widget_big_usage_off_line))- v.setOnClickPendingIntent(R.id.big_right, WidgetHub.openScreen(context, WidgetHub.CODE_BIG_USAGE_SETTINGS, LaunchScreen.SETTINGS))+ /**+ * The right side, never empty: up to two plan-usage rows (a big percentage with a thin bar; only the first when small), or a+ * calm message when plan usage is off (a tap opens Settings) or the computer is not there (a tap opens the app).+ */+ private fun bindUsage(v: RemoteViews, context: Context, c: WidgetContent, small: Boolean) {+ val rows = c.usageRows+ when (c.usageSide) {+ UsageSide.ROWS -> {+ v.setViewVisibility(R.id.big_off, View.GONE)+ bindRow(v, context, rows[0], R.id.big_usage_a, R.id.big_usage_a_label, R.id.big_usage_a_pct, R.id.big_usage_a_bar)+ val second = rows.getOrNull(1)?.takeIf { !small }+ if (second != null) bindRow(v, context, second, R.id.big_usage_b, R.id.big_usage_b_label, R.id.big_usage_b_pct, R.id.big_usage_b_bar)+ else v.setViewVisibility(R.id.big_usage_b, View.GONE)+ v.setOnClickPendingIntent(R.id.big_right, WidgetHub.openApp(context, WidgetHub.CODE_BIG_USAGE_APP))+ }+ UsageSide.OFF, UsageSide.NOT_CONNECTED -> {+ v.setViewVisibility(R.id.big_usage_a, View.GONE)+ v.setViewVisibility(R.id.big_usage_b, View.GONE)+ v.setViewVisibility(R.id.big_off, View.VISIBLE)+ if (c.usageSide == UsageSide.NOT_CONNECTED) {+ v.setTextViewText(R.id.big_off_title, context.getString(R.string.widget_big_not_connected))+ v.setTextViewText(R.id.big_off_line, c.emptyLine.orEmpty())+ v.setOnClickPendingIntent(R.id.big_right, WidgetHub.openApp(context, WidgetHub.CODE_BIG_USAGE_APP))+ } else {+ v.setTextViewText(R.id.big_off_title, context.getString(R.string.widget_big_usage_off))+ v.setTextViewText(R.id.big_off_line, context.getString(R.string.widget_big_usage_off_line))+ v.setOnClickPendingIntent(R.id.big_right, WidgetHub.openScreen(context, WidgetHub.CODE_BIG_USAGE_SETTINGS, LaunchScreen.SETTINGS))+ }+ }}}++ private fun bindRow(v: RemoteViews, context: Context, row: UsageRing, group: Int, label: Int, pct: Int, bar: Int) {+ v.setViewVisibility(group, View.VISIBLE)+ v.setTextViewText(label, row.label)+ v.setTextViewText(pct, "${row.pct}%")+ v.setImageViewBitmap(bar, BarBitmap.of(row, WidgetHub.px(context, 180), WidgetHub.px(context, 6)))+ v.setContentDescription(group, context.getString(R.string.widget_ring_cd, row.plan, row.pct))+ }}}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.ktindex e4d612e6..46766179 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@@ -62,6 +62,9 @@ data class UsageRing(val plan: String, val window: String, val pct: Int) {/** "Claude · 5 h". */val label: String get() = "$plan · $window"+ /** The colour of Mochi Big's bar: green, orange above 75%, red above 90% ([WidgetData.barTone]). */+ val barTone: WidgetTone get() = WidgetData.barTone(pct)+/** Green while there is room, orange from half, red from four fifths: the same rule as Home ([PlanText]). */val tone: WidgetTone get() = when (PlanText.level(pct)) {PlanText.Level.LOW -> WidgetTone.DONE@@ -70,6 +73,9 @@ data class UsageRing(val plan: String, val window: String, val pct: Int) {}}+/** What the right side of Mochi Big shows: the plan usage rows, a calm "Plan usage is off", or "Not connected". Never nothing. */+enum class UsageSide { ROWS, OFF, NOT_CONNECTED }+/*** Everything the 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,@@ -87,7 +93,16 @@ data class WidgetContent(val ring: UsageRing? = null,/** Agent tasks finished today, counted on this phone only. */val today: Int = 0,+ /** Mochi Big's plan usage: Claude then Codex, each with its main window; empty when the computer sends none or is not connected. */+ val usageRows: List<UsageRing> = emptyList(),) {+ /** Which of the three things Mochi Big's right side shows. */+ val usageSide: UsageSide get() = when {+ !connected -> UsageSide.NOT_CONNECTED+ usageRows.isEmpty() -> UsageSide.OFF+ else -> UsageSide.ROWS+ }+/** The agent Solo shows. */val top: WidgetTile? get() = tiles.firstOrNull()@@ -168,7 +183,33 @@ object WidgetData {.take(MAX_ROWS).map { WidgetTile(it.pillId, nameOf(it.pillId), it.state, GlanceBuilder.lineOf(it), colourOf(it)) }val ring = if (glance.connected) ring(usage, nowMs) else null- return WidgetContent(glance.headline, glance.tone, glance.connected, glance.waiting, tiles, ring, today.coerceAtLeast(0))+ val rows = if (glance.connected) usageRows(usage, nowMs) else emptyList()+ return WidgetContent(glance.headline, glance.tone, glance.connected, glance.waiting, tiles, ring, today.coerceAtLeast(0), rows)+ }++ /** Mochi Big's bar turns orange above this percentage... */+ const val BAR_ORANGE_ABOVE = 75++ /** ...and red above this one. */+ const val BAR_RED_ABOVE = 90++ /** Green up to 75%, orange above 75%, red above 90%. */+ fun barTone(pct: Int): WidgetTone = when {+ pct > BAR_RED_ABOVE -> WidgetTone.TROUBLE+ pct > BAR_ORANGE_ABOVE -> WidgetTone.ATTENTION+ else -> WidgetTone.DONE+ }++ /**+ * Up to two rows for Mochi Big: Claude, then Codex, each with its main window (five hours, else the week). A plan the computer+ * does not know has no row. The same data as the Usage Ring, with a window that has reset counted as zero ([PlanText]).+ */+ fun usageRows(usage: UsageSnapshot?, nowMs: Long): List<UsageRing> {+ usage ?: return emptyList()+ return listOf("Claude" to usage.claude, "Codex" to usage.codex).mapNotNull { (name, plan) ->+ plan?.fiveHour?.let { UsageRing(name, "5 h", PlanText.effectivePct(it, nowMs)) }+ ?: plan?.sevenDay?.let { UsageRing(name, "week", PlanText.effectivePct(it, nowMs)) }+ }}/** The content a [WidgetSample] stands for, built like a real one. [nowMs] only sets when the sample plan windows reset. */@@ -195,9 +236,11 @@ object WidgetData {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))+ // Codex has only its week here, so the second row of Mochi Big reads "Codex · week".+ fun week(pct: Int) = PlanUsage(null, PlanWindow(pct, nowMs + 3 * 24 * HOUR_MS))val usage = when (which) {- WidgetSample.WORKING -> UsageSnapshot(plan(42, 18), null)- WidgetSample.WAITING -> UsageSnapshot(plan(83, 61), null)+ WidgetSample.WORKING -> UsageSnapshot(plan(42, 18), week(41))+ WidgetSample.WAITING -> UsageSnapshot(plan(83, 61), week(93))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