diff options
author | Tom Hennen <TomHennen@users.noreply.github.com> | 2016-01-25 14:29:25 -0500 |
---|---|---|
committer | Tom Hennen <TomHennen@users.noreply.github.com> | 2016-01-25 14:29:25 -0500 |
commit | f9afe0d488bdb14798136fa183cb5831329b7cde (patch) | |
tree | 55f9b268a1e1eb58dd0f76989a856449c24445a1 /core/src | |
parent | 7f02570f88091ac46e447e7231583fe998cf6358 (diff) | |
parent | 8b0dac05c93e9e743a22fca3596775c367041855 (diff) | |
download | AntennaPod-f9afe0d488bdb14798136fa183cb5831329b7cde.zip |
Merge pull request #1612 from TomHennen/episode_filter
Episode autodownload filter
Diffstat (limited to 'core/src')
5 files changed, 161 insertions, 3 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedFilter.java b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedFilter.java new file mode 100644 index 000000000..35abb8de6 --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedFilter.java @@ -0,0 +1,111 @@ +package de.danoeh.antennapod.core.feed; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class FeedFilter { + + private static final String TAG = "FeedFilter"; + + private String includeFilter; + private String excludeFilter; + + public FeedFilter() { + this("", ""); + } + + public FeedFilter(String includeFilter, String excludeFilter) { + // We're storing the strings and not the parsed terms because + // 1. It's easier to show the user exactly what they typed in this way + // (we don't have to recreate it) + // 2. We don't know if we'll actually be asked to parse anything anyways. + this.includeFilter = includeFilter; + this.excludeFilter = excludeFilter; + } + + /** + * Parses the text in to a list of single words or quoted strings. + * Example: "One "Two Three"" returns ["One", "Two Three"] + * @param filter string to parse in to terms + * @return list of terms + */ + private List<String> parseTerms(String filter) { + // from http://stackoverflow.com/questions/7804335/split-string-on-spaces-in-java-except-if-between-quotes-i-e-treat-hello-wor + List<String> list = new ArrayList<>(); + Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(filter); + while (m.find()) + list.add(m.group(1).replace("\"", "")); + return list; + } + + /** + * @param item + * @return true if the item should be downloaded + */ + public boolean shouldAutoDownload(FeedItem item) { + + List<String> includeTerms = parseTerms(includeFilter); + List<String> excludeTerms = parseTerms(excludeFilter); + + if (includeTerms.size() == 0 && excludeTerms.size() == 0) { + // nothing has been specified, so include everything + return true; + } + + // check using lowercase so the users don't have to worry about case. + String title = item.getTitle().toLowerCase(); + + // if it's explicitly excluded, it shouldn't be autodownloaded + // even if it has include terms + for (String term : excludeTerms) { + if (title.contains(term.trim().toLowerCase())) { + return false; + } + } + + for (String term : includeTerms) { + if (title.contains(term.trim().toLowerCase())) { + return true; + } + } + + // now's the tricky bit + // if they haven't set an include filter, but they have set an exclude filter + // default to including, but if they've set both, then exclude + if (!hasIncludeFilter() && hasExcludeFilter()) { + return true; + } + + return false; + } + + public String getIncludeFilter() { + return includeFilter; + } + + public String getExcludeFilter() { return excludeFilter; } + + /** + * @return true if only include is set + */ + public boolean includeOnly() { + return hasIncludeFilter() && !hasExcludeFilter(); + } + + /** + * @return true if only exclude is set + */ + public boolean excludeOnly() { + return hasExcludeFilter() && !hasIncludeFilter(); + } + + public boolean hasIncludeFilter() { + return includeFilter.length() > 0; + } + + public boolean hasExcludeFilter() { + return excludeFilter.length() > 0; + } +} diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedPreferences.java b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedPreferences.java index ed568a6e5..9e95d5276 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedPreferences.java +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedPreferences.java @@ -2,6 +2,7 @@ package de.danoeh.antennapod.core.feed; import android.content.Context; import android.database.Cursor; +import android.support.annotation.NonNull; import android.text.TextUtils; import de.danoeh.antennapod.core.preferences.UserPreferences; @@ -13,8 +14,11 @@ import de.danoeh.antennapod.core.storage.PodDBAdapter; */ public class FeedPreferences { + @NonNull + private FeedFilter filter; private long feedID; private boolean autoDownload; + public enum AutoDeleteAction { GLOBAL, YES, @@ -25,11 +29,16 @@ public class FeedPreferences { private String password; public FeedPreferences(long feedID, boolean autoDownload, AutoDeleteAction auto_delete_action, String username, String password) { + this(feedID, autoDownload, auto_delete_action, username, password, new FeedFilter()); + } + + public FeedPreferences(long feedID, boolean autoDownload, AutoDeleteAction auto_delete_action, String username, String password, @NonNull FeedFilter filter) { this.feedID = feedID; this.autoDownload = autoDownload; this.auto_delete_action = auto_delete_action; this.username = username; this.password = password; + this.filter = filter; } public static FeedPreferences fromCursor(Cursor cursor) { @@ -38,6 +47,8 @@ public class FeedPreferences { int indexAutoDeleteAction = cursor.getColumnIndex(PodDBAdapter.KEY_AUTO_DELETE_ACTION); int indexUsername = cursor.getColumnIndex(PodDBAdapter.KEY_USERNAME); int indexPassword = cursor.getColumnIndex(PodDBAdapter.KEY_PASSWORD); + int indexIncludeFilter = cursor.getColumnIndex(PodDBAdapter.KEY_INCLUDE_FILTER); + int indexExcludeFilter = cursor.getColumnIndex(PodDBAdapter.KEY_EXCLUDE_FILTER); long feedId = cursor.getLong(indexId); boolean autoDownload = cursor.getInt(indexAutoDownload) > 0; @@ -45,10 +56,21 @@ public class FeedPreferences { AutoDeleteAction autoDeleteAction = AutoDeleteAction.values()[autoDeleteActionIndex]; String username = cursor.getString(indexUsername); String password = cursor.getString(indexPassword); - return new FeedPreferences(feedId, autoDownload, autoDeleteAction, username, password); + String includeFilter = cursor.getString(indexIncludeFilter); + String excludeFilter = cursor.getString(indexExcludeFilter); + return new FeedPreferences(feedId, autoDownload, autoDeleteAction, username, password, new FeedFilter(includeFilter, excludeFilter)); } + /** + * @return the filter for this feed + */ + public FeedFilter getFilter() { + return filter; + } + public void setFilter(@NonNull FeedFilter filter) { + this.filter = filter; + } /** * Compare another FeedPreferences with this one. The feedID, autoDownload and AutoDeleteAction attribute are excluded from the 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 9e21a55f2..26dc027bf 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 @@ -7,7 +7,9 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import de.danoeh.antennapod.core.feed.FeedFilter; import de.danoeh.antennapod.core.feed.FeedItem; +import de.danoeh.antennapod.core.feed.FeedPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.util.NetworkUtils; import de.danoeh.antennapod.core.util.PowerUtils; @@ -54,7 +56,9 @@ public class APDownloadAlgorithm implements AutomaticDownloadAlgorithm { candidates = new ArrayList<FeedItem>(queue.size() + newItems.size()); candidates.addAll(queue); for(FeedItem newItem : newItems) { - if(candidates.contains(newItem) == false) { + FeedPreferences feedPrefs = newItem.getFeed().getPreferences(); + FeedFilter feedFilter = feedPrefs.getFilter(); + if(candidates.contains(newItem) == false && feedFilter.shouldAutoDownload(newItem)) { candidates.add(newItem); } } 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 63b563525..6ade990cd 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 @@ -102,6 +102,8 @@ public class PodDBAdapter { public static final String KEY_LAST_UPDATE_FAILED = "last_update_failed"; public static final String KEY_HAS_EMBEDDED_PICTURE = "has_embedded_picture"; public static final String KEY_LAST_PLAYED_TIME = "last_played_time"; + public static final String KEY_INCLUDE_FILTER = "include_filter"; + public static final String KEY_EXCLUDE_FILTER = "exclude_filter"; // Table names public static final String TABLE_NAME_FEEDS = "Feeds"; @@ -128,6 +130,8 @@ public class PodDBAdapter { + KEY_FLATTR_STATUS + " INTEGER," + KEY_USERNAME + " TEXT," + KEY_PASSWORD + " TEXT," + + KEY_INCLUDE_FILTER + " TEXT DEFAULT ''," + + KEY_EXCLUDE_FILTER + " TEXT DEFAULT ''," + KEY_IS_PAGED + " INTEGER DEFAULT 0," + KEY_NEXT_PAGE_LINK + " TEXT," + KEY_HIDE + " TEXT," @@ -238,6 +242,8 @@ public class PodDBAdapter { TABLE_NAME_FEEDS + "." + KEY_HIDE, TABLE_NAME_FEEDS + "." + KEY_LAST_UPDATE_FAILED, TABLE_NAME_FEEDS + "." + KEY_AUTO_DELETE_ACTION, + TABLE_NAME_FEEDS + "." + KEY_INCLUDE_FILTER, + TABLE_NAME_FEEDS + "." + KEY_EXCLUDE_FILTER }; /** @@ -395,6 +401,8 @@ public class PodDBAdapter { values.put(KEY_AUTO_DELETE_ACTION,prefs.getAutoDeleteAction().ordinal()); values.put(KEY_USERNAME, prefs.getUsername()); values.put(KEY_PASSWORD, prefs.getPassword()); + values.put(KEY_INCLUDE_FILTER, prefs.getFilter().getIncludeFilter()); + values.put(KEY_EXCLUDE_FILTER, prefs.getFilter().getExcludeFilter()); db.update(TABLE_NAME_FEEDS, values, KEY_ID + "=?", new String[]{String.valueOf(prefs.getFeedID())}); } @@ -1745,6 +1753,7 @@ public class PodDBAdapter { db.execSQL(PodDBAdapter.CREATE_INDEX_FEEDITEMS_PUBDATE); db.execSQL(PodDBAdapter.CREATE_INDEX_FEEDITEMS_READ); } + if (oldVersion < 1050003) { // Migrates feed list filter data @@ -1780,6 +1789,13 @@ public class PodDBAdapter { db.setTransactionSuccessful(); db.endTransaction(); + + // and now get ready for autodownload filters + db.execSQL("ALTER TABLE " + PodDBAdapter.TABLE_NAME_FEEDS + + " ADD COLUMN " + PodDBAdapter.KEY_INCLUDE_FILTER + " TEXT DEFAULT ''"); + + db.execSQL("ALTER TABLE " + PodDBAdapter.TABLE_NAME_FEEDS + + " ADD COLUMN " + PodDBAdapter.KEY_EXCLUDE_FILTER + " TEXT DEFAULT ''"); } EventBus.getDefault().post(ProgressEvent.end()); diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index e4097feed..b8f6f7065 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -530,7 +530,12 @@ <!-- Feed information screen --> <string name="authentication_label">Authentication</string> <string name="authentication_descr">Change your username and password for this podcast and its episodes.</string> - + <string name="auto_download_settings_label">Auto Download Settings</string> + <string name="episode_filters_label">Episode Filter</string> + <string name="episode_filters_description">List of terms used to decide if an episode should be included or excluded when auto downloading</string> + <string name="episode_filters_include">Include</string> + <string name="episode_filters_exclude">Exclude</string> + <string name="episode_filters_hint">Single words \n\"Multiple Words\"</string> <!-- Progress information --> <string name="progress_upgrading_database">Upgrading the database</string> |