Implemented conversation search.
You can now search for messages within a specific conversation.pull/1/head
parent
10631d7e71
commit
9f04c28bfd
@ -0,0 +1,76 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<org.thoughtcrime.securesms.components.ConversationSearchBottomBar
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/conversation_search_nav"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?conversation_background"
|
||||
android:visibility="gone"
|
||||
tools:visibility="visible"
|
||||
tools:parentTag="android.support.constraint.ConstraintLayout">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/conversation_search_position"
|
||||
style="@style/Signal.Text.Body"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="16dp"
|
||||
android:layout_marginRight="16dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@+id/conversation_search_up"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:text="37 of 73" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/conversation_search_up"
|
||||
android:layout_width="40dp"
|
||||
android:layout_height="40dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_marginRight="8dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:background="?selectableItemBackgroundBorderless"
|
||||
android:padding="8dp"
|
||||
android:src="@drawable/ic_keyboard_arrow_up_white_36dp"
|
||||
android:tint="@color/signal_primary"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@+id/conversation_search_down"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/conversation_search_down"
|
||||
android:layout_width="40dp"
|
||||
android:layout_height="40dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_marginRight="8dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:background="?selectableItemBackgroundBorderless"
|
||||
android:padding="8dp"
|
||||
android:src="@drawable/ic_keyboard_arrow_down_white_24dp"
|
||||
android:tint="@color/signal_primary"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<com.pnikosis.materialishprogress.ProgressWheel
|
||||
android:id="@+id/conversation_search_progress_wheel"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_gravity="center"
|
||||
android:layout_marginTop="0dp"
|
||||
android:layout_marginBottom="0dp"
|
||||
android:indeterminate="true"
|
||||
android:padding="8dp"
|
||||
android:background="?conversation_background"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:matProg_barColor="@color/core_grey_25"
|
||||
app:matProg_progressIndeterminate="true" />
|
||||
|
||||
</org.thoughtcrime.securesms.components.ConversationSearchBottomBar>
|
@ -0,0 +1,88 @@
|
||||
package org.thoughtcrime.securesms.components;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.constraint.ConstraintLayout;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
|
||||
/**
|
||||
* Bottom navigation bar shown in the {@link org.thoughtcrime.securesms.conversation.ConversationActivity}
|
||||
* when the user is searching within a conversation. Shows details about the results and allows the
|
||||
* user to move between them.
|
||||
*/
|
||||
public class ConversationSearchBottomBar extends ConstraintLayout {
|
||||
|
||||
private View searchDown;
|
||||
private View searchUp;
|
||||
private TextView searchPositionText;
|
||||
private View progressWheel;
|
||||
|
||||
private EventListener eventListener;
|
||||
|
||||
|
||||
public ConversationSearchBottomBar(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public ConversationSearchBottomBar(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onFinishInflate() {
|
||||
super.onFinishInflate();
|
||||
|
||||
this.searchUp = findViewById(R.id.conversation_search_up);
|
||||
this.searchDown = findViewById(R.id.conversation_search_down);
|
||||
this.searchPositionText = findViewById(R.id.conversation_search_position);
|
||||
this.progressWheel = findViewById(R.id.conversation_search_progress_wheel);
|
||||
}
|
||||
|
||||
public void setData(int position, int count) {
|
||||
progressWheel.setVisibility(GONE);
|
||||
|
||||
searchUp.setOnClickListener(v -> {
|
||||
if (eventListener != null) {
|
||||
eventListener.onSearchMoveUpPressed();
|
||||
}
|
||||
});
|
||||
|
||||
searchDown.setOnClickListener(v -> {
|
||||
if (eventListener != null) {
|
||||
eventListener.onSearchMoveDownPressed();
|
||||
}
|
||||
});
|
||||
|
||||
if (count > 0) {
|
||||
searchPositionText.setText(getResources().getString(R.string.ConversationActivity_search_position, position + 1, count));
|
||||
} else {
|
||||
searchPositionText.setText(R.string.ConversationActivity_no_results);
|
||||
}
|
||||
|
||||
setViewEnabled(searchUp, position < (count - 1));
|
||||
setViewEnabled(searchDown, position > 0);
|
||||
}
|
||||
|
||||
public void showLoading() {
|
||||
progressWheel.setVisibility(VISIBLE);
|
||||
}
|
||||
|
||||
private void setViewEnabled(@NonNull View view, boolean enabled) {
|
||||
view.setEnabled(enabled);
|
||||
view.setAlpha(enabled ? 1f : 0.25f);
|
||||
}
|
||||
|
||||
public void setEventListener(@Nullable EventListener eventListener) {
|
||||
this.eventListener = eventListener;
|
||||
}
|
||||
|
||||
public interface EventListener {
|
||||
void onSearchMoveUpPressed();
|
||||
void onSearchMoveDownPressed();
|
||||
}
|
||||
}
|
@ -0,0 +1,147 @@
|
||||
package org.thoughtcrime.securesms.conversation;
|
||||
|
||||
import android.app.Application;
|
||||
import android.arch.lifecycle.AndroidViewModel;
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import org.thoughtcrime.securesms.contacts.ContactAccessor;
|
||||
import org.thoughtcrime.securesms.database.CursorList;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.search.SearchRepository;
|
||||
import org.thoughtcrime.securesms.search.model.MessageResult;
|
||||
import org.thoughtcrime.securesms.util.CloseableLiveData;
|
||||
import org.thoughtcrime.securesms.util.Debouncer;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.util.List;
|
||||
|
||||
public class ConversationSearchViewModel extends AndroidViewModel {
|
||||
|
||||
private final SearchRepository searchRepository;
|
||||
private final CloseableLiveData<SearchResult> result;
|
||||
private final Debouncer debouncer;
|
||||
|
||||
private boolean firstSearch;
|
||||
private boolean searchOpen;
|
||||
private String activeQuery;
|
||||
private long activeThreadId;
|
||||
|
||||
public ConversationSearchViewModel(@NonNull Application application) {
|
||||
super(application);
|
||||
Context context = application.getApplicationContext();
|
||||
result = new CloseableLiveData<>();
|
||||
debouncer = new Debouncer(500);
|
||||
searchRepository = new SearchRepository(context,
|
||||
DatabaseFactory.getSearchDatabase(context),
|
||||
DatabaseFactory.getContactsDatabase(context),
|
||||
DatabaseFactory.getThreadDatabase(context),
|
||||
ContactAccessor.getInstance(),
|
||||
AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
||||
LiveData<SearchResult> getSearchResults() {
|
||||
return result;
|
||||
}
|
||||
|
||||
void onQueryUpdated(@NonNull String query, long threadId) {
|
||||
if (firstSearch && query.length() < 2) {
|
||||
result.postValue(new SearchResult(CursorList.emptyList(), 0));
|
||||
return;
|
||||
}
|
||||
|
||||
if (query.equals(activeQuery)) {
|
||||
return;
|
||||
}
|
||||
|
||||
updateQuery(query, threadId);
|
||||
}
|
||||
|
||||
void onMissingResult() {
|
||||
if (activeQuery != null) {
|
||||
updateQuery(activeQuery, activeThreadId);
|
||||
}
|
||||
}
|
||||
|
||||
void onMoveUp() {
|
||||
debouncer.clear();
|
||||
|
||||
CursorList<MessageResult> messages = (CursorList<MessageResult>) result.getValue().getResults();
|
||||
int position = Math.min(result.getValue().getPosition() + 1, messages.size() - 1);
|
||||
|
||||
result.setValue(new SearchResult(messages, position), false);
|
||||
}
|
||||
|
||||
void onMoveDown() {
|
||||
debouncer.clear();
|
||||
|
||||
CursorList<MessageResult> messages = (CursorList<MessageResult>) result.getValue().getResults();
|
||||
int position = Math.max(result.getValue().getPosition() - 1, 0);
|
||||
|
||||
result.setValue(new SearchResult(messages, position), false);
|
||||
}
|
||||
|
||||
|
||||
void onSearchOpened() {
|
||||
searchOpen = true;
|
||||
firstSearch = true;
|
||||
}
|
||||
|
||||
void onSearchClosed() {
|
||||
searchOpen = false;
|
||||
debouncer.clear();
|
||||
result.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCleared() {
|
||||
super.onCleared();
|
||||
result.close();
|
||||
}
|
||||
|
||||
private void updateQuery(@NonNull String query, long threadId) {
|
||||
activeQuery = query;
|
||||
activeThreadId = threadId;
|
||||
|
||||
debouncer.publish(() -> {
|
||||
firstSearch = false;
|
||||
|
||||
searchRepository.query(query, threadId, messages -> {
|
||||
Util.runOnMain(() -> {
|
||||
if (searchOpen && query.equals(activeQuery)) {
|
||||
result.setValue(new SearchResult(messages, 0));
|
||||
} else {
|
||||
messages.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
static class SearchResult implements Closeable {
|
||||
|
||||
private final CursorList<MessageResult> results;
|
||||
private final int position;
|
||||
|
||||
SearchResult(CursorList<MessageResult> results, int position) {
|
||||
this.results = results;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public List<MessageResult> getResults() {
|
||||
return results;
|
||||
}
|
||||
|
||||
public int getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
results.close();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import android.arch.lifecycle.MutableLiveData;
|
||||
|
||||
import java.io.Closeable;
|
||||
|
||||
/**
|
||||
* Implementation of {@link android.arch.lifecycle.LiveData} that will handle closing the contained
|
||||
* {@link Closeable} when the value changes.
|
||||
*/
|
||||
public class CloseableLiveData<E extends Closeable> extends MutableLiveData<E> {
|
||||
|
||||
@Override
|
||||
public void setValue(E value) {
|
||||
setValue(value, true);
|
||||
}
|
||||
|
||||
public void setValue(E value, boolean closePrevious) {
|
||||
E previous = getValue();
|
||||
|
||||
if (previous != null && closePrevious) {
|
||||
Util.close(previous);
|
||||
}
|
||||
|
||||
super.setValue(value);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
E value = getValue();
|
||||
|
||||
if (value != null) {
|
||||
Util.close(value);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.text.Spannable;
|
||||
import android.text.SpannableString;
|
||||
import android.text.Spanned;
|
||||
import android.text.TextUtils;
|
||||
import android.text.style.CharacterStyle;
|
||||
|
||||
import com.annimon.stream.Stream;
|
||||
|
||||
import org.whispersystems.libsignal.util.Pair;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class SearchUtil {
|
||||
|
||||
public static Spannable getHighlightedSpan(@NonNull Locale locale,
|
||||
@NonNull StyleFactory styleFactory,
|
||||
@Nullable String text,
|
||||
@Nullable String highlight)
|
||||
{
|
||||
if (TextUtils.isEmpty(text)) {
|
||||
return new SpannableString("");
|
||||
}
|
||||
|
||||
text = text.replaceAll("\n", " ");
|
||||
|
||||
return getHighlightedSpan(locale, styleFactory, new SpannableString(text), highlight);
|
||||
}
|
||||
|
||||
public static Spannable getHighlightedSpan(@NonNull Locale locale,
|
||||
@NonNull StyleFactory styleFactory,
|
||||
@Nullable Spannable text,
|
||||
@Nullable String highlight)
|
||||
{
|
||||
if (TextUtils.isEmpty(text)) {
|
||||
return new SpannableString("");
|
||||
}
|
||||
|
||||
|
||||
if (TextUtils.isEmpty(highlight)) {
|
||||
return text;
|
||||
}
|
||||
|
||||
List<Pair<Integer, Integer>> ranges = getHighlightRanges(locale, text.toString(), highlight);
|
||||
SpannableString spanned = new SpannableString(text);
|
||||
|
||||
for (Pair<Integer, Integer> range : ranges) {
|
||||
spanned.setSpan(styleFactory.create(), range.first(), range.second(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
|
||||
}
|
||||
|
||||
return spanned;
|
||||
}
|
||||
|
||||
static List<Pair<Integer, Integer>> getHighlightRanges(@NonNull Locale locale,
|
||||
@NonNull String text,
|
||||
@NonNull String highlight)
|
||||
{
|
||||
String normalizedText = text.toLowerCase(locale);
|
||||
String normalizedHighlight = highlight.toLowerCase(locale);
|
||||
List<String> highlightTokens = Stream.of(normalizedHighlight.split("\\s")).filter(s -> s.trim().length() > 0).toList();
|
||||
List<String> textTokens = Stream.of(normalizedText.split("\\s")).filter(s -> s.trim().length() > 0).toList();
|
||||
|
||||
List<Pair<Integer, Integer>> ranges = new LinkedList<>();
|
||||
|
||||
int textListIndex = 0;
|
||||
int textCharIndex = 0;
|
||||
|
||||
for (String highlightToken : highlightTokens) {
|
||||
for (int i = textListIndex; i < textTokens.size(); i++) {
|
||||
if (textTokens.get(i).startsWith(highlightToken)) {
|
||||
textListIndex = i + 1;
|
||||
ranges.add(new Pair<>(textCharIndex, textCharIndex + highlightToken.length()));
|
||||
textCharIndex += textTokens.get(i).length() + 1;
|
||||
break;
|
||||
}
|
||||
textCharIndex += textTokens.get(i).length() + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (ranges.size() != highlightTokens.size()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return ranges;
|
||||
}
|
||||
|
||||
public interface StyleFactory {
|
||||
CharacterStyle create();
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.whispersystems.libsignal.util.Pair;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class SearchUtilTest {
|
||||
|
||||
private static final Locale LOCALE = Locale.ENGLISH;
|
||||
|
||||
@Test
|
||||
public void getHighlightRanges_singleHighlightToken() {
|
||||
String text = "abc";
|
||||
String highlight = "a";
|
||||
List<Pair<Integer, Integer>> result = SearchUtil.getHighlightRanges(LOCALE, text, highlight);
|
||||
|
||||
assertEquals(Arrays.asList(new Pair<>(0, 1)), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getHighlightRanges_multipleHighlightTokens() {
|
||||
String text = "a bc";
|
||||
String highlight = "a b";
|
||||
List<Pair<Integer, Integer>> result = SearchUtil.getHighlightRanges(LOCALE, text, highlight);
|
||||
|
||||
assertEquals(Arrays.asList(new Pair<>(0, 1), new Pair<>(2, 3)), result);
|
||||
|
||||
|
||||
text = "abc def";
|
||||
highlight = "ab de";
|
||||
result = SearchUtil.getHighlightRanges(LOCALE, text, highlight);
|
||||
|
||||
assertEquals(Arrays.asList(new Pair<>(0, 2), new Pair<>(4, 6)), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getHighlightRanges_onlyHighlightPrefixes() {
|
||||
String text = "abc";
|
||||
String highlight = "b";
|
||||
List<Pair<Integer, Integer>> result = SearchUtil.getHighlightRanges(LOCALE, text, highlight);
|
||||
|
||||
assertTrue(result.isEmpty());
|
||||
|
||||
text = "abc";
|
||||
highlight = "c";
|
||||
result = SearchUtil.getHighlightRanges(LOCALE, text, highlight);
|
||||
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getHighlightRanges_resultNotInFirstToken() {
|
||||
String text = "abc def ghi";
|
||||
String highlight = "gh";
|
||||
List<Pair<Integer, Integer>> result = SearchUtil.getHighlightRanges(LOCALE, text, highlight);
|
||||
|
||||
assertEquals(Arrays.asList(new Pair<>(8, 10)), result);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue