Refresh all feeds at once instead of sending tons of lists with 1 feed each

This commit is contained in:
ByteHamster 2021-02-06 12:32:53 +01:00
parent 52421d81f7
commit 4aa5443529
2 changed files with 62 additions and 71 deletions

View File

@ -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);
@ -134,38 +145,6 @@ public final class DBTasks {
// See Issue #2577 for the details of the rationale
}
/**
* @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
*
@ -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(
@ -209,19 +188,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
*
@ -231,25 +197,31 @@ 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);
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());
}
f.setId(feed.getId());
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<>();
if (f.isLocalFeed()) {
new Thread(() -> LocalFeedUpdater.updateFeed(f, context)).start();
for (Feed feed : feeds) {
if (feed.isLocalFeed()) {
localFeeds.add(feed);
} else {
DownloadRequester.getInstance().downloadFeed(context, f, loadAllPages, force, initiatedByUser);
normalFeeds.add(feed);
}
}
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);
}
}

View File

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