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.Message;
50import eu.siacs.conversations.services.MessageSearchTask;
51import eu.siacs.conversations.ui.adapter.MessageAdapter;
52import eu.siacs.conversations.ui.interfaces.OnSearchResultsAvailable;
53import eu.siacs.conversations.ui.util.Color;
54import eu.siacs.conversations.ui.util.Drawable;
55import eu.siacs.conversations.ui.util.ListViewUtils;
56
57import static eu.siacs.conversations.ui.util.SoftKeyboardUtils.hideSoftKeyboard;
58import static eu.siacs.conversations.ui.util.SoftKeyboardUtils.showKeyboard;
59
60public class SearchActivity extends XmppActivity implements TextWatcher, OnSearchResultsAvailable {
61
62 private ActivitySearchBinding binding;
63 private MessageAdapter messageListAdapter;
64 private final List<Message> messages = new ArrayList<>();
65
66 @Override
67 public void onCreate(final Bundle savedInstanceState) {
68 super.onCreate(savedInstanceState);
69 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_search);
70 setSupportActionBar((Toolbar) this.binding.toolbar);
71 configureActionBar(getSupportActionBar());
72 this.messageListAdapter = new MessageAdapter(this, this.messages);
73 this.binding.searchResults.setAdapter(messageListAdapter);
74 }
75
76 @Override
77 public boolean onCreateOptionsMenu(final Menu menu) {
78 getMenuInflater().inflate(R.menu.activity_search, menu);
79 MenuItem searchActionMenuItem = menu.findItem(R.id.action_search);
80 EditText searchField = searchActionMenuItem.getActionView().findViewById(R.id.search_field);
81 searchField.addTextChangedListener(this);
82 searchField.setHint(R.string.search_messages);
83 searchField.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE);
84 showKeyboard(searchField);
85 return super.onCreateOptionsMenu(menu);
86 }
87
88 @Override
89 public boolean onOptionsItemSelected(MenuItem item) {
90 if (item.getItemId() == android.R.id.home) {
91 hideSoftKeyboard(this);
92 }
93 return super.onOptionsItemSelected(item);
94 }
95
96 @Override
97 protected void refreshUiReal() {
98
99 }
100
101 @Override
102 void onBackendConnected() {
103
104 }
105
106 private void changeBackground(boolean hasSearch, boolean hasResults) {
107 if (hasSearch) {
108 if (hasResults) {
109 binding.searchResults.setBackgroundColor(Color.get(this,R.attr.color_background_secondary));
110 } else {
111 binding.searchResults.setBackground(Drawable.get(this,R.attr.activity_background_no_results));
112 }
113 } else {
114 binding.searchResults.setBackground(Drawable.get(this,R.attr.activity_background_search));
115 }
116 }
117
118 @Override
119 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
120
121 }
122
123 @Override
124 public void onTextChanged(CharSequence s, int start, int before, int count) {
125
126 }
127
128 @Override
129 public void afterTextChanged(Editable s) {
130 String term = s.toString().trim();
131 if (term.length() > 0) {
132 xmppConnectionService.search(s.toString().trim(), this);
133 } else {
134 MessageSearchTask.cancelRunningTasks();
135 this.messages.clear();
136 messageListAdapter.notifyDataSetChanged();
137 changeBackground(false, false);
138 }
139 }
140
141 @Override
142 public void onSearchResultsAvailable(String term, List<Message> messages) {
143 runOnUiThread(() -> {
144 this.messages.clear();
145 this.messages.addAll(messages);
146 messageListAdapter.notifyDataSetChanged();
147 changeBackground(true, messages.size() > 0);
148 ListViewUtils.scrollToBottom(this.binding.searchResults);
149 });
150 }
151}