|
|
|
@ -124,28 +124,15 @@ public class SearchRepository {
|
|
|
|
|
|
|
|
|
|
public void query(@NonNull String query, long threadId, @NonNull Callback<CursorList<MessageResult>> callback) {
|
|
|
|
|
if (TextUtils.isEmpty(query)) {
|
|
|
|
|
Log.d("[ACL]", "Recognised empty query!");
|
|
|
|
|
|
|
|
|
|
callback.onResult(CursorList.emptyList());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Log.d("[ACL]", "Non-empty query is: " + query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
executor.execute(() -> {
|
|
|
|
|
|
|
|
|
|
Log.d("[ACL]", "Hit query.execute!");
|
|
|
|
|
|
|
|
|
|
// If the search is for a single character and it was stripped by `sanitizeQuery` then abort
|
|
|
|
|
// the search for an empty string to avoid SQLite error.
|
|
|
|
|
// If the sanitized search query is empty or just contains a single space (" ") then abort
|
|
|
|
|
// the search to prevent SQLite errors.
|
|
|
|
|
String cleanQuery = sanitizeQuery(query);
|
|
|
|
|
Log.d("[ACL]", "clean query is: \"" + cleanQuery + "\"");
|
|
|
|
|
if (cleanQuery.isEmpty() || cleanQuery.equalsIgnoreCase(" "))
|
|
|
|
|
{
|
|
|
|
|
Log.d("[ACL]", "Aborting empty search query.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (cleanQuery.equalsIgnoreCase(" ") || cleanQuery.isEmpty()) { return; }
|
|
|
|
|
|
|
|
|
|
CursorList<MessageResult> messages = queryMessages(cleanQuery, threadId);
|
|
|
|
|
callback.onResult(messages);
|
|
|
|
@ -211,19 +198,12 @@ public class SearchRepository {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CursorList<MessageResult> queryMessages(@NonNull String query) {
|
|
|
|
|
|
|
|
|
|
Log.d("[ACL]", "[SearchRepository] Query is:\n" + query);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Cursor messages = searchDatabase.queryMessages(query);
|
|
|
|
|
return messages != null ? new CursorList<>(messages, new MessageModelBuilder(context))
|
|
|
|
|
: CursorList.emptyList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CursorList<MessageResult> queryMessages(@NonNull String query, long threadId) {
|
|
|
|
|
|
|
|
|
|
Log.d("[ACL]", "[SearchRepository] 6 - query is: " + query);
|
|
|
|
|
|
|
|
|
|
Cursor messages = searchDatabase.queryMessages(query, threadId);
|
|
|
|
|
return messages != null ? new CursorList<>(messages, new MessageModelBuilder(context))
|
|
|
|
|
: CursorList.emptyList();
|
|
|
|
@ -237,11 +217,7 @@ public class SearchRepository {
|
|
|
|
|
* However, if we replace the apostrophe with a space, then the query will find the match.
|
|
|
|
|
*/
|
|
|
|
|
private String sanitizeQuery(@NonNull String query) {
|
|
|
|
|
|
|
|
|
|
Log.d("[ACL]", "[SearchRepository] Hit sanitizeQuery - initial query is: " + query);
|
|
|
|
|
|
|
|
|
|
StringBuilder out = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < query.length(); i++) {
|
|
|
|
|
char c = query.charAt(i);
|
|
|
|
|
if (!BANNED_CHARACTERS.contains(c)) {
|
|
|
|
@ -250,11 +226,7 @@ public class SearchRepository {
|
|
|
|
|
out.append(' ');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.d("[ACL]", "[SearchRepository] Hit sanitizeQuery - sanitized query is: " + out.toString());
|
|
|
|
|
|
|
|
|
|
// Querying for an empty string causes a crash so we'll que
|
|
|
|
|
if (out.toString().length() > 0) { return out.toString(); } else { return " "; }
|
|
|
|
|
return out.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class ContactModelBuilder implements CursorList.ModelBuilder<Contact> {
|
|
|
|
|