fix(runtime): first-run setup no longer crashes when the app is in the background (#3212)

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 during first-run setup when the runtime finished while DroidDeck-Proton was still downloading (RuntimeInstallService.kt:49 via onStartCommand :22).

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.
This commit is contained in:
The412Banner authored and GitHub committed 2026-11-25 15:15:49 -05:00
1 parent 6b570c573b
commit 4df3f775e1
1 file changed
+15 -3
@@ -22,7 +22,7 @@ class RuntimeInstallService : Service() {
if (intent?.action == ACTION_PACKAGE) return holdPackage(startId) if (intent?.action == ACTION_PACKAGE) return holdPackage(startId)
val removal = intent?.action == ACTION_REMOVE val removal = intent?.action == ACTION_REMOVE
val release = intent?.let(::releaseFrom) 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) { if (release == null && !removal) {
stopSelf(startId) stopSelf(startId)
return START_NOT_STICKY return START_NOT_STICKY
@@ -31,7 +31,7 @@ class RuntimeInstallService : Service() {
val manager = getSystemService(NotificationManager::class.java) val manager = getSystemService(NotificationManager::class.java)
var shown = "" var shown = ""
val progress = LinuxRuntimeInstaller.ProgressListener { stage, percent -> val progress = LinuxRuntimeInstaller.ProgressListener { stage, percent ->
if ("$stage:$percent" != shown) { if (foreground && "$stage:$percent" != shown) {
shown = "$stage:$percent" shown = "$stage:$percent"
manager?.notify(NOTIFICATION_ID, notification(stage, percent)) manager?.notify(NOTIFICATION_ID, notification(stage, percent))
} }
@@ -46,7 +46,10 @@ class RuntimeInstallService : Service() {
} }
private fun holdPackage(startId: Int): Int { 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({ Thread({
val manager = getSystemService(NotificationManager::class.java) val manager = getSystemService(NotificationManager::class.java)
var shown = "" var shown = ""
@@ -65,6 +68,15 @@ class RuntimeInstallService : Service() {
return START_NOT_STICKY 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 { private fun notification(stage: String, percent: Int, title: String? = null): Notification {
val manager = getSystemService(NotificationManager::class.java) val manager = getSystemService(NotificationManager::class.java)
manager?.createNotificationChannel(NotificationChannel(CHANNEL_ID, getString(R.string.setup_runtime), manager?.createNotificationChannel(NotificationChannel(CHANNEL_ID, getString(R.string.setup_runtime),