From fcbbaa40603a04ba809058327c8c9620f3798570 Mon Sep 17 00:00:00 2001 From: Lincoln Ramsay Date: Thu, 25 Feb 2021 23:13:48 +1000 Subject: [PATCH] Avoid stuck download notifications Cancel the periodic updater before calling stopForeground so that the notification can't come back after it has been closed. Remove the workaround for API 30 slow notification closing because it can cause a stuck notification. However, in order to avoid misleading text being left on the notification if it closes slowly, ensure that the "0 downloads" text is written to the notification when shutting down. Fixes #4556 --- .../service/download/DownloadService.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java index 4a17fbbda..44b673a4d 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java @@ -172,8 +172,7 @@ public class DownloadService extends Service { setupNotificationUpdaterIfNecessary(); syncExecutor.execute(() -> onDownloadQueued(intent)); } else if (numberOfDownloads.get() == 0) { - stopForeground(true); - stopSelf(); + shutdown(); } else { Log.d(TAG, "onStartCommand: Unknown intent"); } @@ -227,10 +226,6 @@ public class DownloadService extends Service { } unregisterReceiver(cancelDownloadReceiver); - stopForeground(true); - NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - nm.cancel(R.id.notification_downloading); - // if this was the initial gpodder sync, i.e. we just synced the feeds successfully, // it is now time to sync the episode actions SyncService.sync(this); @@ -550,14 +545,7 @@ public class DownloadService extends Service { if (numberOfDownloads.get() <= 0 && DownloadRequester.getInstance().hasNoDownloads()) { Log.d(TAG, "Attempting shutdown"); - stopForeground(true); - stopSelf(); - - // Trick to hide the notification more quickly when the service is stopped - // Without this, the second-last update of the notification stays for 3 seconds after onDestroy returns - notificationUpdater.run(); - NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - nm.cancel(R.id.notification_downloading); + shutdown(); } } @@ -647,4 +635,14 @@ public class DownloadService extends Service { new PostDownloaderTask(downloads), 1, 1, TimeUnit.SECONDS); } } + + private void shutdown() { + // If the service was run for a very short time, the system may delay closing + // the notification. Set the notification text now so that a misleading message + // is not left on the notification. + notificationUpdater.run(); + cancelNotificationUpdater(); + stopForeground(true); + stopSelf(); + } }