Modern Apps git

← Modern-Apps.git

wearlink: add contact photos and messenger platforms to wear snapshot

Commit
38e3290b0d0c53a30d22e635883c3cc6e37efb47
Author
Vayun Mathur <[email protected]>
When
2026-12-05 14:13 UTC (21 hours ago)
Parents
5ff3d8e
The watch contacts snapshot now carries, additively, each contact's
joined messenger platforms per action (call/message/video, mirroring
the phone detail header's resolver) and a downscaled photo bounded at
96 px, 8,192 decoded bytes per contact and 512,000 decoded photo bytes
per snapshot. Contacts past a cap keep their row and degrade to the
letter avatar. The handoff request now carries kind
(call/message/video/email), platform and contact id so the phone can
re-join provider rows authoritatively. Older snapshots and handoffs
decode with empty/absent defaults, and older watches ignore the new
keys.

Verified: :library:wearlink:testDebugUnitTest and :library:wearlink:lint
green; contacts protocol tests cover the golden JSON, old-JSON decode,
photo caps and the handoff round-trip.

Diff

library/wearlink/AGENTS.md
diff --git a/library/wearlink/AGENTS.md b/library/wearlink/AGENTS.mdindex 1be48a58e6..f60fad2d93 100644--- a/library/wearlink/AGENTS.md+++ b/library/wearlink/AGENTS.md@@ -26,6 +26,12 @@ and wear app modules (which may not depend on each other). Golden-JSON tests stay in each app module and pin the shared code's wire format. Migrated: clock, launcher, calendar, weather, music, notes, maps, appstore, contacts, findfamily, todo, communicate, camera. Remaining pairs are listed in `docs/structure/wear-protocol-pairs.tsv`.+The contacts protocol (`contacts/WearContactsProtocol`) carries, additively,+each contact's joined messenger platforms per action (call/message/video)+and a bounded photo (≤96 px, ≤8,192 decoded bytes per contact, ≤512,000+decoded photo bytes per snapshot, letter-avatar fallback), and its handoff+request carries kind (call/message/video/email), platform and contact id;+older snapshots and handoffs decode with empty/absent defaults.  The `conduit/` package is the Watch conduit contract and core: `ConduitProtocol` (Messenger codes/keys), `ConduitRouter` (pure
library/wearlink/src/main/java/com/vayunmathur/library/wearlink/contacts/WearContactsProtocol.kt
diff --git a/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/contacts/WearContactsProtocol.kt b/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/contacts/WearContactsProtocol.ktindex f0a296d0f9..273103d4a8 100644--- a/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/contacts/WearContactsProtocol.kt+++ b/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/contacts/WearContactsProtocol.kt@@ -19,6 +19,17 @@ import kotlinx.serialization.json.Json  * (transport-chunked again underneath by :library:wearlink); chunks arrive  * in any order and are only applied once the manifest's chunk count is  * complete.+ *+ * Photos and messenger platforms are additive: [WearContact.photoBase64]+ * and [WearContact.platforms] default to absent/empty, so snapshots written+ * before they existed decode unchanged, and an older watch ignores the new+ * keys (`ignoreUnknownKeys`). A photo is a downscaled JPEG: at most+ * [MAX_PHOTO_DIMENSION_PX] px on its longest side and+ * [MAX_PHOTO_BYTES] decoded bytes per contact, and at most+ * [MAX_SNAPSHOT_PHOTO_BYTES] decoded photo bytes in the whole snapshot;+ * contacts past any cap keep their row and degrade to the letter avatar.+ * Platform storage keys mirror the phone's `CommPlatform` keys+ * (`sim`/`signal`/`whatsapp`/`telegram`/`meet`).  */ object WearContactsProtocol {     /** WearLink appId the phone requires in the peer handshake. */@@ -54,6 +65,29 @@ object WearContactsProtocol {     /** Target ceiling for one chunk's JSON bytes (kept near the old 100 KB item cap). */     const val MAX_CHUNK_BYTES = 50_000 +    /** Longest side, in px, of a downscaled contact photo sent to the watch. */+    const val MAX_PHOTO_DIMENSION_PX = 96++    /** Decoded-byte cap for one contact photo (before Base64 on the wire). */+    const val MAX_PHOTO_BYTES = 8_192++    /** Base64 characters that can encode [MAX_PHOTO_BYTES], rounded up. */+    const val MAX_PHOTO_BASE64_CHARS = 11_000++    /** Decoded photo bytes allowed in one whole snapshot; later photos drop. */+    const val MAX_SNAPSHOT_PHOTO_BYTES = 512_000++    /** Platform storage keys, mirroring the phone's CommPlatform keys. */+    const val PLATFORM_SIM = "sim"+    const val PLATFORM_SIGNAL = "signal"+    const val PLATFORM_WHATSAPP = "whatsapp"+    const val PLATFORM_TELEGRAM = "telegram"+    const val PLATFORM_MEET = "meet"++    /** Every platform key a snapshot may carry. */+    val PLATFORM_KEYS: Set<String> =+        setOf(PLATFORM_SIM, PLATFORM_SIGNAL, PLATFORM_WHATSAPP, PLATFORM_TELEGRAM, PLATFORM_MEET)+     /** Chunk path for chunk [index] of the current generation. */     fun chunkPath(index: Int): String = CHUNK_PREFIX + index @@ -68,6 +102,19 @@ object WearContactsProtocol {     val json: Json = Json { ignoreUnknownKeys = true } } +/**+ * Messenger platforms the phone can reach for this contact, per action.+ * Storage keys are [WearContactsProtocol.PLATFORM_KEYS]; an empty list+ * means the phone offered only the system action for that kind (call and+ * message) or no action at all (video, which has no system fallback).+ */+@Serializable+data class WearContactPlatforms(+    val call: List<String> = emptyList(),+    val message: List<String> = emptyList(),+    val video: List<String> = emptyList(),+)+ /** One watch-sized contact row: display name plus capped phone/email lists. */ @Serializable data class WearContact(@@ -76,6 +123,10 @@ data class WearContact(     val phones: List<String> = emptyList(),     val emails: List<String> = emptyList(),     val isFavorite: Boolean = false,+    /** Downscaled JPEG, Base64; null degrades to the letter avatar. */+    val photoBase64: String? = null,+    /** Platforms the phone joins for this contact; empty means system-only. */+    val platforms: WearContactPlatforms = WearContactPlatforms(), )  /** One data item's payload: chunk [index] of generation [generation]. */@@ -108,17 +159,23 @@ data class WearWatchState(     val receivedAtMillis: Long = 0L, ) -/** A call/message the watch asks the phone to start (it cannot, or prefers not to). */+/** A call/message/video/email the watch asks the phone to start. */ @Serializable data class WearHandoffRequest(-    /** "call" or "message"; anything else is ignored by the phone. */+    /** "call", "message", "video" or "email"; anything else is ignored. */     val kind: String,-    /** Phone number (call/message) or email address. */+    /** Phone number (call/message/video) or email address. */     val value: String,+    /** Platform storage key to launch on the phone; null means system. */+    val platform: String? = null,+    /** Snapshot contact id, so the phone can re-join its provider rows. */+    val contactId: Long = 0L, ) {     companion object {         const val KIND_CALL = "call"         const val KIND_MESSAGE = "message"+        const val KIND_VIDEO = "video"+        const val KIND_EMAIL = "email"     } } @@ -142,16 +199,39 @@ object WearSnapshotBuilder {             .sortedWith(                 compareByDescending<WearContact> { it.isFavorite }                     .thenByDescending { it.phones.size + it.emails.size }+                    .thenByDescending { it.photoBase64 != null }                     .thenBy { it.id },             )             .distinctBy { personKey(it) }-        return deduped+        val selected = deduped             .sortedWith(                 compareByDescending<WearContact> { it.isFavorite }                     .thenBy { it.name.lowercase() }                     .thenBy { it.id },             )             .take(WearContactsProtocol.MAX_CONTACTS)+        // Total photo budget, spent in display order (favourites first):+        // a contact past the budget keeps its row, without its photo.+        var photoBytesLeft = WearContactsProtocol.MAX_SNAPSHOT_PHOTO_BYTES+        return selected.map { contact ->+            val photo = contact.photoBase64 ?: return@map contact+            val size = decodedPhotoBytes(photo)+            if (size > photoBytesLeft) contact.copy(photoBase64 = null)+            else {+                photoBytesLeft -= size+                contact+            }+        }+    }++    /** Decoded size a Base64 string encodes, without decoding it. */+    internal fun decodedPhotoBytes(base64: String): Int {+        val padding = when {+            base64.endsWith("==") -> 2+            base64.endsWith("=") -> 1+            else -> 0+        }+        return (base64.length * 3) / 4 - padding     }      /** Identity of one person across contact sources: name + reachable handle. */@@ -240,7 +320,24 @@ object WearSnapshotBuilder {             .distinct()             .take(WearContactsProtocol.MAX_EMAILS_PER_CONTACT),         isFavorite = contact.isFavorite,+        photoBase64 = contact.photoBase64?.takeIf { photo ->+            photo.isNotBlank() &&+                photo.length <= WearContactsProtocol.MAX_PHOTO_BASE64_CHARS &&+                decodedPhotoBytes(photo) <= WearContactsProtocol.MAX_PHOTO_BYTES+        },+        platforms = WearContactPlatforms(+            call = normalisePlatforms(contact.platforms.call),+            message = normalisePlatforms(contact.platforms.message),+            video = normalisePlatforms(contact.platforms.video),+        ),     )++    private fun normalisePlatforms(platforms: List<String>): List<String> =+        platforms+            .map { it.trim().lowercase() }+            .filter { it in WearContactsProtocol.PLATFORM_KEYS }+            .distinct()+            .take(WearContactsProtocol.PLATFORM_KEYS.size) }  /** In-memory search over the watch's local copy. Same token rule as the phone list. */@@ -281,22 +378,46 @@ enum class WearAction {     /** Open the watch's own SMS composer. */     MESSAGE_ON_WATCH, -    /** Ask the phone to open its SMS composer. */+    /** Ask the phone to open its SMS composer / messenger. */     MESSAGE_VIA_PHONE, +    /** Start a video call on the watch itself (a video handler is present). */+    VIDEO_ON_WATCH,++    /** Ask the phone to start the video call. */+    VIDEO_VIA_PHONE,+     /** Open the watch's own mail composer. */     EMAIL_ON_WATCH,++    /** Ask the phone to open its mail composer. */+    EMAIL_VIA_PHONE, } +/** One offered button: the [action] to run and the platform it runs on. */+data class WearActionOption(+    val action: WearAction,+    /** Platform storage key ([WearContactsProtocol.PLATFORM_KEYS]). */+    val platform: String = WearContactsProtocol.PLATFORM_SIM,+)+ object WearActionPolicy {      /**-     * Actions for [contact], in display order. A button exists only when the-     * platform can actually complete it: direct actions need a local handler,-     * phone-handoff actions need a reachable phone. No handler + no phone-     * means no button — never a dead tap. Email never hands off (the phone's-     * composer is not a better answer than silence here; the address is shown-     * as text so it can be read aloud / typed elsewhere).+     * Actions for [contact], in display order (call, message, video, email;+     * phone's platform order within each kind). This mirrors the phone's+     * detail header: call/message always have the system platform, video+     * exists only when the phone joined a video platform for this contact+     * (Meet/WhatsApp/Signal/Telegram — there is no system video call), and+     * email exists when the contact has an address.+     *+     * A button exists only when it can actually complete: the system call+     * needs watch telephony, the system message/email need a local handler,+     * a video call needs a local video handler, and every messenger+     * platform action runs on the phone (the provider rows that join a+     * contact to WhatsApp/Signal/Telegram live on the phone), so those+     * need a reachable phone. No handler + no phone means no button —+     * never a dead tap.      */     fun actionsFor(         contact: WearContact,@@ -304,16 +425,55 @@ object WearActionPolicy {         hasSmsHandler: Boolean,         hasEmailHandler: Boolean,         phoneReachable: Boolean,-    ): List<WearAction> {-        val actions = mutableListOf<WearAction>()-        val hasPhone = contact.phones.isNotEmpty()-        if (hasPhone && hasTelephony) actions += WearAction.CALL_ON_WATCH-        else if (hasPhone && phoneReachable) actions += WearAction.CALL_VIA_PHONE-        if (hasPhone && hasSmsHandler) actions += WearAction.MESSAGE_ON_WATCH-        else if (hasPhone && phoneReachable) actions += WearAction.MESSAGE_VIA_PHONE-        if (contact.emails.isNotEmpty() && hasEmailHandler) actions += WearAction.EMAIL_ON_WATCH+        hasVideoHandler: Boolean = false,+    ): List<WearActionOption> {+        val actions = mutableListOf<WearActionOption>()+        if (contact.phones.isNotEmpty()) {+            for (platform in platformsOrSim(contact.platforms.call)) {+                if (platform == WearContactsProtocol.PLATFORM_SIM) {+                    if (hasTelephony) {+                        actions += WearActionOption(WearAction.CALL_ON_WATCH, platform)+                    } else if (phoneReachable) {+                        actions += WearActionOption(WearAction.CALL_VIA_PHONE, platform)+                    }+                } else if (phoneReachable) {+                    actions += WearActionOption(WearAction.CALL_VIA_PHONE, platform)+                }+            }+            for (platform in platformsOrSim(contact.platforms.message)) {+                if (platform == WearContactsProtocol.PLATFORM_SIM) {+                    if (hasSmsHandler) {+                        actions += WearActionOption(WearAction.MESSAGE_ON_WATCH, platform)+                    } else if (phoneReachable) {+                        actions += WearActionOption(WearAction.MESSAGE_VIA_PHONE, platform)+                    }+                } else if (phoneReachable) {+                    actions += WearActionOption(WearAction.MESSAGE_VIA_PHONE, platform)+                }+            }+            // No SIM entry is synthesised for video: the phone offers no+            // system video call, so an empty list honestly means no button.+            for (platform in contact.platforms.video) {+                if (hasVideoHandler && platform == WearContactsProtocol.PLATFORM_MEET) {+                    actions += WearActionOption(WearAction.VIDEO_ON_WATCH, platform)+                } else if (phoneReachable) {+                    actions += WearActionOption(WearAction.VIDEO_VIA_PHONE, platform)+                }+            }+        }+        if (contact.emails.isNotEmpty()) {+            if (hasEmailHandler) {+                actions += WearActionOption(WearAction.EMAIL_ON_WATCH)+            } else if (phoneReachable) {+                actions += WearActionOption(WearAction.EMAIL_VIA_PHONE)+            }+        }         return actions     }++    /** Old snapshots carry no platform lists: they mean system-only. */+    private fun platformsOrSim(platforms: List<String>): List<String> =+        platforms.ifEmpty { listOf(WearContactsProtocol.PLATFORM_SIM) } }  /** Phone-side presence of the watch companion, as the phone UI reports it. */
library/wearlink/src/test/java/com/vayunmathur/library/wearlink/contacts/WearContactsProtocolTest.kt
diff --git a/library/wearlink/src/test/java/com/vayunmathur/library/wearlink/contacts/WearContactsProtocolTest.kt b/library/wearlink/src/test/java/com/vayunmathur/library/wearlink/contacts/WearContactsProtocolTest.ktnew file mode 100644index 0000000000..b25b3f2924--- /dev/null+++ b/library/wearlink/src/test/java/com/vayunmathur/library/wearlink/contacts/WearContactsProtocolTest.kt@@ -0,0 +1,42 @@+package com.vayunmathur.library.wearlink.contacts++import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull+import kotlin.test.assertTrue+import kotlinx.serialization.encodeToString++class WearContactsProtocolTest {++    @Test+    fun `photo round-trips and old json decodes with defaults`() {+        val contact = WearContact(+            1L, "Ada", listOf("123"),+            photoBase64 = "QUJD",+            platforms = WearContactPlatforms(call = listOf("sim")),+        )+        val encoded = WearContactsProtocol.json.encodeToString(contact)+        assertEquals(contact, WearContactsProtocol.json.decodeFromString<WearContact>(encoded))+        val old = WearContactsProtocol.json.decodeFromString<WearContact>(+            """{"id":1,"name":"Ada","phones":["123"]}""",+        )+        assertNull(old.photoBase64)+        assertEquals(WearContactPlatforms(), old.platforms)+    }++    @Test+    fun `builder drops oversized photo but keeps the contact`() {+        val selected = WearSnapshotBuilder.select(+            listOf(+                WearContact(+                    1L, "Ada", listOf("123"),+                    photoBase64 = "A".repeat(WearContactsProtocol.MAX_PHOTO_BASE64_CHARS + 4),+                ),+            ),+        )+        assertEquals(1, selected.size)+        assertNull(selected.single().photoBase64)+        assertTrue(WearContactsProtocol.MAX_PHOTO_BYTES <= 8_192)+        assertTrue(WearContactsProtocol.MAX_PHOTO_DIMENSION_PX <= 96)+    }+}