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.content.Intent;
 33import android.databinding.DataBindingUtil;
 34import android.os.Bundle;
 35import android.support.v7.widget.Toolbar;
 36import android.text.Editable;
 37import android.text.InputType;
 38import android.text.TextWatcher;
 39import android.view.ContextMenu;
 40import android.view.Menu;
 41import android.view.MenuItem;
 42import android.view.View;
 43import android.widget.AdapterView;
 44import android.widget.EditText;
 45
 46import com.google.common.base.Strings;
 47
 48import java.lang.ref.WeakReference;
 49import java.util.ArrayList;
 50import java.util.List;
 51
 52import eu.siacs.conversations.R;
 53import eu.siacs.conversations.databinding.ActivitySearchBinding;
 54import eu.siacs.conversations.entities.Contact;
 55import eu.siacs.conversations.entities.Conversation;
 56import eu.siacs.conversations.entities.Conversational;
 57import eu.siacs.conversations.entities.Message;
 58import eu.siacs.conversations.services.MessageSearchTask;
 59import eu.siacs.conversations.ui.adapter.MessageAdapter;
 60import eu.siacs.conversations.ui.interfaces.OnSearchResultsAvailable;
 61import eu.siacs.conversations.ui.util.ChangeWatcher;
 62import eu.siacs.conversations.ui.util.DateSeparator;
 63import eu.siacs.conversations.ui.util.StyledAttributes;
 64import eu.siacs.conversations.ui.util.ListViewUtils;
 65import eu.siacs.conversations.ui.util.PendingItem;
 66import eu.siacs.conversations.ui.util.ShareUtil;
 67import eu.siacs.conversations.utils.FtsUtils;
 68import eu.siacs.conversations.utils.MessageUtils;
 69
 70import static eu.siacs.conversations.ui.util.SoftKeyboardUtils.hideSoftKeyboard;
 71import static eu.siacs.conversations.ui.util.SoftKeyboardUtils.showKeyboard;
 72
 73public class SearchActivity extends XmppActivity implements TextWatcher, OnSearchResultsAvailable, MessageAdapter.OnContactPictureClicked {
 74
 75	private static final String EXTRA_SEARCH_TERM = "search-term";
 76	public static final String EXTRA_CONVERSATION_UUID = "uuid";
 77
 78	private ActivitySearchBinding binding;
 79	private MessageAdapter messageListAdapter;
 80	private final List<Message> messages = new ArrayList<>();
 81	private WeakReference<Message> selectedMessageReference = new WeakReference<>(null);
 82	private String uuid;
 83	private final ChangeWatcher<List<String>> currentSearch = new ChangeWatcher<>();
 84	private final PendingItem<String> pendingSearchTerm = new PendingItem<>();
 85	private final PendingItem<List<String>> pendingSearch = new PendingItem<>();
 86
 87	@Override
 88	public void onCreate(final Bundle bundle) {
 89		final Intent intent = getIntent();
 90		this.uuid = intent == null ? null : Strings.emptyToNull(intent.getStringExtra(EXTRA_CONVERSATION_UUID));
 91		final String searchTerm = bundle == null ? null : bundle.getString(EXTRA_SEARCH_TERM);
 92		if (searchTerm != null) {
 93			pendingSearchTerm.push(searchTerm);
 94		}
 95		super.onCreate(bundle);
 96		this.binding = DataBindingUtil.setContentView(this, R.layout.activity_search);
 97		setSupportActionBar((Toolbar) this.binding.toolbar);
 98		configureActionBar(getSupportActionBar());
 99		this.messageListAdapter = new MessageAdapter(this, this.messages);
100		this.messageListAdapter.setOnContactPictureClicked(this);
101		this.binding.searchResults.setAdapter(messageListAdapter);
102		registerForContextMenu(this.binding.searchResults);
103	}
104
105	@Override
106	public boolean onCreateOptionsMenu(final Menu menu) {
107		getMenuInflater().inflate(R.menu.activity_search, menu);
108		final MenuItem searchActionMenuItem = menu.findItem(R.id.action_search);
109		final EditText searchField = searchActionMenuItem.getActionView().findViewById(R.id.search_field);
110		final String term = pendingSearchTerm.pop();
111		if (term != null) {
112			searchField.append(term);
113			final List<String> searchTerm = FtsUtils.parse(term);
114			if (xmppConnectionService != null) {
115				if (currentSearch.watch(searchTerm)) {
116					xmppConnectionService.search(searchTerm, uuid, this);
117				}
118			} else {
119				pendingSearch.push(searchTerm);
120			}
121		}
122		searchField.addTextChangedListener(this);
123		searchField.setHint(R.string.search_messages);
124		searchField.setContentDescription(getString(R.string.search_messages));
125		searchField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE);
126		if (term == null) {
127			showKeyboard(searchField);
128		}
129		return super.onCreateOptionsMenu(menu);
130	}
131
132	@Override
133	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
134		AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) menuInfo;
135		final Message message = this.messages.get(acmi.position);
136		this.selectedMessageReference = new WeakReference<>(message);
137		getMenuInflater().inflate(R.menu.search_result_context, menu);
138		MenuItem copy = menu.findItem(R.id.copy_message);
139		MenuItem quote = menu.findItem(R.id.quote_message);
140		MenuItem copyUrl = menu.findItem(R.id.copy_url);
141		if (message.isGeoUri()) {
142			copy.setVisible(false);
143			quote.setVisible(false);
144		} else {
145			copyUrl.setVisible(false);
146		}
147		super.onCreateContextMenu(menu, v, menuInfo);
148	}
149
150	@Override
151	public boolean onOptionsItemSelected(MenuItem item) {
152		if (item.getItemId() == android.R.id.home) {
153			hideSoftKeyboard(this);
154		}
155		return super.onOptionsItemSelected(item);
156	}
157
158	@Override
159	public boolean onContextItemSelected(MenuItem item) {
160		final Message message = selectedMessageReference.get();
161		if (message != null) {
162			switch (item.getItemId()) {
163				case R.id.open_conversation:
164					switchToConversation(wrap(message.getConversation()));
165					break;
166				case R.id.share_with:
167					ShareUtil.share(this, message);
168					break;
169				case R.id.copy_message:
170					ShareUtil.copyToClipboard(this, message);
171					break;
172				case R.id.copy_url:
173					ShareUtil.copyUrlToClipboard(this, message);
174					break;
175				case R.id.quote_message:
176					quote(message);
177					break;
178			}
179		}
180		return super.onContextItemSelected(item);
181	}
182
183	@Override
184	public void onSaveInstanceState(Bundle bundle) {
185		List<String> term = currentSearch.get();
186		if (term != null && term.size() > 0) {
187			bundle.putString(EXTRA_SEARCH_TERM,FtsUtils.toUserEnteredString(term));
188		}
189		super.onSaveInstanceState(bundle);
190	}
191
192	private void quote(Message message) {
193		switchToConversationAndQuote(wrap(message.getConversation()), MessageUtils.prepareQuote(message));
194	}
195
196	private Conversation wrap(Conversational conversational) {
197		if (conversational instanceof Conversation) {
198			return (Conversation) conversational;
199		} else {
200			return xmppConnectionService.findOrCreateConversation(conversational.getAccount(),
201					conversational.getJid(),
202					conversational.getMode() == Conversational.MODE_MULTI,
203					true,
204					true);
205		}
206	}
207
208	@Override
209	protected void refreshUiReal() {
210
211	}
212
213	@Override
214	void onBackendConnected() {
215		final List<String> searchTerm = pendingSearch.pop();
216		if (searchTerm != null && currentSearch.watch(searchTerm)) {
217			xmppConnectionService.search(searchTerm, uuid,this);
218		}
219	}
220
221	private void changeBackground(boolean hasSearch, boolean hasResults) {
222		if (hasSearch) {
223			if (hasResults) {
224				binding.searchResults.setBackgroundColor(StyledAttributes.getColor(this, R.attr.color_background_secondary));
225			} else {
226				binding.searchResults.setBackground(StyledAttributes.getDrawable(this, R.attr.activity_background_no_results));
227			}
228		} else {
229			binding.searchResults.setBackground(StyledAttributes.getDrawable(this, R.attr.activity_background_search));
230		}
231	}
232
233	@Override
234	public void beforeTextChanged(CharSequence s, int start, int count, int after) {
235
236	}
237
238	@Override
239	public void onTextChanged(CharSequence s, int start, int before, int count) {
240
241	}
242
243	@Override
244	public void afterTextChanged(Editable s) {
245		final List<String> term = FtsUtils.parse(s.toString().trim());
246		if (!currentSearch.watch(term)) {
247			return;
248		}
249		if (term.size() > 0) {
250			xmppConnectionService.search(term, uuid,this);
251		} else {
252			MessageSearchTask.cancelRunningTasks();
253			this.messages.clear();
254			messageListAdapter.setHighlightedTerm(null);
255			messageListAdapter.notifyDataSetChanged();
256			changeBackground(false, false);
257		}
258	}
259
260	@Override
261	public void onSearchResultsAvailable(List<String> term, List<Message> messages) {
262		runOnUiThread(() -> {
263			this.messages.clear();
264			messageListAdapter.setHighlightedTerm(term);
265			DateSeparator.addAll(messages);
266			this.messages.addAll(messages);
267			messageListAdapter.notifyDataSetChanged();
268			changeBackground(true, messages.size() > 0);
269			ListViewUtils.scrollToBottom(this.binding.searchResults);
270		});
271	}
272
273	@Override
274	public void onContactPictureClicked(Message message) {
275		String fingerprint;
276		if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
277			fingerprint = "pgp";
278		} else {
279			fingerprint = message.getFingerprint();
280		}
281		if (message.getStatus() == Message.STATUS_RECEIVED) {
282			final Contact contact = message.getContact();
283			if (contact != null) {
284				if (contact.isSelf()) {
285					switchToAccount(message.getConversation().getAccount(), fingerprint);
286				} else {
287					switchToContactDetails(contact, fingerprint);
288				}
289			}
290		} else {
291			switchToAccount(message.getConversation().getAccount(), fingerprint);
292		}
293	}
294}