blob: dae02b4f01b3bfc24fc926fee724dc6e319148f8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package org.wordpress.android.util;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.util.Patterns;
import java.util.regex.Pattern;
public class UserEmail {
/**
* Get primary account and return its name if it matches the email address pattern.
*
* @return primary account email address if it can be found or empty string else.
*/
public static String getPrimaryEmail(Context context) {
try {
AccountManager accountManager = AccountManager.get(context);
if (accountManager == null)
return "";
Account[] accounts = accountManager.getAccounts();
Pattern emailPattern = Patterns.EMAIL_ADDRESS;
for (Account account : accounts) {
// make sure account.name is an email address before adding to the list
if (emailPattern.matcher(account.name).matches()) {
return account.name;
}
}
return "";
} catch (SecurityException e) {
// exception will occur if app doesn't have GET_ACCOUNTS permission
return "";
}
}
}
|