Modern Apps git

← Modern-Apps.git

emergency: contact edits after an inconclusive load no longer erase live contacts

Commit
5569aa58eaec34e6368e6b9f413bff5bb0ba0fd7
Author
Vayun Mathur <[email protected]>
When
2026-12-01 09:13 UTC (5 days ago)
Parents
cf65c1b
load() correctly refuses to prune from an inconclusive read
(the landed conclusive-prune fix), but addContact and
removeContact still derived the new persisted URI list from
the in-memory published snapshot — which is allowed to be a
strict subset of storage after such a load. With phones/5's
provider read failing, the published list held only
phones/6; addContact(phones/7) then persisted [phones/6,
phones/7] and removeContact(phones/6) persisted [] — in
both cases phones/5, a live emergency contact, was gone
forever, and once reads recovered it never came back.

Edits now persist against persistedUris: stored URIs in
stored order, plus any published-but-unpersisted URIs (the
pre-unlock case), with only the added URI de-duplicated and
the removed one filtered. Two regression tests cover
add-after and remove-after an inconclusive load (failed
pre-fix). The countdown handoff, SOS receivers, sound
restore and number lookup were audited and cleared.
Verified: repository/SOS tests pass in :emergency:
testDevUnitTest (the 2 failures are pre-existing
scaffold-loading issues on untouched files); lint errors
are pre-existing.

Diff

emergency/src/main/java/com/vayunmathur/emergency/data/EmergencyRepository.kt
diff --git a/emergency/src/main/java/com/vayunmathur/emergency/data/EmergencyRepository.kt b/emergency/src/main/java/com/vayunmathur/emergency/data/EmergencyRepository.ktindex 8b87a39f05..05ac285bc9 100644--- a/emergency/src/main/java/com/vayunmathur/emergency/data/EmergencyRepository.kt+++ b/emergency/src/main/java/com/vayunmathur/emergency/data/EmergencyRepository.kt@@ -80,7 +80,12 @@ class EmergencyRepository private constructor(         val contact = contacts.read(phoneUri) ?: return@withContext false         val current = mutable.value.contacts         if (current.any { it.phoneUri == phoneUri }) return@withContext true-        persistUris(current.map { it.phoneUri } + phoneUri)+        // Persist against storage (plus any published-but-unpersisted URIs),+        // never against the published list alone: an inconclusive load can+        // leave a live stored contact unpublished, and rewriting storage+        // from that list would permanently erase it on this unrelated edit.+        val persisted = persistedUris(current)+        if (phoneUri !in persisted) persistUris(persisted + phoneUri)         mutable.value = mutable.value.copy(contacts = current + contact)         updateSuggestionState()         true@@ -89,12 +94,26 @@ class EmergencyRepository private constructor(     suspend fun removeContact(phoneUri: Uri) = withContext(Dispatchers.IO) {         val current = mutable.value.contacts         val next = current.filterNot { it.phoneUri == phoneUri }-        if (next.size == current.size) return@withContext-        persistUris(next.map { it.phoneUri })+        val persisted = persistedUris(current)+        if (next.size == current.size && phoneUri !in persisted) return@withContext+        // See addContact: only the removed URI may leave storage; a stored+        // contact missing from the published list (inconclusive load) stays.+        persistUris(persisted.filterNot { it == phoneUri })         mutable.value = mutable.value.copy(contacts = next)         updateSuggestionState()     } +    /**+     * The URIs an edit may persist: everything in storage, in stored order,+     * plus any published URIs not yet persisted (e.g. added pre-unlock, where+     * [persistUris] is a no-op). The published list alone is not a safe basis+     * for rewriting storage - see [resolveAndPrune].+     */+    private fun persistedUris(current: List<EmergencyContact>): List<Uri> {+        val stored = storedContactUris()+        return stored + current.map { it.phoneUri }.filterNot { it in stored }+    }+     /** The stored contact URIs exactly as persisted, before any resolution. */     private fun storedContactUris(): List<Uri> {         // Legacy guard (b/28194605): contacts were once a string set; a ClassCastException
emergency/src/test/java/com/vayunmathur/emergency/platform/RepositoryAndServiceTest.kt
diff --git a/emergency/src/test/java/com/vayunmathur/emergency/platform/RepositoryAndServiceTest.kt b/emergency/src/test/java/com/vayunmathur/emergency/platform/RepositoryAndServiceTest.ktindex d117dbec78..2551391da3 100644--- a/emergency/src/test/java/com/vayunmathur/emergency/platform/RepositoryAndServiceTest.kt+++ b/emergency/src/test/java/com/vayunmathur/emergency/platform/RepositoryAndServiceTest.kt@@ -167,6 +167,68 @@ class RepositoryTest {         assertEquals(listOf(phoneUri), repo.info.value.contacts.map { it.phoneUri })         Unit     }++    @Test+    fun `repository add after inconclusive load does not erase unresolved stored contact`() = runBlocking {+        val secondUri = Uri.parse("content://com.android.contacts/data/phones/6")+        val thirdUri = Uri.parse("content://com.android.contacts/data/phones/7")+        FakeContactsProvider.phones[secondUri.toString()] =+            FakeContactsProvider.PhoneRow("Sam Rivera", "(555) 010-2031")+        FakeContactsProvider.phones[thirdUri.toString()] =+            FakeContactsProvider.PhoneRow("Jo Rivera", "(555) 010-2032")+        val repo = EmergencyRepository.get(ctx)+        repo.info.value.contacts.toList().forEach { repo.removeContact(it.phoneUri) }+        assertTrue(repo.addContact(phoneUri))+        assertTrue(repo.addContact(secondUri))++        // An inconclusive load leaves the first contact out of the published+        // list but must keep it in storage.+        FakeContactsProvider.failUris += phoneUri.toString()+        repo.load()+        assertEquals(listOf(secondUri), repo.info.value.contacts.map { it.phoneUri })+        assertTrue(storedContacts().contains(phoneUri.toString()))++        // Adding another contact must persist against the stored list, not+        // the incomplete published list, or the unresolved contact is erased.+        assertTrue(repo.addContact(thirdUri))+        assertTrue(storedContacts().contains(phoneUri.toString()))+        assertTrue(storedContacts().contains(secondUri.toString()))+        assertTrue(storedContacts().contains(thirdUri.toString()))++        FakeContactsProvider.failUris -= phoneUri.toString()+        repo.load()+        assertEquals(+            listOf(phoneUri, secondUri, thirdUri),+            repo.info.value.contacts.map { it.phoneUri },+        )+        Unit+    }++    @Test+    fun `repository remove after inconclusive load does not erase unresolved stored contact`() = runBlocking {+        val secondUri = Uri.parse("content://com.android.contacts/data/phones/6")+        FakeContactsProvider.phones[secondUri.toString()] =+            FakeContactsProvider.PhoneRow("Sam Rivera", "(555) 010-2031")+        val repo = EmergencyRepository.get(ctx)+        repo.info.value.contacts.toList().forEach { repo.removeContact(it.phoneUri) }+        assertTrue(repo.addContact(phoneUri))+        assertTrue(repo.addContact(secondUri))++        FakeContactsProvider.failUris += phoneUri.toString()+        repo.load()+        assertEquals(listOf(secondUri), repo.info.value.contacts.map { it.phoneUri })++        // Removing the visible contact must remove only that URI from+        // storage; the unresolved contact must survive the edit.+        repo.removeContact(secondUri)+        assertFalse(storedContacts().contains(secondUri.toString()))+        assertTrue(storedContacts().contains(phoneUri.toString()))++        FakeContactsProvider.failUris -= phoneUri.toString()+        repo.load()+        assertEquals(listOf(phoneUri), repo.info.value.contacts.map { it.phoneUri })+        Unit+    } }  @RunWith(RobolectricTestRunner::class)