From d266c36c91f98359059ae6f7a68e58e8026451d3 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Thu, 22 Aug 2024 17:36:26 -0300 Subject: [PATCH] Don't use Context#startForegroundService() because we may get killed If the foreground service doesn't have anything to do and terminates quickly, the system will kill us, even though the service had called startForeground(). To prevent this, we don't promise that our service will be a foreground service. We can still be a foreground service, but escape the punishment if we are too quick. --- .../com/stevesoltys/seedvault/restore/AppDataRestoreManager.kt | 3 ++- .../com/stevesoltys/seedvault/restore/install/ApkRestore.kt | 3 ++- .../seedvault/restore/install/ApkBackupRestoreTest.kt | 2 +- .../stevesoltys/seedvault/restore/install/ApkRestoreTest.kt | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/stevesoltys/seedvault/restore/AppDataRestoreManager.kt b/app/src/main/java/com/stevesoltys/seedvault/restore/AppDataRestoreManager.kt index 0bf703a42..8d8bb01fb 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/restore/AppDataRestoreManager.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/restore/AppDataRestoreManager.kt @@ -123,7 +123,8 @@ internal class AppDataRestoreManager( RestoreBackupResult(context.getString(R.string.restore_set_error)) ) } else { - context.startForegroundService(foregroundServiceIntent) + // don't use startForeground(), because we may stop it sooner than the system likes + context.startService(foregroundServiceIntent) } } diff --git a/app/src/main/java/com/stevesoltys/seedvault/restore/install/ApkRestore.kt b/app/src/main/java/com/stevesoltys/seedvault/restore/install/ApkRestore.kt index d62bb9dbc..48aa99f9d 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/restore/install/ApkRestore.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/restore/install/ApkRestore.kt @@ -90,7 +90,8 @@ internal class ApkRestore( val i = Intent(context, RestoreService::class.java) val autoRestore = backupStateManager.isAutoRestoreEnabled try { - context.startForegroundService(i) + // don't use startForeground(), because we may stop it sooner than the system likes + context.startService(i) // disable auto-restore before installing apps, if it was enabled before if (autoRestore) backupManager.setAutoRestore(false) reInstallApps(backup, packages.asIterable().reversed()) diff --git a/app/src/test/java/com/stevesoltys/seedvault/restore/install/ApkBackupRestoreTest.kt b/app/src/test/java/com/stevesoltys/seedvault/restore/install/ApkBackupRestoreTest.kt index 389bfaf2a..5fae19666 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/restore/install/ApkBackupRestoreTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/restore/install/ApkBackupRestoreTest.kt @@ -131,7 +131,7 @@ internal class ApkBackupRestoreTest : TransportTest() { // related to starting/stopping service every { strictContext.packageName } returns "org.foo.bar" every { - strictContext.startForegroundService(any()) + strictContext.startService(any()) } returns ComponentName(strictContext, "org.foo.bar.Class") every { strictContext.stopService(any()) } returns true diff --git a/app/src/test/java/com/stevesoltys/seedvault/restore/install/ApkRestoreTest.kt b/app/src/test/java/com/stevesoltys/seedvault/restore/install/ApkRestoreTest.kt index 8fcaece16..201824f6c 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/restore/install/ApkRestoreTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/restore/install/ApkRestoreTest.kt @@ -112,7 +112,7 @@ internal class ApkRestoreTest : TransportTest() { // related to starting/stopping service every { strictContext.packageName } returns "org.foo.bar" every { - strictContext.startForegroundService(any()) + strictContext.startService(any()) } returns ComponentName(strictContext, "org.foo.bar.Class") every { strictContext.stopService(any()) } returns true }