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;
}
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) {

View File

@ -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");

View File

@ -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

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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();

View File

@ -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>

View File

@ -99,6 +99,12 @@
android:orderInCategory="45"
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"
@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>