summaryrefslogtreecommitdiff
path: root/core/src/main/java/de/danoeh/antennapod
diff options
context:
space:
mode:
authorTom Hennen <tom.hennen@gmail.com>2016-01-23 17:22:10 -0500
committerTom Hennen <tom.hennen@gmail.com>2016-01-25 11:26:46 -0500
commit0b4c41a333e811dda3a3c667bc8d6d0545f77094 (patch)
treed8f2da72bf4f9f8364b3e602979a7fd2beb22199 /core/src/main/java/de/danoeh/antennapod
parent7f02570f88091ac46e447e7231583fe998cf6358 (diff)
downloadAntennaPod-0b4c41a333e811dda3a3c667bc8d6d0545f77094.zip
ability to filter episodes to include or not when autodownloading
Diffstat (limited to 'core/src/main/java/de/danoeh/antennapod')
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/feed/FeedFilter.java111
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/feed/FeedPreferences.java24
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/APDownloadAlgorithm.java6
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java16
4 files changed, 155 insertions, 2 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..a2f65c7e6 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,"
+ + KEY_EXCLUDE_FILTER + " TEXT,"
+ 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());