From ab81d8903a92e1f80105f223a727d75b94754769 Mon Sep 17 00:00:00 2001 From: Thialfihar Date: Wed, 14 May 2014 19:54:20 +0200 Subject: Support mutliple search words and highlight them For the regex matching it would be smart to sort the words by length, so the longest matches come first. This only matters for queries with words containing parts of each other, which is an unlikely event and even then it doesn't break anything. --- .../org/sufficientlysecure/keychain/ui/KeyListFragment.java | 13 +++++++++++-- .../keychain/ui/SelectPublicKeyFragment.java | 13 +++++++++++-- .../keychain/ui/adapter/HighlightQueryCursorAdapter.java | 4 ++-- 3 files changed, 24 insertions(+), 6 deletions(-) (limited to 'OpenKeychain/src/main/java') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index 3fd958bcc..941c95cb0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -264,8 +264,17 @@ public class KeyListFragment extends LoaderFragment String where = null; String whereArgs[] = null; if (mCurQuery != null) { - where = KeyRings.USER_ID + " LIKE ?"; - whereArgs = new String[]{"%" + mCurQuery + "%"}; + String[] words = mCurQuery.trim().split("\\s+"); + whereArgs = new String[words.length]; + for (int i = 0; i < words.length; ++i) { + if (where == null) { + where = ""; + } else { + where += " AND "; + } + where += KeyRings.USER_ID + " LIKE ?"; + whereArgs[i] = "%" + words[i] + "%"; + } } // Now create and return a CursorLoader that will take care of diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SelectPublicKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SelectPublicKeyFragment.java index 2ad769b00..25fa698ef 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SelectPublicKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SelectPublicKeyFragment.java @@ -282,8 +282,17 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements T String where = null; String whereArgs[] = null; if (mCurQuery != null) { - where = KeyRings.USER_ID + " LIKE ?"; - whereArgs = new String[]{"%" + mCurQuery + "%"}; + String[] words = mCurQuery.trim().split("\\s+"); + whereArgs = new String[words.length]; + for (int i = 0; i < words.length; ++i) { + if (where == null) { + where = ""; + } else { + where += " AND "; + } + where += KeyRings.USER_ID + " LIKE ?"; + whereArgs[i] = "%" + words[i] + "%"; + } } // Now create and return a CursorLoader that will take care of diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/HighlightQueryCursorAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/HighlightQueryCursorAdapter.java index fd7a2dc30..4a37a9a3a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/HighlightQueryCursorAdapter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/HighlightQueryCursorAdapter.java @@ -49,9 +49,9 @@ public abstract class HighlightQueryCursorAdapter extends CursorAdapter { Spannable highlight = Spannable.Factory.getInstance().newSpannable(text); if (mCurQuery != null) { - Pattern pattern = Pattern.compile("(?i)" + mCurQuery); + Pattern pattern = Pattern.compile("(?i)(" + mCurQuery.trim().replaceAll("\\s+", "|") + ")"); Matcher matcher = pattern.matcher(text); - if (matcher.find()) { + while (matcher.find()) { highlight.setSpan( new ForegroundColorSpan(mContext.getResources().getColor(R.color.emphasis)), matcher.start(), -- cgit v1.2.3