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.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE);
125 if (term == null) {
126 showKeyboard(searchField);
127 }
128 return super.onCreateOptionsMenu(menu);
129 }
130
131 @Override
132 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
133 AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) menuInfo;
134 final Message message = this.messages.get(acmi.position);
135 this.selectedMessageReference = new WeakReference<>(message);
136 getMenuInflater().inflate(R.menu.search_result_context, menu);
137 MenuItem copy = menu.findItem(R.id.copy_message);
138 MenuItem quote = menu.findItem(R.id.quote_message);
139 MenuItem copyUrl = menu.findItem(R.id.copy_url);
140 if (message.isGeoUri()) {
141 copy.setVisible(false);
142 quote.setVisible(false);
143 } else {
144 copyUrl.setVisible(false);
145 }
146 super.onCreateContextMenu(menu, v, menuInfo);
147 }
148
149 @Override
150 public boolean onOptionsItemSelected(MenuItem item) {
151 if (item.getItemId() == android.R.id.home) {
152 hideSoftKeyboard(this);
153 }
154 return super.onOptionsItemSelected(item);
155 }
156
157 @Override
158 public boolean onContextItemSelected(MenuItem item) {
159 final Message message = selectedMessageReference.get();
160 if (message != null) {
161 switch (item.getItemId()) {
162 case R.id.open_conversation:
163 switchToConversation(wrap(message.getConversation()));
164 break;
165 case R.id.share_with:
166 ShareUtil.share(this, message);
167 break;
168 case R.id.copy_message:
169 ShareUtil.copyToClipboard(this, message);
170 break;
171 case R.id.copy_url:
172 ShareUtil.copyUrlToClipboard(this, message);
173 break;
174 case R.id.quote_message:
175 quote(message);
176 break;
177 }
178 }
179 return super.onContextItemSelected(item);
180 }
181
182 @Override
183 public void onSaveInstanceState(Bundle bundle) {
184 List<String> term = currentSearch.get();
185 if (term != null && term.size() > 0) {
186 bundle.putString(EXTRA_SEARCH_TERM,FtsUtils.toUserEnteredString(term));
187 }
188 super.onSaveInstanceState(bundle);
189 }
190
191 private void quote(Message message) {
192 switchToConversationAndQuote(wrap(message.getConversation()), MessageUtils.prepareQuote(message));
193 }
194
195 private Conversation wrap(Conversational conversational) {
196 if (conversational instanceof Conversation) {
197 return (Conversation) conversational;
198 } else {
199 return xmppConnectionService.findOrCreateConversation(conversational.getAccount(),
200 conversational.getJid(),
201 conversational.getMode() == Conversational.MODE_MULTI,
202 true,
203 true);
204 }
205 }
206
207 @Override
208 protected void refreshUiReal() {
209
210 }
211
212 @Override
213 void onBackendConnected() {
214 final List<String> searchTerm = pendingSearch.pop();
215 if (searchTerm != null && currentSearch.watch(searchTerm)) {
216 xmppConnectionService.search(searchTerm, uuid,this);
217 }
218 }
219
220 private void changeBackground(boolean hasSearch, boolean hasResults) {
221 if (hasSearch) {
222 if (hasResults) {
223 binding.searchResults.setBackgroundColor(StyledAttributes.getColor(this, R.attr.color_background_secondary));
224 } else {
225 binding.searchResults.setBackground(StyledAttributes.getDrawable(this, R.attr.activity_background_no_results));
226 }
227 } else {
228 binding.searchResults.setBackground(StyledAttributes.getDrawable(this, R.attr.activity_background_search));
229 }
230 }
231
232 @Override
233 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
234
235 }
236
237 @Override
238 public void onTextChanged(CharSequence s, int start, int before, int count) {
239
240 }
241
242 @Override
243 public void afterTextChanged(Editable s) {
244 final List<String> term = FtsUtils.parse(s.toString().trim());
245 if (!currentSearch.watch(term)) {
246 return;
247 }
248 if (term.size() > 0) {
249 xmppConnectionService.search(term, uuid,this);
250 } else {
251 MessageSearchTask.cancelRunningTasks();
252 this.messages.clear();
253 messageListAdapter.setHighlightedTerm(null);
254 messageListAdapter.notifyDataSetChanged();
255 changeBackground(false, false);
256 }
257 }
258
259 @Override
260 public void onSearchResultsAvailable(List<String> term, List<Message> messages) {
261 runOnUiThread(() -> {
262 this.messages.clear();
263 messageListAdapter.setHighlightedTerm(term);
264 DateSeparator.addAll(messages);
265 this.messages.addAll(messages);
266 messageListAdapter.notifyDataSetChanged();
267 changeBackground(true, messages.size() > 0);
268 ListViewUtils.scrollToBottom(this.binding.searchResults);
269 });
270 }
271
272 @Override
273 public void onContactPictureClicked(Message message) {
274 String fingerprint;
275 if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
276 fingerprint = "pgp";
277 } else {
278 fingerprint = message.getFingerprint();
279 }
280 if (message.getStatus() == Message.STATUS_RECEIVED) {
281 final Contact contact = message.getContact();
282 if (contact != null) {
283 if (contact.isSelf()) {
284 switchToAccount(message.getConversation().getAccount(), fingerprint);
285 } else {
286 switchToContactDetails(contact, fingerprint);
287 }
288 }
289 } else {
290 switchToAccount(message.getConversation().getAccount(), fingerprint);
291 }
292 }
293}