Skip to content

A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.

fix(vm): survive a background restart and a recreated activity

ExTvb95c16538688
diff --git a/app/src/main/java/com/excp/podroid/MainActivity.kt b/app/src/main/java/com/excp/podroid/MainActivity.kt
index c73a1a3..3b90e8b 100644
--- a/app/src/main/java/com/excp/podroid/MainActivity.kt
+++ b/app/src/main/java/com/excp/podroid/MainActivity.kt
@@ -36,7 +36,9 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
super.onCreate(savedInstanceState)
- handleStartVmIntent(intent)
+ if (savedInstanceState == null) {
+ handleStartVmIntent(intent)
+ }
setContent {
val windowSizeClass = calculateWindowSizeClass(this)
val navVm: NavGraphViewModel = hiltViewModel()
@@ -100,10 +102,11 @@ class MainActivity : ComponentActivity() {
}
/**
- * Foreground-launch path for automation (#78 fix round): a cold
- * background receiver may be denied a foreground-service start, but an
- * Activity launch is always foreground, so this route is reliable on
- * both cold start (onCreate) and warm delivery (onNewIntent).
+ * Foreground-launch path for automation: a cold background receiver may
+ * be denied a foreground-service start, but an Activity launch is always
+ * foreground, so this route is reliable on both cold start (onCreate,
+ * only when there is no saved state to restore) and warm delivery
+ * (onNewIntent).
*/
private fun handleStartVmIntent(intent: Intent?) {
if (intent?.action == VmControlReceiver.ACTION_START_VM) {
diff --git a/app/src/main/java/com/excp/podroid/service/PodroidService.kt b/app/src/main/java/com/excp/podroid/service/PodroidService.kt
index ae750ff..9517c5a 100644
--- a/app/src/main/java/com/excp/podroid/service/PodroidService.kt
+++ b/app/src/main/java/com/excp/podroid/service/PodroidService.kt
@@ -42,6 +42,35 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.flow.combine
import javax.inject.Inject
+/**
+ * Runs [action], swallowing a denied foreground-service start instead of
+ * letting it crash the caller. Both a cold [VmControlReceiver] dispatch and
+ * a guest-requested [PodroidService.scheduleRestart] can call
+ * [PodroidService.start] outside a foreground context, where Android may
+ * deny it with a [android.app.ForegroundServiceStartNotAllowedException]
+ * (API 31+, extends [IllegalStateException]; pre-31 the same denial surfaces
+ * as a plain [IllegalStateException] from `startService()`). Never silent:
+ * logs one warning naming the cure (launch Podroid so its START_VM activity
+ * intent runs in the foreground, or exempt the app from battery
+ * optimization).
+ */
+private fun runIgnoringBackgroundFgsDenial(tag: String, action: () -> Unit) {
+ try {
+ action()
+ } catch (e: IllegalStateException) {
+ val backgroundFgsDenied = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
+ e is android.app.ForegroundServiceStartNotAllowedException
+ Log.w(
+ tag,
+ "could not start/stop the VM from the background " +
+ "(${if (backgroundFgsDenied) "foreground service start disallowed" else e.message}) - " +
+ "launch Podroid so its START_VM activity intent runs in the foreground, " +
+ "or exempt the app from battery optimization",
+ e,
+ )
+ }
+}
+
@AndroidEntryPoint
class PodroidService : Service() {
@@ -504,10 +533,10 @@ class PodroidService : Service() {
// it as terminal could fire start() during a teardown blip.
val terminal = s is VmState.Stopped || s is VmState.Error
when {
- terminal -> PodroidService.start(ctx)
+ terminal -> runIgnoringBackgroundFgsDenial(TAG) { PodroidService.start(ctx) }
tries++ >= 40 -> {
Log.w(TAG, "restart: VM did not reach a stopped state in time (state=$s); starting anyway")
- PodroidService.start(ctx)
+ runIgnoringBackgroundFgsDenial(TAG) { PodroidService.start(ctx) }
}
else -> main.postDelayed(this, 250)
}
@@ -583,30 +612,8 @@ class VmControlReceiver : BroadcastReceiver() {
}
}
- /**
- * Runs a start()/stop() dispatch, swallowing a denied foreground-service
- * start instead of letting it crash the receiver. Never silent: logs one
- * warning naming the cure (launch Podroid itself so its activity's
- * START_VM intent-filter fires the start in the foreground, or exempt
- * the app from battery optimization so a cold background start is
- * allowed).
- */
- private fun dispatch(action: () -> Unit) {
- try {
- action()
- } catch (e: IllegalStateException) {
- val backgroundFgsDenied = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
- e is android.app.ForegroundServiceStartNotAllowedException
- Log.w(
- TAG,
- "VmControlReceiver could not start/stop the VM from the background " +
- "(${if (backgroundFgsDenied) "foreground service start disallowed" else e.message}) - " +
- "launch Podroid so its START_VM activity intent runs in the foreground, " +
- "or exempt the app from battery optimization",
- e,
- )
- }
- }
+ /** See [runIgnoringBackgroundFgsDenial] for what this guards against. */
+ private fun dispatch(action: () -> Unit) = runIgnoringBackgroundFgsDenial(TAG, action)
companion object {
private const val TAG = "VmControlReceiver"