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:
@@ -264,8 +264,17 @@ public class KeyListFragment extends LoaderFragment
|
|||||||
String where = null;
|
String where = null;
|
||||||
String whereArgs[] = null;
|
String whereArgs[] = null;
|
||||||
if (mCurQuery != null) {
|
if (mCurQuery != null) {
|
||||||
where = KeyRings.USER_ID + " LIKE ?";
|
String[] words = mCurQuery.trim().split("\\s+");
|
||||||
whereArgs = new String[]{"%" + mCurQuery + "%"};
|
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
|
// Now create and return a CursorLoader that will take care of
|
||||||
|
|||||||
@@ -282,8 +282,17 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements T
|
|||||||
String where = null;
|
String where = null;
|
||||||
String whereArgs[] = null;
|
String whereArgs[] = null;
|
||||||
if (mCurQuery != null) {
|
if (mCurQuery != null) {
|
||||||
where = KeyRings.USER_ID + " LIKE ?";
|
String[] words = mCurQuery.trim().split("\\s+");
|
||||||
whereArgs = new String[]{"%" + mCurQuery + "%"};
|
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
|
// Now create and return a CursorLoader that will take care of
|
||||||
|
|||||||
@@ -49,9 +49,9 @@ public abstract class HighlightQueryCursorAdapter extends CursorAdapter {
|
|||||||
Spannable highlight = Spannable.Factory.getInstance().newSpannable(text);
|
Spannable highlight = Spannable.Factory.getInstance().newSpannable(text);
|
||||||
|
|
||||||
if (mCurQuery != null) {
|
if (mCurQuery != null) {
|
||||||
Pattern pattern = Pattern.compile("(?i)" + mCurQuery);
|
Pattern pattern = Pattern.compile("(?i)(" + mCurQuery.trim().replaceAll("\\s+", "|") + ")");
|
||||||
Matcher matcher = pattern.matcher(text);
|
Matcher matcher = pattern.matcher(text);
|
||||||
if (matcher.find()) {
|
while (matcher.find()) {
|
||||||
highlight.setSpan(
|
highlight.setSpan(
|
||||||
new ForegroundColorSpan(mContext.getResources().getColor(R.color.emphasis)),
|
new ForegroundColorSpan(mContext.getResources().getColor(R.color.emphasis)),
|
||||||
matcher.start(),
|
matcher.start(),
|
||||||
|
|||||||
Reference in New Issue
Block a user