diff options
author | Michael Kaiser <patheticpat@googlemail.com> | 2013-04-19 20:37:35 +0200 |
---|---|---|
committer | Michael Kaiser <patheticpat@googlemail.com> | 2013-04-19 20:37:35 +0200 |
commit | 63084d8c7988090db76f7dd46575ed5137a44543 (patch) | |
tree | 77e62d5f0d44a017079715aba2dd24c6ab510a2a /src/de/danoeh/antennapod | |
parent | 4707139def5500f1039de669e53822d47855008c (diff) | |
download | AntennaPod-63084d8c7988090db76f7dd46575ed5137a44543.zip |
Delete oldest episodes first when performing auto cleanup.
Diffstat (limited to 'src/de/danoeh/antennapod')
-rw-r--r-- | src/de/danoeh/antennapod/feed/FeedManager.java | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/src/de/danoeh/antennapod/feed/FeedManager.java b/src/de/danoeh/antennapod/feed/FeedManager.java index bdffdc667..64e6ef888 100644 --- a/src/de/danoeh/antennapod/feed/FeedManager.java +++ b/src/de/danoeh/antennapod/feed/FeedManager.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.Map; import java.util.TreeMap; import java.util.TreeSet; +import java.util.Comparator; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; @@ -760,24 +761,44 @@ public class FeedManager { * @return The number of episodes that were actually deleted * */ private int performAutoCleanup(Context context, final int episodeNumber) { - int counter = 0; - if (episodeNumber > 0) { - int episodesLeft = episodeNumber; - feedloop: for (Feed feed : feeds) { - for (FeedItem item : feed.getItems()) { - if (item.hasMedia() && item.getMedia().isDownloaded()) { - if (!isInQueue(item) && item.isRead()) { - deleteFeedMedia(context, item.getMedia()); - counter++; - episodesLeft--; - if (episodesLeft == 0) { - break feedloop; - } - } - } + List<FeedItem> candidates = new ArrayList<FeedItem>(); + List<FeedItem> delete; + for (Feed feed : feeds) { + for (FeedItem item : feed.getItems()) { + if (item.hasMedia() && item.getMedia().isDownloaded() && !isInQueue(item) && item.isRead()) { + candidates.add(item); + } + } + } + + Collections.sort(candidates, new Comparator<FeedItem>() { + @Override + public int compare(FeedItem lhs, FeedItem rhs) { + Date l = lhs.getMedia().getPlaybackCompletionDate(); + Date r = rhs.getMedia().getPlaybackCompletionDate(); + + if (l == null) { + l = new Date(0); + } + if (r == null) { + r = new Date(0); } + return l.compareTo(r); } + }); + + if (candidates.size() > episodeNumber) { + delete = candidates.subList(0, episodeNumber); + } else { + delete = candidates; + } + + for (FeedItem item : delete) { + deleteFeedMedia(context, item.getMedia()); } + + int counter = delete.size(); + if (AppConfig.DEBUG) Log.d(TAG, String.format( "Auto-delete deleted %d episodes (%d requested)", counter, @@ -1980,4 +2001,4 @@ public class FeedManager { } } -}
\ No newline at end of file +} |