Clean up stuck notifications when service gets killed

This commit is contained in:
Torsten Grote 2021-10-13 08:35:09 -03:00 committed by Chirayu Desai
parent c7880b8a8b
commit b0c6eeb9f7
2 changed files with 16 additions and 2 deletions

View file

@ -53,7 +53,7 @@ class ConfigurableBackupTransportService : Service(), KoinComponent {
override fun onDestroy() {
super.onDestroy()
notificationManager.onBackupBackgroundFinished()
notificationManager.onServiceDestroyed()
transport = null
Log.d(TAG, "Service destroyed.")
}

View file

@ -144,8 +144,22 @@ internal class BackupNotificationManager(private val context: Context) {
nm.notify(NOTIFICATION_ID_BACKGROUND, notification)
}
fun onBackupBackgroundFinished() {
fun onServiceDestroyed() {
nm.cancel(NOTIFICATION_ID_BACKGROUND)
// Cancel left-over notifications that are still ongoing.
//
// We have seen a race condition where the service was taken down at the same time
// as BackupObserver#backupFinished() was called, early enough to miss the cancel.
//
// This won't bring back the expected finish notification in this case,
// but at least we don't leave stuck notifications laying around.
nm.activeNotifications.forEach { notification ->
// only consider ongoing notifications in our ID space (storage backup uses > 1000)
if (notification.isOngoing && notification.id < 1000) {
Log.w(TAG, "Needed to clean up notification with ID ${notification.id}")
nm.cancel(notification.id)
}
}
}
fun onBackupFinished(success: Boolean, numBackedUp: Int?) {