diff options
author | Daniel Gultsch <daniel@gultsch.de> | 2020-08-29 09:16:08 +0300 |
---|---|---|
committer | Daniel Gultsch <daniel@gultsch.de> | 2020-08-29 09:16:08 +0300 |
commit | 35af8894d2dedfda8a4b66e8e3fb6a2d5a8346a0 (patch) | |
tree | 06932b66d789136fcca4af8ae3b88a414d7b40b3 /src | |
parent | d158eeaf723002a7aab1833f9eb4dc5a66f32a24 (diff) |
search individual conversations. fixes #3243
Diffstat (limited to 'src')
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<String> 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<String> 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<String> 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<String> term; + private final String uuid; private final OnSearchResultsAvailable onSearchResultsAvailable; private boolean isCancelled = false; - private MessageSearchTask(XmppConnectionService xmppConnectionService, List<String> term, OnSearchResultsAvailable onSearchResultsAvailable) { + private MessageSearchTask(XmppConnectionService xmppConnectionService, List<String> 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<String> term, OnSearchResultsAvailable onSearchResultsAvailable) { - new MessageSearchTask(xmppConnectionService, term, onSearchResultsAvailable).executeInBackground(); + public static void search(XmppConnectionService xmppConnectionService, List<String> 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<String, Conversational> conversationCache = new HashMap<>(); final List<Message> 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<String> term, OnSearchResultsAvailable onSearchResultsAvailable) { - MessageSearchTask.search(this, term, onSearchResultsAvailable); + public void search(final List<String> 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> 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<Message> messages = new ArrayList<>(); private WeakReference<Message> selectedMessageReference = new WeakReference<>(null); + private String uuid; private final ChangeWatcher<List<String>> currentSearch = new ChangeWatcher<>(); private final PendingItem<String> pendingSearchTerm = new PendingItem<>(); private final PendingItem<List<String>> 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<String> searchTerm = FtsUtils.parse(term); + final List<String> 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<String> 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 @@ <menu xmlns:android="http://schemas.android.com/apk/res/android" - xmlns:app="http://schemas.android.com/apk/res-auto"> + xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_scan_qr_code" - android:title="@string/scan_qr_code" - app:showAsAction="always" + android:icon="?attr/icon_scan_qr_code" android:orderInCategory="10" + android:title="@string/scan_qr_code" android:visible="@bool/show_qr_code_scan" - android:icon="?attr/icon_scan_qr_code"/> + app:showAsAction="always" /> + <item + android:icon="?attr/icon_search" + android:orderInCategory="11" + android:title="@string/search_messages" + android:visible="@bool/show_combined_search_options" + app:showAsAction="always"> + <menu> + <item + android:id="@+id/action_search_all_conversations" + android:title="@string/search_all_conversations" /> + <item + android:id="@+id/action_search_this_conversation" + android:title="@string/search_this_conversation" /> + </menu> + </item> <item android:id="@+id/action_accounts" android:orderInCategory="90" android:title="@string/action_accounts" - app:showAsAction="never"/> + app:showAsAction="never" /> <item android:id="@+id/action_account" android:orderInCategory="90" android:title="@string/action_account" - app:showAsAction="never"/> + app:showAsAction="never" /> <item android:id="@+id/action_settings" android:orderInCategory="100" android:title="@string/action_settings" - app:showAsAction="never"/> + app:showAsAction="never" /> </menu>
\ 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 @@ -100,6 +100,12 @@ android:title="@string/invite_contact" app:showAsAction="never" /> <item + android:id="@+id/action_search" + android:orderInCategory="48" + android:title="@string/search_messages" + android:visible="@bool/show_individual_search_options" + app:showAsAction="never" /> + <item android:id="@+id/action_clear_history" android:orderInCategory="50" android:title="@string/action_clear_history" @@ -125,5 +131,5 @@ android:id="@+id/action_toggle_pinned" android:orderInCategory="72" android:title="@string/add_to_favorites" - app:showAsAction="never"/> + app:showAsAction="never" /> </menu>
\ 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 @@ <item android:id="@+id/action_search" android:title="@string/search_messages" + android:visible="@bool/show_individual_search_options" android:orderInCategory="50" app:showAsAction="never"/> </menu>
\ 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 @@ <?xml version="1.0" encoding="utf-8"?> <resources> <bool name="show_qr_code_scan">false</bool> + <bool name="show_individual_search_options">false</bool> + <bool name="show_combined_search_options">true</bool> </resources> 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 @@ <bool name="never_send">false</bool> <bool name="validate_hostname">false</bool> <bool name="show_qr_code_scan">true</bool> + <bool name="show_individual_search_options">true</bool> + <bool name="show_combined_search_options">false</bool> <bool name="scroll_to_bottom">true</bool> <string name="omemo_setting_default">default_on</string> <string name="default_font_size">small</string> 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 @@ <string name="remove_from_favorites">Remove from favorites</string> <string name="gpx_track">GPX track</string> <string name="could_not_correct_message">Could not correct message</string> + <string name="search_all_conversations">All conversations</string> + <string name="search_this_conversation">This conversation</string> <plurals name="view_users"> <item quantity="one">View %1$d Participant</item> <item quantity="other">View %1$d Participants</item> |