Avoid race conditions when updating download notification (#6588)
This commit is contained in:
parent
49ac7a83b8
commit
4f6b563e3f
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue