diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/NavListAdapter.java b/app/src/main/java/de/danoeh/antennapod/adapter/NavListAdapter.java index d43e30d8f..6a99e7ebc 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/NavListAdapter.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/NavListAdapter.java @@ -228,7 +228,10 @@ public class NavListAdapter extends BaseAdapter } } else if(tag.equals(DownloadsFragment.TAG) && UserPreferences.isEnableAutodownload()) { int epCacheSize = UserPreferences.getEpisodeCacheSize(); - if(itemAccess.getNumberOfDownloadedItems() >= epCacheSize) { + // don't count episodes that we could delete when autodownloading + int spaceUsed = itemAccess.getNumberOfDownloadedItems() - + UserPreferences.getEpisodeCleanupAlgorithm().getAvailableSpace(); + if (spaceUsed >= epCacheSize) { holder.count.setText("{md-disc-full 150%}"); Iconify.addIcons(holder.count); holder.count.setVisibility(View.VISIBLE); diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithm.java index 0dc54fb6e..afb7c2f9d 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithm.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithm.java @@ -1,6 +1,7 @@ package de.danoeh.antennapod.core.storage; import android.content.Context; +import android.support.annotation.NonNull; import android.util.Log; import java.util.ArrayList; @@ -28,30 +29,18 @@ public class APCleanupAlgorithm extends EpisodeCleanupAlgorithm { this.numberOfDaysAfterPlayback = numberOfDaysAfterPlayback; } + /** + * @return the number of episodes that *could* be cleaned up, if needed + */ + public int getAvailableSpace() + { + return getCandidates().size(); + } + @Override public int performCleanup(Context context, int numberOfEpisodesToDelete) { - List candidates = new ArrayList<>(); - List downloadedItems = DBReader.getDownloadedItems(); + List candidates = getCandidates(); List delete; - Calendar cal = Calendar.getInstance(); - cal.add(Calendar.DAY_OF_MONTH, -1 * numberOfDaysAfterPlayback); - Date mostRecentDateForDeletion = cal.getTime(); - for (FeedItem item : downloadedItems) { - if (item.hasMedia() - && item.getMedia().isDownloaded() - && !item.isTagged(FeedItem.TAG_QUEUE) - && item.isPlayed() - && !item.isTagged(FeedItem.TAG_FAVORITE)) { - FeedMedia media = item.getMedia(); - // make sure this candidate was played at least the proper amount of days prior - // to now - if (media != null - && media.getPlaybackCompletionDate() != null - && media.getPlaybackCompletionDate().before(mostRecentDateForDeletion)) { - candidates.add(item); - } - } - } Collections.sort(candidates, (lhs, rhs) -> { Date l = lhs.getMedia().getPlaybackCompletionDate(); @@ -90,6 +79,32 @@ public class APCleanupAlgorithm extends EpisodeCleanupAlgorithm { return counter; } + @NonNull + private List getCandidates() { + List candidates = new ArrayList<>(); + List downloadedItems = DBReader.getDownloadedItems(); + Calendar cal = Calendar.getInstance(); + cal.add(Calendar.DAY_OF_MONTH, -1 * numberOfDaysAfterPlayback); + Date mostRecentDateForDeletion = cal.getTime(); + for (FeedItem item : downloadedItems) { + if (item.hasMedia() + && item.getMedia().isDownloaded() + && !item.isTagged(FeedItem.TAG_QUEUE) + && item.isPlayed() + && !item.isTagged(FeedItem.TAG_FAVORITE)) { + FeedMedia media = item.getMedia(); + // make sure this candidate was played at least the proper amount of days prior + // to now + if (media != null + && media.getPlaybackCompletionDate() != null + && media.getPlaybackCompletionDate().before(mostRecentDateForDeletion)) { + candidates.add(item); + } + } + } + return candidates; + } + @Override public int getDefaultCleanupParameter() { return getNumEpisodesToCleanup(0); diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/APNullCleanupAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/APNullCleanupAlgorithm.java index 132b61853..cd99c5a11 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/APNullCleanupAlgorithm.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/APNullCleanupAlgorithm.java @@ -21,4 +21,9 @@ public class APNullCleanupAlgorithm extends EpisodeCleanupAlgorithm { public int getDefaultCleanupParameter() { return 0; } + + @Override + public int getAvailableSpace() { + return 0; + } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/APQueueCleanupAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/APQueueCleanupAlgorithm.java index 234d6162c..9444278b7 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/APQueueCleanupAlgorithm.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/APQueueCleanupAlgorithm.java @@ -1,6 +1,7 @@ package de.danoeh.antennapod.core.storage; import android.content.Context; +import android.support.annotation.NonNull; import android.util.Log; import java.util.ArrayList; @@ -22,19 +23,18 @@ public class APQueueCleanupAlgorithm extends EpisodeCleanupAlgorithm { private static final String TAG = "APQueueCleanupAlgorithm"; + /** + * @return the number of episodes that *could* be cleaned up, if needed + */ + public int getAvailableSpace() + { + return getCandidates().size(); + } + @Override public int performCleanup(Context context, int numberOfEpisodesToDelete) { - List candidates = new ArrayList<>(); - List downloadedItems = DBReader.getDownloadedItems(); + List candidates = getCandidates(); List delete; - for (FeedItem item : downloadedItems) { - if (item.hasMedia() - && item.getMedia().isDownloaded() - && !item.isTagged(FeedItem.TAG_QUEUE) - && !item.isTagged(FeedItem.TAG_FAVORITE)) { - candidates.add(item); - } - } // in the absence of better data, we'll sort by item publication date Collections.sort(candidates, (lhs, rhs) -> { @@ -74,6 +74,21 @@ public class APQueueCleanupAlgorithm extends EpisodeCleanupAlgorithm { return counter; } + @NonNull + private List getCandidates() { + List candidates = new ArrayList<>(); + List downloadedItems = DBReader.getDownloadedItems(); + for (FeedItem item : downloadedItems) { + if (item.hasMedia() + && item.getMedia().isDownloaded() + && !item.isTagged(FeedItem.TAG_QUEUE) + && !item.isTagged(FeedItem.TAG_FAVORITE)) { + candidates.add(item); + } + } + return candidates; + } + @Override public int getDefaultCleanupParameter() { return getNumEpisodesToCleanup(0); diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithm.java index 0f402745c..065374381 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithm.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/EpisodeCleanupAlgorithm.java @@ -39,6 +39,11 @@ public abstract class EpisodeCleanupAlgorithm { return performCleanup(context, getNumEpisodesToCleanup(amountOfRoomNeeded)); } + /** + * @return the number of episodes that *could* be cleaned up, if needed + */ + public abstract int getAvailableSpace(); + /** * @param amountOfRoomNeeded the number of episodes we want to download * @return the number of episodes to delete in order to make room