Modern Apps git

← Modern-Apps.git

findfamily: add Wear Tile for nearest person

Commit
1efb246fa2e22ad0b12ad8bc0f980e378f92465a
Author
Vayun Mathur <[email protected]>
When
2026-12-06 05:42 UTC (5 hours ago)
Parents
4302534
FindFamilyTileService (service/, on the shared
:library:weartiles MaTileService seam) renders the nearest
person's distance from self (haversine over the snapshot's
E7 fixes via the shared wearDistanceMeters) — or the freshest
person when no self fix exists — from WearFindFamilyStore's
conduit snapshot. No fake precision: people without fixes are
excluded and an empty snapshot renders the dash fallback.
Tap opens WearMainActivity. WearFindFamilyStore requests a
tile update on each new snapshot; 15-minute freshness
otherwise. The mapping is pure and covered by
FindFamilyTileServiceTest with a ProtoLayout proto assertion.

Verified: :findfamily:testDevUnitTest, :findfamily:lint and
:findfamily:lintVitalRelease green.

Diff

findfamily/AGENTS.md
diff --git a/findfamily/AGENTS.md b/findfamily/AGENTS.mdindex 0519af0d62..d55843638f 100644--- a/findfamily/AGENTS.md+++ b/findfamily/AGENTS.md@@ -196,3 +196,4 @@ location sharing never starts there. ./gradlew :findfamily:lint ./gradlew :findfamily:checkMetadata ```+- Wear Tile: `service/FindFamilyTileService` (a `MaTileService` in `:library:weartiles`) renders the nearest person's distance from self (freshest person when no self fix) from `WearFindFamilyStore`; the store requests a tile update on every new snapshot, plus 15-minute freshness. Tap opens `WearMainActivity`.
findfamily/build.gradle.kts
diff --git a/findfamily/build.gradle.kts b/findfamily/build.gradle.ktsindex b327839b8a..47dcc7aed8 100644--- a/findfamily/build.gradle.kts+++ b/findfamily/build.gradle.kts@@ -136,6 +136,7 @@ dependencies {     // WearLinkBridge receives it on a watch. FormFactor picks the side     // at runtime.     implementation(project(":library:wearlink"))+    implementation(project(":library:weartiles"))     // Watch experience (ui/Wear*): Wear Compose for the watch screens.     implementation(libs.androidx.wear.compose.material)     implementation(libs.androidx.wear.compose.foundation)
findfamily/src/main/AndroidManifest.xml
diff --git a/findfamily/src/main/AndroidManifest.xml b/findfamily/src/main/AndroidManifest.xmlindex 83af619944..d13921a980 100644--- a/findfamily/src/main/AndroidManifest.xml+++ b/findfamily/src/main/AndroidManifest.xml@@ -208,6 +208,20 @@                 <action android:name="com.vayunmathur.watch.action.SYNC_TRIGGER" />             </intent-filter>         </receiver>++        <!-- Wear OS Tile (watch-home card): renders from the wear snapshot store. -->+        <service+            android:name=".service.FindFamilyTileService"+            android:exported="true"+            android:icon="@mipmap/ic_launcher"+            android:label="FindFamily">+            <intent-filter>+                <action android:name="androidx.wear.tiles.action.BIND_TILE_PROVIDER" />+            </intent-filter>+            <meta-data+                android:name="androidx.wear.tiles.PREVIEW"+                android:resource="@mipmap/ic_launcher" />+        </service>     </application>  </manifest>\ No newline at end of file
findfamily/src/main/java/com/vayunmathur/findfamily/data/WearFindFamilyStore.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/data/WearFindFamilyStore.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/data/WearFindFamilyStore.ktindex 9b043e8a25..0ddb8d26ba 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/data/WearFindFamilyStore.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/data/WearFindFamilyStore.kt@@ -3,7 +3,9 @@ package com.vayunmathur.findfamily.data import android.content.Context import com.vayunmathur.library.wearlink.findfamily.WearFindFamilyProtocol import com.vayunmathur.library.wearlink.findfamily.WearFindFamilySnapshot+import com.vayunmathur.findfamily.service.FindFamilyTileService import com.vayunmathur.library.log.Log+import com.vayunmathur.library.weartiles.MaTileService import java.io.File import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow@@ -45,6 +47,7 @@ class WearFindFamilyStore private constructor(private val context: Context) {      fun setSnapshot(snapshot: WearFindFamilySnapshot) {         _snapshot.value = snapshot+        MaTileService.requestUpdate(context, FindFamilyTileService::class.java)         runCatching { snapshotFile.writeText(WearFindFamilyProtocol.json.encodeToString(snapshot)) }             .onFailure { Log.status(TAG, "persist snapshot failed: ${it.message}") }         com.vayunmathur.findfamily.sync.WearLinkBridge.publishWatchState(context)
findfamily/src/main/java/com/vayunmathur/findfamily/service/FindFamilyTileService.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/service/FindFamilyTileService.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/service/FindFamilyTileService.ktnew file mode 100644index 0000000000..618390692e--- /dev/null+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/service/FindFamilyTileService.kt@@ -0,0 +1,42 @@+package com.vayunmathur.findfamily.service++import android.content.Context+import com.vayunmathur.findfamily.data.WearFindFamilyStore+import com.vayunmathur.library.weartiles.MaTileService+import com.vayunmathur.library.weartiles.TileSnapshot+import com.vayunmathur.library.wearlink.findfamily.WearFindFamilySnapshot+import com.vayunmathur.library.wearlink.findfamily.wearDistanceMeters+import kotlin.math.roundToInt++/** Wear Tile for FindFamily: the nearest person's distance, from the wear snapshot. */+class FindFamilyTileService : MaTileService() {+    override val launchActivityName: String = "com.vayunmathur.findfamily.ui.WearMainActivity"++    override fun loadSnapshot(context: Context): TileSnapshot =+        findFamilyTileSnapshot(WearFindFamilyStore.get(context).snapshot.value)++    override fun previewSnapshot(): TileSnapshot =+        TileSnapshot(appName = "FindFamily", primary = "Alex · 400 m", secondary = "Home")+}++/** Pure nearest-person mapping: distance from self when a self fix exists, else freshest person. */+fun findFamilyTileSnapshot(snapshot: WearFindFamilySnapshot): TileSnapshot {+    val people = snapshot.people.filter { it.latE7 != null && it.lonE7 != null }+    if (people.isEmpty()) return TileSnapshot(appName = "FindFamily", primary = "—", secondary = "Open app to sync")+    val self = people.firstOrNull { it.isSelf }+    val others = people.filter { !it.isSelf }+    if (self != null && others.isNotEmpty()) {+        val nearest = others.minByOrNull {+            wearDistanceMeters(self.latE7!!, self.lonE7!!, it.latE7!!, it.lonE7!!)+        }!!+        val metres = wearDistanceMeters(self.latE7!!, self.lonE7!!, nearest.latE7!!, nearest.lonE7!!)+        val distance = if (metres < 1_000) "${metres.roundToInt()} m" else "${(metres / 1_000.0 * 10).roundToInt() / 10.0} km"+        return TileSnapshot(+            appName = "FindFamily",+            primary = "${nearest.name} · $distance",+            secondary = nearest.placeLabel,+        )+    }+    val freshest = people.maxByOrNull { it.updatedAtMillis } ?: people.first()+    return TileSnapshot(appName = "FindFamily", primary = freshest.name, secondary = freshest.placeLabel)+}
findfamily/src/test/java/com/vayunmathur/findfamily/service/FindFamilyTileServiceTest.kt
diff --git a/findfamily/src/test/java/com/vayunmathur/findfamily/service/FindFamilyTileServiceTest.kt b/findfamily/src/test/java/com/vayunmathur/findfamily/service/FindFamilyTileServiceTest.ktnew file mode 100644index 0000000000..493feacfa7--- /dev/null+++ b/findfamily/src/test/java/com/vayunmathur/findfamily/service/FindFamilyTileServiceTest.kt@@ -0,0 +1,33 @@+package com.vayunmathur.findfamily.service++import com.vayunmathur.library.weartiles.TileLayout+import com.vayunmathur.library.wearlink.findfamily.WearFindFamilySnapshot+import com.vayunmathur.library.wearlink.findfamily.WearPerson+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++class FindFamilyTileServiceTest {+    @Test+    fun nearestPersonDistanceRenders() {+        // Self at (0,0); Alex ~400 m east (0.0036 deg lon at equator), Sam much farther.+        val snapshot = WearFindFamilySnapshot(+            generatedAtMillis = 1L,+            people = listOf(+                WearPerson(id = 1, name = "Me", latE7 = 0, lonE7 = 0, isSelf = true, updatedAtMillis = 10L),+                WearPerson(id = 2, name = "Alex", latE7 = 0, lonE7 = 36_000, placeLabel = "Home", updatedAtMillis = 9L),+                WearPerson(id = 3, name = "Sam", latE7 = 0, lonE7 = 1_000_000, updatedAtMillis = 8L),+            ),+        )+        val tile = findFamilyTileSnapshot(snapshot)+        assertTrue(tile.primary.startsWith("Alex · "), tile.primary)+        assertTrue(tile.primary.contains("m"), tile.primary)+        assertEquals("Home", tile.secondary)+        assertTrue(TileLayout.build(tile, "com.vayunmathur.findfamily", "X").toString().contains("Alex"))+    }++    @Test+    fun noFixesFallsBackToDash() {+        assertEquals("—", findFamilyTileSnapshot(WearFindFamilySnapshot()).primary)+    }+}