FairEmail/app/src/main/java/eu/faircode/email/SearchViewEx.java

145 lines
5.6 KiB
Java
Raw Normal View History

2019-08-12 16:33:03 +00:00
package eu.faircode.email;
import android.content.Context;
2019-08-12 16:40:19 +00:00
import android.content.SharedPreferences;
2019-08-12 16:33:03 +00:00
import android.database.Cursor;
import android.database.MatrixCursor;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MenuItem;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;
import androidx.appcompat.widget.SearchView;
import androidx.cursoradapter.widget.SimpleCursorAdapter;
import androidx.lifecycle.LifecycleOwner;
2019-08-12 16:40:19 +00:00
import androidx.preference.PreferenceManager;
2019-08-12 16:33:03 +00:00
public class SearchViewEx extends SearchView {
private String _searching = null;
public SearchViewEx(Context context) {
super(context);
}
public SearchViewEx(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SearchViewEx(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
void setup(LifecycleOwner owner, MenuItem menuSearch, String searching, ISearch intf) {
_searching = searching;
setQueryHint(getContext().getString(R.string.title_search));
if (!TextUtils.isEmpty(_searching)) {
menuSearch.expandActionView();
setQuery(_searching, false);
}
AutoCompleteTextView autoCompleteTextView = findViewById(androidx.appcompat.R.id.search_src_text);
autoCompleteTextView.setThreshold(0);
2019-08-12 16:40:19 +00:00
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
2019-08-12 16:33:03 +00:00
setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
_searching = newText;
intf.onSave(_searching);
if (TextUtils.isEmpty(newText)) {
MatrixCursor cursor = new MatrixCursor(new String[]{"_id", "suggestion"});
2019-08-12 16:40:19 +00:00
String last_search = prefs.getString("last_search", null);
if (!TextUtils.isEmpty(last_search))
cursor.addRow(new Object[]{-1, last_search});
2019-08-12 16:33:03 +00:00
String prefix = getContext().getString(R.string.title_search_special_prefix);
2019-08-12 16:40:19 +00:00
cursor.addRow(new Object[]{-2, prefix + ":" + getContext().getString(R.string.title_search_special_unseen)});
cursor.addRow(new Object[]{-3, prefix + ":" + getContext().getString(R.string.title_search_special_flagged)});
cursor.addRow(new Object[]{-4, prefix + ":" + getContext().getString(R.string.title_search_special_snoozed)});
2019-08-12 16:33:03 +00:00
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getContext(),
R.layout.search_suggestion,
cursor,
new String[]{"suggestion"},
new int[]{android.R.id.text1},
0);
setSuggestionsAdapter(adapter);
adapter.notifyDataSetChanged();
} else {
Bundle args = new Bundle();
args.putString("query", newText);
new SimpleTask<Cursor>() {
@Override
protected Cursor onExecute(Context context, Bundle args) {
String query = args.getString("query");
DB db = DB.getInstance(context);
return db.message().getSuggestions("%" + query + "%");
}
@Override
protected void onExecuted(Bundle args, Cursor cursor) {
Log.i("Suggestions=" + cursor.getCount());
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getContext(),
R.layout.search_suggestion,
cursor,
new String[]{"suggestion"},
new int[]{android.R.id.text1},
0);
setSuggestionsAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
protected void onException(Bundle args, Throwable ex) {
ToastEx.makeText(getContext(), Helper.formatThrowable(ex), Toast.LENGTH_LONG).show();
}
}.execute(getContext(), owner, args, "messages:suggestions");
}
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
_searching = null;
2019-08-12 16:40:19 +00:00
intf.onSave(query);
2019-08-12 16:33:03 +00:00
menuSearch.collapseActionView();
intf.onSearch(query);
2019-08-12 16:40:19 +00:00
prefs.edit().putString("last_search", query).apply();
2019-08-12 16:33:03 +00:00
return true;
}
});
setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionSelect(int position) {
return false;
}
@Override
public boolean onSuggestionClick(int position) {
Cursor cursor = (Cursor) getSuggestionsAdapter().getItem(position);
2019-08-13 11:47:57 +00:00
long id = cursor.getInt(0);
setQuery(cursor.getString(1), id != -1);
return (id == -1);
2019-08-12 16:33:03 +00:00
}
});
}
interface ISearch {
void onSave(String query);
void onSearch(String query);
}
}