liveactivity 0.11.0

Framework-neutral Live Activities: one interface over ActivityKit, Android Live Updates and other backends
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
use serde::{Deserialize, Deserializer, Serialize};

fn non_null<'de, D: Deserializer<'de>, T: Deserialize<'de>>(
    d: D,
) -> std::result::Result<Option<T>, D::Error> {
    T::deserialize(d).map(Some)
}

/// A countdown, in epoch milliseconds.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Timer {
    /// Start of the countdown.
    pub started_at: u64,
    /// End of the countdown, after `started_at`.
    pub ends_at: u64,
}

/// The content of a template activity: a title, a status and either a progress or a timer.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Snapshot {
    /// The revision of this content: higher than the last accepted one.
    pub revision: u64,
    /// The headline, 1 to 120 characters.
    pub title: String,
    /// The status line, 1 to 240 characters.
    pub status: String,
    #[serde(
        default,
        deserialize_with = "non_null",
        skip_serializing_if = "Option::is_none"
    )]
    /// The progress from 0 to 1, for the progress template.
    pub progress: Option<f64>,
    #[serde(
        default,
        deserialize_with = "non_null",
        skip_serializing_if = "Option::is_none"
    )]
    /// The countdown, for the timer template.
    pub timer: Option<Timer>,
    #[serde(
        default,
        deserialize_with = "non_null",
        skip_serializing_if = "Option::is_none"
    )]
    /// When the platform may show the content as out of date, in epoch milliseconds.
    pub stale_at: Option<u64>,
}

/// The two templates. More may come: match with a `_` arm.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Template {
    /// A title, a status and a progress bar.
    Progress,
    /// A title, a status and a countdown.
    Timer,
}

/// What a start does when the platform cannot give the activity its native presentation.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Fallback {
    /// Fail with `UNSUPPORTED_FEATURE` and show nothing.
    None,
    /// Show the activity in the platform's ordinary presentation.
    Notification,
}

/// The input of `start`: a progress or timer activity.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct StartInput {
    /// The application's own name of the activity: a running activity with the same key and request is the same activity.
    pub activity_key: String,
    /// The version of the request contract: 1 for a template, 2 for a layout.
    pub schema_version: u8,
    /// The template, fixed at start.
    pub template: Template,
    /// What to do when the platform cannot give the activity its native presentation.
    pub fallback: Fallback,
    /// The content, with its revision.
    pub snapshot: Snapshot,
}

/// The input of `update`: the whole content again, under a higher revision.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct UpdateInput {
    /// The handle of the activity, new for every start.
    pub id: String,
    /// The content, with its revision.
    pub snapshot: Snapshot,
}

/// When an ended activity leaves the screen. More may come: match with a `_` arm.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase", deny_unknown_fields)]
pub enum EndPolicy {
    /// Remove the activity from the screen at once.
    Immediate {},
    /// Leave it to the platform.
    Default {},
    /// Keep the ended activity on screen until a time. iOS keeps it for at most four hours; Android keeps its last
    /// content where the device offers `delayedEnd`, and answers `UNSUPPORTED_FEATURE` elsewhere.
    After {
        /// Until when, in epoch milliseconds.
        at: u64,
    },
}

/// The input of `end`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct EndInput {
    /// The handle of the activity, new for every start.
    pub id: String,
    /// When the ended activity leaves the screen.
    pub policy: EndPolicy,
}

/// A permission that a request can ask for.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum PermissionKind {
    /// Posting notifications (Android).
    Notifications,
    /// Showing Live Activities (iOS).
    LiveActivities,
    /// Promoting a notification to a Live Update (Android).
    Promotion,
}

/// Whether a backend provides a feature.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum FeatureSupport {
    /// Provided on this device.
    Supported,
    /// Not provided on this platform or device.
    Unsupported,
    /// Not known, or a value this version does not name.
    Unknown,
}

/// Which backend shows an activity.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Backend {
    /// ActivityKit, on iOS.
    ActivityKit,
    /// An Android notification, promoted to a Live Update where Android allows.
    AndroidNotification,
    /// Not provided on this platform or device.
    Unsupported,
}

/// What the backend can do on this device. A feature is supported, unsupported or unknown, and unknown never counts as supported.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Capabilities {
    /// The platform the backend runs on, such as `ios` or `android`.
    pub platform: String,
    /// Which backend shows the activity.
    pub backend: Backend,
    /// Starting activities.
    pub start: FeatureSupport,
    /// Updating activities.
    pub update: FeatureSupport,
    /// Ending activities.
    pub end: FeatureSupport,
    /// The progress template.
    pub progress: FeatureSupport,
    /// The timer template.
    pub timer: FeatureSupport,
    /// Buttons whose taps reach the event sink.
    pub actions: FeatureSupport,
    /// Reading an activity's state back from the platform.
    pub state_observation: FeatureSupport,
    /// Updating without alerting the person.
    pub silent_update: FeatureSupport,
    /// The platform's native presentation: a Live Activity, or a promoted Live Update.
    pub native_live_activity: FeatureSupport,
    /// The largest content the backend takes, in bytes.
    pub max_payload_bytes: u64,
    /// Layout activities of request contract v2; a native side that predates them omits it.
    #[serde(default = "unsupported")]
    pub layout: FeatureSupport,
    /// Pushes: tokens, channels and push-to-start (iOS); a native side that predates them omits it.
    #[serde(default = "unsupported")]
    pub push: FeatureSupport,
    /// Which notification styles the device draws and which extensions it offers, for the Android content of a
    /// snapshot and for what follows a device: another style in an update, an update that alerts, a last content
    /// that stays. Present when the Android backend runs layouts.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub android_presentation: Option<crate::layout::PresentationCapabilities>,
    /// What an iPhone runs of the features after the first Live Activities. Present on iOS.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ios: Option<IosCapabilities>,
}

/// What an iPhone runs of the features after the first Live Activities, as ActivityKit's authorization says: an
/// application need not try a request to learn it. Each is false on a device without Live Activities.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IosCapabilities {
    /// A push-to-start token: iOS 17.2.
    #[serde(default)]
    pub push_to_start: bool,
    /// A start that subscribes to a broadcast channel: iOS 18.
    #[serde(default)]
    pub push_channels: bool,
    /// A transient start (`ios.transient`): iOS 18.
    #[serde(default)]
    pub transient: bool,
    /// A scheduled start: iOS 26, in an application built with the iOS 26 SDK.
    #[serde(default)]
    pub scheduled_start: bool,
    /// A layout's `small` region on a paired Apple Watch and in CarPlay: iOS 18, and the widget extension draws the
    /// small family.
    #[serde(default)]
    pub small_family: bool,
}

fn unsupported() -> FeatureSupport {
    FeatureSupport::Unsupported
}

fn not_applicable() -> PermissionState {
    PermissionState::NotApplicable
}

/// The state of one permission.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum PermissionState {
    /// Allowed.
    Granted,
    /// Refused.
    Denied,
    /// Not asked yet: a request shows the system's prompt.
    Prompt,
    /// Only the person can change it, in the system settings.
    SettingsRequired,
    /// The platform has no such thing.
    NotApplicable,
    /// Not known, or a value this version does not name.
    Unknown,
}

/// What the operating system and the person allow.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Permissions {
    /// Posting notifications (Android).
    pub notifications: PermissionState,
    /// Showing Live Activities (iOS).
    pub live_activities: PermissionState,
    /// Promoting a notification to a Live Update (Android).
    pub promotion: PermissionState,
    /// Frequent pushes, a switch in Settings on iOS (`granted` or `denied`); `notApplicable` where there are no
    /// pushes. A native side that predates it omits it.
    #[serde(default = "not_applicable")]
    pub frequent_pushes: PermissionState,
}

/// The lifecycle of an activity. More states may come, so a match needs a `_` arm.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum ActivityState {
    /// Scheduled and not started yet: the state of a scheduled start until its time, on iOS 26 and on Android.
    Pending,
    /// On screen and taking updates.
    Active,
    /// Past its stale date; it may still be on screen.
    Stale,
    /// Ended by the application; the platform may still show it.
    Ended,
    /// Gone from the screen.
    Dismissed,
    /// Not known, or a value this version does not name.
    Unknown,
}

/// What is known of an activity being on screen.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Visibility {
    /// Not known, or a value this version does not name.
    Unknown,
}

/// What Android did with a request to promote a notification to a Live Update.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum PromotionState {
    /// Android shows the notification as a Live Update.
    Promoted,
    /// Android took the notification without promoting it.
    NotPromoted,
    /// Not known, or a value this version does not name.
    Unknown,
    /// The platform has no such thing.
    NotApplicable,
}

/// A template activity as the backend reports it.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ActivityRecord {
    /// The handle of the activity, new for every start.
    pub id: String,
    /// The application's own name of the activity: a running activity with the same key and request is the same activity.
    pub activity_key: String,
    /// The version of the request contract: 1 for a template, 2 for a layout.
    pub schema_version: u8,
    /// The template, fixed at start.
    pub template: Template,
    /// The content, with its revision.
    pub snapshot: Snapshot,
    /// Which backend shows the activity.
    pub backend: Backend,
    /// The lifecycle state the platform reports.
    pub state: ActivityState,
    /// What is known of the activity being on screen.
    pub visibility: Visibility,
    /// What the platform left out of the request, such as `nativeLiveActivity` or `layout.<node>`.
    pub degraded_features: Vec<String>,
    /// Whether promotion to a Live Update was requested (Android).
    pub promotion_requested: bool,
    /// What Android did with a request to promote the notification.
    pub promotion_state: PromotionState,
}

impl Timer {
    /// The countdown from `started_at` to `ends_at`, both in epoch milliseconds.
    #[must_use]
    pub fn new(started_at: u64, ends_at: u64) -> Self {
        Self {
            started_at,
            ends_at,
        }
    }
}

impl Snapshot {
    /// The same snapshot, shown as out of date from `at`, in epoch milliseconds.
    #[must_use]
    pub fn stale_at(self, at: u64) -> Self {
        Self {
            stale_at: Some(at),
            ..self
        }
    }

    /// The content of a progress activity at `revision`, with `progress` from 0 to 1 and no stale time.
    #[must_use]
    pub fn progress(revision: u64, title: &str, status: &str, progress: f64) -> Self {
        Self {
            revision,
            title: title.into(),
            status: status.into(),
            progress: Some(progress),
            timer: None,
            stale_at: None,
        }
    }

    /// The content of a timer activity at `revision`, which counts down from `started_at` to `ends_at`, in epoch
    /// milliseconds, and has no stale time.
    #[must_use]
    pub fn timer(revision: u64, title: &str, status: &str, started_at: u64, ends_at: u64) -> Self {
        Self {
            revision,
            title: title.into(),
            status: status.into(),
            progress: None,
            timer: Some(Timer::new(started_at, ends_at)),
            stale_at: None,
        }
    }
}

impl StartInput {
    /// The same start with content that is shown as out of date from `at`, in epoch milliseconds.
    #[must_use]
    pub fn stale(self, at: u64) -> Self {
        Self {
            snapshot: self.snapshot.stale_at(at),
            ..self
        }
    }

    fn of(activity_key: &str, template: Template, snapshot: Snapshot) -> Self {
        Self {
            activity_key: activity_key.into(),
            schema_version: 1,
            template,
            fallback: Fallback::Notification,
            snapshot,
        }
    }

    /// A start that shows `snapshot` under the application's own `activity_key`, with the template that the
    /// snapshot is of: the timer's when it has a timer, the progress's otherwise. As the two constructors below
    /// it falls back to an ordinary notification.
    #[must_use]
    pub fn new(activity_key: impl Into<String>, snapshot: Snapshot) -> Self {
        let template = if snapshot.timer.is_some() {
            Template::Timer
        } else {
            Template::Progress
        };
        Self::of(&activity_key.into(), template, snapshot)
    }

    /// A progress activity under the application's own `activity_key`, with `progress` from 0 to 1.
    pub fn progress(activity_key: &str, title: &str, status: &str, progress: f64) -> Self {
        Self::of(
            activity_key,
            Template::Progress,
            Snapshot::progress(0, title, status, progress),
        )
    }

    /// A countdown from `started_at` to `ends_at`, in epoch milliseconds.
    pub fn timer(
        activity_key: &str,
        title: &str,
        status: &str,
        started_at: u64,
        ends_at: u64,
    ) -> Self {
        Self::of(
            activity_key,
            Template::Timer,
            Snapshot::timer(0, title, status, started_at, ends_at),
        )
    }

    /// The same start, failing with `UNSUPPORTED_FEATURE` where the platform cannot give the activity its native
    /// presentation, instead of showing an ordinary notification.
    #[must_use]
    pub fn without_fallback(self) -> Self {
        Self {
            fallback: Fallback::None,
            ..self
        }
    }
}

impl UpdateInput {
    /// The content `snapshot` for the activity `id`.
    #[must_use]
    pub fn new(id: impl Into<String>, snapshot: Snapshot) -> Self {
        Self {
            id: id.into(),
            snapshot,
        }
    }

    /// New content of the progress activity `id` under `revision`, which is higher than the last.
    pub fn progress(id: &str, revision: u64, title: &str, status: &str, progress: f64) -> Self {
        Self {
            id: id.into(),
            snapshot: Snapshot::progress(revision, title, status, progress),
        }
    }

    /// New content of the timer activity `id` under `revision`.
    pub fn timer(
        id: &str,
        revision: u64,
        title: &str,
        status: &str,
        started_at: u64,
        ends_at: u64,
    ) -> Self {
        Self {
            id: id.into(),
            snapshot: Snapshot::timer(revision, title, status, started_at, ends_at),
        }
    }
}

impl EndInput {
    /// Ends the activity `id` the platform's default way.
    pub fn new(id: &str) -> Self {
        Self {
            id: id.into(),
            policy: EndPolicy::Default {},
        }
    }

    /// Ends the activity `id` and removes it from the screen at once.
    pub fn immediate(id: &str) -> Self {
        Self {
            id: id.into(),
            policy: EndPolicy::Immediate {},
        }
    }

    /// Ends the activity `id` and removes it from the screen at `at`, in epoch milliseconds.
    #[must_use]
    pub fn after(id: &str, at: u64) -> Self {
        Self {
            id: id.into(),
            policy: EndPolicy::After { at },
        }
    }
}