New workaround for hiding the notification directly

This commit is contained in:
ByteHamster 2021-02-06 16:23:38 +01:00
parent c6b5d60a7c
commit b1bf77d4e3
2 changed files with 18 additions and 11 deletions

View File

@ -169,6 +169,7 @@ public class DownloadService extends Service {
Notification notification = notificationManager.updateNotifications( Notification notification = notificationManager.updateNotifications(
requester.getNumberOfDownloads(), downloads); requester.getNumberOfDownloads(), downloads);
startForeground(R.id.notification_downloading, notification); startForeground(R.id.notification_downloading, notification);
setupNotificationUpdaterIfNecessary();
syncExecutor.execute(() -> onDownloadQueued(intent)); syncExecutor.execute(() -> onDownloadQueued(intent));
} else if (numberOfDownloads.get() == 0) { } else if (numberOfDownloads.get() == 0) {
stopForeground(true); stopForeground(true);
@ -254,13 +255,13 @@ public class DownloadService extends Service {
handleSuccessfulDownload(downloader); handleSuccessfulDownload(downloader);
removeDownload(downloader); removeDownload(downloader);
numberOfDownloads.decrementAndGet(); numberOfDownloads.decrementAndGet();
queryDownloadsAsync(); stopServiceIfEverythingDoneAsync();
}); });
} else { } else {
handleFailedDownload(downloader); handleFailedDownload(downloader);
removeDownload(downloader); removeDownload(downloader);
numberOfDownloads.decrementAndGet(); numberOfDownloads.decrementAndGet();
queryDownloadsAsync(); stopServiceIfEverythingDoneAsync();
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
Log.e(TAG, "DownloadCompletionThread was interrupted"); Log.e(TAG, "DownloadCompletionThread was interrupted");
@ -409,7 +410,7 @@ public class DownloadService extends Service {
} }
postDownloaders(); postDownloaders();
} }
queryDownloads(); stopServiceIfEverythingDone();
} }
}; };
@ -480,7 +481,7 @@ public class DownloadService extends Service {
postDownloaders(); postDownloaders();
}); });
} }
handler.post(this::queryDownloads); handler.post(this::stopServiceIfEverythingDone);
} }
private static boolean isEnqueued(@NonNull DownloadRequest request, 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 * 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. * used from a thread other than the main thread.
*/ */
private void queryDownloadsAsync() { private void stopServiceIfEverythingDoneAsync() {
handler.post(DownloadService.this::queryDownloads); handler.post(DownloadService.this::stopServiceIfEverythingDone);
} }
/** /**
* Check if there's something else to download, otherwise stop. * Check if there's something else to download, otherwise stop.
*/ */
private void queryDownloads() { private void stopServiceIfEverythingDone() {
Log.d(TAG, numberOfDownloads.get() + " downloads left"); Log.d(TAG, numberOfDownloads.get() + " downloads left");
setupNotificationUpdaterIfNecessary();
notificationUpdater.run();
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); stopForeground(true);
stopSelf(); 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);
} }
} }

View File

@ -28,7 +28,10 @@ public class DownloadServiceNotification {
private void setupNotificationBuilders() { private void setupNotificationBuilders() {
notificationCompatBuilder = new NotificationCompat.Builder(context, NotificationUtils.CHANNEL_ID_DOWNLOADING) notificationCompatBuilder = new NotificationCompat.Builder(context, NotificationUtils.CHANNEL_ID_DOWNLOADING)
.setOngoing(true) .setOngoing(false)
.setWhen(0)
.setOnlyAlertOnce(true)
.setShowWhen(false)
.setContentIntent(ClientConfig.downloadServiceCallbacks.getNotificationContentIntent(context)) .setContentIntent(ClientConfig.downloadServiceCallbacks.getNotificationContentIntent(context))
.setSmallIcon(R.drawable.ic_notification_sync); .setSmallIcon(R.drawable.ic_notification_sync);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {