diff options
author | Martin Fietz <marf@hadiko-99-4.hadiko.uni-karlsruhe.de> | 2015-05-04 23:47:34 +0200 |
---|---|---|
committer | Martin Fietz <marf@hadiko-99-4.hadiko.uni-karlsruhe.de> | 2015-05-09 00:03:41 +0200 |
commit | c8259daaeee9870bc806c5d8b4bed669e68d061c (patch) | |
tree | 427479a7d9a0280d234dec47e29d4c715fc02b26 /core | |
parent | 11a66f451ab76b1ef5c45f12c6ed7100040b9084 (diff) | |
download | AntennaPod-c8259daaeee9870bc806c5d8b4bed669e68d061c.zip |
Cancel auto download
Diffstat (limited to 'core')
7 files changed, 153 insertions, 95 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedItem.java b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedItem.java index b74875175..75cce120f 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedItem.java +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedItem.java @@ -63,6 +63,8 @@ public class FeedItem extends FeedComponent implements ShownotesProvider, Flattr private List<Chapter> chapters; private FeedImage image; + private boolean autoDownload = true; + public FeedItem() { this.read = true; this.flattrStatus = new FlattrStatus(); @@ -74,7 +76,7 @@ public class FeedItem extends FeedComponent implements ShownotesProvider, Flattr * */ public FeedItem(long id, String title, String link, Date pubDate, String paymentLink, long feedId, FlattrStatus flattrStatus, boolean hasChapters, FeedImage image, boolean read, - String itemIdentifier) { + String itemIdentifier, boolean autoDownload) { this.id = id; this.title = title; this.link = link; @@ -86,6 +88,7 @@ public class FeedItem extends FeedComponent implements ShownotesProvider, Flattr this.image = image; this.read = read; this.itemIdentifier = itemIdentifier; + this.autoDownload = autoDownload; } /** @@ -388,6 +391,22 @@ public class FeedItem extends FeedComponent implements ShownotesProvider, Flattr return hasChapters; } + public void setAutoDownload(boolean autoDownload) { + this.autoDownload = autoDownload; + } + + public boolean getAutoDownload() { + return this.autoDownload; + } + + public boolean isAutoDownloadable() { + return this.hasMedia() && + false == this.getMedia().isPlaying() && + false == this.getMedia().isDownloaded() && + false == this.isRead() && + this.getAutoDownload(); + } + @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/APDownloadAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/APDownloadAlgorithm.java index c5f871f48..92de1eee7 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/APDownloadAlgorithm.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/APDownloadAlgorithm.java @@ -4,10 +4,9 @@ import android.content.Context; import android.util.Log; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Iterator; import java.util.List; -import de.danoeh.antennapod.core.BuildConfig; import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.util.NetworkUtils; @@ -53,75 +52,53 @@ public class APDownloadAlgorithm implements AutomaticDownloadAlgorithm { Log.d(TAG, "Performing auto-dl of undownloaded episodes"); - final List<FeedItem> queue = DBReader.getQueue(context); - final List<FeedItem> unreadItems = DBReader - .getUnreadItemsList(context); + List<FeedItem> candidates; + if(mediaIds.length > 0) { + candidates = DBReader.getFeedItems(context, mediaIds); + } else { + final List<FeedItem> queue = DBReader.getQueue(context); + final List<FeedItem> unreadItems = DBReader.getUnreadItemsList(context); + candidates = new ArrayList<FeedItem>(queue.size() + unreadItems.size()); + candidates.addAll(queue); + for(FeedItem unreadItem : unreadItems) { + if(candidates.contains(unreadItem) == false) { + candidates.add(unreadItem); + } + } + } - int undownloadedEpisodes = DBTasks.getNumberOfUndownloadedEpisodes(queue, - unreadItems); - int downloadedEpisodes = DBReader - .getNumberOfDownloadedEpisodes(context); + // filter items that are not auto downloadable + Iterator<FeedItem> it = candidates.iterator(); + while(it.hasNext()) { + FeedItem item = it.next(); + if(item.isAutoDownloadable() == false) { + it.remove(); + } + } + + int autoDownloadableEpisodes = candidates.size(); + int downloadedEpisodes = DBReader.getNumberOfDownloadedEpisodes(context); int deletedEpisodes = cleanupAlgorithm.performCleanup(context, - APCleanupAlgorithm.getPerformAutoCleanupArgs(context, undownloadedEpisodes)); - int episodeSpaceLeft = undownloadedEpisodes; + APCleanupAlgorithm.getPerformAutoCleanupArgs(context, autoDownloadableEpisodes)); boolean cacheIsUnlimited = UserPreferences.getEpisodeCacheSize() == UserPreferences .getEpisodeCacheSizeUnlimited(); - - if (!cacheIsUnlimited - && UserPreferences.getEpisodeCacheSize() < downloadedEpisodes - + undownloadedEpisodes) { - episodeSpaceLeft = UserPreferences.getEpisodeCacheSize() - - (downloadedEpisodes - deletedEpisodes); + int episodeCacheSize = UserPreferences.getEpisodeCacheSize(); + + int episodeSpaceLeft; + if (cacheIsUnlimited || + episodeCacheSize >= downloadedEpisodes + autoDownloadableEpisodes) { + episodeSpaceLeft = autoDownloadableEpisodes; + } else { + episodeSpaceLeft = episodeCacheSize - (downloadedEpisodes - deletedEpisodes); } - Arrays.sort(mediaIds); // sort for binary search - final boolean ignoreMediaIds = mediaIds.length == 0; - List<FeedItem> itemsToDownload = new ArrayList<FeedItem>(); - - if (episodeSpaceLeft > 0 && undownloadedEpisodes > 0) { - for (int i = 0; i < queue.size(); i++) { // ignore playing item - FeedItem item = queue.get(i); - long mediaId = (item.hasMedia()) ? item.getMedia().getId() : -1; - if ((ignoreMediaIds || Arrays.binarySearch(mediaIds, mediaId) >= 0) - && item.hasMedia() - && !item.getMedia().isDownloaded() - && !item.getMedia().isPlaying() - && item.getFeed().getPreferences().getAutoDownload()) { - itemsToDownload.add(item); - episodeSpaceLeft--; - undownloadedEpisodes--; - if (episodeSpaceLeft == 0 || undownloadedEpisodes == 0) { - break; - } - } - } - } + FeedItem[] itemsToDownload = candidates.subList(0, episodeSpaceLeft) + .toArray(new FeedItem[episodeSpaceLeft]); - if (episodeSpaceLeft > 0 && undownloadedEpisodes > 0) { - for (FeedItem item : unreadItems) { - long mediaId = (item.hasMedia()) ? item.getMedia().getId() : -1; - if ((ignoreMediaIds || Arrays.binarySearch(mediaIds, mediaId) >= 0) - && item.hasMedia() - && !item.getMedia().isDownloaded() - && item.getFeed().getPreferences().getAutoDownload()) { - itemsToDownload.add(item); - episodeSpaceLeft--; - undownloadedEpisodes--; - if (episodeSpaceLeft == 0 || undownloadedEpisodes == 0) { - break; - } - } - } - } - if (BuildConfig.DEBUG) - Log.d(TAG, "Enqueueing " + itemsToDownload.size() - + " items for download"); + Log.d(TAG, "Enqueueing " + itemsToDownload.length + " items for download"); try { - DBTasks.downloadFeedItems(false, context, - itemsToDownload.toArray(new FeedItem[itemsToDownload - .size()]) - ); + DBTasks.downloadFeedItems(false, context, itemsToDownload); } catch (DownloadRequestException e) { e.printStackTrace(); } @@ -130,4 +107,5 @@ public class APDownloadAlgorithm implements AutomaticDownloadAlgorithm { } }; } + } diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java index 94fb6f0a7..fd96b0cba 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java @@ -4,6 +4,8 @@ import android.content.Context; import android.database.Cursor; import android.util.Log; +import org.apache.commons.lang3.StringUtils; + import java.util.ArrayList; import java.util.Collections; import java.util.Date; @@ -228,7 +230,9 @@ public final class DBReader { itemlistCursor.getInt(PodDBAdapter.IDX_FI_SMALL_HAS_CHAPTERS) > 0, image, (itemlistCursor.getInt(PodDBAdapter.IDX_FI_SMALL_READ) > 0), - itemlistCursor.getString(PodDBAdapter.IDX_FI_SMALL_ITEM_IDENTIFIER)); + itemlistCursor.getString(PodDBAdapter.IDX_FI_SMALL_ITEM_IDENTIFIER), + itemlistCursor.getInt(itemlistCursor.getColumnIndex(PodDBAdapter.KEY_AUTO_DOWNLOAD)) > 0 + ); itemIds.add(String.valueOf(item.getId())); @@ -719,6 +723,31 @@ public final class DBReader { } } return item; + } + + static List<FeedItem> getFeedItems(final Context context, PodDBAdapter adapter, final long... itemIds) { + + String[] ids = new String[itemIds.length]; + for(int i = 0; i < itemIds.length; i++) { + long itemId = itemIds[i]; + ids[i] = Long.toString(itemId); + } + + List<FeedItem> result; + + Cursor itemCursor = adapter.getFeedItemCursor(ids); + if (itemCursor.moveToFirst()) { + result = extractItemlistFromCursor(adapter, itemCursor); + loadFeedDataOfFeedItemlist(context, result); + for(FeedItem item : result) { + if (item.hasChapters()) { + loadChaptersOfFeedItem(adapter, item); + } + } + } else { + result = Collections.emptyList(); + } + return result; } @@ -740,7 +769,6 @@ public final class DBReader { FeedItem item = getFeedItem(context, itemId, adapter); adapter.close(); return item; - } static FeedItem getFeedItem(final Context context, final String podcastUrl, final String episodeUrl, PodDBAdapter adapter) { @@ -761,6 +789,26 @@ public final class DBReader { } /** + * Loads specific FeedItems from the database. This method canbe used for loading more + * than one FeedItem + * + * @param context A context that is used for opening a database connection. + * @param itemIds The IDs of the FeedItems + * @return The FeedItems or an empty list if none of the FeedItems could be found. All FeedComponent-attributes + * as well as chapter marks of the FeedItems will also be loaded from the database. + */ + public static List<FeedItem> getFeedItems(final Context context, final long... itemIds) { + Log.d(TAG, "Loading feeditem with ids: " + StringUtils.join(itemIds, ",")); + + PodDBAdapter adapter = new PodDBAdapter(context); + adapter.open(); + List<FeedItem> items = getFeedItems(context, adapter, itemIds); + adapter.close(); + return items; + } + + + /** * Returns credentials based on image URL * * @param context A context that is used for opening a database connection. diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java index 7846f3dfc..1ec5a0949 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java @@ -428,25 +428,6 @@ public final class DBTasks { } } - static int getNumberOfUndownloadedEpisodes( - final List<FeedItem> queue, final List<FeedItem> unreadItems) { - int counter = 0; - for (FeedItem item : queue) { - if (item.hasMedia() && !item.getMedia().isDownloaded() - && !item.getMedia().isPlaying() - && item.getFeed().getPreferences().getAutoDownload()) { - counter++; - } - } - for (FeedItem item : unreadItems) { - if (item.hasMedia() && !item.getMedia().isDownloaded() - && item.getFeed().getPreferences().getAutoDownload()) { - counter++; - } - } - return counter; - } - /** * Looks for undownloaded episodes in the queue or list of unread items and request a download if * 1. Network is available @@ -599,8 +580,7 @@ public final class DBTasks { newFeedsList.add(newFeed); resultFeeds[feedIdx] = newFeed; } else { - if (BuildConfig.DEBUG) - Log.d(TAG, "Feed with title " + newFeed.getTitle() + Log.d(TAG, "Feed with title " + newFeed.getTitle() + " already exists. Syncing new with existing one."); Collections.sort(newFeed.getItems(), new FeedItemPubdateComparator()); @@ -608,21 +588,17 @@ public final class DBTasks { final boolean markNewItemsAsUnread; if (newFeed.getPageNr() == savedFeed.getPageNr()) { if (savedFeed.compareWithOther(newFeed)) { - if (BuildConfig.DEBUG) - Log.d(TAG, - "Feed has updated attribute values. Updating old feed's attributes"); + Log.d(TAG, "Feed has updated attribute values. Updating old feed's attributes"); savedFeed.updateFromOther(newFeed); } markNewItemsAsUnread = true; } else { - if (BuildConfig.DEBUG) - Log.d(TAG, "New feed has a higher page number. Merging without marking as unread"); + Log.d(TAG, "New feed has a higher page number. Merging without marking as unread"); markNewItemsAsUnread = false; savedFeed.setNextPageLink(newFeed.getNextPageLink()); } if (savedFeed.getPreferences().compareWithOther(newFeed.getPreferences())) { - if (BuildConfig.DEBUG) - Log.d(TAG, "Feed has updated preferences. Updating old feed's preferences"); + Log.d(TAG, "Feed has updated preferences. Updating old feed's preferences"); savedFeed.getPreferences().updateFromOther(newFeed.getPreferences()); } // Look for new or updated Items @@ -634,6 +610,7 @@ public final class DBTasks { // item is new final int i = idx; item.setFeed(savedFeed); + item.setAutoDownload(savedFeed.getPreferences().getAutoDownload()); savedFeed.getItems().add(i, item); if (markNewItemsAsUnread) { item.setRead(false); diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java index 8a5becac6..b8b04f673 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java @@ -1039,4 +1039,28 @@ public class DBWriter { } }); } + + /** + * Sets the 'auto_download'-attribute of specific FeedItem. + * + * @param context A context that is used for opening a database connection. + * @param feedItem FeedItem. + */ + public static Future<?> setFeedItemAutoDownload(final Context context, final FeedItem feedItem, + final boolean autoDownload) { + Log.d(TAG, "FeedItem[id=" + feedItem.getId() + "] SET auto_download " + autoDownload); + return dbExec.submit(new Runnable() { + + @Override + public void run() { + final PodDBAdapter adapter = new PodDBAdapter(context); + adapter.open(); + adapter.setFeedItemAutoDownload(feedItem, autoDownload); + adapter.close(); + + EventDistributor.getInstance().sendUnreadItemsUpdateBroadcast(); + } + }); + + } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java index 0d680fe6f..65caa7384 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java @@ -186,7 +186,8 @@ public class PodDBAdapter { + KEY_MEDIA + " INTEGER," + KEY_FEED + " INTEGER," + KEY_HAS_CHAPTERS + " INTEGER," + KEY_ITEM_IDENTIFIER + " TEXT," + KEY_FLATTR_STATUS + " INTEGER," - + KEY_IMAGE + " INTEGER)"; + + KEY_IMAGE + " INTEGER," + + KEY_AUTO_DOWNLOAD + " INTEGER)"; public static final String CREATE_TABLE_FEED_IMAGES = "CREATE TABLE " + TABLE_NAME_FEED_IMAGES + " (" + TABLE_PRIMARY_KEY + KEY_TITLE @@ -286,7 +287,9 @@ public class PodDBAdapter { TABLE_NAME_FEED_ITEMS + "." + KEY_HAS_CHAPTERS, TABLE_NAME_FEED_ITEMS + "." + KEY_ITEM_IDENTIFIER, TABLE_NAME_FEED_ITEMS + "." + KEY_FLATTR_STATUS, - TABLE_NAME_FEED_ITEMS + "." + KEY_IMAGE}; + TABLE_NAME_FEED_ITEMS + "." + KEY_IMAGE, + TABLE_NAME_FEED_ITEMS + "." + KEY_AUTO_DOWNLOAD + }; /** * Contains FEEDITEM_SEL_FI_SMALL as comma-separated list. Useful for raw queries. @@ -696,6 +699,7 @@ public class PodDBAdapter { values.put(KEY_HAS_CHAPTERS, item.getChapters() != null || item.hasChapters()); values.put(KEY_ITEM_IDENTIFIER, item.getItemIdentifier()); values.put(KEY_FLATTR_STATUS, item.getFlattrStatus().toLong()); + values.put(KEY_AUTO_DOWNLOAD, item.getAutoDownload()); if (item.hasItemImage()) { if (item.getImage().getId() == 0) { setImage(item.getImage()); @@ -787,6 +791,13 @@ public class PodDBAdapter { return status.getId(); } + public void setFeedItemAutoDownload(FeedItem feedItem, boolean autoDownload) { + ContentValues values = new ContentValues(); + values.put(KEY_AUTO_DOWNLOAD, autoDownload); + db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?", + new String[] { String.valueOf(feedItem.getId()) } ); + } + public long getDownloadLogSize() { final String query = String.format("SELECT COUNT(%s) FROM %s", KEY_ID, TABLE_NAME_DOWNLOAD_LOG); Cursor result = db.rawQuery(query, null); diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 14222be56..d6988348f 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -129,6 +129,7 @@ <string name="download_error_unauthorized">Authentication error</string> <string name="cancel_all_downloads_label">Cancel all downloads</string> <string name="download_cancelled_msg">Download cancelled</string> + <string name="download_cancelled_autodownload_enabled_msg">Download cancelled\nDisabled <i>Auto Download</i> for this item</string> <string name="download_report_title">Downloads completed with error(s)</string> <string name="download_report_content_title">Download report</string> <string name="download_error_malformed_url">Malformed URL</string> |