do some performance optimizations on jid host suggestions. fixes #3174

This commit is contained in:
Daniel Gultsch 2018-09-14 14:00:26 +02:00
parent e0266d0efb
commit 63f203c1d1

View file

@ -7,76 +7,70 @@ import android.widget.Filter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.Locale; import java.util.Locale;
public class KnownHostsAdapter extends ArrayAdapter<String> { public class KnownHostsAdapter extends ArrayAdapter<String> {
private ArrayList<String> domains; private ArrayList<String> domains;
private Filter domainFilter = new Filter() { private Filter domainFilter = new Filter() {
@Override @Override
protected FilterResults performFiltering(CharSequence constraint) { protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) { final ArrayList<String> suggestions = new ArrayList<>();
ArrayList<String> suggestions = new ArrayList<>(); final String[] split = constraint == null ? new String[0] : constraint.toString().split("@");
final String[] split = constraint.toString().split("@"); if (split.length == 1) {
if (split.length == 1) { final String local = split[0].toLowerCase(Locale.ENGLISH);
for (String domain : domains) { for (String domain : domains) {
suggestions.add(split[0].toLowerCase(Locale suggestions.add(local + "@" + domain);
.getDefault()) + "@" + domain); }
} } else if (split.length == 2) {
} else if (split.length == 2) { final String localPart = split[0].toLowerCase(Locale.ENGLISH);
for (String domain : domains) { final String domainPart = split[1].toLowerCase(Locale.ENGLISH);
if (domain.contentEquals(split[1])) { if (domains.contains(domainPart)) {
suggestions.clear(); return new FilterResults();
break; }
} else if (domain.contains(split[1])) { for (String domain : domains) {
suggestions.add(split[0].toLowerCase(Locale if (domain.contains(domainPart)) {
.getDefault()) + "@" + domain); suggestions.add(localPart + "@" + domain);
} }
} }
} else { } else {
return new FilterResults(); return new FilterResults();
} }
FilterResults filterResults = new FilterResults(); FilterResults filterResults = new FilterResults();
filterResults.values = suggestions; filterResults.values = suggestions;
filterResults.count = suggestions.size(); filterResults.count = suggestions.size();
return filterResults; return filterResults;
} else { }
return new FilterResults();
}
}
@Override @Override
protected void publishResults(CharSequence constraint, FilterResults results) { protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayList filteredList = (ArrayList) results.values; ArrayList filteredList = (ArrayList) results.values;
if (results.count > 0) { if (results.count > 0) {
clear(); clear();
for (Object c : filteredList) { addAll(filteredList);
add((String) c); notifyDataSetChanged();
} }
notifyDataSetChanged(); }
} };
}
};
public KnownHostsAdapter(Context context, int viewResourceId, Collection<String> mKnownHosts) { public KnownHostsAdapter(Context context, int viewResourceId, Collection<String> mKnownHosts) {
super(context, viewResourceId, new ArrayList<>()); super(context, viewResourceId, new ArrayList<>());
domains = new ArrayList<>(mKnownHosts); domains = new ArrayList<>(mKnownHosts);
} }
public KnownHostsAdapter(Context context, int viewResourceId) { public KnownHostsAdapter(Context context, int viewResourceId) {
super(context, viewResourceId, new ArrayList<>()); super(context, viewResourceId, new ArrayList<>());
domains = new ArrayList<>(); domains = new ArrayList<>();
} }
public void refresh(Collection<String> knownHosts) { public void refresh(Collection<String> knownHosts) {
domains = new ArrayList<>(knownHosts); domains = new ArrayList<>(knownHosts);
notifyDataSetChanged(); notifyDataSetChanged();
} }
@Override @Override
@NonNull @NonNull
public Filter getFilter() { public Filter getFilter() {
return domainFilter; return domainFilter;
} }
} }