From 63f203c1d1dd596663ee6f7fafd608695cef38ae Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 14 Sep 2018 14:00:26 +0200 Subject: [PATCH] do some performance optimizations on jid host suggestions. fixes #3174 --- .../ui/adapter/KnownHostsAdapter.java | 120 +++++++++--------- 1 file changed, 57 insertions(+), 63 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/KnownHostsAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/KnownHostsAdapter.java index 9107c7788..2f2b1e7ac 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/KnownHostsAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/KnownHostsAdapter.java @@ -7,76 +7,70 @@ import android.widget.Filter; import java.util.ArrayList; import java.util.Collection; -import java.util.List; import java.util.Locale; public class KnownHostsAdapter extends ArrayAdapter { - private ArrayList domains; - private Filter domainFilter = new Filter() { + private ArrayList domains; + private Filter domainFilter = new Filter() { - @Override - protected FilterResults performFiltering(CharSequence constraint) { - if (constraint != null) { - ArrayList suggestions = new ArrayList<>(); - 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 + protected FilterResults performFiltering(CharSequence constraint) { + final ArrayList suggestions = new ArrayList<>(); + final String[] split = constraint == null ? new String[0] : constraint.toString().split("@"); + if (split.length == 1) { + final String local = split[0].toLowerCase(Locale.ENGLISH); + for (String domain : domains) { + suggestions.add(local + "@" + domain); + } + } else if (split.length == 2) { + final String localPart = split[0].toLowerCase(Locale.ENGLISH); + final String domainPart = split[1].toLowerCase(Locale.ENGLISH); + if (domains.contains(domainPart)) { + return new FilterResults(); + } + for (String domain : domains) { + if (domain.contains(domainPart)) { + suggestions.add(localPart + "@" + domain); + } + } + } else { + return new FilterResults(); + } + FilterResults filterResults = new FilterResults(); + filterResults.values = suggestions; + filterResults.count = suggestions.size(); + return filterResults; + } - @Override - protected void publishResults(CharSequence constraint, FilterResults results) { - ArrayList filteredList = (ArrayList) results.values; - if (results.count > 0) { - clear(); - for (Object c : filteredList) { - add((String) c); - } - notifyDataSetChanged(); - } - } - }; + @Override + protected void publishResults(CharSequence constraint, FilterResults results) { + ArrayList filteredList = (ArrayList) results.values; + if (results.count > 0) { + clear(); + addAll(filteredList); + notifyDataSetChanged(); + } + } + }; - public KnownHostsAdapter(Context context, int viewResourceId, Collection mKnownHosts) { - super(context, viewResourceId, new ArrayList<>()); - domains = new ArrayList<>(mKnownHosts); - } + public KnownHostsAdapter(Context context, int viewResourceId, Collection mKnownHosts) { + super(context, viewResourceId, new ArrayList<>()); + domains = new ArrayList<>(mKnownHosts); + } - public KnownHostsAdapter(Context context, int viewResourceId) { - super(context, viewResourceId, new ArrayList<>()); - domains = new ArrayList<>(); - } + public KnownHostsAdapter(Context context, int viewResourceId) { + super(context, viewResourceId, new ArrayList<>()); + domains = new ArrayList<>(); + } - public void refresh(Collection knownHosts) { - domains = new ArrayList<>(knownHosts); - notifyDataSetChanged(); - } + public void refresh(Collection knownHosts) { + domains = new ArrayList<>(knownHosts); + notifyDataSetChanged(); + } - @Override - @NonNull - public Filter getFilter() { - return domainFilter; - } + @Override + @NonNull + public Filter getFilter() { + return domainFilter; + } }