Store last refresh attempt for feeds (#7022)

This commit is contained in:
ByteHamster 2024-03-24 17:57:00 +01:00 committed by GitHub
parent 5218e06904
commit 084b9c2317
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 63 additions and 95 deletions

View File

@ -128,7 +128,8 @@ public class PlaybackServiceMediaPlayerTest {
}
private Playable writeTestPlayable(String downloadUrl, String fileUrl) {
Feed f = new Feed(0, null, "f", "l", "d", null, null, null, null, "i", null, null, "l", false);
Feed f = new Feed(0, null, "f", "l", "d", null, null, null, null,
"i", null, null, "l", System.currentTimeMillis());
FeedPreferences prefs = new FeedPreferences(f.getId(), false, FeedPreferences.AutoDeleteAction.NEVER,
VolumeAdaptionSetting.OFF, FeedPreferences.NewEpisodesAction.NOTHING, null, null);
f.setPreferences(prefs);

View File

@ -63,7 +63,8 @@ public class PlaybackServiceTaskManagerTest {
private List<FeedItem> writeTestQueue(String pref) {
final int NUM_ITEMS = 10;
Feed f = new Feed(0, null, "title", "link", "d", null, null, null, null, "id", null, "null", "url", false);
Feed f = new Feed(0, null, "title", "link", "d", null, null, null, null, "id",
null, "null", "url", System.currentTimeMillis());
f.setItems(new ArrayList<>());
for (int i = 0; i < NUM_ITEMS; i++) {
f.getItems().add(new FeedItem(0, pref + i, pref + i, "link", new Date(), FeedItem.PLAYED, f));

View File

@ -123,7 +123,7 @@ public class UITestUtils {
for (int i = 0; i < NUM_FEEDS; i++) {
Feed feed = new Feed(0, null, "Title " + i, "http://example.com/" + i, "Description of feed " + i,
"http://example.com/pay/feed" + i, "author " + i, "en", Feed.TYPE_RSS2, "feed" + i, null, null,
"http://example.com/feed/src/" + i, false);
"http://example.com/feed/src/" + i, System.currentTimeMillis());
// create items
List<FeedItem> items = new ArrayList<>();
@ -169,7 +169,6 @@ public class UITestUtils {
List<FeedItem> queue = new ArrayList<>();
for (Feed feed : hostedFeeds) {
feed.setDownloaded(true);
if (downloadEpisodes) {
for (FeedItem item : feed.getItems()) {
if (item.hasMedia()) {

View File

@ -23,7 +23,7 @@ import de.danoeh.antennapod.core.service.download.DefaultDownloaderFactory;
import de.danoeh.antennapod.core.service.download.DownloadRequestCreator;
import de.danoeh.antennapod.core.service.download.Downloader;
import de.danoeh.antennapod.core.service.download.NewEpisodesNotification;
import de.danoeh.antennapod.core.service.download.handler.FeedSyncTask;
import de.danoeh.antennapod.core.service.download.handler.FeedParserTask;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.DBWriter;
@ -35,6 +35,7 @@ import de.danoeh.antennapod.model.feed.Feed;
import de.danoeh.antennapod.model.download.DownloadRequest;
import de.danoeh.antennapod.net.download.serviceinterface.DownloadRequestBuilder;
import de.danoeh.antennapod.parser.feed.FeedHandlerResult;
import de.danoeh.antennapod.ui.notifications.NotificationUtils;
import java.util.ArrayList;
import java.util.Collections;
@ -190,29 +191,30 @@ public class FeedUpdateWorker extends Worker {
return;
}
FeedSyncTask feedSyncTask = new FeedSyncTask(getApplicationContext(), request);
boolean success = feedSyncTask.run();
if (!success) {
FeedParserTask parserTask = new FeedParserTask(request);
FeedHandlerResult feedHandlerResult = parserTask.call();
if (!parserTask.isSuccessful()) {
DBWriter.setFeedLastUpdateFailed(request.getFeedfileId(), true);
DBWriter.addDownloadStatus(feedSyncTask.getDownloadStatus());
DBWriter.addDownloadStatus(parserTask.getDownloadStatus());
return;
}
feedHandlerResult.feed.setLastRefreshAttempt(System.currentTimeMillis());
Feed savedFeed = DBTasks.updateFeed(getApplicationContext(), feedHandlerResult.feed, false);
if (request.getFeedfileId() == 0) {
return; // No download logs for new subscriptions
}
// we create a 'successful' download log if the feed's last refresh failed
List<DownloadResult> log = DBReader.getFeedDownloadLog(request.getFeedfileId());
if (log.size() > 0 && !log.get(0).isSuccessful()) {
DBWriter.addDownloadStatus(feedSyncTask.getDownloadStatus());
if (!log.isEmpty() && !log.get(0).isSuccessful()) {
DBWriter.addDownloadStatus(parserTask.getDownloadStatus());
}
newEpisodesNotification.showIfNeeded(getApplicationContext(), feedSyncTask.getSavedFeed());
newEpisodesNotification.showIfNeeded(getApplicationContext(), savedFeed);
if (downloader.permanentRedirectUrl != null) {
DBWriter.updateFeedDownloadURL(request.getSource(), downloader.permanentRedirectUrl);
} else if (feedSyncTask.getRedirectUrl() != null
&& !feedSyncTask.getRedirectUrl().equals(request.getSource())) {
DBWriter.updateFeedDownloadURL(request.getSource(), feedSyncTask.getRedirectUrl());
} else if (feedHandlerResult.redirectUrl != null
&& !feedHandlerResult.redirectUrl.equals(request.getSource())) {
DBWriter.updateFeedDownloadURL(request.getSource(), feedHandlerResult.redirectUrl);
}
}
}

View File

@ -41,7 +41,6 @@ public class FeedParserTask implements Callable<FeedHandlerResult> {
Feed feed = new Feed(request.getSource(), request.getLastModified());
feed.setLocalFileUrl(request.getDestination());
feed.setId(request.getFeedfileId());
feed.setDownloaded(true);
feed.setPreferences(new FeedPreferences(0, true, FeedPreferences.AutoDeleteAction.GLOBAL,
VolumeAdaptionSetting.OFF, FeedPreferences.NewEpisodesAction.GLOBAL, request.getUsername(),
request.getPassword()));

View File

@ -1,44 +0,0 @@
package de.danoeh.antennapod.core.service.download.handler;
import android.content.Context;
import androidx.annotation.NonNull;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.model.download.DownloadResult;
import de.danoeh.antennapod.model.feed.Feed;
import de.danoeh.antennapod.model.download.DownloadRequest;
import de.danoeh.antennapod.parser.feed.FeedHandlerResult;
public class FeedSyncTask {
private final Context context;
private Feed savedFeed;
private final FeedParserTask task;
private FeedHandlerResult feedHandlerResult;
public FeedSyncTask(Context context, DownloadRequest request) {
this.context = context;
this.task = new FeedParserTask(request);
}
public boolean run() {
feedHandlerResult = task.call();
if (!task.isSuccessful()) {
return false;
}
savedFeed = DBTasks.updateFeed(context, feedHandlerResult.feed, false);
return true;
}
@NonNull
public DownloadResult getDownloadStatus() {
return task.getDownloadStatus();
}
public Feed getSavedFeed() {
return savedFeed;
}
public String getRedirectUrl() {
return feedHandlerResult.redirectUrl;
}
}

View File

@ -8,7 +8,7 @@ public class FeedMother {
public static Feed anyFeed() {
return new Feed(0, null, "title", "http://example.com", "This is the description",
"http://example.com/payment", "Daniel", "en", null, "http://example.com/feed", IMAGE_URL,
null, "http://example.com/feed", true);
null, "http://example.com/feed", System.currentTimeMillis());
}
}

View File

@ -80,10 +80,11 @@ public class DbReaderTest {
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
Feed feed1 = new Feed(0, null, "A", "link", "d", null, null, null, "rss", "A", null, "", "", true);
Feed feed2 = new Feed(0, null, "b", "link", "d", null, null, null, "rss", "b", null, "", "", true);
Feed feed3 = new Feed(0, null, "C", "link", "d", null, null, null, "rss", "C", null, "", "", true);
Feed feed4 = new Feed(0, null, "d", "link", "d", null, null, null, "rss", "d", null, "", "", true);
final long lastRefreshed = System.currentTimeMillis();
Feed feed1 = new Feed(0, null, "A", "link", "d", null, null, null, "rss", "A", null, "", "", lastRefreshed);
Feed feed2 = new Feed(0, null, "b", "link", "d", null, null, null, "rss", "b", null, "", "", lastRefreshed);
Feed feed3 = new Feed(0, null, "C", "link", "d", null, null, null, "rss", "C", null, "", "", lastRefreshed);
Feed feed4 = new Feed(0, null, "d", "link", "d", null, null, null, "rss", "d", null, "", "", lastRefreshed);
adapter.setCompleteFeed(feed1);
adapter.setCompleteFeed(feed2);
adapter.setCompleteFeed(feed3);

View File

@ -43,7 +43,7 @@ abstract class DbTestUtils {
adapter.open();
for (int i = 0; i < numFeeds; i++) {
Feed f = new Feed(0, null, "feed " + i, "link" + i, "descr", null, null,
null, null, "id" + i, null, null, "url" + i, false);
null, null, "id" + i, null, null, "url" + i, System.currentTimeMillis());
f.setItems(new ArrayList<>());
for (int j = 0; j < numItems; j++) {
FeedItem item = new FeedItem(0, "item " + j, "id" + j, "link" + j, new Date(),

View File

@ -55,7 +55,7 @@ public class FeedCursorMapperTest {
assertEquals("feed image url", feed.getImageUrl());
assertEquals("feed file url", feed.getLocalFileUrl());
assertEquals("feed download url", feed.getDownloadUrl());
assertTrue(feed.isDownloaded());
assertEquals(42, feed.getLastRefreshAttempt());
assertEquals("feed last update", feed.getLastModified());
assertEquals("feed type", feed.getType());
assertEquals("feed identifier", feed.getFeedIdentifier());
@ -85,7 +85,7 @@ public class FeedCursorMapperTest {
values.put(PodDBAdapter.KEY_FILE_URL, "feed file url");
values.put(PodDBAdapter.KEY_DOWNLOAD_URL, "feed download url");
values.put(PodDBAdapter.KEY_DOWNLOADED, true);
values.put(PodDBAdapter.KEY_LAST_REFRESH_ATTEMPT, 42);
values.put(PodDBAdapter.KEY_LASTUPDATE, "feed last update");
values.put(PodDBAdapter.KEY_TYPE, "feed type");
values.put(PodDBAdapter.KEY_FEED_IDENTIFIER, "feed identifier");

View File

@ -24,7 +24,6 @@ public class Feed {
private long id;
private String localFileUrl;
private String downloadUrl;
private boolean downloaded;
/**
* title as defined by the feed.
*/
@ -56,6 +55,7 @@ public class Feed {
* String that identifies the last update (adopted from Last-Modified or ETag header).
*/
private String lastModified;
private long lastRefreshAttempt;
private ArrayList<FeedFunding> fundingList;
/**
@ -109,11 +109,11 @@ public class Feed {
public Feed(long id, String lastModified, String title, String customTitle, String link,
String description, String paymentLinks, String author, String language,
String type, String feedIdentifier, String imageUrl, String fileUrl,
String downloadUrl, boolean downloaded, boolean paged, String nextPageLink,
String downloadUrl, long lastRefreshAttempt, boolean paged, String nextPageLink,
String filter, @Nullable SortOrder sortOrder, boolean lastUpdateFailed) {
this.localFileUrl = fileUrl;
this.downloadUrl = downloadUrl;
this.downloaded = downloaded;
this.lastRefreshAttempt = lastRefreshAttempt;
this.id = id;
this.feedTitle = title;
this.customTitle = customTitle;
@ -143,9 +143,9 @@ public class Feed {
*/
public Feed(long id, String lastModified, String title, String link, String description, String paymentLink,
String author, String language, String type, String feedIdentifier, String imageUrl, String fileUrl,
String downloadUrl, boolean downloaded) {
String downloadUrl, long lastRefreshAttempt) {
this(id, lastModified, title, null, link, description, paymentLink, author, language, type, feedIdentifier,
imageUrl, fileUrl, downloadUrl, downloaded, false, null, null, null, false);
imageUrl, fileUrl, downloadUrl, lastRefreshAttempt, false, null, null, null, false);
}
/**
@ -155,7 +155,7 @@ public class Feed {
public Feed(String url, String lastModified) {
this.localFileUrl = null;
this.downloadUrl = url;
this.downloaded = false;
this.lastRefreshAttempt = 0;
this.lastModified = lastModified;
}
@ -241,6 +241,9 @@ public class Feed {
if (other.fundingList != null) {
fundingList = other.fundingList;
}
if (other.lastRefreshAttempt > lastRefreshAttempt) {
lastRefreshAttempt = other.lastRefreshAttempt;
}
// this feed's nextPage might already point to a higher page, so we only update the nextPage value
// if this feed is not paged and the other feed is.
if (!this.paged && other.paged) {
@ -395,9 +398,6 @@ public class Feed {
public void setLocalFileUrl(String fileUrl) {
this.localFileUrl = fileUrl;
if (fileUrl == null) {
downloaded = false;
}
}
public String getDownloadUrl() {
@ -408,12 +408,12 @@ public class Feed {
this.downloadUrl = downloadUrl;
}
public boolean isDownloaded() {
return downloaded;
public long getLastRefreshAttempt() {
return lastRefreshAttempt;
}
public void setDownloaded(boolean downloaded) {
this.downloaded = downloaded;
public void setLastRefreshAttempt(long lastRefreshAttempt) {
this.lastRefreshAttempt = lastRefreshAttempt;
}
public int getPageNr() {

View File

@ -29,7 +29,6 @@ public abstract class FeedParserTestHelper {
FeedHandler handler = new FeedHandler();
Feed parsedFeed = new Feed("http://example.com/feed", null);
parsedFeed.setLocalFileUrl(feedFile.getAbsolutePath());
parsedFeed.setDownloaded(true);
handler.parseFeed(parsedFeed);
return parsedFeed;
}

View File

@ -77,6 +77,7 @@ public class PodDBAdapter {
public static final String KEY_FEED = "feed";
public static final String KEY_MEDIA = "media";
public static final String KEY_DOWNLOADED = "downloaded";
public static final String KEY_LAST_REFRESH_ATTEMPT = "downloaded";
public static final String KEY_LASTUPDATE = "last_update";
public static final String KEY_FEEDFILE = "feedfile";
public static final String KEY_REASON = "reason";
@ -135,14 +136,22 @@ public class PodDBAdapter {
private static final String TABLE_PRIMARY_KEY = KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT ,";
private static final String CREATE_TABLE_FEEDS = "CREATE TABLE "
+ TABLE_NAME_FEEDS + " (" + TABLE_PRIMARY_KEY + KEY_TITLE
+ " TEXT," + KEY_CUSTOM_TITLE + " TEXT," + KEY_FILE_URL + " TEXT," + KEY_DOWNLOAD_URL + " TEXT,"
+ KEY_DOWNLOADED + " INTEGER," + KEY_LINK + " TEXT,"
+ KEY_DESCRIPTION + " TEXT," + KEY_PAYMENT_LINK + " TEXT,"
+ KEY_LASTUPDATE + " TEXT," + KEY_LANGUAGE + " TEXT," + KEY_AUTHOR
+ " TEXT," + KEY_IMAGE_URL + " TEXT," + KEY_TYPE + " TEXT,"
+ KEY_FEED_IDENTIFIER + " TEXT," + KEY_AUTO_DOWNLOAD_ENABLED + " INTEGER DEFAULT 1,"
private static final String CREATE_TABLE_FEEDS = "CREATE TABLE " + TABLE_NAME_FEEDS + " ("
+ TABLE_PRIMARY_KEY + KEY_TITLE + " TEXT,"
+ KEY_CUSTOM_TITLE + " TEXT,"
+ KEY_FILE_URL + " TEXT,"
+ KEY_DOWNLOAD_URL + " TEXT,"
+ KEY_LAST_REFRESH_ATTEMPT + " INTEGER,"
+ KEY_LINK + " TEXT,"
+ KEY_DESCRIPTION + " TEXT,"
+ KEY_PAYMENT_LINK + " TEXT,"
+ KEY_LASTUPDATE + " TEXT,"
+ KEY_LANGUAGE + " TEXT,"
+ KEY_AUTHOR + " TEXT,"
+ KEY_IMAGE_URL + " TEXT,"
+ KEY_TYPE + " TEXT,"
+ KEY_FEED_IDENTIFIER + " TEXT,"
+ KEY_AUTO_DOWNLOAD_ENABLED + " INTEGER DEFAULT 1,"
+ KEY_USERNAME + " TEXT,"
+ KEY_PASSWORD + " TEXT,"
+ KEY_INCLUDE_FILTER + " TEXT DEFAULT '',"
@ -284,7 +293,7 @@ public class PodDBAdapter {
+ TABLE_NAME_FEEDS + "." + KEY_CUSTOM_TITLE + ", "
+ TABLE_NAME_FEEDS + "." + KEY_FILE_URL + ", "
+ TABLE_NAME_FEEDS + "." + KEY_DOWNLOAD_URL + ", "
+ TABLE_NAME_FEEDS + "." + KEY_DOWNLOADED + ", "
+ TABLE_NAME_FEEDS + "." + KEY_LAST_REFRESH_ATTEMPT + ", "
+ TABLE_NAME_FEEDS + "." + KEY_LINK + ", "
+ TABLE_NAME_FEEDS + "." + KEY_DESCRIPTION + ", "
+ TABLE_NAME_FEEDS + "." + KEY_PAYMENT_LINK + ", "
@ -418,7 +427,7 @@ public class PodDBAdapter {
values.put(KEY_FILE_URL, feed.getLocalFileUrl());
values.put(KEY_DOWNLOAD_URL, feed.getDownloadUrl());
values.put(KEY_DOWNLOADED, feed.isDownloaded());
values.put(KEY_LAST_REFRESH_ATTEMPT, feed.getLastRefreshAttempt());
values.put(KEY_LASTUPDATE, feed.getLastModified());
values.put(KEY_TYPE, feed.getType());
values.put(KEY_FEED_IDENTIFIER, feed.getFeedIdentifier());
@ -748,6 +757,7 @@ public class PodDBAdapter {
public void setFeedLastUpdateFailed(long feedId, boolean failed) {
final String sql = "UPDATE " + TABLE_NAME_FEEDS
+ " SET " + KEY_LAST_UPDATE_FAILED + "=" + (failed ? "1" : "0")
+ "," + KEY_LAST_REFRESH_ATTEMPT + "=" + System.currentTimeMillis()
+ " WHERE " + KEY_ID + "=" + feedId;
db.execSQL(sql);
}

View File

@ -32,7 +32,7 @@ public abstract class FeedCursorMapper {
int indexFeedIdentifier = cursor.getColumnIndexOrThrow(PodDBAdapter.KEY_FEED_IDENTIFIER);
int indexFileUrl = cursor.getColumnIndexOrThrow(PodDBAdapter.KEY_FILE_URL);
int indexDownloadUrl = cursor.getColumnIndexOrThrow(PodDBAdapter.KEY_DOWNLOAD_URL);
int indexDownloaded = cursor.getColumnIndexOrThrow(PodDBAdapter.KEY_DOWNLOADED);
int indexLastRefreshed = cursor.getColumnIndexOrThrow(PodDBAdapter.KEY_LAST_REFRESH_ATTEMPT);
int indexIsPaged = cursor.getColumnIndexOrThrow(PodDBAdapter.KEY_IS_PAGED);
int indexNextPageLink = cursor.getColumnIndexOrThrow(PodDBAdapter.KEY_NEXT_PAGE_LINK);
int indexHide = cursor.getColumnIndexOrThrow(PodDBAdapter.KEY_HIDE);
@ -55,7 +55,7 @@ public abstract class FeedCursorMapper {
cursor.getString(indexImageUrl),
cursor.getString(indexFileUrl),
cursor.getString(indexDownloadUrl),
cursor.getInt(indexDownloaded) > 0,
cursor.getLong(indexLastRefreshed),
cursor.getInt(indexIsPaged) > 0,
cursor.getString(indexNextPageLink),
cursor.getString(indexHide),