|
23 | 23 | import android.accounts.AccountManager;
|
24 | 24 | import android.accounts.AccountManagerFuture;
|
25 | 25 | import android.accounts.AccountsException;
|
| 26 | +import android.accounts.AuthenticatorDescription; |
26 | 27 | import android.accounts.AuthenticatorException;
|
27 | 28 | import android.accounts.OperationCanceledException;
|
28 | 29 | import android.app.Activity;
|
@@ -50,11 +51,40 @@ public class AccountUtils {
|
50 | 51 |
|
51 | 52 | private static final String TAG = "AccountUtils";
|
52 | 53 |
|
| 54 | + private static boolean AUTHENTICATOR_CHECKED; |
| 55 | + |
| 56 | + private static boolean HAS_AUTHENTICATOR; |
| 57 | + |
53 | 58 | private static class AuthenticatorConflictException extends IOException {
|
54 | 59 |
|
55 | 60 | private static final long serialVersionUID = 641279204734869183L;
|
56 | 61 | }
|
57 | 62 |
|
| 63 | + /** |
| 64 | + * Verify authenticator registered for account type matches the package name |
| 65 | + * of this application |
| 66 | + * |
| 67 | + * @param manager |
| 68 | + * @return true is authenticator registered, false otherwise |
| 69 | + */ |
| 70 | + public static boolean hasAuthenticator(final AccountManager manager) { |
| 71 | + if (!AUTHENTICATOR_CHECKED) { |
| 72 | + final AuthenticatorDescription[] types = manager |
| 73 | + .getAuthenticatorTypes(); |
| 74 | + if (types != null && types.length > 0) |
| 75 | + for (AuthenticatorDescription descriptor : types) |
| 76 | + if (descriptor != null |
| 77 | + && ACCOUNT_TYPE.equals(descriptor.type)) { |
| 78 | + HAS_AUTHENTICATOR = "com.github.mobile" |
| 79 | + .equals(descriptor.packageName); |
| 80 | + break; |
| 81 | + } |
| 82 | + AUTHENTICATOR_CHECKED = true; |
| 83 | + } |
| 84 | + |
| 85 | + return HAS_AUTHENTICATOR; |
| 86 | + } |
| 87 | + |
58 | 88 | /**
|
59 | 89 | * Is the given user the owner of the default account?
|
60 | 90 | *
|
@@ -146,6 +176,9 @@ public static Account getAccount(final AccountManager manager,
|
146 | 176 |
|
147 | 177 | Account[] accounts;
|
148 | 178 | try {
|
| 179 | + if (!hasAuthenticator(manager)) |
| 180 | + throw new AuthenticatorConflictException(); |
| 181 | + |
149 | 182 | while ((accounts = getAccounts(manager)).length == 0) {
|
150 | 183 | if (loggable)
|
151 | 184 | Log.d(TAG, "No GitHub accounts for activity=" + activity);
|
|
0 commit comments