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;
|
2018-03-11 11:13:56 +00:00
|
|
|
import java.util.Collection;
|
2018-10-25 18:50:10 +00:00
|
|
|
import java.util.Collections;
|
2015-07-20 12:26:29 +00:00
|
|
|
import java.util.Locale;
|
2018-10-25 18:50:10 +00:00
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.Config;
|
2015-07-20 12:26:29 +00:00
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public class KnownHostsAdapter extends ArrayAdapter<String> {
|
2018-10-25 18:50:10 +00:00
|
|
|
|
2019-12-06 10:16:33 +00:00
|
|
|
private static Pattern E164_PATTERN = Pattern.compile("^\\+[1-9]\\d{1,14}$");
|
2018-10-25 18:50:10 +00:00
|
|
|
|
2018-09-14 12:00:26 +00:00
|
|
|
private ArrayList<String> domains;
|
|
|
|
private Filter domainFilter = new Filter() {
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2018-09-14 12:00:26 +00:00
|
|
|
@Override
|
|
|
|
protected FilterResults performFiltering(CharSequence constraint) {
|
|
|
|
final ArrayList<String> 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);
|
2018-10-25 18:50:10 +00:00
|
|
|
if (Config.QUICKSY_DOMAIN != null && E164_PATTERN.matcher(local).matches()) {
|
|
|
|
suggestions.add(local + '@' + Config.QUICKSY_DOMAIN);
|
|
|
|
} else {
|
|
|
|
for (String domain : domains) {
|
|
|
|
suggestions.add(local + '@' + domain);
|
|
|
|
}
|
2018-09-14 12:00:26 +00:00
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2018-09-14 12:00:26 +00:00
|
|
|
@Override
|
|
|
|
protected void publishResults(CharSequence constraint, FilterResults results) {
|
|
|
|
ArrayList filteredList = (ArrayList) results.values;
|
|
|
|
if (results.count > 0) {
|
|
|
|
clear();
|
|
|
|
addAll(filteredList);
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2018-09-14 12:00:26 +00:00
|
|
|
public KnownHostsAdapter(Context context, int viewResourceId, Collection<String> mKnownHosts) {
|
|
|
|
super(context, viewResourceId, new ArrayList<>());
|
|
|
|
domains = new ArrayList<>(mKnownHosts);
|
2018-10-25 18:50:10 +00:00
|
|
|
Collections.sort(domains);
|
2018-09-14 12:00:26 +00:00
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2018-09-14 12:00:26 +00:00
|
|
|
public KnownHostsAdapter(Context context, int viewResourceId) {
|
|
|
|
super(context, viewResourceId, new ArrayList<>());
|
|
|
|
domains = new ArrayList<>();
|
|
|
|
}
|
2018-03-14 09:07:52 +00:00
|
|
|
|
2018-09-14 12:00:26 +00:00
|
|
|
public void refresh(Collection<String> knownHosts) {
|
|
|
|
domains = new ArrayList<>(knownHosts);
|
2018-10-25 18:50:10 +00:00
|
|
|
Collections.sort(domains);
|
2018-09-14 12:00:26 +00:00
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
2018-03-14 09:07:52 +00:00
|
|
|
|
2018-09-14 12:00:26 +00:00
|
|
|
@Override
|
|
|
|
@NonNull
|
|
|
|
public Filter getFilter() {
|
|
|
|
return domainFilter;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|