Improve way of getting possible names by filtering out emails

This commit is contained in:
Dominik Schürmann 2015-03-06 11:14:27 +01:00
parent f31ada30d0
commit a03f6d35d6

View File

@ -43,9 +43,11 @@ import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher;
public class ContactHelper { public class ContactHelper {
@ -54,6 +56,17 @@ public class ContactHelper {
public static List<String> getPossibleUserEmails(Context context) { public static List<String> getPossibleUserEmails(Context context) {
Set<String> accountMails = getAccountEmails(context); Set<String> accountMails = getAccountEmails(context);
accountMails.addAll(getMainProfileContactEmails(context)); accountMails.addAll(getMainProfileContactEmails(context));
// remove items that are not an email
Iterator<String> it = accountMails.iterator();
while (it.hasNext()) {
String email = it.next();
Matcher emailMatcher = Patterns.EMAIL_ADDRESS.matcher(email);
if (!emailMatcher.matches()) {
it.remove();
}
}
// now return the Set (without duplicates) as a List // now return the Set (without duplicates) as a List
return new ArrayList<>(accountMails); return new ArrayList<>(accountMails);
} }
@ -62,6 +75,17 @@ public class ContactHelper {
Set<String> accountMails = getAccountEmails(context); Set<String> accountMails = getAccountEmails(context);
Set<String> names = getContactNamesFromEmails(context, accountMails); Set<String> names = getContactNamesFromEmails(context, accountMails);
names.addAll(getMainProfileContactName(context)); names.addAll(getMainProfileContactName(context));
// remove items that are an email
Iterator<String> it = names.iterator();
while (it.hasNext()) {
String email = it.next();
Matcher emailMatcher = Patterns.EMAIL_ADDRESS.matcher(email);
if (emailMatcher.matches()) {
it.remove();
}
}
return new ArrayList<>(names); return new ArrayList<>(names);
} }