From f69822582d6cb650bf5eca072f76874b6140aa07 Mon Sep 17 00:00:00 2001 From: 0x082c8bf1 <70177827+0x082c8bf1@users.noreply.github.com> Date: Sat, 27 Apr 2024 03:44:09 -0500 Subject: [PATCH] Use multiple threads for refreshing feeds (#7126) --- .../service/feed/FeedUpdateWorker.java | 65 ++++++++++++------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/net/download/service/src/main/java/de/danoeh/antennapod/net/download/service/feed/FeedUpdateWorker.java b/net/download/service/src/main/java/de/danoeh/antennapod/net/download/service/feed/FeedUpdateWorker.java index c835f08c66..9e363b5852 100644 --- a/net/download/service/src/main/java/de/danoeh/antennapod/net/download/service/feed/FeedUpdateWorker.java +++ b/net/download/service/src/main/java/de/danoeh/antennapod/net/download/service/feed/FeedUpdateWorker.java @@ -12,6 +12,7 @@ import androidx.core.content.ContextCompat; import androidx.work.ForegroundInfo; import androidx.work.WorkManager; +import java.util.concurrent.TimeUnit; import androidx.work.Worker; import androidx.work.WorkerParameters; import com.google.common.util.concurrent.Futures; @@ -39,6 +40,8 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; public class FeedUpdateWorker extends Worker { private static final String TAG = "FeedUpdateWorker"; @@ -127,6 +130,13 @@ private Notification createNotification(@Nullable List toUpdate) { .build(); } + private void updateNotification(List toUpdate) { + if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.POST_NOTIFICATIONS) + == PackageManager.PERMISSION_GRANTED) { + notificationManager.notify(R.id.notification_updating_feeds, createNotification(toUpdate)); + } + } + @NonNull @Override public ListenableFuture getForegroundInfoAsync() { @@ -134,29 +144,40 @@ public ListenableFuture getForegroundInfoAsync() { } private void refreshFeeds(List toUpdate, boolean force) { - while (!toUpdate.isEmpty()) { - if (isStopped()) { - return; - } - if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.POST_NOTIFICATIONS) - == PackageManager.PERMISSION_GRANTED) { - notificationManager.notify(R.id.notification_updating_feeds, createNotification(toUpdate)); - } - Feed feed = toUpdate.get(0); - try { - if (feed.isLocalFeed()) { - LocalFeedUpdater.updateFeed(feed, getApplicationContext(), null); - } else { - refreshFeed(feed, force); + List notificationRemainingFeeds = new ArrayList<>(toUpdate); + updateNotification(notificationRemainingFeeds); + ExecutorService executor = Executors.newFixedThreadPool(4); + for (Feed feed : toUpdate) { + executor.submit(() -> { + if (isStopped()) { + return; } - } catch (Exception e) { - DBWriter.setFeedLastUpdateFailed(feed.getId(), true); - DownloadResult status = new DownloadResult(feed.getTitle(), - feed.getId(), Feed.FEEDFILETYPE_FEED, false, - DownloadError.ERROR_IO_ERROR, e.getMessage()); - DBWriter.addDownloadStatus(status); - } - toUpdate.remove(0); + try { + if (feed.isLocalFeed()) { + LocalFeedUpdater.updateFeed(feed, getApplicationContext(), null); + } else { + refreshFeed(feed, force); + } + } catch (Exception e) { + DBWriter.setFeedLastUpdateFailed(feed.getId(), true); + DownloadResult status = new DownloadResult(feed.getTitle(), + feed.getId(), Feed.FEEDFILETYPE_FEED, false, + DownloadError.ERROR_IO_ERROR, e.getMessage()); + DBWriter.addDownloadStatus(status); + } + synchronized (notificationRemainingFeeds) { + notificationRemainingFeeds.remove(feed); + if (!notificationRemainingFeeds.isEmpty()) { + updateNotification(notificationRemainingFeeds); + } + } + }); + } + executor.shutdown(); + try { + executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); + } catch (InterruptedException e) { + //~300 years have elapsed } }