From b1bf77d4e3c368641eb6801dd36affbe9846a154 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Sat, 6 Feb 2021 16:23:38 +0100 Subject: [PATCH] New workaround for hiding the notification directly --- .../service/download/DownloadService.java | 24 +++++++++++-------- .../download/DownloadServiceNotification.java | 5 +++- 2 files changed, 18 insertions(+), 11 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 bc696f444..4a17fbbda 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 @@ -169,6 +169,7 @@ public class DownloadService extends Service { Notification notification = notificationManager.updateNotifications( requester.getNumberOfDownloads(), downloads); startForeground(R.id.notification_downloading, notification); + setupNotificationUpdaterIfNecessary(); syncExecutor.execute(() -> onDownloadQueued(intent)); } else if (numberOfDownloads.get() == 0) { stopForeground(true); @@ -254,13 +255,13 @@ public class DownloadService extends Service { handleSuccessfulDownload(downloader); removeDownload(downloader); numberOfDownloads.decrementAndGet(); - queryDownloadsAsync(); + stopServiceIfEverythingDoneAsync(); }); } else { handleFailedDownload(downloader); removeDownload(downloader); numberOfDownloads.decrementAndGet(); - queryDownloadsAsync(); + stopServiceIfEverythingDoneAsync(); } } catch (InterruptedException e) { Log.e(TAG, "DownloadCompletionThread was interrupted"); @@ -409,7 +410,7 @@ public class DownloadService extends Service { } postDownloaders(); } - queryDownloads(); + stopServiceIfEverythingDone(); } }; @@ -480,7 +481,7 @@ public class DownloadService extends Service { postDownloaders(); }); } - handler.post(this::queryDownloads); + handler.post(this::stopServiceIfEverythingDone); } private static boolean isEnqueued(@NonNull DownloadRequest request, @@ -537,23 +538,26 @@ public class DownloadService extends Service { * Calls query downloads on the services main thread. This method should be used instead of queryDownloads if it is * used from a thread other than the main thread. */ - private void queryDownloadsAsync() { - handler.post(DownloadService.this::queryDownloads); + private void stopServiceIfEverythingDoneAsync() { + handler.post(DownloadService.this::stopServiceIfEverythingDone); } /** * Check if there's something else to download, otherwise stop. */ - private void queryDownloads() { + private void stopServiceIfEverythingDone() { Log.d(TAG, numberOfDownloads.get() + " downloads left"); - setupNotificationUpdaterIfNecessary(); - notificationUpdater.run(); - 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); } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadServiceNotification.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadServiceNotification.java index 4fae9f34a..64ed85cf3 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadServiceNotification.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadServiceNotification.java @@ -28,7 +28,10 @@ public class DownloadServiceNotification { private void setupNotificationBuilders() { notificationCompatBuilder = new NotificationCompat.Builder(context, NotificationUtils.CHANNEL_ID_DOWNLOADING) - .setOngoing(true) + .setOngoing(false) + .setWhen(0) + .setOnlyAlertOnce(true) + .setShowWhen(false) .setContentIntent(ClientConfig.downloadServiceCallbacks.getNotificationContentIntent(context)) .setSmallIcon(R.drawable.ic_notification_sync); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {