Modern Apps git

← Modern-Apps.git

things: visitor-mode scale records are no longer filed to Health Connect

Commit
9c7e39b1a6e06d249b3a65655af21ee00337aacc
Author
Vayun Mathur <[email protected]>
When
2026-11-30 22:27 UTC (5 days ago)
Parents
5c3335b
In visitor mode (no user slot), the stored-records request
selected bit 0 — the unattributed bucket its own comment says
to leave out — and handleVaStored skipped its ownership check
entirely when ours == null, so every stored weigh-in on the
scale (any slot, plus unattributed 0xF0 records) was inserted
into the user's Health Connect. Other people's offline
weigh-ins became the user's health data.

requestStoredRecords now returns early without a slot (live
visitor measurements unaffected) and handleVaStored skips
anything not provably ours as defense-in-depth. New regression
test dispatches other-slot and 0xF0 records in visitor mode and
asserts zero Health Connect inserts (failed pre-fix). Bottle
logging, snapshot merges and history dedup were audited and
cleared. Verified: :things:testDevUnitTest 157 tests pass; lint
errors are pre-existing on untouched files.

Diff

things/src/main/java/com/vayunmathur/things/platform/ScaleBleMeasurement.kt
diff --git a/things/src/main/java/com/vayunmathur/things/platform/ScaleBleMeasurement.kt b/things/src/main/java/com/vayunmathur/things/platform/ScaleBleMeasurement.ktindex b7c41a3139..33c5a64a91 100644--- a/things/src/main/java/com/vayunmathur/things/platform/ScaleBleMeasurement.kt+++ b/things/src/main/java/com/vayunmathur/things/platform/ScaleBleMeasurement.kt@@ -130,7 +130,11 @@ internal fun ScaleBleManager.handleUserSyncResult(v: ByteArray) {  * likely to be someone else's than ours.  */ internal fun ScaleBleManager.requestStoredRecords() {-    val mask = vaUserIndex?.let { 1 shl it } ?: 1+    // Visitor mode holds no slot, so there is no mask that selects only our records:+    // bit 0 is the unattributed bucket, and asking for it would drain weigh-ins that+    // cannot be told apart from anyone else's. Live measurements still work.+    val index = vaUserIndex ?: return+    val mask = 1 shl index     enqueue(         bleWriteChar,         buildCmd(CMD_START, (mask shr KEY_HIGH_SHIFT) and BYTE_MASK, mask and BYTE_MASK),@@ -153,7 +157,7 @@ internal fun ScaleBleManager.handleVaStored(v: ByteArray) {     // The mask should already have filtered these scale-side; re-check rather than risk     // filing someone else's weigh-in, or an unattributed one (0xF0), as ours.     val ours = vaUserIndex-    if (ours != null && recordUser != ours) {+    if (ours == null || recordUser != ours) {         Log.dev(ScaleBleManager.TAG, "stored record $index/$total belongs to user $recordUser; skipped")         return     }
things/src/test/java/com/vayunmathur/things/platform/ScaleBlePacketTest.kt
diff --git a/things/src/test/java/com/vayunmathur/things/platform/ScaleBlePacketTest.kt b/things/src/test/java/com/vayunmathur/things/platform/ScaleBlePacketTest.ktindex 99dcf28d7d..c01b2da0b7 100644--- a/things/src/test/java/com/vayunmathur/things/platform/ScaleBlePacketTest.kt+++ b/things/src/test/java/com/vayunmathur/things/platform/ScaleBlePacketTest.kt@@ -1,5 +1,7 @@ package com.vayunmathur.things.platform +// FIXTURE RELATIVE-TIME EXCEPTION: timestamps are seeded relative to the wall clock the code under test reads internally, so the fresh/expired/relative-time branch under test keeps its meaning; a fixed past instant would silently change that branch.+ import android.content.Context import android.os.Looper import androidx.health.connect.client.HealthConnectClient@@ -292,6 +294,32 @@ class ScaleBlePacketTest {         }     } +    @Test+    fun `va stored records are ignored in visitor mode without a user slot`() {+        val mgr = manager(va = true)+        // Visitor mode: the visit to 0xFE left us with no slot of our own.+        mgr.vaUserIndex = null+        val fake = FakeHealthConnect()+        withFakeHealth(fake) {+            fun vaStored(user: Int, secondsSince2000: Long): ByteArray {+                val b = ByteArray(18)+                b[0] = PACKET_STORED.toByte()+                b[3] = 1; b[4] = 1; b[5] = user.toByte()+                for (i in 0 until 4) b[6 + i] = (secondsSince2000 shr (8 * i)).toByte()+                b[10] = 0x02; b[11] = 0xEE.toByte() // 750 * 0.1 = 75+                b[12] = 0x01; b[13] = 0xF4.toByte(); b[14] = 0x01; b[15] = 0x2C+                return b+            }+            val nowSec = System.currentTimeMillis() / 1000 - BASE_TIME_2000_SECONDS+            // Another slot's record and an unattributed (0xF0) record must not be+            // filed to Health Connect as ours when we hold no slot.+            mgr.dispatch(vaStored(user = 2, secondsSince2000 = nowSec - 60))+            mgr.dispatch(vaStored(user = 0xF0, secondsSince2000 = nowSec - 60))+            flush()+            assertEquals(0, fake.inserted.size)+        }+    }+     @Test     fun `user sync results register visit and delete`() {         val mgr = manager(va = true)