search individual conversations. fixes #3243

This commit is contained in:
Daniel Gultsch 2020-08-29 08:16:08 +02:00
parent d158eeaf72
commit 35af8894d2
12 changed files with 90 additions and 23 deletions

View file

@ -776,11 +776,20 @@ public class DatabaseBackend extends SQLiteOpenHelper {
return list; return list;
} }
public Cursor getMessageSearchCursor(List<String> term) { public Cursor getMessageSearchCursor(final List<String> term, final String uuid) {
SQLiteDatabase db = this.getReadableDatabase(); final 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; 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)); 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) { public List<String> markFileAsDeleted(final File file, final boolean internal) {

View file

@ -56,18 +56,20 @@ public class MessageSearchTask implements Runnable, Cancellable {
private final XmppConnectionService xmppConnectionService; private final XmppConnectionService xmppConnectionService;
private final List<String> term; private final List<String> term;
private final String uuid;
private final OnSearchResultsAvailable onSearchResultsAvailable; private final OnSearchResultsAvailable onSearchResultsAvailable;
private boolean isCancelled = false; 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.xmppConnectionService = xmppConnectionService;
this.term = term; this.term = term;
this.uuid = uuid;
this.onSearchResultsAvailable = onSearchResultsAvailable; this.onSearchResultsAvailable = onSearchResultsAvailable;
} }
public static void search(XmppConnectionService xmppConnectionService, List<String> term, OnSearchResultsAvailable onSearchResultsAvailable) { public static void search(XmppConnectionService xmppConnectionService, List<String> term, final String uuid, OnSearchResultsAvailable onSearchResultsAvailable) {
new MessageSearchTask(xmppConnectionService, term, onSearchResultsAvailable).executeInBackground(); new MessageSearchTask(xmppConnectionService, term, uuid, onSearchResultsAvailable).executeInBackground();
} }
public static void cancelRunningTasks() { public static void cancelRunningTasks() {
@ -86,7 +88,7 @@ public class MessageSearchTask implements Runnable, Cancellable {
try { try {
final HashMap<String, Conversational> conversationCache = new HashMap<>(); final HashMap<String, Conversational> conversationCache = new HashMap<>();
final List<Message> result = new ArrayList<>(); final List<Message> result = new ArrayList<>();
cursor = xmppConnectionService.databaseBackend.getMessageSearchCursor(term); cursor = xmppConnectionService.databaseBackend.getMessageSearchCursor(term, uuid);
long dbTimer = SystemClock.elapsedRealtime(); long dbTimer = SystemClock.elapsedRealtime();
if (isCancelled) { if (isCancelled) {
Log.d(Config.LOGTAG, "canceled search task"); Log.d(Config.LOGTAG, "canceled search task");

View file

@ -615,8 +615,8 @@ public class XmppConnectionService extends Service {
return c != null && c.getMode() == Conversational.MODE_MULTI; return c != null && c.getMode() == Conversational.MODE_MULTI;
} }
public void search(List<String> term, OnSearchResultsAvailable onSearchResultsAvailable) { public void search(final List<String> term, final String uuid, final OnSearchResultsAvailable onSearchResultsAvailable) {
MessageSearchTask.search(this, term, onSearchResultsAvailable); MessageSearchTask.search(this, term, uuid, onSearchResultsAvailable);
} }
@Override @Override

View file

@ -1240,6 +1240,9 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
case R.id.attach_location: case R.id.attach_location:
handleAttachmentSelection(item); handleAttachmentSelection(item);
break; break;
case R.id.action_search:
startSearch();
break;
case R.id.action_archive: case R.id.action_archive:
activity.xmppConnectionService.archiveConversation(conversation); activity.xmppConnectionService.archiveConversation(conversation);
break; break;
@ -1289,6 +1292,12 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
return super.onOptionsItemSelected(item); 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() { private void returnToOngoingCall() {
final Optional<OngoingRtpSession> ongoingRtpSession = activity.xmppConnectionService.getJingleConnectionManager().getOngoingRtpConnection(conversation.getContact()); final Optional<OngoingRtpSession> ongoingRtpSession = activity.xmppConnectionService.getJingleConnectionManager().getOngoingRtpConnection(conversation.getContact());
if (ongoingRtpSession.isPresent()) { if (ongoingRtpSession.isPresent()) {

View file

@ -489,6 +489,18 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio
case R.id.action_scan_qr_code: case R.id.action_scan_qr_code:
UriHandlerActivity.scan(this); UriHandlerActivity.scan(this);
return true; 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); return super.onOptionsItemSelected(item);
} }

View file

@ -29,6 +29,7 @@
package eu.siacs.conversations.ui; package eu.siacs.conversations.ui;
import android.content.Intent;
import android.databinding.DataBindingUtil; import android.databinding.DataBindingUtil;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
@ -42,6 +43,8 @@ import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.EditText; import android.widget.EditText;
import com.google.common.base.Strings;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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 { public class SearchActivity extends XmppActivity implements TextWatcher, OnSearchResultsAvailable, MessageAdapter.OnContactPictureClicked {
private static final String EXTRA_SEARCH_TERM = "search-term"; private static final String EXTRA_SEARCH_TERM = "search-term";
public static final String EXTRA_CONVERSATION_UUID = "uuid";
private ActivitySearchBinding binding; private ActivitySearchBinding binding;
private MessageAdapter messageListAdapter; private MessageAdapter messageListAdapter;
private final List<Message> messages = new ArrayList<>(); private final List<Message> messages = new ArrayList<>();
private WeakReference<Message> selectedMessageReference = new WeakReference<>(null); private WeakReference<Message> selectedMessageReference = new WeakReference<>(null);
private String uuid;
private final ChangeWatcher<List<String>> currentSearch = new ChangeWatcher<>(); private final ChangeWatcher<List<String>> currentSearch = new ChangeWatcher<>();
private final PendingItem<String> pendingSearchTerm = new PendingItem<>(); private final PendingItem<String> pendingSearchTerm = new PendingItem<>();
private final PendingItem<List<String>> pendingSearch = new PendingItem<>(); private final PendingItem<List<String>> pendingSearch = new PendingItem<>();
@Override @Override
public void onCreate(final Bundle bundle) { 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); final String searchTerm = bundle == null ? null : bundle.getString(EXTRA_SEARCH_TERM);
if (searchTerm != null) { if (searchTerm != null) {
pendingSearchTerm.push(searchTerm); pendingSearchTerm.push(searchTerm);
@ -103,10 +110,10 @@ public class SearchActivity extends XmppActivity implements TextWatcher, OnSearc
final String term = pendingSearchTerm.pop(); final String term = pendingSearchTerm.pop();
if (term != null) { if (term != null) {
searchField.append(term); searchField.append(term);
List<String> searchTerm = FtsUtils.parse(term); final List<String> searchTerm = FtsUtils.parse(term);
if (xmppConnectionService != null) { if (xmppConnectionService != null) {
if (currentSearch.watch(searchTerm)) { if (currentSearch.watch(searchTerm)) {
xmppConnectionService.search(searchTerm, this); xmppConnectionService.search(searchTerm, uuid, this);
} }
} else { } else {
pendingSearch.push(searchTerm); pendingSearch.push(searchTerm);
@ -206,7 +213,7 @@ public class SearchActivity extends XmppActivity implements TextWatcher, OnSearc
void onBackendConnected() { void onBackendConnected() {
final List<String> searchTerm = pendingSearch.pop(); final List<String> searchTerm = pendingSearch.pop();
if (searchTerm != null && currentSearch.watch(searchTerm)) { 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; return;
} }
if (term.size() > 0) { if (term.size() > 0) {
xmppConnectionService.search(term, this); xmppConnectionService.search(term, uuid,this);
} else { } else {
MessageSearchTask.cancelRunningTasks(); MessageSearchTask.cancelRunningTasks();
this.messages.clear(); this.messages.clear();

View file

@ -2,11 +2,26 @@
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<item <item
android:id="@+id/action_scan_qr_code" android:id="@+id/action_scan_qr_code"
android:title="@string/scan_qr_code" android:icon="?attr/icon_scan_qr_code"
app:showAsAction="always"
android:orderInCategory="10" android:orderInCategory="10"
android:title="@string/scan_qr_code"
android:visible="@bool/show_qr_code_scan" 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 <item
android:id="@+id/action_accounts" android:id="@+id/action_accounts"
android:orderInCategory="90" android:orderInCategory="90"

View file

@ -99,6 +99,12 @@
android:orderInCategory="45" android:orderInCategory="45"
android:title="@string/invite_contact" android:title="@string/invite_contact"
app:showAsAction="never" /> 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 <item
android:id="@+id/action_clear_history" android:id="@+id/action_clear_history"
android:orderInCategory="50" android:orderInCategory="50"

View file

@ -32,6 +32,7 @@
<item <item
android:id="@+id/action_search" android:id="@+id/action_search"
android:title="@string/search_messages" android:title="@string/search_messages"
android:visible="@bool/show_individual_search_options"
android:orderInCategory="50" android:orderInCategory="50"
app:showAsAction="never"/> app:showAsAction="never"/>
</menu> </menu>

View file

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<bool name="show_qr_code_scan">false</bool> <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> </resources>

View file

@ -35,6 +35,8 @@
<bool name="never_send">false</bool> <bool name="never_send">false</bool>
<bool name="validate_hostname">false</bool> <bool name="validate_hostname">false</bool>
<bool name="show_qr_code_scan">true</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> <bool name="scroll_to_bottom">true</bool>
<string name="omemo_setting_default">default_on</string> <string name="omemo_setting_default">default_on</string>
<string name="default_font_size">small</string> <string name="default_font_size">small</string>

View file

@ -927,6 +927,8 @@
<string name="remove_from_favorites">Remove from favorites</string> <string name="remove_from_favorites">Remove from favorites</string>
<string name="gpx_track">GPX track</string> <string name="gpx_track">GPX track</string>
<string name="could_not_correct_message">Could not correct message</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"> <plurals name="view_users">
<item quantity="one">View %1$d Participant</item> <item quantity="one">View %1$d Participant</item>
<item quantity="other">View %1$d Participants</item> <item quantity="other">View %1$d Participants</item>