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
This commit is contained in:
Lincoln Ramsay 2021-02-25 23:13:48 +10:00
parent 1cd680a2af
commit fcbbaa4060
1 changed files with 12 additions and 14 deletions

View File

@ -172,8 +172,7 @@ public class DownloadService extends Service {
setupNotificationUpdaterIfNecessary(); setupNotificationUpdaterIfNecessary();
syncExecutor.execute(() -> onDownloadQueued(intent)); syncExecutor.execute(() -> onDownloadQueued(intent));
} else if (numberOfDownloads.get() == 0) { } else if (numberOfDownloads.get() == 0) {
stopForeground(true); shutdown();
stopSelf();
} else { } else {
Log.d(TAG, "onStartCommand: Unknown intent"); Log.d(TAG, "onStartCommand: Unknown intent");
} }
@ -227,10 +226,6 @@ public class DownloadService extends Service {
} }
unregisterReceiver(cancelDownloadReceiver); 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, // if this was the initial gpodder sync, i.e. we just synced the feeds successfully,
// it is now time to sync the episode actions // it is now time to sync the episode actions
SyncService.sync(this); SyncService.sync(this);
@ -550,14 +545,7 @@ public class DownloadService extends Service {
if (numberOfDownloads.get() <= 0 && DownloadRequester.getInstance().hasNoDownloads()) { if (numberOfDownloads.get() <= 0 && DownloadRequester.getInstance().hasNoDownloads()) {
Log.d(TAG, "Attempting shutdown"); Log.d(TAG, "Attempting shutdown");
stopForeground(true); shutdown();
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);
} }
} }
@ -647,4 +635,14 @@ public class DownloadService extends Service {
new PostDownloaderTask(downloads), 1, 1, TimeUnit.SECONDS); 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();
}
} }