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.util.Log;
39import android.view.Menu;
40import android.view.MenuItem;
41import android.widget.EditText;
42
43import java.util.ArrayList;
44import java.util.List;
45
46import eu.siacs.conversations.Config;
47import eu.siacs.conversations.R;
48import eu.siacs.conversations.databinding.ActivitySearchBinding;
49import eu.siacs.conversations.entities.Contact;
50import eu.siacs.conversations.entities.Message;
51import eu.siacs.conversations.services.MessageSearchTask;
52import eu.siacs.conversations.ui.adapter.MessageAdapter;
53import eu.siacs.conversations.ui.interfaces.OnSearchResultsAvailable;
54import eu.siacs.conversations.ui.util.Color;
55import eu.siacs.conversations.ui.util.Drawable;
56import eu.siacs.conversations.ui.util.ListViewUtils;
57
58import static eu.siacs.conversations.ui.util.SoftKeyboardUtils.hideSoftKeyboard;
59import static eu.siacs.conversations.ui.util.SoftKeyboardUtils.showKeyboard;
60
61public class SearchActivity extends XmppActivity implements TextWatcher, OnSearchResultsAvailable, MessageAdapter.OnContactPictureClicked {
62
63 private ActivitySearchBinding binding;
64 private MessageAdapter messageListAdapter;
65 private final List<Message> messages = new ArrayList<>();
66
67 @Override
68 public void onCreate(final Bundle savedInstanceState) {
69 super.onCreate(savedInstanceState);
70 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_search);
71 setSupportActionBar((Toolbar) this.binding.toolbar);
72 configureActionBar(getSupportActionBar());
73 this.messageListAdapter = new MessageAdapter(this, this.messages);
74 this.messageListAdapter.setOnContactPictureClicked(this);
75 this.binding.searchResults.setAdapter(messageListAdapter);
76 }
77
78 @Override
79 public boolean onCreateOptionsMenu(final Menu menu) {
80 getMenuInflater().inflate(R.menu.activity_search, menu);
81 MenuItem searchActionMenuItem = menu.findItem(R.id.action_search);
82 EditText searchField = searchActionMenuItem.getActionView().findViewById(R.id.search_field);
83 searchField.addTextChangedListener(this);
84 searchField.setHint(R.string.search_messages);
85 searchField.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE);
86 showKeyboard(searchField);
87 return super.onCreateOptionsMenu(menu);
88 }
89
90 @Override
91 public boolean onOptionsItemSelected(MenuItem item) {
92 if (item.getItemId() == android.R.id.home) {
93 hideSoftKeyboard(this);
94 }
95 return super.onOptionsItemSelected(item);
96 }
97
98 @Override
99 protected void refreshUiReal() {
100
101 }
102
103 @Override
104 void onBackendConnected() {
105
106 }
107
108 private void changeBackground(boolean hasSearch, boolean hasResults) {
109 if (hasSearch) {
110 if (hasResults) {
111 binding.searchResults.setBackgroundColor(Color.get(this,R.attr.color_background_secondary));
112 } else {
113 binding.searchResults.setBackground(Drawable.get(this,R.attr.activity_background_no_results));
114 }
115 } else {
116 binding.searchResults.setBackground(Drawable.get(this,R.attr.activity_background_search));
117 }
118 }
119
120 @Override
121 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
122
123 }
124
125 @Override
126 public void onTextChanged(CharSequence s, int start, int before, int count) {
127
128 }
129
130 @Override
131 public void afterTextChanged(Editable s) {
132 String term = s.toString().trim();
133 if (term.length() > 0) {
134 xmppConnectionService.search(s.toString().trim(), this);
135 } else {
136 MessageSearchTask.cancelRunningTasks();
137 this.messages.clear();
138 messageListAdapter.notifyDataSetChanged();
139 changeBackground(false, false);
140 }
141 }
142
143 @Override
144 public void onSearchResultsAvailable(String term, List<Message> messages) {
145 runOnUiThread(() -> {
146 this.messages.clear();
147 this.messages.addAll(messages);
148 messageListAdapter.notifyDataSetChanged();
149 changeBackground(true, messages.size() > 0);
150 ListViewUtils.scrollToBottom(this.binding.searchResults);
151 });
152 }
153
154 @Override
155 public void onContactPictureClicked(Message message) {
156 String fingerprint;
157 if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
158 fingerprint = "pgp";
159 } else {
160 fingerprint = message.getFingerprint();
161 }
162 if (message.getStatus() == Message.STATUS_RECEIVED) {
163 final Contact contact = message.getContact();
164 if (contact != null) {
165 if (contact.isSelf()) {
166 switchToAccount(message.getConversation().getAccount(), fingerprint);
167 } else {
168 switchToContactDetails(contact, fingerprint);
169 }
170 }
171 } else {
172 switchToAccount(message.getConversation().getAccount(), fingerprint);
173 }
174 }
175}