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