From 35af8894d2dedfda8a4b66e8e3fb6a2d5a8346a0 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 29 Aug 2020 08:16:08 +0200 Subject: [PATCH] search individual conversations. fixes #3243 --- .../persistance/DatabaseBackend.java | 17 ++++++++--- .../services/MessageSearchTask.java | 10 ++++--- .../services/XmppConnectionService.java | 4 +-- .../ui/ConversationFragment.java | 11 ++++++- .../ui/ConversationsActivity.java | 12 ++++++++ .../conversations/ui/SearchActivity.java | 15 +++++++--- src/main/res/menu/activity_conversations.xml | 29 ++++++++++++++----- src/main/res/menu/fragment_conversation.xml | 8 ++++- .../menu/fragment_conversations_overview.xml | 1 + src/main/res/values-w945dp/defaults.xml | 2 ++ src/main/res/values/defaults.xml | 2 ++ src/main/res/values/strings.xml | 2 ++ 12 files changed, 90 insertions(+), 23 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java index 86662f530..4d8e8d22d 100644 --- a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -776,11 +776,20 @@ public class DatabaseBackend extends SQLiteOpenHelper { return list; } - public Cursor getMessageSearchCursor(List term) { - SQLiteDatabase db = this.getReadableDatabase(); - String SQL = "SELECT " + Message.TABLENAME + ".*," + Conversation.TABLENAME + '.' + Conversation.CONTACTJID + ',' + Conversation.TABLENAME + '.' + Conversation.ACCOUNT + ',' + Conversation.TABLENAME + '.' + Conversation.MODE + " FROM " + Message.TABLENAME + " join " + Conversation.TABLENAME + " on " + Message.TABLENAME + '.' + Message.CONVERSATION + '=' + Conversation.TABLENAME + '.' + Conversation.UUID + " join messages_index ON messages_index.uuid=messages.uuid where " + Message.ENCRYPTION + " NOT IN(" + Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE + ',' + Message.ENCRYPTION_PGP + ',' + Message.ENCRYPTION_DECRYPTION_FAILED + ',' + Message.ENCRYPTION_AXOLOTL_FAILED + ") AND " + Message.TYPE + " IN(" + Message.TYPE_TEXT + ',' + Message.TYPE_PRIVATE + ") AND messages_index.body MATCH ? ORDER BY " + Message.TIME_SENT + " DESC limit " + Config.MAX_SEARCH_RESULTS; + public Cursor getMessageSearchCursor(final List term, final String uuid) { + final SQLiteDatabase db = this.getReadableDatabase(); + final StringBuilder SQL = new StringBuilder(); + final String[] selectionArgs; + SQL.append("SELECT " + Message.TABLENAME + ".*," + Conversation.TABLENAME + '.' + Conversation.CONTACTJID + ',' + Conversation.TABLENAME + '.' + Conversation.ACCOUNT + ',' + Conversation.TABLENAME + '.' + Conversation.MODE + " FROM " + Message.TABLENAME + " join " + Conversation.TABLENAME + " on " + Message.TABLENAME + '.' + Message.CONVERSATION + '=' + Conversation.TABLENAME + '.' + Conversation.UUID + " join messages_index ON messages_index.uuid=messages.uuid where " + Message.ENCRYPTION + " NOT IN(" + Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE + ',' + Message.ENCRYPTION_PGP + ',' + Message.ENCRYPTION_DECRYPTION_FAILED + ',' + Message.ENCRYPTION_AXOLOTL_FAILED + ") AND " + Message.TYPE + " IN(" + Message.TYPE_TEXT + ',' + Message.TYPE_PRIVATE + ") AND messages_index.body MATCH ?"); + if (uuid == null) { + selectionArgs = new String[]{FtsUtils.toMatchString(term)}; + } else { + selectionArgs = new String[]{FtsUtils.toMatchString(term), uuid}; + SQL.append(" AND "+Conversation.TABLENAME+'.'+Conversation.UUID+"=?"); + } + SQL.append(" ORDER BY " + Message.TIME_SENT + " DESC limit " + Config.MAX_SEARCH_RESULTS); Log.d(Config.LOGTAG, "search term: " + FtsUtils.toMatchString(term)); - return db.rawQuery(SQL, new String[]{FtsUtils.toMatchString(term)}); + return db.rawQuery(SQL.toString(), selectionArgs); } public List markFileAsDeleted(final File file, final boolean internal) { diff --git a/src/main/java/eu/siacs/conversations/services/MessageSearchTask.java b/src/main/java/eu/siacs/conversations/services/MessageSearchTask.java index 6fd2413f8..c18c94cf2 100644 --- a/src/main/java/eu/siacs/conversations/services/MessageSearchTask.java +++ b/src/main/java/eu/siacs/conversations/services/MessageSearchTask.java @@ -56,18 +56,20 @@ public class MessageSearchTask implements Runnable, Cancellable { private final XmppConnectionService xmppConnectionService; private final List term; + private final String uuid; private final OnSearchResultsAvailable onSearchResultsAvailable; private boolean isCancelled = false; - private MessageSearchTask(XmppConnectionService xmppConnectionService, List term, OnSearchResultsAvailable onSearchResultsAvailable) { + private MessageSearchTask(XmppConnectionService xmppConnectionService, List term, final String uuid, OnSearchResultsAvailable onSearchResultsAvailable) { this.xmppConnectionService = xmppConnectionService; this.term = term; + this.uuid = uuid; this.onSearchResultsAvailable = onSearchResultsAvailable; } - public static void search(XmppConnectionService xmppConnectionService, List term, OnSearchResultsAvailable onSearchResultsAvailable) { - new MessageSearchTask(xmppConnectionService, term, onSearchResultsAvailable).executeInBackground(); + public static void search(XmppConnectionService xmppConnectionService, List term, final String uuid, OnSearchResultsAvailable onSearchResultsAvailable) { + new MessageSearchTask(xmppConnectionService, term, uuid, onSearchResultsAvailable).executeInBackground(); } public static void cancelRunningTasks() { @@ -86,7 +88,7 @@ public class MessageSearchTask implements Runnable, Cancellable { try { final HashMap conversationCache = new HashMap<>(); final List result = new ArrayList<>(); - cursor = xmppConnectionService.databaseBackend.getMessageSearchCursor(term); + cursor = xmppConnectionService.databaseBackend.getMessageSearchCursor(term, uuid); long dbTimer = SystemClock.elapsedRealtime(); if (isCancelled) { Log.d(Config.LOGTAG, "canceled search task"); diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index dc371fec6..d539ff30f 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -615,8 +615,8 @@ public class XmppConnectionService extends Service { return c != null && c.getMode() == Conversational.MODE_MULTI; } - public void search(List term, OnSearchResultsAvailable onSearchResultsAvailable) { - MessageSearchTask.search(this, term, onSearchResultsAvailable); + public void search(final List term, final String uuid, final OnSearchResultsAvailable onSearchResultsAvailable) { + MessageSearchTask.search(this, term, uuid, onSearchResultsAvailable); } @Override diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index c195fb13b..7b231f7bf 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -1240,6 +1240,9 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke case R.id.attach_location: handleAttachmentSelection(item); break; + case R.id.action_search: + startSearch(); + break; case R.id.action_archive: activity.xmppConnectionService.archiveConversation(conversation); break; @@ -1289,6 +1292,12 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke return super.onOptionsItemSelected(item); } + private void startSearch() { + final Intent intent = new Intent(getActivity(), SearchActivity.class); + intent.putExtra(SearchActivity.EXTRA_CONVERSATION_UUID, conversation.getUuid()); + startActivity(intent); + } + private void returnToOngoingCall() { final Optional ongoingRtpSession = activity.xmppConnectionService.getJingleConnectionManager().getOngoingRtpConnection(conversation.getContact()); if (ongoingRtpSession.isPresent()) { @@ -2757,7 +2766,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke correctMessage(lastEditableMessage); return true; } else { - Toast.makeText(getActivity(),R.string.could_not_correct_message, Toast.LENGTH_LONG).show(); + Toast.makeText(getActivity(), R.string.could_not_correct_message, Toast.LENGTH_LONG).show(); return false; } } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java index 451f4d105..967c2fd3d 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java @@ -489,6 +489,18 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio case R.id.action_scan_qr_code: UriHandlerActivity.scan(this); return true; + case R.id.action_search_all_conversations: + startActivity(new Intent(this, SearchActivity.class)); + return true; + case R.id.action_search_this_conversation: + final Conversation conversation = ConversationFragment.getConversation(this); + if (conversation == null) { + return true; + } + final Intent intent = new Intent(this, SearchActivity.class); + intent.putExtra(SearchActivity.EXTRA_CONVERSATION_UUID, conversation.getUuid()); + startActivity(intent); + return true; } return super.onOptionsItemSelected(item); } diff --git a/src/main/java/eu/siacs/conversations/ui/SearchActivity.java b/src/main/java/eu/siacs/conversations/ui/SearchActivity.java index 3b337e0c2..f1979828c 100644 --- a/src/main/java/eu/siacs/conversations/ui/SearchActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/SearchActivity.java @@ -29,6 +29,7 @@ package eu.siacs.conversations.ui; +import android.content.Intent; import android.databinding.DataBindingUtil; import android.os.Bundle; import android.support.v7.widget.Toolbar; @@ -42,6 +43,8 @@ import android.view.View; import android.widget.AdapterView; import android.widget.EditText; +import com.google.common.base.Strings; + import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; @@ -70,17 +73,21 @@ import static eu.siacs.conversations.ui.util.SoftKeyboardUtils.showKeyboard; public class SearchActivity extends XmppActivity implements TextWatcher, OnSearchResultsAvailable, MessageAdapter.OnContactPictureClicked { private static final String EXTRA_SEARCH_TERM = "search-term"; + public static final String EXTRA_CONVERSATION_UUID = "uuid"; private ActivitySearchBinding binding; private MessageAdapter messageListAdapter; private final List messages = new ArrayList<>(); private WeakReference selectedMessageReference = new WeakReference<>(null); + private String uuid; private final ChangeWatcher> currentSearch = new ChangeWatcher<>(); private final PendingItem pendingSearchTerm = new PendingItem<>(); private final PendingItem> pendingSearch = new PendingItem<>(); @Override public void onCreate(final Bundle bundle) { + final Intent intent = getIntent(); + this.uuid = intent == null ? null : Strings.emptyToNull(intent.getStringExtra(EXTRA_CONVERSATION_UUID)); final String searchTerm = bundle == null ? null : bundle.getString(EXTRA_SEARCH_TERM); if (searchTerm != null) { pendingSearchTerm.push(searchTerm); @@ -103,10 +110,10 @@ public class SearchActivity extends XmppActivity implements TextWatcher, OnSearc final String term = pendingSearchTerm.pop(); if (term != null) { searchField.append(term); - List searchTerm = FtsUtils.parse(term); + final List searchTerm = FtsUtils.parse(term); if (xmppConnectionService != null) { if (currentSearch.watch(searchTerm)) { - xmppConnectionService.search(searchTerm, this); + xmppConnectionService.search(searchTerm, uuid, this); } } else { pendingSearch.push(searchTerm); @@ -206,7 +213,7 @@ public class SearchActivity extends XmppActivity implements TextWatcher, OnSearc void onBackendConnected() { final List searchTerm = pendingSearch.pop(); if (searchTerm != null && currentSearch.watch(searchTerm)) { - xmppConnectionService.search(searchTerm, this); + xmppConnectionService.search(searchTerm, uuid,this); } } @@ -239,7 +246,7 @@ public class SearchActivity extends XmppActivity implements TextWatcher, OnSearc return; } if (term.size() > 0) { - xmppConnectionService.search(term, this); + xmppConnectionService.search(term, uuid,this); } else { MessageSearchTask.cancelRunningTasks(); this.messages.clear(); diff --git a/src/main/res/menu/activity_conversations.xml b/src/main/res/menu/activity_conversations.xml index 3275fdc6d..bb399a68a 100644 --- a/src/main/res/menu/activity_conversations.xml +++ b/src/main/res/menu/activity_conversations.xml @@ -1,26 +1,41 @@ + xmlns:app="http://schemas.android.com/apk/res-auto"> + app:showAsAction="always" /> + + + + + + + app:showAsAction="never" /> + app:showAsAction="never" /> + app:showAsAction="never" /> \ No newline at end of file diff --git a/src/main/res/menu/fragment_conversation.xml b/src/main/res/menu/fragment_conversation.xml index 5eb0b313b..294807bea 100644 --- a/src/main/res/menu/fragment_conversation.xml +++ b/src/main/res/menu/fragment_conversation.xml @@ -99,6 +99,12 @@ android:orderInCategory="45" android:title="@string/invite_contact" app:showAsAction="never" /> + + app:showAsAction="never" /> \ No newline at end of file diff --git a/src/main/res/menu/fragment_conversations_overview.xml b/src/main/res/menu/fragment_conversations_overview.xml index 39daf7d8c..3e2865493 100644 --- a/src/main/res/menu/fragment_conversations_overview.xml +++ b/src/main/res/menu/fragment_conversations_overview.xml @@ -32,6 +32,7 @@ \ No newline at end of file diff --git a/src/main/res/values-w945dp/defaults.xml b/src/main/res/values-w945dp/defaults.xml index 855f5b520..f17a59a91 100644 --- a/src/main/res/values-w945dp/defaults.xml +++ b/src/main/res/values-w945dp/defaults.xml @@ -1,5 +1,7 @@ false + false + true diff --git a/src/main/res/values/defaults.xml b/src/main/res/values/defaults.xml index 4ea78ef51..a031a9149 100644 --- a/src/main/res/values/defaults.xml +++ b/src/main/res/values/defaults.xml @@ -35,6 +35,8 @@ false false true + true + false true default_on small diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index c6a77aaa9..409039203 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -927,6 +927,8 @@ Remove from favorites GPX track Could not correct message + All conversations + This conversation View %1$d Participant View %1$d Participants