Skip to content

Commit

Permalink
Remove unnecessary autodownload code (#6832)
Browse files Browse the repository at this point in the history
This should not change any behavior.
The retry count and timing are managed by WorkManager, so this code is irrelevant.
  • Loading branch information
ByteHamster committed Dec 29, 2023
1 parent 7508e15 commit 9db26b7
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ public Runnable autoDownloadUndownloadedItems(final Context context) {
Iterator<FeedItem> it = candidates.iterator();
while (it.hasNext()) {
FeedItem item = it.next();
if (!item.isAutoDownloadable(System.currentTimeMillis())
if (!item.isAutoDownloadEnabled()
|| item.isDownloaded()
|| !item.hasMedia()
|| PlaybackStatus.isPlaying(item.getMedia())
|| item.getFeed().isLocalFeed()) {
it.remove();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.danoeh.antennapod.core.feed;

import de.danoeh.antennapod.model.feed.FeedItem;
import de.danoeh.antennapod.model.feed.FeedMedia;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -11,13 +10,11 @@
import static de.danoeh.antennapod.core.feed.FeedItemMother.anyFeedItemWithImage;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class FeedItemTest {

private static final String TEXT_LONG = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
private static final String TEXT_SHORT = "Lorem ipsum";
private static final long ONE_HOUR = 1000L * 3600L;

private FeedItem original;
private FeedItem changedFeedItem;
Expand Down Expand Up @@ -139,36 +136,4 @@ private void testShownotes(String description, String contentEncoded) {
item.setDescriptionIfLonger(contentEncoded);
assertEquals(TEXT_LONG, item.getDescription());
}

@Test
public void testAutoDownloadBackoff() {
FeedItem item = new FeedItem();
item.setMedia(new FeedMedia(item, "https://example.com/file.mp3", 0, "audio/mpeg"));

long now = ONE_HOUR; // In reality, this is System.currentTimeMillis()
assertTrue(item.isAutoDownloadable(now));
item.increaseFailedAutoDownloadAttempts(now);
assertFalse(item.isAutoDownloadable(now));

now += ONE_HOUR;
assertTrue(item.isAutoDownloadable(now));
item.increaseFailedAutoDownloadAttempts(now);
assertFalse(item.isAutoDownloadable(now));

now += ONE_HOUR;
assertFalse(item.isAutoDownloadable(now)); // Should backoff, so more than 1 hour needed

now += ONE_HOUR;
assertTrue(item.isAutoDownloadable(now)); // Now it's enough
item.increaseFailedAutoDownloadAttempts(now);
item.increaseFailedAutoDownloadAttempts(now);
item.increaseFailedAutoDownloadAttempts(now);

now += 1000L * ONE_HOUR;
assertFalse(item.isAutoDownloadable(now)); // Should have given up
item.increaseFailedAutoDownloadAttempts(now);

now += 1000L * ONE_HOUR;
assertFalse(item.isAutoDownloadable(now)); // Still given up
}
}
52 changes: 6 additions & 46 deletions model/src/main/java/de/danoeh/antennapod/model/feed/FeedItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
* Item (episode) within a feed.
Expand Down Expand Up @@ -65,7 +64,7 @@ public class FeedItem extends FeedComponent implements Serializable {
private transient List<Chapter> chapters;
private String imageUrl;

private long autoDownload = 1;
private boolean autoDownloadEnabled = true;

/**
* Any tags assigned to this item
Expand All @@ -82,7 +81,7 @@ public FeedItem() {
* */
public FeedItem(long id, String title, String link, Date pubDate, String paymentLink, long feedId,
boolean hasChapters, String imageUrl, int state,
String itemIdentifier, long autoDownload, String podcastIndexChapterUrl) {
String itemIdentifier, boolean autoDownloadEnabled, String podcastIndexChapterUrl) {
this.id = id;
this.title = title;
this.link = link;
Expand All @@ -93,7 +92,7 @@ public FeedItem(long id, String title, String link, Date pubDate, String payment
this.imageUrl = imageUrl;
this.state = state;
this.itemIdentifier = itemIdentifier;
this.autoDownload = autoDownload;
this.autoDownloadEnabled = autoDownloadEnabled;
this.podcastIndexChapterUrl = podcastIndexChapterUrl;
}

Expand Down Expand Up @@ -361,50 +360,11 @@ public boolean hasChapters() {
}

public void disableAutoDownload() {
this.autoDownload = 0;
this.autoDownloadEnabled = false;
}

public long getAutoDownloadAttemptsAndTime() {
return autoDownload;
}

public int getFailedAutoDownloadAttempts() {
// 0: auto download disabled
// 1: auto download enabled (default)
// > 1: auto download enabled, timestamp of last failed attempt, last digit denotes number of failed attempts
if (autoDownload <= 1) {
return 0;
}
int failedAttempts = (int)(autoDownload % 10);
if (failedAttempts == 0) {
failedAttempts = 10;
}
return failedAttempts;
}

public void increaseFailedAutoDownloadAttempts(long now) {
if (autoDownload == 0) {
return; // Don't re-enable
}
int failedAttempts = getFailedAutoDownloadAttempts() + 1;
if (failedAttempts >= 5) {
disableAutoDownload(); // giving up
} else {
autoDownload = (now / 10) * 10 + failedAttempts;
}
}

public boolean isAutoDownloadable(long now) {
if (media == null || media.isDownloaded() || autoDownload == 0) {
return false;
}
if (autoDownload == 1) {
return true; // Never failed
}
int failedAttempts = getFailedAutoDownloadAttempts();
long waitingTime = TimeUnit.HOURS.toMillis((long) Math.pow(2, failedAttempts - 1));
long lastAttempt = (autoDownload / 10) * 10;
return now >= (lastAttempt + waitingTime);
public boolean isAutoDownloadEnabled() {
return this.autoDownloadEnabled;
}

public boolean isDownloaded() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ static void upgrade(final SQLiteDatabase db, final int oldVersion, final int new
}
if (oldVersion <= 14) {
db.execSQL("ALTER TABLE " + PodDBAdapter.TABLE_NAME_FEED_ITEMS
+ " ADD COLUMN " + PodDBAdapter.KEY_AUTO_DOWNLOAD_ATTEMPTS + " INTEGER");
+ " ADD COLUMN " + PodDBAdapter.KEY_AUTO_DOWNLOAD_ENABLED + " INTEGER");
db.execSQL("UPDATE " + PodDBAdapter.TABLE_NAME_FEED_ITEMS
+ " SET " + PodDBAdapter.KEY_AUTO_DOWNLOAD_ATTEMPTS + " = "
+ " SET " + PodDBAdapter.KEY_AUTO_DOWNLOAD_ENABLED + " = "
+ "(SELECT " + PodDBAdapter.KEY_AUTO_DOWNLOAD_ENABLED
+ " FROM " + PodDBAdapter.TABLE_NAME_FEEDS
+ " WHERE " + PodDBAdapter.TABLE_NAME_FEEDS + "." + PodDBAdapter.KEY_ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public class PodDBAdapter {
public static final String KEY_REASON_DETAILED = "reason_detailed";
public static final String KEY_DOWNLOADSTATUS_TITLE = "title";
public static final String KEY_PLAYBACK_COMPLETION_DATE = "playback_completion_date";
public static final String KEY_AUTO_DOWNLOAD_ATTEMPTS = "auto_download";
public static final String KEY_AUTO_DOWNLOAD_ENABLED = "auto_download"; // Both tables use the same key
public static final String KEY_KEEP_UPDATED = "keep_updated";
public static final String KEY_AUTO_DELETE_ACTION = "auto_delete_action";
Expand Down Expand Up @@ -171,7 +170,7 @@ public class PodDBAdapter {
+ KEY_MEDIA + " INTEGER," + KEY_FEED + " INTEGER,"
+ KEY_HAS_CHAPTERS + " INTEGER," + KEY_ITEM_IDENTIFIER + " TEXT,"
+ KEY_IMAGE_URL + " TEXT,"
+ KEY_AUTO_DOWNLOAD_ATTEMPTS + " INTEGER,"
+ KEY_AUTO_DOWNLOAD_ENABLED + " INTEGER,"
+ KEY_PODCASTINDEX_CHAPTER_URL + " TEXT)";

private static final String CREATE_TABLE_FEED_MEDIA = "CREATE TABLE "
Expand Down Expand Up @@ -259,7 +258,7 @@ public class PodDBAdapter {
+ TABLE_NAME_FEED_ITEMS + "." + KEY_HAS_CHAPTERS + ", "
+ TABLE_NAME_FEED_ITEMS + "." + KEY_ITEM_IDENTIFIER + ", "
+ TABLE_NAME_FEED_ITEMS + "." + KEY_IMAGE_URL + ", "
+ TABLE_NAME_FEED_ITEMS + "." + KEY_AUTO_DOWNLOAD_ATTEMPTS + ", "
+ TABLE_NAME_FEED_ITEMS + "." + KEY_AUTO_DOWNLOAD_ENABLED + ", "
+ TABLE_NAME_FEED_ITEMS + "." + KEY_PODCASTINDEX_CHAPTER_URL;

private static final String KEYS_FEED_MEDIA =
Expand Down Expand Up @@ -652,7 +651,7 @@ private long updateOrInsertFeedItem(FeedItem item, boolean saveFeed) {
}
values.put(KEY_HAS_CHAPTERS, item.getChapters() != null || item.hasChapters());
values.put(KEY_ITEM_IDENTIFIER, item.getItemIdentifier());
values.put(KEY_AUTO_DOWNLOAD_ATTEMPTS, item.getAutoDownloadAttemptsAndTime());
values.put(KEY_AUTO_DOWNLOAD_ENABLED, item.isAutoDownloadEnabled());
values.put(KEY_IMAGE_URL, item.getImageUrl());
values.put(KEY_PODCASTINDEX_CHAPTER_URL, item.getPodcastIndexChapterUrl());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static FeedItem convert(@NonNull Cursor cursor) {
int indexHasChapters = cursor.getColumnIndexOrThrow(PodDBAdapter.KEY_HAS_CHAPTERS);
int indexRead = cursor.getColumnIndexOrThrow(PodDBAdapter.KEY_READ);
int indexItemIdentifier = cursor.getColumnIndexOrThrow(PodDBAdapter.KEY_ITEM_IDENTIFIER);
int indexAutoDownload = cursor.getColumnIndexOrThrow(PodDBAdapter.KEY_AUTO_DOWNLOAD_ATTEMPTS);
int indexAutoDownload = cursor.getColumnIndexOrThrow(PodDBAdapter.KEY_AUTO_DOWNLOAD_ENABLED);
int indexImageUrl = cursor.getColumnIndexOrThrow(PodDBAdapter.KEY_IMAGE_URL);
int indexPodcastIndexChapterUrl = cursor.getColumnIndexOrThrow(PodDBAdapter.KEY_PODCASTINDEX_CHAPTER_URL);

Expand All @@ -38,11 +38,11 @@ public static FeedItem convert(@NonNull Cursor cursor) {
boolean hasChapters = cursor.getInt(indexHasChapters) > 0;
int state = cursor.getInt(indexRead);
String itemIdentifier = cursor.getString(indexItemIdentifier);
long autoDownload = cursor.getLong(indexAutoDownload);
boolean autoDownloadEnabled = cursor.getLong(indexAutoDownload) > 0;
String imageUrl = cursor.getString(indexImageUrl);
String podcastIndexChapterUrl = cursor.getString(indexPodcastIndexChapterUrl);

return new FeedItem(id, title, link, pubDate, paymentLink, feedId,
hasChapters, imageUrl, state, itemIdentifier, autoDownload, podcastIndexChapterUrl);
hasChapters, imageUrl, state, itemIdentifier, autoDownloadEnabled, podcastIndexChapterUrl);
}
}

0 comments on commit 9db26b7

Please sign in to comment.