diff options
author | ByteHamster <ByteHamster@users.noreply.github.com> | 2023-08-19 10:46:43 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-19 10:46:43 +0200 |
commit | 4f6b563e3fd42fa444a4b5ddcc5768ef4c85e641 (patch) | |
tree | 4c4c00ffe5438a913b1617bffb1c663d5d5789b8 | |
parent | 49ac7a83b8cd82a6bdbb14e046aab7e3e3ba0146 (diff) | |
download | AntennaPod-4f6b563e3fd42fa444a4b5ddcc5768ef4c85e641.zip |
Avoid race conditions when updating download notification (#6588)
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/service/download/EpisodeDownloadWorker.java | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/EpisodeDownloadWorker.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/EpisodeDownloadWorker.java index 2489ba2ba..56aee63a2 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/download/EpisodeDownloadWorker.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/EpisodeDownloadWorker.java @@ -62,10 +62,14 @@ public class EpisodeDownloadWorker extends Worker { Thread progressUpdaterThread = new Thread() { @Override public void run() { - while (!isInterrupted()) { + while (true) { try { - Thread.sleep(1000); - notificationProgress.put(media.getEpisodeTitle(), request.getProgressPercent()); + synchronized (notificationProgress) { + if (isInterrupted()) { + return; + } + notificationProgress.put(media.getEpisodeTitle(), request.getProgressPercent()); + } setProgressAsync( new Data.Builder() .putInt(DownloadServiceInterface.WORK_DATA_PROGRESS, request.getProgressPercent()) @@ -74,6 +78,7 @@ public class EpisodeDownloadWorker extends Worker { NotificationManager nm = (NotificationManager) getApplicationContext() .getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(R.id.notification_downloading, generateProgressNotification()); + Thread.sleep(1000); } catch (InterruptedException | ExecutionException e) { return; } @@ -94,11 +99,13 @@ public class EpisodeDownloadWorker extends Worker { } catch (InterruptedException e) { e.printStackTrace(); } - notificationProgress.remove(media.getEpisodeTitle()); - if (notificationProgress.isEmpty()) { - NotificationManager nm = (NotificationManager) getApplicationContext() - .getSystemService(Context.NOTIFICATION_SERVICE); - nm.cancel(R.id.notification_downloading); + synchronized (notificationProgress) { + notificationProgress.remove(media.getEpisodeTitle()); + if (notificationProgress.isEmpty()) { + NotificationManager nm = (NotificationManager) getApplicationContext() + .getSystemService(Context.NOTIFICATION_SERVICE); + nm.cancel(R.id.notification_downloading); + } } Log.d(TAG, "Worker for " + media.getDownload_url() + " returned."); return result; |