diff options
author | ByteHamster <info@bytehamster.com> | 2021-02-06 12:32:53 +0100 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2021-02-15 11:43:57 +0100 |
commit | 4aa54435298b4d21d070069c2569adb894435604 (patch) | |
tree | ae84a2893e4123ba5d445732018877261604b178 /core/src/main | |
parent | 52421d81f73277557a2ba2ad097379f105213d98 (diff) | |
download | AntennaPod-4aa54435298b4d21d070069c2569adb894435604.zip |
Refresh all feeds at once instead of sending tons of lists with 1 feed each
Diffstat (limited to 'core/src/main')
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java | 100 | ||||
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java | 31 |
2 files changed, 61 insertions, 70 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java index 48a3f574a..f5a8fb07a 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java @@ -16,7 +16,6 @@ import de.danoeh.antennapod.core.event.MessageEvent; import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedMedia; -import de.danoeh.antennapod.core.feed.FeedPreferences; import de.danoeh.antennapod.core.feed.LocalFeedUpdater; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.service.download.DownloadStatus; @@ -31,6 +30,7 @@ import java.util.Collections; import java.util.Date; import java.util.Iterator; import java.util.List; +import java.util.ListIterator; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; @@ -121,7 +121,18 @@ public final class DBTasks { throw new IllegalStateException("DBTasks.refreshAllFeeds() must not be called from the main thread."); } - refreshFeeds(context, DBReader.getFeedList(), initiatedByUser); + List<Feed> feeds = DBReader.getFeedList(); + ListIterator<Feed> iterator = feeds.listIterator(); + while (iterator.hasNext()) { + if (!iterator.next().getPreferences().getKeepUpdated()) { + iterator.remove(); + } + } + try { + refreshFeeds(context, feeds, false, false, false); + } catch (DownloadRequestException e) { + e.printStackTrace(); + } isRefreshing.set(false); SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, MODE_PRIVATE); @@ -135,38 +146,6 @@ public final class DBTasks { } /** - * @param context - * @param feedList the list of feeds to refresh - * @param initiatedByUser a boolean indicating if the refresh was triggered by user action. - */ - private static void refreshFeeds(final Context context, - final List<Feed> feedList, - boolean initiatedByUser) { - - for (Feed feed : feedList) { - FeedPreferences prefs = feed.getPreferences(); - // feeds with !getKeepUpdated can only be refreshed - // directly from the FeedActivity - if (prefs.getKeepUpdated()) { - try { - refreshFeed(context, feed); - } catch (DownloadRequestException e) { - e.printStackTrace(); - DBWriter.addDownloadStatus( - new DownloadStatus(feed, - feed.getHumanReadableIdentifier(), - DownloadError.ERROR_REQUEST_ERROR, - false, - e.getMessage(), - initiatedByUser) - ); - } - } - } - - } - - /** * Downloads all pages of the given feed even if feed has not been modified since last refresh * * @param context Used for requesting the download. @@ -174,7 +153,7 @@ public final class DBTasks { */ public static void forceRefreshCompleteFeed(final Context context, final Feed feed) { try { - refreshFeed(context, feed, true, true, false); + refreshFeeds(context, Collections.singletonList(feed), true, true, false); } catch (DownloadRequestException e) { e.printStackTrace(); DBWriter.addDownloadStatus( @@ -210,19 +189,6 @@ public final class DBTasks { } /** - * Refresh a specific Feed. The refresh may get canceled if the feed does not seem to be modified - * and the last update was only few days ago. - * - * @param context Used for requesting the download. - * @param feed The Feed object. - */ - private static void refreshFeed(Context context, Feed feed) - throws DownloadRequestException { - Log.d(TAG, "refreshFeed(feed.id: " + feed.getId() +")"); - refreshFeed(context, feed, false, false, false); - } - - /** * Refresh a specific feed even if feed has not been modified since last refresh * * @param context Used for requesting the download. @@ -230,26 +196,32 @@ public final class DBTasks { */ public static void forceRefreshFeed(Context context, Feed feed, boolean initiatedByUser) throws DownloadRequestException { - Log.d(TAG, "refreshFeed(feed.id: " + feed.getId() +")"); - refreshFeed(context, feed, false, true, initiatedByUser); + Log.d(TAG, "refreshFeed(feed.id: " + feed.getId() + ")"); + refreshFeeds(context, Collections.singletonList(feed), false, true, initiatedByUser); } - private static void refreshFeed(Context context, Feed feed, boolean loadAllPages, boolean force, boolean initiatedByUser) - throws DownloadRequestException { - Feed f; - String lastUpdate = feed.hasLastUpdateFailed() ? null : feed.getLastUpdate(); - if (feed.getPreferences() == null) { - f = new Feed(feed.getDownload_url(), lastUpdate, feed.getTitle()); - } else { - f = new Feed(feed.getDownload_url(), lastUpdate, feed.getTitle(), - feed.getPreferences().getUsername(), feed.getPreferences().getPassword()); + private static void refreshFeeds(Context context, List<Feed> feeds, boolean loadAllPages, + boolean force, boolean initiatedByUser) throws DownloadRequestException { + List<Feed> localFeeds = new ArrayList<>(); + List<Feed> normalFeeds = new ArrayList<>(); + + for (Feed feed : feeds) { + if (feed.isLocalFeed()) { + localFeeds.add(feed); + } else { + normalFeeds.add(feed); + } } - f.setId(feed.getId()); - if (f.isLocalFeed()) { - new Thread(() -> LocalFeedUpdater.updateFeed(f, context)).start(); - } else { - DownloadRequester.getInstance().downloadFeed(context, f, loadAllPages, force, initiatedByUser); + if (!localFeeds.isEmpty()) { + new Thread(() -> { + for (Feed feed : localFeeds) { + LocalFeedUpdater.updateFeed(feed, context); + } + }).start(); + } + if (!normalFeeds.isEmpty()) { + DownloadRequester.getInstance().downloadFeeds(context, feeds, loadAllPages, force, initiatedByUser); } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java index e3121caa2..638c1bef5 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java @@ -17,6 +17,7 @@ import org.apache.commons.io.FilenameUtils; import java.io.File; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -184,16 +185,31 @@ public class DownloadRequester implements DownloadStateProvider { } /** - * Downloads a feed + * Downloads a feed. * * @param context The application's environment. - * @param feed Feed to download + * @param feed Feeds to download * @param loadAllPages Set to true to download all pages */ public synchronized void downloadFeed(Context context, Feed feed, boolean loadAllPages, - boolean force, boolean initiatedByUser) - throws DownloadRequestException { - if (feedFileValid(feed)) { + boolean force, boolean initiatedByUser) throws DownloadRequestException { + downloadFeeds(context, Collections.singletonList(feed), loadAllPages, force, initiatedByUser); + } + + /** + * Downloads a list of feeds. + * + * @param context The application's environment. + * @param feeds Feeds to download + * @param loadAllPages Set to true to download all pages + */ + public synchronized void downloadFeeds(Context context, List<Feed> feeds, boolean loadAllPages, + boolean force, boolean initiatedByUser) throws DownloadRequestException { + List<DownloadRequest> requests = new ArrayList<>(); + for (Feed feed : feeds) { + if (!feedFileValid(feed)) { + continue; + } String username = (feed.getPreferences() != null) ? feed.getPreferences().getUsername() : null; String password = (feed.getPreferences() != null) ? feed.getPreferences().getPassword() : null; String lastModified = feed.isPaged() || force ? null : feed.getLastUpdate(); @@ -206,9 +222,12 @@ public class DownloadRequester implements DownloadStateProvider { true, username, password, lastModified, true, args, initiatedByUser ); if (request != null) { - download(context, request); + requests.add(request); } } + if (!requests.isEmpty()) { + download(context, requests.toArray(new DownloadRequest[0])); + } } public synchronized void downloadFeed(Context context, Feed feed) throws DownloadRequestException { |