SearchActivity.java

  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.util.Color;
 51import eu.siacs.conversations.ui.util.Drawable;
 52
 53import static eu.siacs.conversations.ui.util.SoftKeyboardUtils.hideSoftKeyboard;
 54import static eu.siacs.conversations.ui.util.SoftKeyboardUtils.showKeyboard;
 55
 56public class SearchActivity extends XmppActivity implements TextWatcher {
 57
 58	private ActivitySearchBinding binding;
 59	private MessageAdapter messageListAdapter;
 60	private final List<Message> messages = new ArrayList<>();
 61
 62	@Override
 63	public void onCreate(final Bundle savedInstanceState) {
 64		super.onCreate(savedInstanceState);
 65		this.binding = DataBindingUtil.setContentView(this, R.layout.activity_search);
 66		setSupportActionBar((Toolbar) this.binding.toolbar);
 67		configureActionBar(getSupportActionBar());
 68		this.messageListAdapter = new MessageAdapter(this, this.messages);
 69		this.binding.searchResults.setAdapter(messageListAdapter);
 70	}
 71
 72	@Override
 73	public boolean onCreateOptionsMenu(final Menu menu) {
 74		getMenuInflater().inflate(R.menu.activity_search, menu);
 75		MenuItem searchActionMenuItem = menu.findItem(R.id.action_search);
 76		EditText searchField = searchActionMenuItem.getActionView().findViewById(R.id.search_field);
 77		searchField.addTextChangedListener(this);
 78		searchField.setHint(R.string.search_messages);
 79		showKeyboard(searchField);
 80		return super.onCreateOptionsMenu(menu);
 81	}
 82
 83	@Override
 84	public boolean onOptionsItemSelected(MenuItem item) {
 85		if (item.getItemId() == android.R.id.home) {
 86			hideSoftKeyboard(this);
 87		}
 88		return super.onOptionsItemSelected(item);
 89	}
 90
 91	@Override
 92	protected void refreshUiReal() {
 93
 94	}
 95
 96	@Override
 97	void onBackendConnected() {
 98
 99	}
100
101	private void changeBackground(boolean hasSearch, boolean hasResults) {
102		if (hasSearch) {
103			if (hasResults) {
104				binding.searchResults.setBackgroundColor(Color.get(this,R.attr.color_background_secondary));
105			} else {
106				binding.searchResults.setBackground(Drawable.get(this,R.attr.activity_background_no_results));
107			}
108		} else {
109			binding.searchResults.setBackground(Drawable.get(this,R.attr.activity_background_search));
110		}
111	}
112
113	@Override
114	public void beforeTextChanged(CharSequence s, int start, int count, int after) {
115
116	}
117
118	@Override
119	public void onTextChanged(CharSequence s, int start, int before, int count) {
120
121	}
122
123	@Override
124	public void afterTextChanged(Editable s) {
125		Log.d(Config.LOGTAG,"searching for "+s);
126	}
127
128}