Custom authentication: how to get custom user data values?
Asked Answered
D

1

0

I have custom authenticator. And put account information like it shown below:

final AccountManager am = AccountManager.get(AuthActivity.this);
final Bundle result = new Bundle();
final Bundle userData = new Bundle();
userData.putString(KEY_NAME, mName);
userData.putString(KEY_EMAIL, mEmail);

Account account = new Account(mLogin, vcs.getAccountType());

if (am.addAccountExplicitly(account, null, userData)) {
    result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
    result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
    result.putString(AccountManager.KEY_AUTHTOKEN, mAccessToken);
    result.putString(KEY_REFRESH_TOKEN, mRefreshToken);
    am.setAuthToken(account, account.type, mAccessToken);
} else {
    result.putString(AccountManager.KEY_ERROR_MESSAGE, "F@ck|n8 authenticator");
}

setAccountAuthenticatorResult(result);
setResult(RESULT_OK);
finish();

But how I can get these custom values when I need it?

I try to get it so:

tvUsername.setText(accountManager.getUserData(account, AuthActivity.KEY_NAME));
tvEmail.setText(accountManager.getUserData(account, AuthActivity.KEY_EMAIL));

What I do wrong?

Dorset answered 4/1, 2016 at 21:4 Comment(0)
H
3

Check out Android AccountManager.getUserData() returns null. There is a known issue with addAccountExplicitly. Instead of passing the values in the Bundle, pass an empty Bundle and set the values after creating the account using setUserData(Account, String, String).

IIRC, the problem is a caching issue in the account manager service code. If you remove an account and recreate the same account right away the user data cache in not populated with the values passed in the bundle. Using setUserData works around this issue.

Hog answered 4/1, 2016 at 22:36 Comment(3)
Thank you! I didn't know about this bug.Dorset
Can you provide a link to the actual bug?Gide
Sorry, I can't. I never searched for a bug report, nor did I file one. My findings were to vague to create a substantiated bug report. However, we've never seen any issues like that with setUserData.Hog

© 2022 - 2024 — McMap. All rights reserved.