Leave the server when the app is swiped away

The process lived on without a screen, or was killed without a word, and
the server held our identity as a ghost until it timed out, refusing a
reconnect in the meantime as a clone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-25 18:31:50 +00:00
parent 5fa7024bcb
commit 491cbcb259
2 changed files with 19 additions and 0 deletions

View File

@@ -12,6 +12,7 @@ import android.net.wifi.WifiManager
import android.os.PowerManager
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
@@ -66,6 +67,18 @@ class ConnectionService : LifecycleService() {
return START_NOT_STICKY
}
/**
* The user swiped the app away: they are done with it, connection included. Leaving
* properly spares the server a ghost that would hold our identity until it times out.
*/
override fun onTaskRemoved(rootIntent: Intent?) {
super.onTaskRemoved(rootIntent)
lifecycleScope.launch(Dispatchers.IO) {
sessions.leave()
stopSelf()
}
}
override fun onDestroy() {
wifiLock.release()
wakeLock.release()

View File

@@ -162,6 +162,12 @@ class SessionController(private val context: Context) {
session.disconnect()
}
/** Leaves the server before returning, for when the app goes away with the connection. */
fun leave() {
network.leaving()
session.shutdown()
}
// ---- bookmarks ----
fun addBookmark(label: String, address: String, nickname: String, password: String) {