conversations-classic/src/main/java/eu/siacs/conversations/ui/adapter/KnownHostsAdapter.java

72 lines
1.9 KiB
Java
Raw Normal View History

2014-10-22 16:38:44 +00:00
package eu.siacs.conversations.ui.adapter;
import android.content.Context;
2018-03-03 11:27:46 +00:00
import android.support.annotation.NonNull;
2014-10-22 16:38:44 +00:00
import android.widget.ArrayAdapter;
import android.widget.Filter;
2015-07-20 12:26:29 +00:00
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
2014-10-22 16:38:44 +00:00
public class KnownHostsAdapter extends ArrayAdapter<String> {
private ArrayList<String> domains;
private Filter domainFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) {
2015-10-12 10:36:54 +00:00
ArrayList<String> suggestions = new ArrayList<>();
2014-10-22 16:38:44 +00:00
final String[] split = constraint.toString().split("@");
if (split.length == 1) {
for (String domain : domains) {
suggestions.add(split[0].toLowerCase(Locale
.getDefault()) + "@" + domain);
}
} else if (split.length == 2) {
for (String domain : domains) {
if (domain.contentEquals(split[1])) {
suggestions.clear();
break;
} else if (domain.contains(split[1])) {
suggestions.add(split[0].toLowerCase(Locale
.getDefault()) + "@" + domain);
}
}
} else {
return new FilterResults();
}
FilterResults filterResults = new FilterResults();
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;
} else {
return new FilterResults();
}
}
@Override
2018-03-03 11:27:46 +00:00
protected void publishResults(CharSequence constraint, FilterResults results) {
2014-10-22 16:38:44 +00:00
ArrayList filteredList = (ArrayList) results.values;
2018-03-03 11:27:46 +00:00
if (results.count > 0) {
2014-10-22 16:38:44 +00:00
clear();
for (Object c : filteredList) {
add((String) c);
}
notifyDataSetChanged();
}
}
};
2015-10-12 10:36:54 +00:00
public KnownHostsAdapter(Context context, int viewResourceId, List<String> mKnownHosts) {
2018-03-03 11:27:46 +00:00
super(context, viewResourceId, new ArrayList<>());
2015-10-12 10:36:54 +00:00
domains = new ArrayList<>(mKnownHosts);
2014-10-22 16:38:44 +00:00
}
@Override
2018-03-03 11:27:46 +00:00
@NonNull
2014-10-22 16:38:44 +00:00
public Filter getFilter() {
return domainFilter;
}
}