Avoid race conditions when updating download notification (#6588)

This commit is contained in:
ByteHamster 2023-08-19 10:46:43 +02:00 committed by GitHub
parent 49ac7a83b8
commit 4f6b563e3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 8 deletions

View File

@ -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;