From cd88c3e712e14eb39873029419ffc6832138ff4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Bene=C5=A1?= Date: Tue, 10 Nov 2026 13:45:12 +0000 Subject: [PATCH] Scheduled-result notifications: first lines, a lock-screen version, Open / Run again From the push's `notification` (server fd0a386): the task's name and the result's first lines in BigTextStyle for an unlocked phone, setPublicVersion with the task's name and Finished / Failed for the lock screen, and the actions with the server's labels in the owner's language. Run again calls POST /schedules/{id}/run and needs the phone unlocked (setAuthenticationRequired); Pause stays as it was. An older server's payload (no `notification`) keeps the app's fixed sentences. ScheduleContractTest reads push_schedule.json and push_schedule_failed.json. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 5 ++ .../java/eu/llembas/android/api/Models.kt | 6 ++ .../main/java/eu/llembas/android/push/Push.kt | 62 ++++++++++++------- .../llembas/android/push/ScheduledNotice.kt | 33 ++++++++-- app/src/main/res/values-cs/strings.xml | 1 + app/src/main/res/values-de/strings.xml | 1 + app/src/main/res/values-es/strings.xml | 1 + app/src/main/res/values-fr/strings.xml | 1 + app/src/main/res/values-hu/strings.xml | 1 + app/src/main/res/values-it/strings.xml | 1 + app/src/main/res/values-pl/strings.xml | 1 + app/src/main/res/values-sk/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + .../llembas/android/ScheduleContractTest.kt | 29 +++++++++ scripts/strings.py | 1 + 15 files changed, 119 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3b21eb..daafaa6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,11 @@ is in alpha and releases come off `main`. stops it too. - **A scheduled task that watches a web page** says so on its page (*Only when this page changes*, and the address), and a run that found the page unchanged is shown as *Unchanged*. +- **A scheduled task's notification shows its result.** Unlocked, it carries the task's name + and the first lines of what it found (pull it open for more); on the lock screen only the + task's name and *Finished* or *Failed*. **Run again** starts the task again from the + notification, once the phone is unlocked. Needs an instance that sends these; an older one + keeps the fixed sentence. - **A message waiting behind a reply says how many are ahead of it** and for how long it has waited, where the instance tells. diff --git a/app/src/main/java/eu/llembas/android/api/Models.kt b/app/src/main/java/eu/llembas/android/api/Models.kt index 92678f6..554c534 100644 --- a/app/src/main/java/eu/llembas/android/api/Models.kt +++ b/app/src/main/java/eu/llembas/android/api/Models.kt @@ -557,6 +557,12 @@ data class PushPayload( val status: String? = null, val visibility: String? = null, val actions: List = emptyList(), + /** + * The run's notification as the server wrote it (newer servers; the payload is encrypted end + * to end): the task's name and the result's first lines, the lock-screen version, and the + * actions' labels in the owner's language. Null from an older server: the fixed sentence then. + */ + val notification: RunNotification? = null, ) // --- The activity panel (`GET /chats/{chat}/activity`) ------------------------------------- diff --git a/app/src/main/java/eu/llembas/android/push/Push.kt b/app/src/main/java/eu/llembas/android/push/Push.kt index e202777..6cf3167 100644 --- a/app/src/main/java/eu/llembas/android/push/Push.kt +++ b/app/src/main/java/eu/llembas/android/push/Push.kt @@ -134,10 +134,11 @@ object Notifications { /** * A scheduled run's result or failure, on its own channel, built from the server's payload - * alone ([ScheduledNotice]): the payload carries ids and one of the server's own fixed - * sentences, never the result. Unlocked: the instance's name and that sentence. On the lock - * screen the public version: the app's name and "a scheduled task has a result" (or failed). - * A tap and Open go to the run; Pause is there when the server offers it. + * alone ([ScheduledNotice]). Unlocked: the task's name and the result's first lines, as the + * server cut them (BigTextStyle opens them). On the lock screen only the public version: the + * task's name and Finished / Failed (the owner's choice for beta.9; an older server sends no + * words, and the app's name and a fixed sentence stand in). A tap and Open go to the run; + * Run again starts it again, once the phone is unlocked; Pause is there when offered. */ private fun scheduled(context: Context, instance: String, payload: PushPayload) { val notice = ScheduledNotice.from(payload) ?: return @@ -146,13 +147,14 @@ object Notifications { val open = opener(context, instance, notice.id, chatId = null, scheduleId = notice.scheduleId, runId = notice.runId) val public = NotificationCompat.Builder(context, channel) .setSmallIcon(R.drawable.ic_notification) - .setContentTitle(context.getString(R.string.app_name)) - .setContentText(fixed) + .setContentTitle(notice.publicTitle.ifBlank { context.getString(R.string.app_name) }) + .setContentText(notice.publicBody.ifBlank { fixed }) .build() - val body = payload.body.ifBlank { fixed } + val title = notice.title.ifBlank { payload.title.ifBlank { context.getString(R.string.scheduled) } } + val body = notice.body.ifBlank { payload.body.ifBlank { fixed } } val builder = NotificationCompat.Builder(context, channel) .setSmallIcon(R.drawable.ic_notification) - .setContentTitle(payload.title.ifBlank { context.getString(R.string.scheduled) }) + .setContentTitle(title) .setContentText(body) .setStyle(NotificationCompat.BigTextStyle().bigText(body)) .setAutoCancel(true) @@ -160,8 +162,15 @@ object Notifications { .setCategory(if (notice.failed) Notification.CATEGORY_ERROR else Notification.CATEGORY_STATUS) .setVisibility(NotificationCompat.VISIBILITY_PRIVATE) .setPublicVersion(public) - if (notice.open) builder.addAction(0, context.getString(R.string.viewer_open), open) - if (notice.pause) builder.addAction(0, context.getString(R.string.schedule_pause), pause(context, instance, notice.scheduleId, notice.id)) + if (notice.open) builder.addAction(0, notice.label("open", context.getString(R.string.viewer_open)), open) + if (notice.runAgain) { + val again = scheduleAction(context, instance, notice.scheduleId, notice.id, ScheduleActionReceiver.RUN_AGAIN) + builder.addAction(unlocked(notice.label("run_again", context.getString(R.string.schedule_run_again)), again)) + } + if (notice.pause) { + val pause = scheduleAction(context, instance, notice.scheduleId, notice.id, ScheduleActionReceiver.PAUSE) + builder.addAction(0, notice.label("pause", context.getString(R.string.schedule_pause)), pause) + } NotificationManagerCompat.from(context).notify(notice.id, builder.build()) } @@ -182,15 +191,17 @@ object Notifications { PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT, ) - private fun pause(context: Context, instance: String, schedule: String, notification: Int): PendingIntent = PendingIntent.getBroadcast( - context, - ("pause$schedule").hashCode(), - Intent(context, ScheduleActionReceiver::class.java) - .putExtra("instance", instance) - .putExtra("schedule", schedule) - .putExtra("notification", notification), - PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT, - ) + private fun scheduleAction(context: Context, instance: String, schedule: String, notification: Int, action: String): PendingIntent = + PendingIntent.getBroadcast( + context, + (action + schedule).hashCode(), + Intent(context, ScheduleActionReceiver::class.java) + .putExtra("instance", instance) + .putExtra("schedule", schedule) + .putExtra("action", action) + .putExtra("notification", notification), + PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT, + ) private fun answer(context: Context, instance: String, chat: String, card: String, verdict: String, notification: Int): PendingIntent = PendingIntent.getBroadcast( @@ -229,16 +240,20 @@ class CardActionReceiver : BroadcastReceiver() { } } -/** A scheduled task's Pause, from its notification. */ +/** A scheduled task's Pause or Run again, from its notification. */ class ScheduleActionReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val pending = goAsync() val instance = intent.getStringExtra("instance") ?: return pending.finish() val schedule = intent.getStringExtra("schedule") ?: return pending.finish() + val action = intent.getStringExtra("action") ?: PAUSE background.launch { try { Push.instance(context, instance)?.let { found -> - runCatching { context.lembas.instances.clientFor(found).pauseSchedule(schedule, true) } + val client = context.lembas.instances.clientFor(found) + // A failure (gone, busy) is the server's to say on the run page; the + // notification goes either way, and a new run sends its own. + runCatching { if (action == RUN_AGAIN) client.runScheduleNow(schedule) else client.pauseSchedule(schedule, true) } } NotificationManagerCompat.from(context).cancel(intent.getIntExtra("notification", 0)) } finally { @@ -246,4 +261,9 @@ class ScheduleActionReceiver : BroadcastReceiver() { } } } + + companion object { + const val PAUSE = "pause" + const val RUN_AGAIN = "run_again" + } } diff --git a/app/src/main/java/eu/llembas/android/push/ScheduledNotice.kt b/app/src/main/java/eu/llembas/android/push/ScheduledNotice.kt index d3985d5..d89e8b2 100644 --- a/app/src/main/java/eu/llembas/android/push/ScheduledNotice.kt +++ b/app/src/main/java/eu/llembas/android/push/ScheduledNotice.kt @@ -5,8 +5,11 @@ import eu.llembas.android.api.PushPayload /** * What a scheduled run's push asks the app to show, read from the server's payload alone * (services/schedule/notify.py `payload`): the schedule and the run it is about, whether it - * failed, and the actions the server offers. A payload without ids falls back to its `url` - * (`/scheduled/{schedule}?run={run}`), as a browser would open it. + * failed, the actions the server offers, and -- from a server that sends its `notification` -- + * the words: [title] and [body] (the result's first lines) for an unlocked phone, [publicTitle] + * and [publicBody] (the task's name, Finished / Failed) for the lock screen, and the actions' + * [labels]. A payload without ids falls back to its `url` (`/scheduled/{schedule}?run={run}`), + * as a browser would open it. */ data class ScheduledNotice( val scheduleId: String, @@ -16,10 +19,20 @@ data class ScheduledNotice( val askPause: Boolean, val open: Boolean, val pause: Boolean, + val runAgain: Boolean = false, + /** Empty when the server sent no `notification`: the app's fixed sentences are used then. */ + val title: String = "", + val body: String = "", + val publicTitle: String = "", + val publicBody: String = "", + val labels: Map = emptyMap(), ) { /** One notification per run: a later push about the same run replaces it. */ val id: Int get() = "scheduled:${runId ?: scheduleId}".hashCode() + /** The server's label for [action] in the owner's language, or [fallback]. */ + fun label(action: String, fallback: String): String = labels[action]?.takeIf { it.isNotBlank() } ?: fallback + companion object { const val KIND_RESULT = "schedule" const val KIND_FAILED = "schedule_failed" @@ -30,9 +43,13 @@ data class ScheduledNotice( val schedule = payload.scheduleId?.takeIf { it.isNotBlank() } ?: urlSchedule ?: return null val run = payload.runId?.takeIf { it.isNotBlank() } ?: urlRun val failed = payload.kind == KIND_FAILED - val askPause = payload.ask == "pause" - // An older payload named no actions: Open always, Pause for a failure. - val actions = payload.actions.ifEmpty { if (failed) listOf("open", "pause") else listOf("open") } + val words = payload.notification + val ask = words?.ask?.takeIf { it.isNotBlank() } ?: payload.ask + val askPause = ask == "pause" + // The notification's own actions first; an older payload named none: Open always, + // Pause for a failure. + val actions = words?.actions?.takeIf { it.isNotEmpty() } + ?: payload.actions.ifEmpty { if (failed) listOf("open", "pause") else listOf("open") } return ScheduledNotice( scheduleId = schedule, runId = run, @@ -40,6 +57,12 @@ data class ScheduledNotice( askPause = askPause, open = "open" in actions, pause = "pause" in actions || askPause, + runAgain = "run_again" in actions, + title = words?.title.orEmpty(), + body = words?.body.orEmpty(), + publicTitle = words?.public?.title.orEmpty(), + publicBody = words?.public?.body.orEmpty(), + labels = words?.labels.orEmpty(), ) } diff --git a/app/src/main/res/values-cs/strings.xml b/app/src/main/res/values-cs/strings.xml index e6f4273..567da4a 100644 --- a/app/src/main/res/values-cs/strings.xml +++ b/app/src/main/res/values-cs/strings.xml @@ -218,6 +218,7 @@ Selhání naplánovaných úloh Naplánovaná úloha má výsledek. Naplánovaná úloha selhala. + Spustit znovu Povolit to? Povolit jednou Toto vždy povolit diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index cc52ee4..44e1177 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -218,6 +218,7 @@ Geplante Fehler Eine geplante Aufgabe hat ein Ergebnis. Eine geplante Aufgabe ist fehlgeschlagen. + Erneut ausführen Das erlauben? Einmal erlauben Dies immer erlauben diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 1f1f21e..68372be 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -218,6 +218,7 @@ Fallos programados Una tarea programada tiene un resultado. Una tarea programada falló. + Ejecutar de nuevo ¿Permitir esto? Permitir una vez Permitir siempre esto diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index ad3cfd9..8aad5a0 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -218,6 +218,7 @@ Échecs planifiés Une tâche planifiée a un résultat. Une tâche planifiée a échoué. + Relancer Autoriser ceci ? Autoriser une fois Toujours autoriser ceci diff --git a/app/src/main/res/values-hu/strings.xml b/app/src/main/res/values-hu/strings.xml index 07b7444..a21e72b 100644 --- a/app/src/main/res/values-hu/strings.xml +++ b/app/src/main/res/values-hu/strings.xml @@ -218,6 +218,7 @@ Ütemezett hibák Egy ütemezett feladatnak eredménye van. Egy ütemezett feladat sikertelen volt. + Futtatás újra Engedélyezzük ezt? Engedélyezés egyszer Ennek mindig engedélyezése diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 3f7c9ab..328e2fb 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -218,6 +218,7 @@ Errori pianificati Un’attività pianificata ha un risultato. Un’attività pianificata non è riuscita. + Esegui di nuovo Consentire questo? Consenti una volta Consenti sempre questo diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 936a104..b6cdd26 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -218,6 +218,7 @@ Błędy zaplanowanych zadań Zaplanowane zadanie ma wynik. Zaplanowane zadanie się nie powiodło. + Uruchom ponownie Zezwolić na to? Zezwól raz Zawsze na to zezwalaj diff --git a/app/src/main/res/values-sk/strings.xml b/app/src/main/res/values-sk/strings.xml index 759d1ff..ed679b6 100644 --- a/app/src/main/res/values-sk/strings.xml +++ b/app/src/main/res/values-sk/strings.xml @@ -218,6 +218,7 @@ Zlyhania naplánovaných úloh Naplánovaná úloha má výsledok. Naplánovaná úloha zlyhala. + Spustiť znova Povoliť to? Povoliť raz Toto vždy povoliť diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 62a7f94..d3a7b20 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -219,6 +219,7 @@ Scheduled failures A scheduled task has a result. A scheduled task failed. + Run again Allow this? Allow once Always allow this diff --git a/app/src/test/java/eu/llembas/android/ScheduleContractTest.kt b/app/src/test/java/eu/llembas/android/ScheduleContractTest.kt index e4c8853..fe07acc 100644 --- a/app/src/test/java/eu/llembas/android/ScheduleContractTest.kt +++ b/app/src/test/java/eu/llembas/android/ScheduleContractTest.kt @@ -209,6 +209,35 @@ class ScheduleContractTest { assertTrue(notice.id != notice.copy(runId = "run3").id) } + @Test + fun theServersNotificationWordsAndRunAgain() { + // `push_schedule.json`: the task's name and first lines unlocked, Finished on the lock + // screen, Open and Run again with the owner's labels. + val done = ScheduledNotice.from(read("push_schedule", PushPayload.serializer()))!! + assertEquals("Waybread check", done.title) + assertEquals("All the waybread is wrapped.", done.body) + assertEquals("Waybread check", done.publicTitle) + assertEquals("Finished", done.publicBody) + assertTrue(done.open) + assertTrue(done.runAgain) + assertFalse(done.pause) + assertEquals("Run again", done.label("run_again", "x")) + assertEquals("x", done.label("pause", "x")) + + // `push_schedule_failed.json`: three failures ask about Pause, beside Open and Run again. + val failed = ScheduledNotice.from(read("push_schedule_failed", PushPayload.serializer()))!! + assertTrue(failed.failed) + assertTrue(failed.askPause) + assertTrue(failed.pause) + assertTrue(failed.runAgain) + assertEquals("Failed", failed.publicBody) + assertEquals("run3", failed.runId) + + // The run's own copy (the schedules frame) says the same. + val run = read("schedule_run", RunResult.serializer()).run + assertEquals(listOf("open", "run_again"), run.notification!!.actions) + } + @Test fun aPushWithOnlyItsUrlAndOneThatIsNotScheduled() { val bare = PushPayload(kind = "schedule", url = "/scheduled/sched1?run=run1") diff --git a/scripts/strings.py b/scripts/strings.py index c317a91..58295ed 100755 --- a/scripts/strings.py +++ b/scripts/strings.py @@ -235,6 +235,7 @@ STRINGS: dict[str, str] = { "channel_scheduled_failures": "Scheduled failures", "scheduled_result_public": "A scheduled task has a result.", "scheduled_failed_public": "A scheduled task failed.", + "schedule_run_again": "Run again", # Cards "card_approval": "Allow this?", "card_allow_once": "Allow once",