liveactivity 0.9.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
//! An in-memory Android driver and a string bridge over it. The driver behaves like the Android side: `post` makes
//! a notification observable, `cancel` removes it, and storage keeps exactly what `write` received. The post of
//! an ended record is its last content, which Android shows until its time and no longer observes. A post whose
//! content names a URL of the scheme [`UNOPENED`] is refused, as Android refuses a URL that nothing in the
//! application opens. A scheduled receipt is posted when the device's clock has reached its time: by
//! [`MemoryDriver::fire_alarms`], which is the alarm while the application does not run, or when the lifecycle asks
//! for the starts, which is how a start whose alarm was lost begins.
#![allow(dead_code)]

use liveactivity::liveupdate::{
    self, ActivityEvent, ActivityState, Bridge, Capabilities, Driver, DriverRecord, EventBatch,
    Observation, PresentationCapabilities, PromotionState, StartOutcome, StartReport,
};
use serde::de::DeserializeOwned;
use serde_json::{Value, json};
use std::{
    collections::{BTreeMap, BTreeSet},
    sync::{Arc, Mutex, MutexGuard},
};

/// The scheme of a URL that no activity of the application opens.
pub const UNOPENED: &str = "nobody-opens-this:";

pub struct MemoryState {
    pub capabilities: Capabilities,
    pub records: Vec<DriverRecord>,
    pub observed: BTreeMap<String, Observation>,
    /// The ended records posted as their last content, by id.
    pub finals: BTreeMap<String, DriverRecord>,
    pub events: EventBatch,
    pub posts: usize,
    /// How many of the next posts fail as a platform that could not post, whatever they hold.
    pub failing_posts: usize,
    /// How often the capabilities were asked.
    pub capability_calls: usize,
    /// The device's clock, in epoch milliseconds: a notification whose stale time has come is listed as stale.
    pub now: u64,
    /// While set, a post is not listed yet, as when Android shows a notification a moment after it was posted.
    pub delay_listing: bool,
    /// The posts that are not listed yet, by id.
    pub unlisted: BTreeMap<String, Observation>,
    /// How often Android was asked what it shows.
    pub observations: usize,
    /// How many of the next such questions fail.
    pub failing_observations: usize,
    /// The scheduled receipts that the Android side has begun, by id: each is posted once.
    pub begun: BTreeSet<String>,
    /// How often a receipt was handed over to begin at its time.
    pub schedules: usize,
    /// How many of the next such hand-overs fail, as an alarm that could not be set.
    pub failing_schedules: usize,
    /// The ids the lifecycle asked to take away: a notification, or the alarm of a scheduled receipt.
    pub cancelled: Vec<String>,
}

impl MemoryState {
    /// Counts a post, and fails it while posts are set to fail.
    fn attempt(&mut self) -> liveupdate::Result<()> {
        self.posts += 1;
        if self.failing_posts > 0 {
            self.failing_posts -= 1;
            return Err(liveupdate::Error::new(
                liveupdate::ErrorCode::NativeError,
                "Posting failed.",
            ));
        }
        Ok(())
    }

    /// Shows `record` as Android does: listed at once, or a moment later while `delay_listing` is set.
    fn list(&mut self, record: &DriverRecord) {
        let promotion_state = if record.promotion_requested {
            PromotionState::Promoted
        } else {
            PromotionState::NotApplicable
        };
        let observation = Observation {
            id: record.id.clone(),
            snapshot: record.snapshot.clone(),
            state: ActivityState::Active,
            promotion_requested: record.promotion_requested,
            promotion_state,
        };
        if self.delay_listing {
            self.unlisted.insert(record.id.clone(), observation);
        } else {
            self.observed.insert(record.id.clone(), observation);
        }
    }

    /// Posts a scheduled receipt whose time has come. One that cannot be posted shows nothing.
    fn begin(&mut self, record: &DriverRecord) {
        self.begun.insert(record.id.clone());
        if self.attempt().is_ok() {
            self.list(record);
        }
    }

    /// Begins the scheduled receipts whose time has come, each once. A receipt whose hand-over was not confirmed
    /// has no alarm: it waits for the start that hands it over again.
    fn begin_due(&mut self) {
        let due = |record: &&DriverRecord| {
            record.state == ActivityState::Scheduled
                && !record.post_failed
                && record.origin.start_at.is_some_and(|at| at <= self.now)
                && !self.begun.contains(&record.id)
        };
        let due: Vec<DriverRecord> = self.records.iter().filter(due).cloned().collect();
        for record in due {
            self.begin(&record);
        }
    }
}

/// Whether `record` names a URL that nothing in the application opens.
fn unopened(record: &DriverRecord) -> bool {
    record.snapshot.presentation.as_ref().is_some_and(|shown| {
        let of_actions = shown.actions.iter().map(|action| &action.open_url);
        std::iter::once(&shown.open_url)
            .chain(of_actions)
            .flatten()
            .any(|url| url.starts_with(UNOPENED))
    })
}

#[derive(Clone)]
pub struct MemoryDriver {
    state: Arc<Mutex<MemoryState>>,
}

impl Default for MemoryDriver {
    fn default() -> Self {
        Self {
            state: Arc::new(Mutex::new(MemoryState {
                capabilities: Capabilities {
                    supported: true,
                    notifications_enabled: true,
                    promotion_enabled: false,
                    presentation: PresentationCapabilities {
                        actions: true,
                        events: true,
                        ..PresentationCapabilities::default()
                    },
                },
                records: Vec::new(),
                observed: BTreeMap::new(),
                finals: BTreeMap::new(),
                events: EventBatch::default(),
                posts: 0,
                failing_posts: 0,
                capability_calls: 0,
                now: 0,
                delay_listing: false,
                unlisted: BTreeMap::new(),
                observations: 0,
                failing_observations: 0,
                begun: BTreeSet::new(),
                schedules: 0,
                failing_schedules: 0,
                cancelled: Vec::new(),
            })),
        }
    }
}

impl MemoryDriver {
    pub fn lock(&self) -> MutexGuard<'_, MemoryState> {
        self.state.lock().unwrap()
    }

    /// Queues a tap on a notification action, as the Android side does.
    pub fn tap(&self, id: &str, sequence: u64, action: &str) {
        let event: ActivityEvent = serde_json::from_value(json!({
            "sequence": sequence,
            "activityId": id,
            "occurredAt": 1_800_000_000_000_u64,
            "kind": {"kind": "action", "actionId": action},
        }))
        .unwrap();
        self.lock().events.events.push(event);
    }
}

impl MemoryDriver {
    /// Lists the posts that were held back, as Android does a moment after they were posted.
    pub fn show_unlisted(&self) {
        let mut state = self.lock();
        state.delay_listing = false;
        let shown = std::mem::take(&mut state.unlisted);
        state.observed.extend(shown);
    }

    /// The person removes a notification: it is gone, and the Android side queues the dismissal.
    pub fn dismiss(&self, id: &str, sequence: u64) {
        self.lock().observed.remove(id);
        let event: ActivityEvent = serde_json::from_value(json!({
            "sequence": sequence,
            "activityId": id,
            "occurredAt": 1_800_000_000_000_u64,
            "kind": {"kind": "dismissed"},
        }))
        .unwrap();
        self.lock().events.events.push(event);
    }

    /// Queues what the Android side queues when the host reports that the person opened the application from
    /// the notification, by its content or by `action`.
    pub fn open(&self, id: &str, sequence: u64, action: Option<&str>) {
        let mut kind = json!({"kind": "opened"});
        if let Some(action) = action {
            kind["actionId"] = json!(action);
        }
        let event: ActivityEvent = serde_json::from_value(json!({
            "sequence": sequence,
            "activityId": id,
            "occurredAt": 1_800_000_000_000_u64,
            "kind": kind,
        }))
        .unwrap();
        self.lock().events.events.push(event);
    }

    /// Queues an event of the kind `kind`, which this liveupdate does not know, as a later AAR stores one.
    pub fn later(&self, id: &str, sequence: u64, kind: &str) {
        let event: ActivityEvent = serde_json::from_value(json!({
            "sequence": sequence,
            "activityId": id,
            "occurredAt": 1_800_000_000_000_u64,
            "kind": {"kind": kind, "until": 1_800_000_060_000_u64},
        }))
        .unwrap();
        assert_eq!(event.kind, liveupdate::EventKind::Unknown);
        self.lock().events.events.push(event);
    }

    /// The alarm of the Android side, which fires while the application does not run: the scheduled receipts
    /// whose time has come are posted.
    pub fn fire_alarms(&self) {
        self.lock().begin_due();
    }

    /// The snapshot liveupdate last posted for `id`.
    pub fn posted(&self, id: &str) -> liveupdate::Snapshot {
        self.lock().observed[id].snapshot.clone()
    }

    /// A device that offers the extensions `offer` names.
    pub fn offering(offer: fn(&mut PresentationCapabilities)) -> Self {
        let driver = Self::default();
        offer(&mut driver.lock().capabilities.presentation);
        driver
    }
}

impl Driver for MemoryDriver {
    fn capabilities(&self) -> liveupdate::Result<Capabilities> {
        let mut state = self.lock();
        state.capability_calls += 1;
        Ok(state.capabilities)
    }

    fn read(&self) -> liveupdate::Result<Vec<DriverRecord>> {
        Ok(self.lock().records.clone())
    }

    fn write(&self, records: &[DriverRecord]) -> liveupdate::Result<()> {
        self.lock().records = records.to_vec();
        Ok(())
    }

    fn observe(&self) -> liveupdate::Result<Vec<Observation>> {
        let mut state = self.lock();
        state.observations += 1;
        if state.failing_observations > 0 {
            state.failing_observations -= 1;
            return Err(liveupdate::Error::new(
                liveupdate::ErrorCode::NativeError,
                "Listing failed.",
            ));
        }
        // The lifecycle has no clock: the Android side says that a stale time has come.
        let listed = |observation: &Observation| Observation {
            state: match observation.snapshot.stale_at {
                Some(stale_at) if stale_at <= state.now => ActivityState::Stale,
                _ => ActivityState::Active,
            },
            ..observation.clone()
        };
        Ok(state.observed.values().map(listed).collect())
    }

    fn post(&self, record: &DriverRecord) -> liveupdate::Result<()> {
        let mut state = self.lock();
        state.attempt()?;
        if unopened(record) {
            return Err(liveupdate::Error::new(
                liveupdate::ErrorCode::InvalidInput,
                "No activity of the application opens the URL.",
            ));
        }
        if record.state == ActivityState::Ended {
            state.observed.remove(&record.id);
            state.finals.insert(record.id.clone(), record.clone());
            return Ok(());
        }
        state.list(record);
        Ok(())
    }

    fn schedule(&self, record: &DriverRecord) -> liveupdate::Result<()> {
        let mut state = self.lock();
        state.schedules += 1;
        if state.failing_schedules > 0 {
            state.failing_schedules -= 1;
            return Err(liveupdate::Error::new(
                liveupdate::ErrorCode::NativeError,
                "The alarm could not be set.",
            ));
        }
        // The receipt is checked as a post is.
        if unopened(record) {
            return Err(liveupdate::Error::new(
                liveupdate::ErrorCode::InvalidInput,
                "No activity of the application opens the URL.",
            ));
        }
        // A time that has passed begins the activity at once.
        if record.origin.start_at.is_some_and(|at| at <= state.now) {
            state.begin(record);
        }
        Ok(())
    }

    fn starts(&self) -> liveupdate::Result<Vec<StartReport>> {
        let mut state = self.lock();
        state.begin_due();
        // Of the receipts it began, those that Android does not list: posted a moment ago, or not shown.
        let scheduled = |id: &&String| {
            let waits = |record: &DriverRecord| {
                record.id == **id && record.state == ActivityState::Scheduled
            };
            state.records.iter().any(waits) && !state.observed.contains_key(*id)
        };
        let report = |id: &String| StartReport {
            id: id.clone(),
            outcome: if state.unlisted.contains_key(id) {
                StartOutcome::Posted
            } else {
                StartOutcome::Gone
            },
        };
        Ok(state.begun.iter().filter(scheduled).map(report).collect())
    }

    fn cancel(&self, id: &str) -> liveupdate::Result<()> {
        let mut state = self.lock();
        state.observed.remove(id);
        state.cancelled.push(id.to_owned());
        Ok(())
    }

    fn drain_events(&self) -> liveupdate::Result<EventBatch> {
        Ok(std::mem::take(&mut self.lock().events))
    }
}

/// The Android side of liveupdate's JSON bridge protocol, as a framework's native call reaches it: a command name
/// and a JSON payload in, a response envelope out.
pub struct StringBridge(pub MemoryDriver);

fn member<T: DeserializeOwned>(payload: &Value, name: &str) -> T {
    serde_json::from_value(payload[name].clone()).unwrap()
}

impl Bridge for StringBridge {
    fn invoke(&self, command: &str, payload: &str) -> liveupdate::Result<String> {
        let payload: Value = serde_json::from_str(payload).unwrap();
        let driver = &self.0;
        let data = match command {
            "capabilities" => driver.capabilities().map(|data| json!(data)),
            "read" => driver.read().map(|data| json!(data)),
            "write" => driver
                .write(&member::<Vec<DriverRecord>>(&payload, "records"))
                .map(|()| Value::Null),
            "observe" => driver.observe().map(|data| json!(data)),
            "post" => driver
                .post(&member::<DriverRecord>(&payload, "record"))
                .map(|()| Value::Null),
            "cancel" => driver
                .cancel(&member::<String>(&payload, "id"))
                .map(|()| Value::Null),
            "events" => driver.drain_events().map(|data| json!(data)),
            "schedule" => driver
                .schedule(&member::<DriverRecord>(&payload, "record"))
                .map(|()| Value::Null),
            "starts" => driver.starts().map(|data| json!(data)),
            other => panic!("unknown bridge command {other}"),
        };
        let envelope = match data {
            Ok(data) => json!({"status": "ok", "data": data}),
            Err(error) => json!({"status": "error", "error": error}),
        };
        Ok(envelope.to_string())
    }
}

/// The same bridge with a native call that panics while the test says so, as a framework's call may: the call of
/// one command, and every other reaches the Android side.
#[derive(Clone)]
pub struct PanickingBridge {
    pub android: MemoryDriver,
    panics: Arc<Mutex<Option<&'static str>>>,
}

impl PanickingBridge {
    pub fn new(android: MemoryDriver) -> Self {
        Self {
            android,
            panics: Arc::default(),
        }
    }

    /// The command whose call panics from now on, or none.
    pub fn panic_in(&self, command: Option<&'static str>) {
        *self.panics.lock().unwrap() = command;
    }
}

impl Bridge for PanickingBridge {
    fn invoke(&self, command: &str, payload: &str) -> liveupdate::Result<String> {
        // Read before the panic, which then unwinds with no lock of the test held.
        let panics = *self.panics.lock().unwrap() == Some(command);
        assert!(!panics, "the native call of `{command}` panicked");
        StringBridge(self.android.clone()).invoke(command, payload)
    }
}