feat(widgets): next alarm, timer and clock home-screen widgets

Three Glance widgets, mirroring state the app already computes rather
than owning any of their own: the next alarm (AlarmEngine.upcoming()),
the live pill's subject timer (same selection logic, no ticker of its
own — a running countdown is the platform chronometer, exactly like
the ring notification), and the local time growing to world clocks at
larger sizes. WidgetSync collects the same read-only flows the shell
already reads and redraws on change, event-driven (updatePeriodMillis
= 0) — it touches no engine, only re-renders, and covers every write
path including SystemEventReceiver's since the process is already
alive whenever those run.

Tapping a widget reuses the same internal navigation actions the
AlarmClock contract's door already produces; the timer buttons send
the same unexported broadcasts its notification does. A widget is
just another caller of machinery that already exists.

ManifestRulesTest is updated deliberately: three GlanceAppWidgetReceivers
join the exported set, each pinned to exactly the platform-required
APPWIDGET_UPDATE filter and no permission — no new permission is
requested anywhere in this change.
This commit is contained in:
makiolaj committed 2026-10-01 22:32:07 +02:00
1 parent a11396b058
commit fb81bfbf2e
25 files changed
+1039 -4

No files matched your search

@@ -31,7 +31,7 @@ class ManifestRulesTest {
}
@Test
fun `exactly two components are exported`() {
fun `exactly the launcher, the door and the three widget receivers are exported`() {
val exported = Regex("<(activity|service|receiver|provider)\\b[\\s\\S]*?(?:/>|</\\1>)")
.findAll(text)
.map { it.value }
@@ -39,7 +39,18 @@ class ManifestRulesTest {
.map { Regex("android:name=\"([^\"]+)\"").find(it)!!.groupValues[1] }
.toList()
assertThat(exported).containsExactly(".MainActivity", ".interop.AlarmClockActivity")
// M10 adds exactly three: a GlanceAppWidgetReceiver must be exported
// for the platform to pick it as a widget provider at all, and
// `the three widget receivers answer only APPWIDGET_UPDATE` pins that
// each one is guarded to that one platform-delivered broadcast and
// nothing wider.
assertThat(exported).containsExactly(
".MainActivity",
".interop.AlarmClockActivity",
".widget.NextAlarmWidgetReceiver",
".widget.TimerWidgetReceiver",
".widget.ClockWidgetReceiver",
)
}
@Test
@@ -85,14 +96,48 @@ class ManifestRulesTest {
}
@Test
fun `every receiver is still unexported`() {
fun `every receiver but the three widget providers is still unexported`() {
val widgetReceivers = setOf(
".widget.NextAlarmWidgetReceiver",
".widget.TimerWidgetReceiver",
".widget.ClockWidgetReceiver",
)
val exportedReceivers = Regex("<receiver\\b[\\s\\S]*?(?:/>|</receiver>)")
.findAll(text)
.map { it.value }
.filterNot { it.contains("android:exported=\"false\"") }
.map { Regex("android:name=\"([^\"]+)\"").find(it)!!.groupValues[1] }
.toList()
assertThat(exportedReceivers).isEmpty()
assertThat(exportedReceivers).containsExactlyElementsIn(widgetReceivers)
}
/**
* M10: a `GlanceAppWidgetReceiver` must be exported for the platform to
* offer it as a widget provider, but that is the *only* door it may open —
* each one answers `APPWIDGET_UPDATE` and nothing else, so being exported
* cannot be widened into a second, wider surface by accident.
*/
@Test
fun `the three widget receivers answer only APPWIDGET_UPDATE`() {
val widgetReceivers = listOf(
".widget.NextAlarmWidgetReceiver",
".widget.TimerWidgetReceiver",
".widget.ClockWidgetReceiver",
)
for (name in widgetReceivers) {
val block = Regex("<receiver\\b[\\s\\S]*?(?:/>|</receiver>)")
.findAll(text)
.map { it.value }
.first { it.contains("android:name=\"$name\"") }
val actions = Regex("<action\\s+android:name=\"([^\"]+)\"").findAll(block)
.map { it.groupValues[1] }
.toList()
assertThat(actions).containsExactly("android.appwidget.action.APPWIDGET_UPDATE")
assertThat(block).doesNotContain("android:permission")
}
}
@Test
@@ -0,0 +1,42 @@
package de.jeanlucmakiola.clockula.domain.widget
import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.clockula.domain.WorldClock
import org.junit.jupiter.api.Test
class ClockWidgetModelTest {
private fun clock(id: Long, zoneId: String, sortOrder: Int = 0) =
WorldClock(id = id, zoneId = zoneId, label = null, sortOrder = sortOrder)
@Test
fun `no world clocks is empty`() {
assertThat(ClockWidgetPresenter.present(emptyList())).isEmpty()
}
@Test
fun `a non-IANA zone id is dropped`() {
val clocks = listOf(clock(1, "Not/AZone"), clock(2, "Europe/Berlin"))
val selected = ClockWidgetPresenter.present(clocks)
assertThat(selected.map { it.zoneId }).containsExactly("Europe/Berlin")
}
@Test
fun `at most three cities are shown, in stored order`() {
val clocks = listOf(
clock(1, "Europe/Berlin", sortOrder = 0),
clock(2, "America/New_York", sortOrder = 1),
clock(3, "Asia/Tokyo", sortOrder = 2),
clock(4, "Australia/Sydney", sortOrder = 3),
)
val selected = ClockWidgetPresenter.present(clocks)
assertThat(selected).hasSize(ClockWidgetPresenter.MAX_CITIES)
assertThat(selected.map { it.zoneId }).containsExactly(
"Europe/Berlin", "America/New_York", "Asia/Tokyo",
).inOrder()
}
}
@@ -0,0 +1,83 @@
package de.jeanlucmakiola.clockula.domain.widget
import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.clockula.testing.T0
import de.jeanlucmakiola.clockula.testing.upcomingAlarm
import org.junit.jupiter.api.Test
import java.time.ZoneId
import java.time.ZonedDateTime
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
import kotlin.time.Instant
import kotlin.time.toJavaInstant
class NextAlarmWidgetModelTest {
private val zone: ZoneId = ZoneId.of("UTC")
@Test
fun `no alarms at all reads as empty`() {
val model = NextAlarmWidgetPresenter.present(emptyList(), T0, zone)
assertThat(model.hasAlarm).isFalse()
}
@Test
fun `every alarm with no resolved fire time also reads as empty`() {
val rows = listOf(upcomingAlarm(id = 1, nextFire = null), upcomingAlarm(id = 2, nextFire = null))
val model = NextAlarmWidgetPresenter.present(rows, T0, zone)
assertThat(model.hasAlarm).isFalse()
}
@Test
fun `the first row with a resolved fire time wins, in list order`() {
val fireAt = T0 + 2.hours
val rows = listOf(
upcomingAlarm(id = 1, nextFire = null, label = "no fire"),
upcomingAlarm(id = 2, nextFire = fireAt, label = "Wake up"),
upcomingAlarm(id = 3, nextFire = fireAt + 1.hours, label = "later"),
)
val model = NextAlarmWidgetPresenter.present(rows, T0, zone)
assertThat(model.hasAlarm).isTrue()
assertThat(model.label).isEqualTo("Wake up")
}
@Test
fun `hour and minute are read in the given zone`() {
val fireAt = T0 + 2.hours
val rows = listOf(upcomingAlarm(nextFire = fireAt))
val model = NextAlarmWidgetPresenter.present(rows, T0, zone)
val expected = fireAt.toJavaTime(zone)
assertThat(model.hour).isEqualTo(expected.hour)
assertThat(model.minute).isEqualTo(expected.minute)
assertThat(model.dayOfWeek).isEqualTo(expected.dayOfWeek)
}
@Test
fun `a fire time later today is today`() {
// T0 is 2023-11-14 22:13:20 UTC — one hour later is still the 14th.
val rows = listOf(upcomingAlarm(nextFire = T0 + 1.hours))
val model = NextAlarmWidgetPresenter.present(rows, T0, zone)
assertThat(model.isToday).isTrue()
}
@Test
fun `a fire time tomorrow is not today`() {
val rows = listOf(upcomingAlarm(nextFire = T0 + 1.days))
val model = NextAlarmWidgetPresenter.present(rows, T0, zone)
assertThat(model.isToday).isFalse()
}
private fun Instant.toJavaTime(zone: ZoneId): ZonedDateTime =
ZonedDateTime.ofInstant(toJavaInstant(), zone)
}
@@ -0,0 +1,76 @@
package de.jeanlucmakiola.clockula.domain.widget
import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.clockula.domain.timer.TimerMode
import de.jeanlucmakiola.clockula.testing.T0
import de.jeanlucmakiola.clockula.testing.expiredTimer
import de.jeanlucmakiola.clockula.testing.idleTimer
import de.jeanlucmakiola.clockula.testing.pausedTimer
import de.jeanlucmakiola.clockula.testing.runningTimerWith
import org.junit.jupiter.api.Test
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes
class TimerWidgetModelTest {
@Test
fun `no timers at all is empty`() {
val model = TimerWidgetPresenter.present(emptyList(), Duration.ZERO, T0)
assertThat(model.items).isEmpty()
}
@Test
fun `every timer idle is also empty`() {
val timers = listOf(idleTimer(id = 1), idleTimer(id = 2))
val model = TimerWidgetPresenter.present(timers, Duration.ZERO, T0)
assertThat(model.items).isEmpty()
}
@Test
fun `an expired timer outranks a running one`() {
val timers = listOf(
runningTimerWith(id = 1, left = 1.minutes),
expiredTimer(id = 2),
)
val model = TimerWidgetPresenter.present(timers, Duration.ZERO, T0)
assertThat(model.items.first().id).isEqualTo(2L)
assertThat(model.items.first().mode).isEqualTo(TimerMode.EXPIRED)
}
@Test
fun `at most three timers are shown, in subject order`() {
val timers = (1..5L).map { id -> expiredTimer(id = id) }
val model = TimerWidgetPresenter.present(timers, Duration.ZERO, T0)
assertThat(model.items).hasSize(TimerWidgetPresenter.MAX_ITEMS)
}
@Test
fun `a paused timer keeps its remaining and endsAtWallClock is null`() {
val timers = listOf(pausedTimer(id = 1, remaining = 90.minutes))
val model = TimerWidgetPresenter.present(timers, Duration.ZERO, T0)
val item = model.items.single()
assertThat(item.mode).isEqualTo(TimerMode.PAUSED)
assertThat(item.remaining).isEqualTo(90.minutes)
assertThat(item.endsAtWallClock).isNull()
}
@Test
fun `a running timer carries its wall-clock end, for the widget's chronometer`() {
val timers = listOf(runningTimerWith(id = 1, left = 10.minutes, wallNow = T0))
val model = TimerWidgetPresenter.present(timers, Duration.ZERO, T0)
val item = model.items.single()
assertThat(item.mode).isEqualTo(TimerMode.RUNNING)
assertThat(item.endsAtWallClock).isEqualTo(T0 + 10.minutes)
}
}