summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/feed/FeedManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/de/danoeh/antennapod/feed/FeedManager.java')
-rw-r--r--src/de/danoeh/antennapod/feed/FeedManager.java180
1 files changed, 179 insertions, 1 deletions
diff --git a/src/de/danoeh/antennapod/feed/FeedManager.java b/src/de/danoeh/antennapod/feed/FeedManager.java
index ed72f0d47..4fe28a14b 100644
--- a/src/de/danoeh/antennapod/feed/FeedManager.java
+++ b/src/de/danoeh/antennapod/feed/FeedManager.java
@@ -29,6 +29,7 @@ import de.danoeh.antennapod.storage.PodDBAdapter;
import de.danoeh.antennapod.util.DownloadError;
import de.danoeh.antennapod.util.EpisodeFilter;
import de.danoeh.antennapod.util.FeedtitleComparator;
+import de.danoeh.antennapod.util.NetworkUtils;
import de.danoeh.antennapod.util.comparator.DownloadStatusComparator;
import de.danoeh.antennapod.util.comparator.FeedItemPubdateComparator;
import de.danoeh.antennapod.util.comparator.PlaybackCompletionDateComparator;
@@ -573,10 +574,26 @@ public class FeedManager {
}
}
- /** Downloads FeedItems if they have not been downloaded yet. */
public void downloadFeedItem(final Context context, FeedItem... items)
throws DownloadRequestException {
+ downloadFeedItem(true, context, items);
+ }
+
+ /** Downloads FeedItems if they have not been downloaded yet. */
+ private void downloadFeedItem(boolean performAutoCleanup,
+ final Context context, final FeedItem... items)
+ throws DownloadRequestException {
+ if (performAutoCleanup) {
+ new Thread() {
+ @Override
+ public void run() {
+ performAutoCleanup(context,
+ getPerformAutoCleanupArgs(items.length));
+ }
+
+ }.start();
+ }
for (FeedItem item : items) {
if (item.getMedia() != null
&& !requester.isDownloadingFile(item.getMedia())
@@ -601,6 +618,167 @@ public class FeedManager {
}
/**
+ * This method will try to download undownloaded items in the queue or the
+ * unread items list. If not enough space is available, an episode cleanup
+ * will be performed first.
+ */
+ public void autodownloadUndownloadedItems(Context context) {
+ if (AppConfig.DEBUG)
+ Log.d(TAG, "Performing auto-dl of undownloaded episodes");
+ if (NetworkUtils.autodownloadNetworkAvailable(context)) {
+ int undownloadedEpisodes = getNumberOfUndownloadedEpisodes();
+ if (undownloadedEpisodes > 0) {
+ int downloadedEpisodes = getNumberOfDownloadedEpisodes();
+ int deletedEpisodes = performAutoCleanup(context,
+ getPerformAutoCleanupArgs(undownloadedEpisodes));
+ int episodeSpaceLeft = undownloadedEpisodes;
+ if (UserPreferences.getEpisodeCacheSize() < downloadedEpisodes
+ + undownloadedEpisodes) {
+ episodeSpaceLeft = UserPreferences.getEpisodeCacheSize()
+ - (downloadedEpisodes - deletedEpisodes);
+ }
+
+ List<FeedItem> itemsToDownload = new ArrayList<FeedItem>();
+ if (episodeSpaceLeft > 0 && undownloadedEpisodes > 0) {
+ for (FeedItem item : queue) {
+ if (item.hasMedia() && !item.getMedia().isDownloaded()) {
+ itemsToDownload.add(item);
+ episodeSpaceLeft--;
+ undownloadedEpisodes--;
+ if (episodeSpaceLeft == 0
+ || undownloadedEpisodes == 0) {
+ break;
+ }
+ }
+ }
+ }
+ if (episodeSpaceLeft > 0 && undownloadedEpisodes > 0) {
+ for (FeedItem item : unreadItems) {
+ if (item.hasMedia() && !item.getMedia().isDownloaded()) {
+ itemsToDownload.add(item);
+ episodeSpaceLeft--;
+ undownloadedEpisodes--;
+ if (episodeSpaceLeft == 0
+ || undownloadedEpisodes == 0) {
+ break;
+ }
+ }
+ }
+ }
+ if (AppConfig.DEBUG)
+ Log.d(TAG, "Enqueueing " + itemsToDownload.size()
+ + " items for download");
+
+ try {
+ downloadFeedItem(false, context,
+ itemsToDownload
+ .toArray(new FeedItem[itemsToDownload
+ .size()]));
+ } catch (DownloadRequestException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ }
+ }
+
+ /**
+ * This method will determine the number of episodes that have to be deleted
+ * depending on a given number of episodes.
+ *
+ * @return The argument that has to be passed to performAutoCleanup() so
+ * that the number of episodes fits into the episode cache.
+ * */
+ private int getPerformAutoCleanupArgs(final int episodeNumber) {
+ if (episodeNumber >= 0) {
+ int downloadedEpisodes = getNumberOfDownloadedEpisodes();
+ if (downloadedEpisodes + episodeNumber >= UserPreferences
+ .getEpisodeCacheSize()) {
+
+ return downloadedEpisodes + episodeNumber
+ - UserPreferences.getEpisodeCacheSize();
+ }
+ }
+ return 0;
+ }
+
+ /**
+ * Performs an auto-cleanup so that the number of downloaded episodes is
+ * below or equal to the episode cache size. The method will be executed in
+ * the caller's thread.
+ */
+ public void performAutoCleanup(Context context) {
+ performAutoCleanup(context, getPerformAutoCleanupArgs(0));
+ }
+
+ /**
+ * This method will try to delete a given number of episodes. An episode
+ * will only be deleted if it is not in the queue.
+ *
+ * @return The number of episodes that were actually deleted
+ * */
+ private int performAutoCleanup(Context context, final int episodeNumber) {
+ int counter = 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;
+ }
+ }
+ }
+ }
+ }
+ if (AppConfig.DEBUG)
+ Log.d(TAG, String.format(
+ "Auto-delete deleted %d episodes (%d requested)", counter,
+ episodeNumber));
+
+ return counter;
+ }
+
+ /**
+ * Counts items in the queue and the unread items list which haven't been
+ * downloaded yet.
+ */
+ private int getNumberOfUndownloadedEpisodes() {
+ int counter = 0;
+ for (FeedItem item : queue) {
+ if (item.hasMedia() && !item.getMedia().isDownloaded()) {
+ counter++;
+ }
+ }
+ for (FeedItem item : unreadItems) {
+ if (item.hasMedia() && !item.getMedia().isDownloaded()) {
+ counter++;
+ }
+ }
+ return counter;
+
+ }
+
+ /** Counts all downloaded items. */
+ private int getNumberOfDownloadedEpisodes() {
+ int counter = 0;
+ for (Feed feed : feeds) {
+ for (FeedItem item : feed.getItems()) {
+ if (item.hasMedia() && item.getMedia().isDownloaded()) {
+ counter++;
+ }
+ }
+ }
+ if (AppConfig.DEBUG)
+ Log.d(TAG, "Number of downloaded episodes: " + counter);
+ return counter;
+ }
+
+ /**
* Enqueues all items that are currently in the unreadItems list and marks
* them as 'read'.
*/