diff options
author | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2022-11-03 02:24:54 +0000 |
---|---|---|
committer | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2022-11-03 02:24:54 +0000 |
commit | 785bb205f63fd3b269c2450e4b1f0e8a6a34c075 (patch) | |
tree | d45f211bb4b26f57e114dd38245c002cfdc46c47 /app/src/main/java/ml/docilealligator/infinityforreddit | |
parent | 15b979d3bac19e87e12f6beaf5bae88f2f8f0480 (diff) | |
download | infinity-for-reddit-785bb205f63fd3b269c2450e4b1f0e8a6a34c075.tar infinity-for-reddit-785bb205f63fd3b269c2450e4b1f0e8a6a34c075.tar.gz infinity-for-reddit-785bb205f63fd3b269c2450e4b1f0e8a6a34c075.tar.bz2 infinity-for-reddit-785bb205f63fd3b269c2450e4b1f0e8a6a34c075.tar.lz infinity-for-reddit-785bb205f63fd3b269c2450e4b1f0e8a6a34c075.tar.xz infinity-for-reddit-785bb205f63fd3b269c2450e4b1f0e8a6a34c075.tar.zst infinity-for-reddit-785bb205f63fd3b269c2450e4b1f0e8a6a34c075.zip |
Handle invalid regex pattern in post filter.
Diffstat (limited to 'app/src/main/java/ml/docilealligator/infinityforreddit')
2 files changed, 39 insertions, 20 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java index 4e96ec0f..d86da54a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java @@ -35,6 +35,8 @@ import com.r0adkll.slidr.Slidr; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.concurrent.Executor; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; import javax.inject.Inject; import javax.inject.Named; @@ -540,20 +542,28 @@ public class CustomizePostFilterActivity extends BaseActivity { finish(); return true; } else if (item.getItemId() == R.id.action_save_customize_post_filter_activity) { - constructPostFilter(); - Intent returnIntent = new Intent(); - returnIntent.putExtra(RETURN_EXTRA_POST_FILTER, postFilter); - setResult(Activity.RESULT_OK, returnIntent); - finish(); + try { + constructPostFilter(); + Intent returnIntent = new Intent(); + returnIntent.putExtra(RETURN_EXTRA_POST_FILTER, postFilter); + setResult(Activity.RESULT_OK, returnIntent); + finish(); + } catch (PatternSyntaxException e) { + Toast.makeText(this, R.string.invalid_regex, Toast.LENGTH_SHORT).show(); + } return true; } else if (item.getItemId() == R.id.action_save_to_database_customize_post_filter_activity) { - constructPostFilter(); + try { + constructPostFilter(); - if (!postFilter.name.equals("")) { - savePostFilter(originalName); - } else { - Toast.makeText(CustomizePostFilterActivity.this, R.string.post_filter_requires_a_name, Toast.LENGTH_LONG).show(); + if (!postFilter.name.equals("")) { + savePostFilter(originalName); + } else { + Toast.makeText(CustomizePostFilterActivity.this, R.string.post_filter_requires_a_name, Toast.LENGTH_LONG).show(); + } + } catch (PatternSyntaxException e) { + Toast.makeText(this, R.string.invalid_regex, Toast.LENGTH_SHORT).show(); } } return false; @@ -627,7 +637,7 @@ public class CustomizePostFilterActivity extends BaseActivity { } } - private void constructPostFilter() { + private void constructPostFilter() throws PatternSyntaxException { postFilter.name = nameTextInputEditText.getText().toString(); postFilter.maxVote = maxVoteTextInputEditText.getText() == null || maxVoteTextInputEditText.getText().toString().equals("") ? -1 : Integer.parseInt(maxVoteTextInputEditText.getText().toString()); postFilter.minVote = minVoteTextInputEditText.getText() == null || minVoteTextInputEditText.getText().toString().equals("") ? -1 : Integer.parseInt(minVoteTextInputEditText.getText().toString()); @@ -636,7 +646,9 @@ public class CustomizePostFilterActivity extends BaseActivity { postFilter.maxAwards = maxAwardsTextInputEditText.getText() == null || maxAwardsTextInputEditText.getText().toString().equals("") ? -1 : Integer.parseInt(maxAwardsTextInputEditText.getText().toString()); postFilter.minAwards = minAwardsTextInputEditText.getText() == null || minAwardsTextInputEditText.getText().toString().equals("") ? -1 : Integer.parseInt(minAwardsTextInputEditText.getText().toString()); postFilter.postTitleExcludesRegex = titleExcludesRegexTextInputEditText.getText().toString(); + Pattern.compile(postFilter.postTitleExcludesRegex); postFilter.postTitleContainsRegex = titleContainsRegexTextInputEditText.getText().toString(); + Pattern.compile(postFilter.postTitleContainsRegex); postFilter.postTitleExcludesStrings = titleExcludesStringsTextInputEditText.getText().toString(); postFilter.postTitleContainsStrings = titleContainsStringsTextInputEditText.getText().toString(); postFilter.excludeSubreddits = excludesSubredditsTextInputEditText.getText().toString(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java index 833cd5a8..a22ad6fd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java @@ -12,6 +12,7 @@ import androidx.room.PrimaryKey; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; import ml.docilealligator.infinityforreddit.post.Post; @@ -173,17 +174,23 @@ public class PostFilter implements Parcelable { return false; } if (postFilter.postTitleExcludesRegex != null && !postFilter.postTitleExcludesRegex.equals("")) { - Pattern pattern = Pattern.compile(postFilter.postTitleExcludesRegex); - Matcher matcher = pattern.matcher(post.getTitle()); - if (matcher.find()) { - return false; - } + try { + Pattern pattern = Pattern.compile(postFilter.postTitleExcludesRegex); + Matcher matcher = pattern.matcher(post.getTitle()); + if (matcher.find()) { + return false; + } + } catch (PatternSyntaxException ignore) {} } if (postFilter.postTitleContainsRegex != null && !postFilter.postTitleContainsRegex.equals("")) { - Pattern pattern = Pattern.compile(postFilter.postTitleContainsRegex); - Matcher matcher = pattern.matcher(post.getTitle()); - if (!matcher.find()) { - return false; + try { + Pattern pattern = Pattern.compile(postFilter.postTitleContainsRegex); + Matcher matcher = pattern.matcher(post.getTitle()); + if (!matcher.find()) { + return false; + } + } catch (PatternSyntaxException e) { + e.printStackTrace(); } } if (postFilter.postTitleExcludesStrings != null && !postFilter.postTitleExcludesStrings.equals("")) { |