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