Merge branch 'delete-oldest-episodes' of git://github.com/patheticpat/AntennaPod into patheticpat-delete-oldest-episodes

This commit is contained in:
daniel oeh 2013-04-20 11:19:03 +02:00
commit c0de44d0ee
1 changed files with 37 additions and 16 deletions

View File

@ -10,6 +10,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.Comparator;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
@ -760,24 +761,44 @@ public class FeedManager {
* @return The number of episodes that were actually deleted * @return The number of episodes that were actually deleted
* */ * */
private int performAutoCleanup(Context context, final int episodeNumber) { private int performAutoCleanup(Context context, final int episodeNumber) {
int counter = 0; List<FeedItem> candidates = new ArrayList<FeedItem>();
if (episodeNumber > 0) { List<FeedItem> delete;
int episodesLeft = episodeNumber; for (Feed feed : feeds) {
feedloop: for (Feed feed : feeds) {
for (FeedItem item : feed.getItems()) { for (FeedItem item : feed.getItems()) {
if (item.hasMedia() && item.getMedia().isDownloaded()) { if (item.hasMedia() && item.getMedia().isDownloaded() && !isInQueue(item) && item.isRead()) {
if (!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()); deleteFeedMedia(context, item.getMedia());
counter++;
episodesLeft--;
if (episodesLeft == 0) {
break feedloop;
}
}
}
}
}
} }
int counter = delete.size();
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, String.format( Log.d(TAG, String.format(
"Auto-delete deleted %d episodes (%d requested)", counter, "Auto-delete deleted %d episodes (%d requested)", counter,