From 303cc89b61b6f2aace584a7e9b9bf428260722ee Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 1 May 2018 10:33:21 +0200 Subject: [PATCH] persist search across device rotations --- .../conversations/ui/SearchActivity.java | 43 ++++++++++++++++--- .../conversations/ui/util/ChangeWatcher.java | 3 ++ .../siacs/conversations/utils/FtsUtils.java | 10 +++++ src/main/res/layout/actionview_search.xml | 2 +- 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/SearchActivity.java b/src/main/java/eu/siacs/conversations/ui/SearchActivity.java index 83f80d3fc..b54fdecaa 100644 --- a/src/main/java/eu/siacs/conversations/ui/SearchActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/SearchActivity.java @@ -63,6 +63,7 @@ import eu.siacs.conversations.ui.util.Color; import eu.siacs.conversations.ui.util.DateSeparator; import eu.siacs.conversations.ui.util.Drawable; import eu.siacs.conversations.ui.util.ListViewUtils; +import eu.siacs.conversations.ui.util.PendingItem; import eu.siacs.conversations.ui.util.ShareUtil; import eu.siacs.conversations.utils.FtsUtils; import eu.siacs.conversations.utils.MessageUtils; @@ -72,15 +73,23 @@ 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"; + private ActivitySearchBinding binding; private MessageAdapter messageListAdapter; private final List messages = new ArrayList<>(); private WeakReference selectedMessageReference = new WeakReference<>(null); private final ChangeWatcher> currentSearch = new ChangeWatcher<>(); + private final PendingItem pendingSearchTerm = new PendingItem<>(); + private final PendingItem> pendingSearch = new PendingItem<>(); @Override - public void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + public void onCreate(final Bundle bundle) { + final String searchTerm = bundle == null ? null : bundle.getString(EXTRA_SEARCH_TERM); + if (searchTerm != null) { + pendingSearchTerm.push(searchTerm); + } + super.onCreate(bundle); this.binding = DataBindingUtil.setContentView(this, R.layout.activity_search); setSupportActionBar((Toolbar) this.binding.toolbar); configureActionBar(getSupportActionBar()); @@ -93,8 +102,20 @@ public class SearchActivity extends XmppActivity implements TextWatcher, OnSearc @Override public boolean onCreateOptionsMenu(final Menu menu) { getMenuInflater().inflate(R.menu.activity_search, menu); - MenuItem searchActionMenuItem = menu.findItem(R.id.action_search); - EditText searchField = searchActionMenuItem.getActionView().findViewById(R.id.search_field); + final MenuItem searchActionMenuItem = menu.findItem(R.id.action_search); + final EditText searchField = searchActionMenuItem.getActionView().findViewById(R.id.search_field); + final String term = pendingSearchTerm.pop(); + if (term != null) { + searchField.append(term); + List searchTerm = FtsUtils.parse(term); + if (xmppConnectionService != null) { + if (currentSearch.watch(searchTerm)) { + xmppConnectionService.search(searchTerm, this); + } + } else { + pendingSearch.push(searchTerm); + } + } searchField.addTextChangedListener(this); searchField.setHint(R.string.search_messages); searchField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE); @@ -153,6 +174,15 @@ public class SearchActivity extends XmppActivity implements TextWatcher, OnSearc return super.onContextItemSelected(item); } + @Override + public void onSaveInstanceState(Bundle bundle) { + List term = currentSearch.get(); + if (term != null && term.size() > 0) { + bundle.putString(EXTRA_SEARCH_TERM,FtsUtils.toUserEnteredString(term)); + } + super.onSaveInstanceState(bundle); + } + private void quote(Message message) { switchToConversationAndQuote(wrap(message.getConversation()), MessageUtils.prepareQuote(message)); } @@ -176,7 +206,10 @@ public class SearchActivity extends XmppActivity implements TextWatcher, OnSearc @Override void onBackendConnected() { - + final List searchTerm = pendingSearch.pop(); + if (searchTerm != null && currentSearch.watch(searchTerm)) { + xmppConnectionService.search(searchTerm, this); + } } private void changeBackground(boolean hasSearch, boolean hasResults) { diff --git a/src/main/java/eu/siacs/conversations/ui/util/ChangeWatcher.java b/src/main/java/eu/siacs/conversations/ui/util/ChangeWatcher.java index 259236ae8..fc0c6eb66 100644 --- a/src/main/java/eu/siacs/conversations/ui/util/ChangeWatcher.java +++ b/src/main/java/eu/siacs/conversations/ui/util/ChangeWatcher.java @@ -44,4 +44,7 @@ public class ChangeWatcher { } } + public T get() { + return object; + } } diff --git a/src/main/java/eu/siacs/conversations/utils/FtsUtils.java b/src/main/java/eu/siacs/conversations/utils/FtsUtils.java index 7ffb41bed..783ca476d 100644 --- a/src/main/java/eu/siacs/conversations/utils/FtsUtils.java +++ b/src/main/java/eu/siacs/conversations/utils/FtsUtils.java @@ -109,4 +109,14 @@ public class FtsUtils { } } + public static String toUserEnteredString(List term) { + final StringBuilder builder = new StringBuilder(); + for(String part : term) { + if (builder.length() != 0) { + builder.append(' '); + } + builder.append(part); + } + return builder.toString(); + } } diff --git a/src/main/res/layout/actionview_search.xml b/src/main/res/layout/actionview_search.xml index 9cee924e0..90783b776 100644 --- a/src/main/res/layout/actionview_search.xml +++ b/src/main/res/layout/actionview_search.xml @@ -12,7 +12,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="true" - android:imeOptions="actionSearch" + android:imeOptions="flagNoExtractUi|actionSearch" android:inputType="textEmailAddress|textNoSuggestions"/> \ No newline at end of file