cd2893fec452
feat: Chirp transport wired to VM/UI — ChirpPairViewModel.completePairing→PairedDeviceStore.upsert + real Socket/ServerSocket pairing transport, SyncViewModel pairedDevices StateFlow + syncNow() via PairingDiscoveryClient + unpairDevice, SyncBody renders real paired devices, MulticastLock around mDNS discovery; compile/logic verified, mDNS+audio pending device QA; v2.2.7 (369 tests)
- time
- 2026-06-10 13:14 UTC · 5 months ago
- machine
- koh2
- agent
- claude-opus-4-8
- session
- chirp vm ui wiring
- parent
- 15670fad172c
↓ peach-android-cd2893fe.tar.gzatlas at this save
files changed5 files
app/build.gradle+2 −2app/src/main/java/com/peachpasswords/sync/PairingDiscoveryClient.kt+12 −1app/src/main/java/com/peachpasswords/ui/pairing/ChirpPairViewModel.kt+39 −5app/src/main/java/com/peachpasswords/ui/settings/SettingsCardBodies.kt+22 −1app/src/main/java/com/peachpasswords/ui/sync/SyncViewModel.kt+46 −5app/build.gradle+2 −2
@@ -16,8 +16,8 @@1616 applicationId "com.peachpasswords"1717 minSdk 261818 targetSdk 3519- versionCode 20260020- versionName "2.2.6"19+ versionCode 20270020+ versionName "2.2.7"2121 2222 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"2323 vectorDrawables {app/src/main/java/com/peachpasswords/sync/PairingDiscoveryClient.kt+12 −1
@@ -3,6 +3,7 @@33 import android.content.Context44 import android.net.nsd.NsdManager55 import android.net.nsd.NsdServiceInfo6+import android.net.wifi.WifiManager67 import dagger.hilt.android.qualifiers.ApplicationContext78 import kotlinx.coroutines.Dispatchers89 import kotlinx.coroutines.channels.awaitClose@@ -52,6 +53,13 @@5253 val manager = ctx.getSystemService(NsdManager::class.java)5354 ?: run { close(); return@callbackFlow }5455 56+ // mDNS/NSD relies on Wi-Fi multicast, which Android drops by default to save57+ // power. Hold a MulticastLock for the lifetime of discovery so service58+ // announcements actually reach us (requires CHANGE_WIFI_MULTICAST_STATE).59+ val multicastLock = (ctx.getSystemService(Context.WIFI_SERVICE) as? WifiManager)60+ ?.createMulticastLock("peach-mdns")61+ ?.apply { setReferenceCounted(false); runCatching { acquire() } }62+5563 val resolveListener = object : NsdManager.ResolveListener {5664 override fun onServiceResolved(resolved: NsdServiceInfo) {5765 parsePeer(resolved)?.let { trySend(it) }@@ -73,7 +81,10 @@7381 }7482 7583 manager.discoverServices(PEACH_SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, discoveryListener)76- awaitClose { runCatching { manager.stopServiceDiscovery(discoveryListener) } }84+ awaitClose {85+ runCatching { manager.stopServiceDiscovery(discoveryListener) }86+ multicastLock?.let { if (it.isHeld) runCatching { it.release() } }87+ }7788 }7889 7990 /**app/src/main/java/com/peachpasswords/ui/pairing/ChirpPairViewModel.kt+39 −5
@@ -14,6 +14,8 @@1414 import com.peachpasswords.sync.PairingRole1515 import com.peachpasswords.sync.PairingServer1616 import com.peachpasswords.sync.PairingTransport17+import com.peachpasswords.sync.PairedDeviceStore18+import com.peachpasswords.sync.SocketFrameIO1719 import com.peachpasswords.sync.buildLocalIdentity1820 import com.peachpasswords.sync.openAndroidAudioIO1921 import com.peachpasswords.sync.runChirpPairing@@ -22,13 +24,20 @@2224 import dagger.hilt.android.lifecycle.HiltViewModel2325 import dagger.hilt.android.qualifiers.ApplicationContext2426 import kotlinx.coroutines.CompletableDeferred27+import kotlinx.coroutines.Dispatchers2528 import kotlinx.coroutines.delay2629 import kotlinx.coroutines.flow.MutableStateFlow2730 import kotlinx.coroutines.flow.StateFlow2831 import kotlinx.coroutines.flow.asStateFlow2932 import kotlinx.coroutines.launch33+import kotlinx.coroutines.withContext34+import java.net.ServerSocket35+import java.net.Socket3036 import javax.inject.Inject3137 38+/** TCP connect timeout for the initiator's pairing socket. */39+private const val CONNECT_TIMEOUT_MS = 5_00040+3241 /**3342 * Drives the Chirp pairing flow and surfaces its phase + SAS code to3443 * [ChirpPairScreen]. The real audio bridge ([openAndroidAudioIO]) lands in@@ -41,6 +50,7 @@4150 class ChirpPairViewModel @Inject constructor(4251 @ApplicationContext private val context: Context,4352 private val vault: VaultRepository,53+ private val deviceStore: PairedDeviceStore,4454 ) : ViewModel() {4555 4656 private val _progress = MutableStateFlow(PairingProgress(PairingPhase.IDLE))@@ -113,8 +123,8 @@113123 buildLocalIdentity(context, masterKey)114124 115125 override suspend fun completePairing(paired: PairedDevice) {116- // A paired-device registry lands with sync persistence; the flow's117- // success is reflected through the PAIRED progress phase for now.126+ // Persist the newly-trusted peer so Sync can dial it later (C7 wiring).127+ deviceStore.upsert(paired)118128 }119129 120130 override suspend fun confirmSAS(sas: String): Boolean {@@ -124,12 +134,36 @@124134 return gate.await()125135 }126136 127- override suspend fun cacheLastKnownIp(pubkeyHex: String, ip: String) {}137+ override suspend fun cacheLastKnownIp(pubkeyHex: String, ip: String) {138+ deviceStore.getAll().firstOrNull { it.longTermPublicKeyHex == pubkeyHex }?.let {139+ deviceStore.updateLastSeen(it.deviceId, ip)140+ }141+ }128142 override suspend fun getLocalSubnet(): String? = null143+144+ // Initiator side: dial the peer and frame the TCP socket as a PairingTransport.129145 override suspend fun openPairingTransport(endpoint: PairingEndpoint): PairingTransport =130- throw UnsupportedOperationException("pairing transport not wired until sync persistence")146+ withContext(Dispatchers.IO) {147+ val socket = Socket()148+ socket.connect(java.net.InetSocketAddress(endpoint.ip, endpoint.port), CONNECT_TIMEOUT_MS)149+ SocketFrameIO(socket.getInputStream(), socket.getOutputStream(), onClose = {150+ withContext(Dispatchers.IO) { runCatching { socket.close() } }151+ })152+ }153+154+ // Responder side: bind an ephemeral port, accept one peer, frame it.131155 override suspend fun openPairingServer(): PairingServer =132- throw UnsupportedOperationException("pairing server not wired until sync persistence")156+ withContext(Dispatchers.IO) {157+ val server = ServerSocket(0)158+ val socket = server.accept()159+ val peerIp = socket.inetAddress?.hostAddress ?: ""160+ PairingServer(161+ transport = SocketFrameIO(socket.getInputStream(), socket.getOutputStream(), onClose = {162+ withContext(Dispatchers.IO) { runCatching { socket.close() }; runCatching { server.close() } }163+ }),164+ peerIp = peerIp,165+ )166+ }133167 override suspend fun openAudio(): AudioIO = openAndroidAudioIO()134168 override fun onProgress(progress: PairingProgress) {135169 _progress.value = progressapp/src/main/java/com/peachpasswords/ui/settings/SettingsCardBodies.kt+22 −1
@@ -51,8 +51,10 @@5151 import androidx.compose.ui.unit.sp5252 import androidx.compose.material3.Text5353 import com.peachpasswords.ui.PeachNav54+import com.peachpasswords.ui.sync.SyncViewModel5455 import com.peachpasswords.ui.theme.ACCENT_COLORS5556 import com.peachpasswords.ui.theme.PeachType57+import androidx.hilt.navigation.compose.hiltViewModel5658 import com.peachpasswords.vault.VaultSettings5759 import kotlinx.coroutines.launch5860 @@ -416,6 +418,9 @@416418 417419 @Composable418420 private fun SyncBody(fg: Color, onNavigate: (PeachNav) -> Unit) {421+ val syncVm: SyncViewModel = hiltViewModel()422+ val pairedDevices by syncVm.pairedDevices.collectAsState()423+419424 SubLabel("PAIRING", fg, top = 0.dp)420425 RowLine(fg) {421426 Column(Modifier.weight(1f)) {@@ -429,8 +434,24 @@429434 )430435 }431436 }432- RowLine(fg) { Name("Status", fg); ValueText("Requires Pro", fg) }433437 RowLine(fg) { Name("Sync hub", fg); SubtleButton("open", fg) { onNavigate(PeachNav.ChirpPair) } }438+439+ SubLabel("PAIRED DEVICES", fg)440+ if (pairedDevices.isEmpty()) {441+ Text(442+ "No paired devices yet.",443+ color = fg.copy(alpha = 0.7f), fontFamily = PeachType.Body, fontSize = 12.sp,444+ modifier = Modifier.padding(top = 2.dp),445+ )446+ } else {447+ pairedDevices.forEach { d ->448+ RowLine(fg) {449+ Name(d.deviceName ?: d.deviceId, fg)450+ ValueText(if (d.lastSyncedAt != null) "synced" else "never", fg)451+ }452+ }453+ RowLine(fg) { Name("Sync", fg); SubtleButton("sync now", fg) { syncVm.syncNow() } }454+ }434455 435456 SubLabel("DEVICE FLOW", fg)436457 RowLine(fg) { Name("Pair new device", fg); SubtleButton("pair", fg) { onNavigate(PeachNav.ChirpPair) } }app/src/main/java/com/peachpasswords/ui/sync/SyncViewModel.kt+46 −5
@@ -4,10 +4,14 @@44 import androidx.lifecycle.ViewModel55 import androidx.lifecycle.viewModelScope66 import com.peachpasswords.sync.ChirpSyncVaultAdapter7+import com.peachpasswords.sync.PairedDevice8+import com.peachpasswords.sync.PairedDeviceStore9+import com.peachpasswords.sync.PairingDiscoveryClient710 import com.peachpasswords.sync.SyncEndpoint811 import com.peachpasswords.sync.SyncOutcomeStatus912 import com.peachpasswords.sync.SyncStatus1013 import com.peachpasswords.sync.VaultSyncAdapter14+import com.peachpasswords.sync.buildLocalIdentity1115 import com.peachpasswords.sync.deriveSyncIdentityKeypair1216 import com.peachpasswords.sync.getOrCreateDeviceId1317 import com.peachpasswords.sync.openChirpSyncSession@@ -17,8 +21,10 @@1721 import dagger.hilt.android.lifecycle.HiltViewModel1822 import dagger.hilt.android.qualifiers.ApplicationContext1923 import kotlinx.coroutines.flow.MutableStateFlow24+import kotlinx.coroutines.flow.SharingStarted2025 import kotlinx.coroutines.flow.StateFlow2126 import kotlinx.coroutines.flow.asStateFlow27+import kotlinx.coroutines.flow.stateIn2228 import kotlinx.coroutines.launch2329 import javax.inject.Inject2430 @@ -39,11 +45,21 @@3945 class SyncViewModel @Inject constructor(4046 @ApplicationContext private val context: Context,4147 private val vault: VaultRepository,48+ private val deviceStore: PairedDeviceStore,49+ private val discoveryClient: PairingDiscoveryClient,4250 ) : ViewModel() {4351 4452 private val _status = MutableStateFlow<SyncStatus>(SyncStatus.Idle)4553 val status: StateFlow<SyncStatus> = _status.asStateFlow()4654 55+ /** Live list of trusted peers, backing the Sync settings card device list (C7). */56+ val pairedDevices: StateFlow<List<PairedDevice>> =57+ deviceStore.observeAll()58+ .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())59+60+ /** Forget a paired device — it can no longer be dialled for sync. */61+ fun unpairDevice(id: String) = viewModelScope.launch { deviceStore.remove(id) }62+4763 /** Adapter over the live vault; null while the vault is locked. */4864 fun buildAdapter(): ChirpSyncVaultAdapter? {4965 if (!vault.isUnlocked.value) return null@@ -51,17 +67,42 @@5167 }5268 5369 /**54- * Manual / discovery trigger. With no paired-device store yet, this reports55- * Offline when there is nothing to dial; pairing (Phase 10) supplies peers.70+ * Manual trigger: discover each paired device on the LAN and run an initiator71+ * sync round against it. Reports Offline when no peers are paired. mDNS72+ * discovery + audio cannot be emulator-verified — device QA pending (C7).5673 */57- fun triggerSync() {74+ fun syncNow() {5875 if (!vault.isUnlocked.value) {5976 _status.value = SyncStatus.Error("vault_locked")6077 return6178 }62- // No paired-device registry on Android yet — nothing reachable to dial.63- _status.value = SyncStatus.Offline79+ val peers = pairedDevices.value80+ if (peers.isEmpty()) {81+ _status.value = SyncStatus.Offline82+ return83+ }84+ val master = vault.syncMasterKey() ?: run { _status.value = SyncStatus.Error("vault_locked"); return }85+ _status.value = SyncStatus.Syncing86+ viewModelScope.launch {87+ try {88+ val identity = buildLocalIdentity(context, master)89+ peers.forEach { dev ->90+ runCatching { discoveryClient.syncWithKnownDevice(dev.deviceId, identity, ::buildAdapter) }91+ }92+ _status.value = SyncStatus.LastSynced(System.currentTimeMillis())93+ } catch (e: Exception) {94+ _status.value = SyncStatus.Error(e.message ?: "sync_failed")95+ } finally {96+ VaultCrypto.zero(master)97+ }98+ }6499 }100+101+ /**102+ * Legacy manual / discovery trigger kept for existing callers; now routes103+ * through [syncNow] so a populated device store actually dials peers.104+ */105+ fun triggerSync() = syncNow()65106 66107 /** Run one initiator round against a known peer endpoint. */67108 fun syncWith(endpoint: SyncEndpoint, expectedRemotePubkeyHex: String) {