Files
pipelines/android/app/src/main/java/net/awan/pipelines/PipelinesApplication.kt
T
alan 3c47b08d98 Restore connections on app foregrounding
Add a process-lifecycle foreground reconnector and pure recovery policy, wiring repository recovery through the validated saved endpoint. Include lifecycle-process and focused unit coverage while preserving explicit Disconnect and stale socket callback guards.

Checked packaged templates and packaged authoring skill references: neither documents Android connection lifecycle behavior, so neither needs updating. Shared android README changes are recorded in docs/notes/foreground-reconnect.md for the docs-fold writer.
2026-10-18 12:37:20 +01:00

72 lines
2.9 KiB
Kotlin

package net.awan.pipelines
import android.app.Application
import androidx.lifecycle.ProcessLifecycleOwner
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import net.awan.pipelines.data.ForegroundReconnector
import net.awan.pipelines.data.PipelinesRepository
import net.awan.pipelines.data.SettingsRepository
import net.awan.pipelines.data.transport.PipelinesClient
import net.awan.pipelines.data.transport.ServerUrl
import net.awan.pipelines.notify.RunNotifier
/**
* Holds the single long-lived repository, WebSocket client and application
* scope. There is intentionally no dependency-injection framework here.
*/
class PipelinesApplication : Application() {
private val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
val settings: SettingsRepository by lazy { SettingsRepository(this) }
val client: PipelinesClient by lazy { PipelinesClient(scope = applicationScope) }
val repository: PipelinesRepository by lazy { PipelinesRepository(client, settings) }
/**
* Posts the app's own Android notifications for runs that need the operator.
* Its channel is created once, before anything can post, so a notification is
* never dropped for a missing channel.
*/
val runNotifier: RunNotifier by lazy { RunNotifier(this) }
/**
* The one owner of return-to-foreground recovery: when the process comes back
* to the front it restores a connection the background took away, with no
* interaction. It is created once with the Application and observes the
* process lifecycle, so every Activity and ViewModel recreation leaves it
* untouched. The liveness check is the existing `ping`; the reconnect is the
* same shared-client connect the app's own Connect/Reconnect uses, so the
* policy lives only in `foregroundAction` and the client's backoff stays the
* only reconnect mechanism.
*/
private val foregroundReconnector: ForegroundReconnector by lazy {
ForegroundReconnector(
state = { repository.connectionState.value },
confirmAlive = {
// The existing ping is the positive check: on a dead socket it
// throws (or never answers) instead of returning, so reaching the
// next line is the proof of life.
repository.ping()
true
},
reconnect = {
// A saved URL that no longer validates is left for Settings to
// report; there is nothing to reconnect to.
val validated = ServerUrl.validate(settings.serverUrl.value)
if (validated.valid) repository.recover(validated)
},
scope = applicationScope,
)
}
override fun onCreate() {
super.onCreate()
runNotifier.ensureChannel()
ProcessLifecycleOwner.get().lifecycle.addObserver(foregroundReconnector)
}
}