From ca8070b49fe5a9b1bf8c1a721b32b0478f4ef1c5 Mon Sep 17 00:00:00 2001 From: The412Banner Date: Wed, 25 Nov 2026 13:29:22 -0500 Subject: [PATCH] fix(runtime): first-run setup no longer crashes when the app is in the background With targetSdk 34, Android refuses startForeground() while the app is in the background. RuntimeInstallService called it unguarded on the main thread, so the refusal (ForegroundServiceStartNotAllowedException) killed the app. Seen on a Pocket FIT twice in a row at first run: the runtime finished while DroidDeck-Proton was still downloading, Onboarding asked for a package hold (RuntimeInstallService.kt:49 via onStartCommand :22) and the user was in another app. Both foreground starts now catch the refusal: a package hold stops the service and the downloads go on in the app as when startForegroundService() itself is refused; a runtime install or removal still runs, without progress notifications. --- .../launcher/runtime/RuntimeInstallService.kt | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/RuntimeInstallService.kt b/app/src/main/java/com/droiddeck/launcher/runtime/RuntimeInstallService.kt index a641cf1f28..b6e5666c67 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/RuntimeInstallService.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/RuntimeInstallService.kt @@ -22,7 +22,7 @@ class RuntimeInstallService : Service() { if (intent?.action == ACTION_PACKAGE) return holdPackage(startId) val removal = intent?.action == ACTION_REMOVE val release = intent?.let(::releaseFrom) - startForeground(NOTIFICATION_ID, notification(getString(if (LinuxRuntimeInstaller.isMaintaining()) R.string.desktop_removing else if (removal) R.string.rtinst_removing else R.string.user_apps_starting), -1)) + val foreground = enterForeground(notification(getString(if (LinuxRuntimeInstaller.isMaintaining()) R.string.desktop_removing else if (removal) R.string.rtinst_removing else R.string.user_apps_starting), -1)) if (release == null && !removal) { stopSelf(startId) return START_NOT_STICKY @@ -31,7 +31,7 @@ class RuntimeInstallService : Service() { val manager = getSystemService(NotificationManager::class.java) var shown = "" val progress = LinuxRuntimeInstaller.ProgressListener { stage, percent -> - if ("$stage:$percent" != shown) { + if (foreground && "$stage:$percent" != shown) { shown = "$stage:$percent" manager?.notify(NOTIFICATION_ID, notification(stage, percent)) } @@ -46,7 +46,10 @@ class RuntimeInstallService : Service() { } private fun holdPackage(startId: Int): Int { - startForeground(NOTIFICATION_ID, notification(packageStage, packagePercent, packageTitle)) + if (!enterForeground(notification(packageStage, packagePercent, packageTitle))) { + stopSelf(startId) + return START_NOT_STICKY + } Thread({ val manager = getSystemService(NotificationManager::class.java) var shown = "" @@ -65,6 +68,15 @@ class RuntimeInstallService : Service() { return START_NOT_STICKY } + /** Android 12+ refuses a foreground start while the app is in the background; the work then goes on without the notification. */ + private fun enterForeground(notification: Notification): Boolean = try { + startForeground(NOTIFICATION_ID, notification) + true + } catch (e: IllegalStateException) { + android.util.Log.w("RuntimeInstallService", "running without a foreground service: $e") + false + } + private fun notification(stage: String, percent: Int, title: String? = null): Notification { val manager = getSystemService(NotificationManager::class.java) manager?.createNotificationChannel(NotificationChannel(CHANNEL_ID, getString(R.string.setup_runtime),