SearchActivity.java

  1/*
  2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
  3 *
  4 * Redistribution and use in source and binary forms, with or without modification,
  5 * are permitted provided that the following conditions are met:
  6 *
  7 * 1. Redistributions of source code must retain the above copyright notice, this
  8 * list of conditions and the following disclaimer.
  9 *
 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
 11 * this list of conditions and the following disclaimer in the documentation and/or
 12 * other materials provided with the distribution.
 13 *
 14 * 3. Neither the name of the copyright holder nor the names of its contributors
 15 * may be used to endorse or promote products derived from this software without
 16 * specific prior written permission.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28 */
 29
 30package eu.siacs.conversations.ui;
 31
 32import android.databinding.DataBindingUtil;
 33import android.os.Bundle;
 34import android.support.v7.widget.Toolbar;
 35import android.text.Editable;
 36import android.text.InputType;
 37import android.text.TextWatcher;
 38import android.view.ContextMenu;
 39import android.view.Menu;
 40import android.view.MenuItem;
 41import android.view.View;
 42import android.widget.AdapterView;
 43import android.widget.EditText;
 44
 45import java.lang.ref.WeakReference;
 46import java.util.ArrayList;
 47import java.util.List;
 48
 49import eu.siacs.conversations.R;
 50import eu.siacs.conversations.databinding.ActivitySearchBinding;
 51import eu.siacs.conversations.entities.Contact;
 52import eu.siacs.conversations.entities.Conversation;
 53import eu.siacs.conversations.entities.Conversational;
 54import eu.siacs.conversations.entities.Message;
 55import eu.siacs.conversations.services.MessageSearchTask;
 56import eu.siacs.conversations.ui.adapter.MessageAdapter;
 57import eu.siacs.conversations.ui.interfaces.OnSearchResultsAvailable;
 58import eu.siacs.conversations.ui.util.ChangeWatcher;
 59import eu.siacs.conversations.ui.util.DateSeparator;
 60import eu.siacs.conversations.ui.util.StyledAttributes;
 61import eu.siacs.conversations.ui.util.ListViewUtils;
 62import eu.siacs.conversations.ui.util.PendingItem;
 63import eu.siacs.conversations.ui.util.ShareUtil;
 64import eu.siacs.conversations.utils.FtsUtils;
 65import eu.siacs.conversations.utils.MessageUtils;
 66
 67import static eu.siacs.conversations.ui.util.SoftKeyboardUtils.hideSoftKeyboard;
 68import static eu.siacs.conversations.ui.util.SoftKeyboardUtils.showKeyboard;
 69
 70public class SearchActivity extends XmppActivity implements TextWatcher, OnSearchResultsAvailable, MessageAdapter.OnContactPictureClicked {
 71
 72	private static final String EXTRA_SEARCH_TERM = "search-term";
 73
 74	private ActivitySearchBinding binding;
 75	private MessageAdapter messageListAdapter;
 76	private final List<Message> messages = new ArrayList<>();
 77	private WeakReference<Message> selectedMessageReference = new WeakReference<>(null);
 78	private final ChangeWatcher<List<String>> currentSearch = new ChangeWatcher<>();
 79	private final PendingItem<String> pendingSearchTerm = new PendingItem<>();
 80	private final PendingItem<List<String>> pendingSearch = new PendingItem<>();
 81
 82	@Override
 83	public void onCreate(final Bundle bundle) {
 84		final String searchTerm = bundle == null ? null : bundle.getString(EXTRA_SEARCH_TERM);
 85		if (searchTerm != null) {
 86			pendingSearchTerm.push(searchTerm);
 87		}
 88		super.onCreate(bundle);
 89		this.binding = DataBindingUtil.setContentView(this, R.layout.activity_search);
 90		setSupportActionBar((Toolbar) this.binding.toolbar);
 91		configureActionBar(getSupportActionBar());
 92		this.messageListAdapter = new MessageAdapter(this, this.messages);
 93		this.messageListAdapter.setOnContactPictureClicked(this);
 94		this.binding.searchResults.setAdapter(messageListAdapter);
 95		registerForContextMenu(this.binding.searchResults);
 96	}
 97
 98	@Override
 99	public boolean onCreateOptionsMenu(final Menu menu) {
100		getMenuInflater().inflate(R.menu.activity_search, menu);
101		final MenuItem searchActionMenuItem = menu.findItem(R.id.action_search);
102		final EditText searchField = searchActionMenuItem.getActionView().findViewById(R.id.search_field);
103		final String term = pendingSearchTerm.pop();
104		if (term != null) {
105			searchField.append(term);
106			List<String> searchTerm = FtsUtils.parse(term);
107			if (xmppConnectionService != null) {
108				if (currentSearch.watch(searchTerm)) {
109					xmppConnectionService.search(searchTerm, this);
110				}
111			} else {
112				pendingSearch.push(searchTerm);
113			}
114		}
115		searchField.addTextChangedListener(this);
116		searchField.setHint(R.string.search_messages);
117		searchField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE);
118		if (term == null) {
119			showKeyboard(searchField);
120		}
121		return super.onCreateOptionsMenu(menu);
122	}
123
124	@Override
125	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
126		AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) menuInfo;
127		final Message message = this.messages.get(acmi.position);
128		this.selectedMessageReference = new WeakReference<>(message);
129		getMenuInflater().inflate(R.menu.search_result_context, menu);
130		MenuItem copy = menu.findItem(R.id.copy_message);
131		MenuItem quote = menu.findItem(R.id.quote_message);
132		MenuItem copyUrl = menu.findItem(R.id.copy_url);
133		if (message.isGeoUri()) {
134			copy.setVisible(false);
135			quote.setVisible(false);
136		} else {
137			copyUrl.setVisible(false);
138		}
139		super.onCreateContextMenu(menu, v, menuInfo);
140	}
141
142	@Override
143	public boolean onOptionsItemSelected(MenuItem item) {
144		if (item.getItemId() == android.R.id.home) {
145			hideSoftKeyboard(this);
146		}
147		return super.onOptionsItemSelected(item);
148	}
149
150	@Override
151	public boolean onContextItemSelected(MenuItem item) {
152		final Message message = selectedMessageReference.get();
153		if (message != null) {
154			switch (item.getItemId()) {
155				case R.id.open_conversation:
156					switchToConversation(wrap(message.getConversation()));
157					break;
158				case R.id.share_with:
159					ShareUtil.share(this, message);
160					break;
161				case R.id.copy_message:
162					ShareUtil.copyToClipboard(this, message);
163					break;
164				case R.id.copy_url:
165					ShareUtil.copyUrlToClipboard(this, message);
166					break;
167				case R.id.quote_message:
168					quote(message);
169					break;
170			}
171		}
172		return super.onContextItemSelected(item);
173	}
174
175	@Override
176	public void onSaveInstanceState(Bundle bundle) {
177		List<String> term = currentSearch.get();
178		if (term != null && term.size() > 0) {
179			bundle.putString(EXTRA_SEARCH_TERM,FtsUtils.toUserEnteredString(term));
180		}
181		super.onSaveInstanceState(bundle);
182	}
183
184	private void quote(Message message) {
185		switchToConversationAndQuote(wrap(message.getConversation()), MessageUtils.prepareQuote(message));
186	}
187
188	private Conversation wrap(Conversational conversational) {
189		if (conversational instanceof Conversation) {
190			return (Conversation) conversational;
191		} else {
192			return xmppConnectionService.findOrCreateConversation(conversational.getAccount(),
193					conversational.getJid(),
194					conversational.getMode() == Conversational.MODE_MULTI,
195					true,
196					true);
197		}
198	}
199
200	@Override
201	protected void refreshUiReal() {
202
203	}
204
205	@Override
206	void onBackendConnected() {
207		final List<String> searchTerm = pendingSearch.pop();
208		if (searchTerm != null && currentSearch.watch(searchTerm)) {
209			xmppConnectionService.search(searchTerm, this);
210		}
211	}
212
213	private void changeBackground(boolean hasSearch, boolean hasResults) {
214		if (hasSearch) {
215			if (hasResults) {
216				binding.searchResults.setBackgroundColor(StyledAttributes.getColor(this, R.attr.color_background_secondary));
217			} else {
218				binding.searchResults.setBackground(StyledAttributes.getDrawable(this, R.attr.activity_background_no_results));
219			}
220		} else {
221			binding.searchResults.setBackground(StyledAttributes.getDrawable(this, R.attr.activity_background_search));
222		}
223	}
224
225	@Override
226	public void beforeTextChanged(CharSequence s, int start, int count, int after) {
227
228	}
229
230	@Override
231	public void onTextChanged(CharSequence s, int start, int before, int count) {
232
233	}
234
235	@Override
236	public void afterTextChanged(Editable s) {
237		final List<String> term = FtsUtils.parse(s.toString().trim());
238		if (!currentSearch.watch(term)) {
239			return;
240		}
241		if (term.size() > 0) {
242			xmppConnectionService.search(term, this);
243		} else {
244			MessageSearchTask.cancelRunningTasks();
245			this.messages.clear();
246			messageListAdapter.setHighlightedTerm(null);
247			messageListAdapter.notifyDataSetChanged();
248			changeBackground(false, false);
249		}
250	}
251
252	@Override
253	public void onSearchResultsAvailable(List<String> term, List<Message> messages) {
254		runOnUiThread(() -> {
255			this.messages.clear();
256			messageListAdapter.setHighlightedTerm(term);
257			DateSeparator.addAll(messages);
258			this.messages.addAll(messages);
259			messageListAdapter.notifyDataSetChanged();
260			changeBackground(true, messages.size() > 0);
261			ListViewUtils.scrollToBottom(this.binding.searchResults);
262		});
263	}
264
265	@Override
266	public void onContactPictureClicked(Message message) {
267		String fingerprint;
268		if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
269			fingerprint = "pgp";
270		} else {
271			fingerprint = message.getFingerprint();
272		}
273		if (message.getStatus() == Message.STATUS_RECEIVED) {
274			final Contact contact = message.getContact();
275			if (contact != null) {
276				if (contact.isSelf()) {
277					switchToAccount(message.getConversation().getAccount(), fingerprint);
278				} else {
279					switchToContactDetails(contact, fingerprint);
280				}
281			}
282		} else {
283			switchToAccount(message.getConversation().getAccount(), fingerprint);
284		}
285	}
286}