Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.
Pictures in mirrored notifications
Beeca49243e8222
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/notifications/NotificationReader.kt b/android/app/src/main/kotlin/app/nectarlink/android/notifications/NotificationReader.ktindex b9c280f..5650195 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/notifications/NotificationReader.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/notifications/NotificationReader.kt@@ -6,12 +6,17 @@ import android.app.NotificationManagerimport android.content.Contextimport android.content.pm.PackageManagerimport android.graphics.Bitmap+import android.graphics.BitmapFactory+import android.graphics.drawable.Icon+import android.net.Uri+import android.os.Buildimport android.service.notification.NotificationListenerService.Rankingimport android.service.notification.NotificationListenerService.RankingMapimport android.service.notification.StatusBarNotificationimport android.util.LruCacheimport androidx.core.app.NotificationCompatimport androidx.core.graphics.drawable.toBitmap+import androidx.core.graphics.scaleimport java.io.ByteArrayOutputStreamimport app.nectarlink.core.Notification as Mirroredimport app.nectarlink.core.NotificationAction as MirroredAction@@ -24,6 +29,8 @@ internal class NotificationReader(private val context: Context) {private val packages: PackageManager = context.packageManagerprivate val icons = LruCache<String, ByteArray>(48)private val names = LruCache<String, String>(128)+ /** Pictures already made, by notification and picture (updates repeat them). */+ private val images = LruCache<String, ByteArray>(16)/** `null` when this notification isn't mirrored. */fun read(sbn: StatusBarNotification, ranking: RankingMap?, update: Boolean): Mirrored? {@@ -42,6 +49,7 @@ internal class NotificationReader(private val context: Context) {actions = actions(n),silent = silent,icon = icon(sbn.packageName),+ image = image(sbn.key, n),)}@@ -100,6 +108,85 @@ internal class NotificationReader(private val context: Context) {)}+ /**+ * The picture a notification shows, as a small JPEG: a big picture, or+ * the last image in a conversation. `null` when there's none or it+ * can't be read (images in messages often aren't shared with listeners).+ */+ private fun image(key: String, n: Notification): ByteArray? = runCatching {+ val source = pictureSource(n) ?: return null+ images.get("$key\u0001${source.id}")?.let { return it }+ val bitmap = source.load() ?: return null+ jpeg(bitmap).also { bytes ->+ if (bitmap !== source.owned) bitmap.recycle()+ if (bytes != null) images.put("$key\u0001${source.id}", bytes)+ }+ }.getOrNull()++ /** Where a picture comes from: an ID for caching, and how to load it. */+ private class Picture(val id: String, val owned: Bitmap?, val load: () -> Bitmap?)++ private fun pictureSource(n: Notification): Picture? {+ val extras = n.extras+ val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {+ extras.getParcelable(Notification.EXTRA_PICTURE, Bitmap::class.java)+ } else {+ @Suppress("DEPRECATION")+ extras.getParcelable(Notification.EXTRA_PICTURE) as? Bitmap+ }+ if (bitmap != null) {+ return Picture("${bitmap.width}x${bitmap.height}:${bitmap.generationId}", bitmap) { bitmap }+ }+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {+ val icon = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {+ extras.getParcelable(Notification.EXTRA_PICTURE_ICON, Icon::class.java)+ } else {+ @Suppress("DEPRECATION")+ extras.getParcelable(Notification.EXTRA_PICTURE_ICON) as? Icon+ }+ if (icon != null) {+ return Picture("icon:${icon.hashCode()}", null) { icon.loadDrawable(context)?.toBitmap() }+ }+ }+ val message = NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(n)+ ?.messages+ ?.lastOrNull { it.dataMimeType?.startsWith("image/") == true && it.dataUri != null }+ val uri = message?.dataUri ?: return null+ return Picture(uri.toString(), null) { decode(uri) }+ }++ /** Decodes an image URI at about the size it's sent at (it may be huge). */+ private fun decode(uri: Uri): Bitmap? {+ val resolver = context.contentResolver+ val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }+ resolver.openInputStream(uri)?.use { BitmapFactory.decodeStream(it, null, bounds) } ?: return null+ val longest = maxOf(bounds.outWidth, bounds.outHeight)+ if (longest <= 0) return null+ var sample = 1+ while (longest / (sample * 2) >= IMAGE_PX) sample *= 2+ val options = BitmapFactory.Options().apply { inSampleSize = sample }+ return resolver.openInputStream(uri)?.use { BitmapFactory.decodeStream(it, null, options) }+ }++ /** A JPEG that fits the protocol's limit, smaller and softer if needed. */+ private fun jpeg(bitmap: Bitmap): ByteArray? {+ for ((side, quality) in listOf(IMAGE_PX to 82, IMAGE_PX to 70, IMAGE_PX to 55, SMALL_IMAGE_PX to 70)) {+ val scale = minOf(1f, side.toFloat() / maxOf(bitmap.width, bitmap.height))+ val sized = if (scale < 1f) {+ bitmap.scale((bitmap.width * scale).toInt().coerceAtLeast(1), (bitmap.height * scale).toInt().coerceAtLeast(1))+ } else {+ bitmap+ }+ val bytes = ByteArrayOutputStream().use { out ->+ sized.compress(Bitmap.CompressFormat.JPEG, quality, out)+ out.toByteArray()+ }+ if (sized !== bitmap) sized.recycle()+ if (bytes.size <= MAX_IMAGE_BYTES) return bytes+ }+ return null+ }+private fun appName(pkg: String): String = names.get(pkg) ?: runCatching {packages.getApplicationLabel(packages.getApplicationInfo(pkg, 0)).toString()}.getOrDefault(pkg).also { names.put(pkg, it) }@@ -116,5 +203,9 @@ internal class NotificationReader(private val context: Context) {private companion object {const val ICON_PX = 96const val MAX_ICON_BYTES = 64 * 1024+ /** Pictures' longest side, and the fallback for large ones. */+ const val IMAGE_PX = 512+ const val SMALL_IMAGE_PX = 360+ const val MAX_IMAGE_BYTES = 160 * 1024}}diff --git a/android/app/src/test/kotlin/app/nectarlink/android/core/CoreStateTest.kt b/android/app/src/test/kotlin/app/nectarlink/android/core/CoreStateTest.ktindex d4f47c3..c74f20a 100644--- a/android/app/src/test/kotlin/app/nectarlink/android/core/CoreStateTest.kt+++ b/android/app/src/test/kotlin/app/nectarlink/android/core/CoreStateTest.kt@@ -77,7 +77,7 @@ class CoreStateTest {@Testfun notificationEventsLeaveThePhoneStateAlone() {val state = CoreState().reduce(Event.DeviceAdded(pc("a", "Desk", 10)))- val note = Notification("k", "com.chat", "Chat", "Sam", "Hi", null, 0L, emptyList(), false, null)+ val note = Notification("k", "com.chat", "Chat", "Sam", "Hi", null, 0L, emptyList(), false, null, null)assertEquals(state, state.reduce(Event.NotificationPosted("a", note)))assertEquals(state, state.reduce(Event.NotificationRemoved("a", "k")))assertEquals(state, state.reduce(Event.NotificationsReset("a", listOf(note))))diff --git a/core/nectarlink-cli/src/main.rs b/core/nectarlink-cli/src/main.rsindex 7ff8ae9..f19c432 100644--- a/core/nectarlink-cli/src/main.rs+++ b/core/nectarlink-cli/src/main.rs@@ -190,6 +190,9 @@ enum Command {/// Don't alert (like a silent notification on the phone).#[arg(long)]silent: bool,+ /// A picture it shows (JPEG, at most 160 KiB).+ #[arg(long)]+ image: Option<PathBuf>,},}@@ -515,7 +518,8 @@ async fn run(cli: &Cli, node: &Node) -> Result<()> {node.update_power(node_power(cli), offers).await;play(node, title, artist, app, *length, art.as_deref()).await?;}- Command::Notify { title, text, app, reply, silent } => {+ Command::Notify { title, text, app, reply, silent, image } => {+ let image = image.as_deref().map(std::fs::read).transpose().context("can't read the picture")?;if !cli.as_phone {bail!("notifications come from phones: add --as-phone");}@@ -540,6 +544,7 @@ async fn run(cli: &Cli, node: &Node) -> Result<()> {actions,silent: *silent,icon: None,+ image,}).await;println!("Notification sent to connected PCs (and to others when they connect).");@@ -776,10 +781,11 @@ fn print_event(node: &Node, event: &NodeEvent) {}NodeEvent::NotificationPosted { device, notification: n } => {println!(- "{}: {} · {} [key {}]",+ "{}: {} · {}{} [key {}]",name(device),n.app_name,n.title.as_deref().or(n.text.as_deref()).unwrap_or_default(),+ n.image.as_ref().map(|i| format!(" (with a picture, {} bytes)", i.len())).unwrap_or_default(),n.key);for a in &n.actions {diff --git a/core/nectarlink-core/src/notifications.rs b/core/nectarlink-core/src/notifications.rsindex fd40171..ee75d5d 100644--- a/core/nectarlink-core/src/notifications.rs+++ b/core/nectarlink-core/src/notifications.rs@@ -109,12 +109,16 @@ impl Feed {let mut budget = SNAPSHOT_BUDGET;let mut out = Vec::new();for n in items.into_iter().take(SNAPSHOT_ITEMS) {- let size = approx_size(n);+ let mut n = n.clone();+ // Pictures go while there's room; the rest go without.+ if n.image.is_some() && approx_size(&n) > budget {+ n.image = None;+ }+ let size = approx_size(&n);if size > budget {break;}budget -= size;- let mut n = n.clone();if !sent.contains(&n.app)&& let Some(icon) = state.icons.get(&n.app)&& icon.len() <= budget@@ -146,7 +150,7 @@ impl Feed {fn approx_size(n: &Notification) -> usize {let text = [&n.title, &n.text, &n.sub].iter().map(|s| s.as_ref().map_or(0, String::len)).sum::<usize>();let actions = n.actions.iter().map(|a| a.id.len() + a.title.len() + 16).sum::<usize>();- n.key.len() + n.app.len() + n.app_name.len() + text + actions + 64+ n.key.len() + n.app.len() + n.app_name.len() + text + actions + n.image.as_ref().map_or(0, Vec::len) + 64}impl Shared {diff --git a/core/nectarlink-core/tests/two_devices.rs b/core/nectarlink-core/tests/two_devices.rsindex 49eee38..c080396 100644--- a/core/nectarlink-core/tests/two_devices.rs+++ b/core/nectarlink-core/tests/two_devices.rs@@ -483,6 +483,7 @@ fn note(key: &str, app: &str, title: &str, icon: bool) -> Notification {],silent: false,icon: icon.then(|| vec![0x89, b'P', b'N', b'G', 1, 2, 3]),+ image: None,}}diff --git a/core/nectarlink-ffi/src/lib.rs b/core/nectarlink-ffi/src/lib.rsindex 7a9417f..d51375f 100644--- a/core/nectarlink-ffi/src/lib.rs+++ b/core/nectarlink-ffi/src/lib.rs@@ -163,6 +163,9 @@ pub struct Notification {/// The app icon as PNG; pass it every time, the core sends it once per/// device and connection.pub icon: Option<Vec<u8>>,+ /// A picture it shows (a photo in a message, a big picture): JPEG, at+ /// most 160 KiB.+ pub image: Option<Vec<u8>>,}/// Something playing (or paused) on a device (docs/protocol/media.md).@@ -623,6 +626,7 @@ impl From<core::Notification> for Notification {.collect(),silent: n.silent,icon: n.icon,+ image: n.image,}}}@@ -644,6 +648,7 @@ impl From<Notification> for core::Notification {.collect(),silent: n.silent,icon: n.icon,+ image: n.image,}}}diff --git a/core/nectarlink-protocol/src/messages.rs b/core/nectarlink-protocol/src/messages.rsindex 15fe5cc..2c50ab7 100644--- a/core/nectarlink-protocol/src/messages.rs+++ b/core/nectarlink-protocol/src/messages.rs@@ -200,6 +200,7 @@ pub mod notify_limits {pub const ACTION_TITLE_CHARS: usize = 64;pub const ACTIONS: usize = 5;pub const ICON_BYTES: usize = 64 * 1024;+ pub const IMAGE_BYTES: usize = 160 * 1024;pub const SNAPSHOT_ITEMS: usize = 100;}@@ -241,6 +242,9 @@ pub struct Notification {/// session.#[serde(default, skip_serializing_if = "Option::is_none", with = "serde_bytes")]pub icon: Option<Vec<u8>>,+ /// A picture it shows (a photo in a message, a big picture), JPEG.+ #[serde(default, skip_serializing_if = "Option::is_none", with = "serde_bytes")]+ pub image: Option<Vec<u8>>,}/// Never prints content: notification text must not reach logs (protocol@@ -292,6 +296,9 @@ impl Notification {if self.icon.as_ref().is_some_and(|i| i.is_empty() || i.len() > ICON_BYTES) {self.icon = None;}+ if self.image.as_ref().is_some_and(|i| i.is_empty() || i.len() > IMAGE_BYTES) {+ self.image = None;+ }Some(self)}}@@ -656,6 +663,7 @@ mod tests {actions: vec![NotificationAction { id: "0".into(), title: "Reply".into(), reply: true }],silent: false,icon: Some(vec![0x89, b'P', b'N', b'G']),+ image: None,}}@@ -683,11 +691,13 @@ mod tests {.collect();n.actions.push(NotificationAction { id: String::new(), title: "No id".into(), reply: false });n.icon = Some(vec![0; notify_limits::ICON_BYTES + 1]);+ n.image = Some(vec![0; notify_limits::IMAGE_BYTES + 1]);let n = n.sanitized().unwrap();assert_eq!(n.title.unwrap().chars().count(), notify_limits::TITLE_CHARS);assert_eq!(n.text, None, "blank text is dropped");assert_eq!(n.actions.len(), notify_limits::ACTIONS);assert_eq!(n.icon, None);+ assert_eq!(n.image, None);}#[test]diff --git a/desktop/app/Cargo.toml b/desktop/app/Cargo.tomlindex f3f51b1..68b487a 100644--- a/desktop/app/Cargo.toml+++ b/desktop/app/Cargo.toml@@ -58,6 +58,10 @@ windows = { workspace = true, features = ["Win32_UI_Shell","Win32_UI_WindowsAndMessaging","Win32_NetworkManagement_IpHelper",+ "Win32_NetworkManagement_WindowsFirewall",+ "Win32_Networking_NetworkListManager",+ "Win32_System_Ole",+ "Win32_System_Variant","Win32_Networking_WinSock","Win32_Media_Audio",] }diff --git a/desktop/app/build.rs b/desktop/app/build.rsindex 7309fba..687de62 100644--- a/desktop/app/build.rs+++ b/desktop/app/build.rs@@ -34,6 +34,7 @@ const QML: &[&str] = &["qml/components/NotificationItem.qml","qml/components/NowPlaying.qml","qml/components/QrCode.qml",+ "qml/components/RoundedImage.qml","qml/components/Segmented.qml","qml/components/Sheet.qml","qml/components/Spinner.qml",diff --git a/desktop/app/qml/components/NotificationItem.qml b/desktop/app/qml/components/NotificationItem.qmlindex 739071f..baaf92b 100644--- a/desktop/app/qml/components/NotificationItem.qml+++ b/desktop/app/qml/components/NotificationItem.qml@@ -16,6 +16,7 @@ Item {required property string subrequired property real whenrequired property string iconUrl+ required property string imageUrlrequired property string actionsrequired property string replyActionrequired property string replyLabel@@ -118,6 +119,17 @@ Item {maximumLineCount: 4}+ // The picture it shows (a photo in a message), up to a modest size.+ RoundedImage {+ id: picture+ width: Math.min(parent.width, 320)+ height: status === Image.Ready+ ? Math.min(220, width * implicitImageHeight / Math.max(1, implicitImageWidth)) : 0+ visible: item.imageUrl.length > 0 && status === Image.Ready+ source: item.imageUrl+ sourceSize.width: 640+ }+// Replies sent from this PC.Repeater {model: item.sentdiff --git a/desktop/app/qml/components/NowPlaying.qml b/desktop/app/qml/components/NowPlaying.qmlindex 9f0e27d..21f94d8 100644--- a/desktop/app/qml/components/NowPlaying.qml+++ b/desktop/app/qml/components/NowPlaying.qml@@ -71,15 +71,12 @@ Card {path: Icons.musiccolor: Theme.secondaryContainerContent}- Image {+ RoundedImage {id: artworkanchors.fill: parent+ radius: cover.radiussource: card.art- fillMode: Image.PreserveAspectCrop- asynchronous: truesourceSize: Qt.size(144, 144)- opacity: status === Image.Ready ? 1 : 0- Behavior on opacity { NumberAnimation { duration: Theme.fadeNormal } }}}Column {diff --git a/desktop/app/qml/components/RoundedImage.qml b/desktop/app/qml/components/RoundedImage.qmlnew file mode 100644index 0000000..9077aa8--- /dev/null+++ b/desktop/app/qml/components/RoundedImage.qml@@ -0,0 +1,43 @@+// SPDX-License-Identifier: GPL-3.0-or-later+import QtQuick+import QtQuick.Effects+import app.nectarlink++// An image with rounded corners (a plain clip only cuts rectangles).+Item {+ id: root+ property alias source: image.source+ property alias status: image.status+ property alias fillMode: image.fillMode+ property alias sourceSize: image.sourceSize+ property alias implicitImageWidth: image.implicitWidth+ property alias implicitImageHeight: image.implicitHeight+ property real radius: Theme.radiusMd++ Image {+ id: image+ anchors.fill: parent+ asynchronous: true+ fillMode: Image.PreserveAspectCrop+ visible: false+ layer.enabled: true+ }+ Rectangle {+ id: mask+ anchors.fill: parent+ radius: root.radius+ visible: false+ layer.enabled: true+ layer.smooth: true+ }+ MultiEffect {+ anchors.fill: parent+ source: image+ maskEnabled: true+ maskSource: mask+ maskThresholdMin: 0.5+ maskSpreadAtMin: 1.0+ opacity: image.status === Image.Ready ? 1 : 0+ Behavior on opacity { NumberAnimation { duration: Theme.fadeNormal } }+ }+}diff --git a/desktop/app/src/bridge/notifications.rs b/desktop/app/src/bridge/notifications.rsindex 284e8af..1db73cf 100644--- a/desktop/app/src/bridge/notifications.rs+++ b/desktop/app/src/bridge/notifications.rs@@ -141,6 +141,8 @@ struct Row {device_name: String,/// A file URL, or "" (the UI then shows the app's initial).icon_url: String,+ /// The notification's picture as a file URL, or "".+ image_url: String,/// Replies sent from this PC.replies: Vec<SentReply>,}@@ -165,6 +167,7 @@ const ROLES: &[&str] = &["sub","when","iconUrl",+ "imageUrl","actions","replyAction","replyLabel",@@ -188,6 +191,7 @@ fn role_value(row: &Row, role: &str) -> QVariant {"sub" => text(n.sub.as_deref().unwrap_or_default()),"when" => QVariant::from(&(n.when as f64)),"iconUrl" => text(&row.icon_url),+ "imageUrl" => text(&row.image_url),// Buttons other than the reply, as JSON: [{ "id", "title" }]."actions" => text(&serde_json::Value::from(@@ -271,6 +275,11 @@ impl qobject::NotificationList {.get(&view.notification.app).map(|p| icons::file_url(p)).unwrap_or_default(),+ image_url: s+ .notification_images+ .get(&(view.device, view.notification.key.clone()))+ .map(|p| icons::file_url(p))+ .unwrap_or_default(),replies: s.replies.get(&(view.device, view.notification.key.clone()))@@ -436,10 +445,12 @@ mod tests {],silent: false,icon: None,+ image: None,},},device_name: "Pixel".into(),icon_url: String::new(),+ image_url: "file:///C:/data/cache/images/1.jpg".into(),replies: vec![SentReply { text: "On my way".into(), pending: true }],};let text = |role| role_value(&row, role).value::<QString>().map(String::from).unwrap_or_default();diff --git a/desktop/app/src/links.rs b/desktop/app/src/links.rsindex f49d49d..18ff224 100644--- a/desktop/app/src/links.rs+++ b/desktop/app/src/links.rs@@ -49,6 +49,7 @@ fn notice(title: &str, body: &str) {body: body.into(),attribution: "Nectarlink".into(),icon: None,+ image: None,actions: Vec::new(),reply: None,silent: true,diff --git a/desktop/app/src/main.rs b/desktop/app/src/main.rsindex 91c042e..9bb697b 100644--- a/desktop/app/src/main.rs+++ b/desktop/app/src/main.rs@@ -223,6 +223,7 @@ fn main() -> ExitCode {state::Changes::APPS | state::Changes::HISTORY});notification_store::start(data_dir.clone());+ std::thread::spawn(notifications::prune_images);watch_network();start_toasts();clipboard::start();diff --git a/desktop/app/src/notification_store.rs b/desktop/app/src/notification_store.rsindex 39a7d24..6a31afa 100644--- a/desktop/app/src/notification_store.rs+++ b/desktop/app/src/notification_store.rs@@ -174,6 +174,7 @@ mod tests {actions: Vec::new(),silent: false,icon: None,+ image: None,}}diff --git a/desktop/app/src/notifications.rs b/desktop/app/src/notifications.rsindex d1f6a5b..e63e76f 100644--- a/desktop/app/src/notifications.rs+++ b/desktop/app/src/notifications.rs@@ -25,6 +25,65 @@ fn toasted<T>(f: impl FnOnce(&mut HashMap<DeviceId, HashSet<String>>) -> T) -> Tf(TOASTED.lock().unwrap_or_else(|e| e.into_inner()).get_or_insert_with(HashMap::new))}+/// Pictures from notifications, kept for a couple of days (history lasts one).+const KEEP_IMAGES: std::time::Duration = std::time::Duration::from_secs(2 * 24 * 3600);++fn images_dir() -> std::path::PathBuf {+ core_host::host().data_dir.join("cache").join("images")+}++/// Saves the pictures an event's notifications carry; returns where each+/// went, by device and key.+fn save_images(event: &NodeEvent) -> Vec<((DeviceId, String), std::path::PathBuf)> {+ let (device, items): (DeviceId, Vec<&Notification>) = match event {+ NodeEvent::NotificationsReset { device, items } => (*device, items.iter().collect()),+ NodeEvent::NotificationPosted { device, notification } => (*device, vec![notification]),+ _ => return Vec::new(),+ };+ let dir = images_dir();+ items+ .into_iter()+ .filter_map(|n| {+ let image = n.image.as_ref()?;+ // Named after the notification and the picture: an update with a+ // new picture gets a new file (the UI caches by name).+ let name = format!(+ "{:016x}.jpg",+ fnv(&[device.to_string().as_bytes(), n.key.as_bytes(), image].concat())+ );+ let path = dir.join(name);+ let saved = std::fs::create_dir_all(&dir)+ .and_then(|()| if path.exists() { Ok(()) } else { std::fs::write(&path, image) });+ match saved {+ Ok(()) => Some(((device, n.key.clone()), path)),+ Err(e) => {+ tracing::debug!(error = %e, "can't save a notification picture");+ None+ }+ }+ })+ .collect()+}++/// Deletes pictures older than [`KEEP_IMAGES`].+pub fn prune_images() {+ let Ok(entries) = std::fs::read_dir(images_dir()) else { return };+ let now = std::time::SystemTime::now();+ for entry in entries.flatten() {+ let old = entry+ .metadata()+ .and_then(|m| m.modified())+ .is_ok_and(|t| now.duration_since(t).unwrap_or_default() > KEEP_IMAGES);+ if old {+ let _ = std::fs::remove_file(entry.path());+ }+ }+}++fn fnv(bytes: &[u8]) -> u64 {+ bytes.iter().fold(0xcbf2_9ce4_8422_2325_u64, |h, b| (h ^ u64::from(*b)).wrapping_mul(0x0100_0000_01b3))+}+/// Saves the app icons an event carries; returns where each app's went.fn save_icons(event: &NodeEvent) -> Vec<(String, std::path::PathBuf)> {let items: Vec<&Notification> = match event {@@ -51,12 +110,17 @@ fn save_icons(event: &NodeEvent) -> Vec<(String, std::path::PathBuf)> {/// Folds an event into the hub, saving icons first.pub fn apply(event: &NodeEvent) {let icons = save_icons(event);+ let images = save_images(event);core_host::host().hub.update(|s| {let mut changes = Changes::NONE;for (app, path) in icons {changes |= s.set_app_icon(&app, path);}- changes | s.apply(event)+ changes |= s.apply(event);+ for (key, path) in images {+ changes |= s.set_notification_image(key, path);+ }+ changes});}@@ -89,9 +153,14 @@ pub fn update_toasts(event: &NodeEvent) {}fn show(device: DeviceId, n: &Notification) {- let (device_name, icon, rule) = core_host::host()- .hub- .read(|s| (s.name_of(&device), s.app_icons.get(&n.app).cloned(), s.app_rule(&n.app)));+ let (device_name, icon, rule, image) = core_host::host().hub.read(|s| {+ (+ s.name_of(&device),+ s.app_icons.get(&n.app).cloned(),+ s.app_rule(&n.app),+ s.notification_images.get(&(device, n.key.clone())).cloned(),+ )+ });// The user chose no pop-ups (or nothing at all) for this app.if rule != AppRule::Show {return;@@ -107,6 +176,7 @@ fn show(device: DeviceId, n: &Notification) {None => n.app_name.clone(),},icon,+ image,actions: n.actions.iter().filter(|a| !a.reply).map(|a| (a.id.clone(), a.title.clone())).collect(),reply: reply.map(|a| (a.id.clone(), a.title.clone())),silent: n.silent,diff --git a/desktop/app/src/send_to.rs b/desktop/app/src/send_to.rsindex 2757013..851b62c 100644--- a/desktop/app/src/send_to.rs+++ b/desktop/app/src/send_to.rs@@ -256,6 +256,7 @@ fn failed(title: &str, body: &str) {body: body.into(),attribution: "Nectarlink".into(),icon: None,+ image: None,actions: Vec::new(),reply: None,silent: false,@@ -299,6 +300,7 @@ fn update(map: &mut HashMap<String, Tracked>, transfer: &Transfer) {body,attribution: "Nectarlink".into(),icon: None,+ image: None,actions: Vec::new(),reply: None,silent: false,diff --git a/desktop/app/src/state.rs b/desktop/app/src/state.rsindex af330a8..74ad92e 100644--- a/desktop/app/src/state.rs+++ b/desktop/app/src/state.rs@@ -212,6 +212,8 @@ pub struct AppState {pub notifications: Vec<NotificationView>,/// App icon files by package name.pub app_icons: HashMap<String, PathBuf>,+ /// Picture files of notifications that have one, by device and key.+ pub notification_images: HashMap<(DeviceId, String), PathBuf>,/// Replies sent from this PC, per notification, oldest first.pub replies: HashMap<(DeviceId, String), Vec<SentReply>>,/// File transfers, newest first.@@ -464,7 +466,9 @@ impl AppState {/// Adds a notification in time order (newest first), without its icon.fn insert_notification(&mut self, device: DeviceId, mut notification: Notification) -> Changes {+ // Icons and pictures live on disk (see crate::notifications).notification.icon = None;+ notification.image = None;let mut changes = Changes::NONE;if self.app_names.get(¬ification.app) != Some(¬ification.app_name) {self.app_names.insert(notification.app.clone(), notification.app_name.clone());@@ -534,6 +538,15 @@ impl AppState {Changes::HISTORY}+ /// Records where a notification's picture is stored.+ pub fn set_notification_image(&mut self, key: (DeviceId, String), path: PathBuf) -> Changes {+ if self.notification_images.get(&key) == Some(&path) {+ return Changes::NONE;+ }+ self.notification_images.insert(key, path);+ Changes::NOTIFICATIONS+ }+/// Records where an app's icon is stored.pub fn set_app_icon(&mut self, app: &str, path: PathBuf) -> Changes {if self.app_icons.get(app) == Some(&path) {@@ -749,6 +762,7 @@ mod tests {actions: Vec::new(),silent: false,icon: Some(vec![1, 2, 3]),+ image: None,}}diff --git a/desktop/app/src/transfers.rs b/desktop/app/src/transfers.rsindex 3ca18b4..a71ff43 100644--- a/desktop/app/src/transfers.rs+++ b/desktop/app/src/transfers.rs@@ -85,6 +85,7 @@ pub fn on_event(event: &NodeEvent) {body: format!("From {device}, saved in Downloads\\Nectarlink"),attribution: "Nectarlink".into(),icon: None,+ image: None,actions: vec![(ACTION_SHOW.into(), "Show in folder".into())],reply: None,silent: false,diff --git a/desktop/app/src/win/toast.rs b/desktop/app/src/win/toast.rsindex 4627289..067cb37 100644--- a/desktop/app/src/win/toast.rs+++ b/desktop/app/src/win/toast.rs@@ -63,6 +63,8 @@ pub struct Toast {/// "WhatsApp · Pixel 9".pub attribution: String,pub icon: Option<PathBuf>,+ /// A picture shown under the text (a photo in a message).+ pub image: Option<PathBuf>,/// `(action id, label)`.pub actions: Vec<(String, String)>,/// `(action id, placeholder)` for an inline reply.@@ -385,6 +387,9 @@ fn toast_xml(toast: &Toast) -> String {xml.push_str(&format!("<text>{}</text>", escape(&toast.body)));}xml.push_str(&format!(r#"<text placement="attribution">{}</text>"#, escape(&toast.attribution)));+ if let Some(image) = &toast.image {+ xml.push_str(&format!(r#"<image src="{}"/>"#, escape(&crate::icons::file_url(image))));+ }if toast.progress.is_some() {xml.push_str(&format!(r#"<progress value="{{{PROGRESS_VALUE}}}" status="{{{PROGRESS_STATUS}}}" valueStringOverride="{{{PROGRESS_LABEL}}}"/>"#@@ -435,6 +440,7 @@ mod tests {body: "Lunch \"today\"?\u{7}".into(),attribution: "Chat · Pixel".into(),icon: Some(PathBuf::from(r"C:\data\cache\icons\com.chat.png")),+ image: Some(PathBuf::from(r"C:\data\cache\images\1.jpg")),actions: (0..6).map(|i| (i.to_string(), format!("Action {i}"))).collect(),reply: Some(("r".into(), "Reply".into())),silent: true,@@ -448,6 +454,7 @@ mod tests {assert!(xml.contains("Sam & <Alex>"), "{xml}");assert!(xml.contains("Lunch "today"?</text>"), "control characters are dropped: {xml}");assert!(xml.contains(r#"src="file:///C:/data/cache/icons/com.chat.png""#));+ assert!(xml.contains(r#"<image src="file:///C:/data/cache/images/1.jpg"/>"#), "{xml}");assert!(xml.contains(r#"arguments="a:r" hint-inputId="reply""#));assert_eq!(xml.matches("<action ").count(), 5, "five buttons at most");assert!(xml.ends_with(r#"<audio silent="true"/></toast>"#));diff --git a/docs/protocol/actions.md b/docs/protocol/actions.mdindex f969e46..49863e9 100644--- a/docs/protocol/actions.md+++ b/docs/protocol/actions.md@@ -3,21 +3,22 @@> Status: **draft.** Builds on [protocol v0](v0.md); breaking changes are> allowed until v1. License: CC BY 4.0.-One-shot actions on a paired device: a phone locks or sleeps a PC, and-either device sends a web link to open on the other.+One-shot requests from one paired device to another: a phone locks its PC+or puts it to sleep, and either device opens a web link on the other.## 1. Capabilities| ID | Offered by | Meaning ||---|---|---|-| `pc.power` | PCs | Locks or sleeps on request |-| `link.open` | both | Opens web links sent to it |+| `pc.power` | PC | Locks or sleeps when a paired phone asks |+| `link.open` | both | Opens web links a paired device sends |-A device sends these requests only to peers that announce the capability.+Senders check the other device's capabilities first and don't send a+request it doesn't offer.## 2. Messages-All on the control stream.+All on the control stream, each answered with `ok` or `error`.```t = "pc.power" id = n b = { action: "lock" | "sleep" }@@ -25,24 +26,30 @@ t = "link.open" id = n b = { url: text }t = "ok" re = n```-**`pc.power`** asks a PC to lock or sleep. The PC answers `ok` first, then-acts (sleep after a short moment, so the answer leaves before the network-goes down). Shutting down is deliberately not offered (too easy to trigger-by accident from a pocket).+### 2.1 `pc.power`-Errors: `UNSUPPORTED` when the device doesn't lock or sleep on request, or-for an unknown action; `DENIED` when the user turned "PC actions" off for the-phone (the `pc_actions` device toggle, on by default).+Asks the PC to lock (show the sign-in screen) or to sleep. The PC answers+`ok` before it acts, and waits a moment before sleeping so the answer+leaves before its network goes down.-**`link.open`** asks the receiver to open `url`. Only http and https links-are allowed, at most 4096 bytes, without whitespace or control characters;-receivers **MUST** check and answer `BAD_MESSAGE` otherwise.+Errors: `DENIED` when the user turned PC actions off for the phone (the+`pc_actions` device toggle, on by default); `UNSUPPORTED` when the device+doesn't do this, or for an action it doesn't know.-A PC opens the link in the default browser; a phone shows a notification the-user taps to open it (Android doesn't let apps in the background open-screens).+Shutting down and restarting are deliberately not part of this version:+they're too easy to set off by accident, and lose unsaved work.-Errors: `BAD_MESSAGE` as above; `INTERNAL` when it couldn't be opened.+### 2.2 `link.open`++Asks the receiver to open `url`, which **MUST** be an `http` or `https`+link of at most 4,096 bytes with no whitespace or control characters.+Receivers **MUST** check this themselves and answer `BAD_MESSAGE` to+anything else; no other kind of link is ever opened.++A PC opens the link in its default browser. A phone shows it in a+notification the user taps to open it, since Android doesn't let apps in+the background open screens. `INTERNAL` means it couldn't be opened or+shown.## 3. Rulesdiff --git a/docs/protocol/notifications.md b/docs/protocol/notifications.mdindex d70acf5..758d169 100644--- a/docs/protocol/notifications.md+++ b/docs/protocol/notifications.md@@ -35,6 +35,7 @@ Notification = {actions: [Action], // In display order, at most 5silent: bool, // Arrived without sound or pop-up on the phone; don't alert eithericon: bytes?, // The app's icon, PNG, at most 64 KiB (see §2.1)+ image: bytes?, // The picture it shows (a photo in a message, a big picture), JPEG, at most 160 KiB}Action = {@@ -49,6 +50,11 @@ characters, `text` to 4,096 and action titles to 64; receivers **MUST**enforce the same limits. A notification with neither `title` nor `text` isnot sent.+`image` is sent with every post of a notification that shows a picture,+scaled so its longest side is at most 512 pixels. Receivers drop an image+over the limit. A snapshot carries pictures only while they fit its size+budget; the rest arrive without one.+### 2.1 App iconsThe icon of an app is sent with the **first** notification of that app in a