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.
This commit is contained in:
Thialfihar 2014-05-14 19:54:20 +02:00
parent 9025de2b74
commit ab81d8903a
3 changed files with 24 additions and 6 deletions

View File

@ -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

View File

@ -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

View File

@ -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(),