From 0b62b06cdf90521571f0d916a12d7925a6dd0eed Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Mon, 6 Aug 2012 11:07:49 +0200 Subject: [PATCH] Implemented auto-enqueue feature --- res/values/strings.xml | 2 +- .../danoeh/antennapod/feed/FeedManager.java | 39 +++++++++++++------ .../antennapod/service/DownloadService.java | 27 +++++++++++-- 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 172ecbf64..1a2746953 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -169,7 +169,7 @@ Add feed Feed is being added Buffering - Auto-queue + Auto-enqueue Add an episode to the queue after it has been downloaded. diff --git a/src/de/danoeh/antennapod/feed/FeedManager.java b/src/de/danoeh/antennapod/feed/FeedManager.java index d645cf3b6..450bd8522 100644 --- a/src/de/danoeh/antennapod/feed/FeedManager.java +++ b/src/de/danoeh/antennapod/feed/FeedManager.java @@ -106,7 +106,8 @@ public class FeedManager { context.startService(launchIntent); if (showPlayer) { // Launch Mediaplayer - context.startActivity(PlaybackService.getPlayerActivityIntent(context, media)); + context.startActivity(PlaybackService.getPlayerActivityIntent( + context, media)); } } @@ -161,7 +162,8 @@ public class FeedManager { public void run() { feeds.remove(feed); sendFeedUpdateBroadcast(context); - }}); + } + }); } @@ -182,7 +184,7 @@ public class FeedManager { } context.sendBroadcast(update); } - + private void sendFeedUpdateBroadcast(Context context) { context.sendBroadcast(new Intent(ACITON_FEED_LIST_UPDATE)); } @@ -191,7 +193,8 @@ public class FeedManager { * Sets the 'read'-attribute of a FeedItem. Should be used by all Classes * instead of the setters of FeedItem. */ - public void markItemRead(final Context context, final FeedItem item, final boolean read) { + public void markItemRead(final Context context, final FeedItem item, + final boolean read) { if (AppConfig.DEBUG) Log.d(TAG, "Setting item with title " + item.getTitle() + " as read/unread"); @@ -205,11 +208,13 @@ public class FeedManager { unreadItems.remove(item); } else { unreadItems.add(item); - Collections.sort(unreadItems, new FeedItemPubdateComparator()); + Collections.sort(unreadItems, + new FeedItemPubdateComparator()); } sendUnreadItemsUpdateBroadcast(context, item); - }}); - + } + }); + } /** @@ -303,13 +308,20 @@ public class FeedManager { return result; } - public void addQueueItem(Context context, FeedItem item) { + public void addQueueItem(final Context context, final FeedItem item) { + contentChanger.post(new Runnable() { + + @Override + public void run() { + queue.add(item); + sendQueueUpdateBroadcast(context, item); + + } + }); PodDBAdapter adapter = new PodDBAdapter(context); - queue.add(item); adapter.open(); adapter.setQueue(queue); adapter.close(); - sendQueueUpdateBroadcast(context, item); } /** Removes all items in queue */ @@ -447,8 +459,11 @@ public class FeedManager { } return null; } - - /** Returns true if a feed with the given download link is already in the feedlist. */ + + /** + * Returns true if a feed with the given download link is already in the + * feedlist. + */ public boolean feedExists(String downloadUrl) { for (Feed feed : feeds) { if (feed.getDownload_url().equals(downloadUrl)) { diff --git a/src/de/danoeh/antennapod/service/DownloadService.java b/src/de/danoeh/antennapod/service/DownloadService.java index 2f2bebfd3..b322bdf28 100644 --- a/src/de/danoeh/antennapod/service/DownloadService.java +++ b/src/de/danoeh/antennapod/service/DownloadService.java @@ -18,6 +18,7 @@ import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.SAXException; import de.danoeh.antennapod.AppConfig; +import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.activity.DownloadActivity; import de.danoeh.antennapod.activity.AudioplayerActivity; import de.danoeh.antennapod.activity.MainActivity; @@ -53,6 +54,7 @@ import android.os.Debug; import android.os.Handler; import android.os.Message; import android.os.Messenger; +import android.preference.PreferenceManager; public class DownloadService extends Service { private static final String TAG = "DownloadService"; @@ -398,7 +400,7 @@ public class DownloadService extends Service { private Feed feed; private DownloadService service; - + private int reason; private boolean successful; @@ -469,14 +471,15 @@ public class DownloadService extends Service { sendDownloadHandledIntent(downloadId, statusId, hasImage, imageId); queryDownloads(); } - + /** Checks if the feed was parsed correctly. */ private boolean checkFeedData(Feed feed) { if (feed.getTitle() == null) { Log.e(TAG, "Feed has no title."); return false; } else { - if (AppConfig.DEBUG) Log.d(TAG, "Feed appears to be valid."); + if (AppConfig.DEBUG) + Log.d(TAG, "Feed appears to be valid."); return true; } } @@ -553,6 +556,24 @@ public class DownloadService extends Service { sendDownloadHandledIntent(media.getDownloadId(), statusId, false, 0); media.setDownloadId(0); manager.setFeedMedia(service, media); + boolean autoQueue = PreferenceManager.getDefaultSharedPreferences( + getApplicationContext()).getBoolean( + PodcastApp.PREF_AUTO_QUEUE, true); + + if (!manager.isInQueue(media.getItem())) { + // Auto-queue + if (autoQueue) { + if (AppConfig.DEBUG) + Log.d(TAG, "Autoqueue is enabled. Adding item to queue"); + manager.addQueueItem(DownloadService.this, media.getItem()); + } else { + if (AppConfig.DEBUG) + Log.d(TAG, "Autoqueue is disabled"); + } + } else { + if (AppConfig.DEBUG) Log.d(TAG, "Item is already in queue"); + } + queryDownloads(); } }