A - Type of the user account that will be managed by the UserAccountManager subclass.public abstract class UserAccountManager<A extends UserAccount> extends Object
AccountManager that can be used to simplify management of an Android application
accounts. Each instance of UserAccountManager can manage accounts only of a single type that is
specified during initialization via UserAccountManager(Context, String) constructor.
UserAccountManager can be used for both creation and deletion of Android accounts via
createAccount(UserAccount) and deleteAccount(UserAccount) or via theirs asynchronous
relatives createAccountAsync(UserAccount) and deleteAccountAsync(UserAccount).
This manager also provides API methods to store and peek authentication tokens for a specific
account via setAccountAuthToken(Account, String, String) and peekAccountAuthToken(Account, String)
along with account's password management via setAccountPassword(Account, String) and
getAccountPassword(Account). Data for a specific account can be stored either as single
values via setAccountData(Account, String, String) or as data Bundle via
setAccountDataBundle(Account, Bundle) which is basically bulk method for the single
value storing method. Stored account data can be than obtained via getAccountData(Account, String)
or via getAccountDataBundle(Account, String...).
| Modifier and Type | Class and Description |
|---|---|
static interface |
UserAccountManager.AccountWatcher<A extends UserAccount>
Watcher that may be used to listen for callbacks fired whenever a new Android
Account
is created or deleted asynchronously for its associated UserAccount. |
| Modifier and Type | Field and Description |
|---|---|
static int |
ERROR_CREATE_ACCOUNT
Error code indicating that an error occurred during asynchronous execution of
onCreateAccount(UserAccount) requested via createAccountAsync(UserAccount). |
static int |
ERROR_DELETE_ACCOUNT
Error code indicating that an error occurred during asynchronous execution of
onDeleteAccount(UserAccount) requested via deleteAccountAsync(UserAccount). |
protected String |
mAccountType
Deprecated.
Use
getAccountType() instead. |
protected android.content.Context |
mContext
Deprecated.
Use
getContext() instead. |
protected android.accounts.AccountManager |
mManager
Account manager used to create/update/delete accounts of the type specified for this manager.
|
protected static String |
PERMISSION_AUTHENTICATE_ACCOUNTS
Value for Android permission to AUTHENTICATE accounts.
|
protected static String |
PERMISSION_GET_ACCOUNTS
Value for Android permission to GET accounts.
|
protected static String |
PERMISSION_MANAGE_ACCOUNTS
Value for Android permission to MANAGE accounts.
|
| Constructor and Description |
|---|
UserAccountManager(android.content.Context context,
String accountType)
Creates a new instance of UserAccountManager for the specified accountType.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clearAccountPassword(android.accounts.Account account)
Clears the password for the given account.
|
boolean |
createAccount(A userAccount)
Creates a new Android
Account for the given userAccount. |
void |
createAccountAsync(A userAccount)
Same as
createAccount(UserAccount) where creation of the given userAccount
will be executed asynchronously using AsyncTask. |
boolean |
deleteAccount(A userAccount)
Deletes an existing Android
Account for the given userAccount. |
void |
deleteAccountAsync(A userAccount)
Same as
deleteAccount(UserAccount) where deletion of the given userAccount
will be executed asynchronously using AsyncTask. |
protected android.accounts.Account |
findAccountForUser(A userAccount)
Called to find the Android
Account associated with the given userAccount. |
String |
getAccountData(android.accounts.Account account,
String key)
Returns the single data for the given account stored under the specified key.
|
android.os.Bundle |
getAccountDataBundle(android.accounts.Account account,
String... keys)
Returns the bundle with data for the given account.
|
String |
getAccountPassword(android.accounts.Account account)
Returns the password for the given account.
|
String |
getAccountType()
Returns the type of account that this manager can manage (add, rename, remove).
|
android.content.Context |
getContext()
Returns the context with which has been this manager created.
|
void |
invalidateAccountAuthToken(android.accounts.Account account,
String authToken)
Invalidates the specified authToken for the given account.
|
boolean |
isAccountAuthenticated(android.accounts.Account account,
String authTokenType)
Checks whether the given account is authenticated or not.
|
protected boolean |
onCreateAccount(A userAccount)
Invoked whenever
createAccount(UserAccount) or createAccountAsync(UserAccount)
is called to create new Android Account. |
protected boolean |
onDeleteAccount(A userAccount)
Invoked whenever
createAccount(UserAccount) or createAccountAsync(UserAccount)
is called to create new Android Account. |
String |
peekAccountAuthToken(android.accounts.Account account,
String authTokenType)
Returns the authentication token for the given account stored for the specified
authTokenType.
|
void |
registerWatcher(UserAccountManager.AccountWatcher<A> watcher)
Registers a watcher to be notified whenever a new user account is created or an old one
deleted.
|
void |
setAccountAuthToken(android.accounts.Account account,
String authTokenType,
String authToken)
Sets an authentication token for the given account with the specified authTokenType.
|
void |
setAccountData(android.accounts.Account account,
String key,
String value)
Sets a single data value for the given account with the specified key.
|
void |
setAccountDataBundle(android.accounts.Account account,
android.os.Bundle dataBundle)
Sets a bundle with data for the given account.
|
void |
setAccountPassword(android.accounts.Account account,
String password)
Sets a password for the given account.
|
void |
setDataCrypto(Crypto crypto)
Sets an implementation of
Crypto that should be used by this account manager to
perform account data encryption/decryption operations. |
void |
setKeyEncrypto(Encrypto encrypto)
Sets an implementation of
Encrypto that should be used by this account manager to
perform account data keys encryption operation. |
void |
unregisterWatcher(UserAccountManager.AccountWatcher<A> watcher)
Unregisters the given watcher from the registered ones.
|
public static final int ERROR_CREATE_ACCOUNT
onCreateAccount(UserAccount) requested via createAccountAsync(UserAccount).public static final int ERROR_DELETE_ACCOUNT
onDeleteAccount(UserAccount) requested via deleteAccountAsync(UserAccount).protected static final String PERMISSION_GET_ACCOUNTS
protected static final String PERMISSION_MANAGE_ACCOUNTS
protected static final String PERMISSION_AUTHENTICATE_ACCOUNTS
protected final android.accounts.AccountManager mManager
@Deprecated protected final android.content.Context mContext
getContext() instead.mManager and other needed application data about accounts.@Deprecated protected final String mAccountType
getAccountType() instead.public UserAccountManager(android.content.Context context,
String accountType)
context - Context used to access AccountManager.accountType - The desired type of accounts that can be managed by the new manager.public final android.content.Context getContext()
UserAccountManager(Context, String)public final String getAccountType()
UserAccountManager(Context, String)public void registerWatcher(UserAccountManager.AccountWatcher<A> watcher)
watcher - The desired watcher to register.unregisterWatcher(AccountWatcher)public void unregisterWatcher(UserAccountManager.AccountWatcher<A> watcher)
watcher - The desired watcher to unregister.registerWatcher(AccountWatcher)public final void setKeyEncrypto(Encrypto encrypto)
Encrypto that should be used by this account manager to
perform account data keys encryption operation.encrypto - The desired encrypto implementation. May be null to not perform keys
encryption.setDataCrypto(Crypto)public final void setDataCrypto(Crypto crypto)
Crypto that should be used by this account manager to
perform account data encryption/decryption operations.
Note, that if specified, the provided crypto will be also used for password encryption/decryption operations.
crypto - The desired crypto implementation. May be null to not perform data
encryption/decryption.setKeyEncrypto(Encrypto),
setAccountData(Account, String, String),
getAccountData(Account, String),
getAccountDataBundle(Account, String...)public void createAccountAsync(A userAccount)
createAccount(UserAccount) where creation of the given userAccount
will be executed asynchronously using AsyncTask. When the creation process is
finished the current AccountWatchers (if any) will be notified through
UserAccountManager.AccountWatcher.onAccountCreated(UserAccount) callback.
This method requires the caller to hold PERMISSION_GET_ACCOUNTS along with
PERMISSION_AUTHENTICATE_ACCOUNTS permissions.
userAccount - The desired user account for which to create a corresponding Android Account.registerWatcher(AccountWatcher),
deleteAccountAsync(UserAccount)public boolean createAccount(A userAccount)
Account for the given userAccount.
This method requires the caller to hold PERMISSION_GET_ACCOUNTS along with
PERMISSION_AUTHENTICATE_ACCOUNTS permissions.
userAccount - The desired user account for which to create the corresponding Android Account.True if the account has been created, false otherwise.createAccountAsync(UserAccount),
deleteAccount(UserAccount),
AccountManager.addAccountExplicitly(Account, String, Bundle),
AccountManager.setAuthToken(Account, String, String)protected boolean onCreateAccount(A userAccount)
createAccount(UserAccount) or createAccountAsync(UserAccount)
is called to create new Android Account.
Note, that this method can be invoked on a background thread.
Current implementation always returns true.
This method requires the caller to hold PERMISSION_GET_ACCOUNTS along with
PERMISSION_AUTHENTICATE_ACCOUNTS permissions.
userAccount - The desired user account for which to create the corresponding Android
Account.True if account has been created, false otherwise.onDeleteAccount(UserAccount),
AccountManager.addAccountExplicitly(Account, String, Bundle),
AccountManager.setAuthToken(Account, String, String)public void setAccountAuthToken(android.accounts.Account account,
String authTokenType,
String authToken)
This method requires the caller to hold PERMISSION_AUTHENTICATE_ACCOUNTS permission.
account - The account for which to update its authentication token.authTokenType - Type of the authentication token to be stored used as key.authToken - The desired authentication token to be stored.peekAccountAuthToken(Account, String),
isAccountAuthenticated(Account, String),
AccountManager.setAuthToken(Account, String, String)public boolean isAccountAuthenticated(android.accounts.Account account,
String authTokenType)
By default the account is considered authenticated if there is stored authentication token for that account with the specified authTokenType.
This method requires the caller to hold PERMISSION_AUTHENTICATE_ACCOUNTS permission.
account - The account for which to check its authentication state.authTokenType - Type of the authentication token used to resolve whether the account is
authenticated or not.True if there is stored valid authentication token for the account with the
specified token type, false otherwise.setAccountAuthToken(Account, String, String),
peekAccountAuthToken(Account, String)public String peekAccountAuthToken(android.accounts.Account account, String authTokenType)
This method requires the caller to hold PERMISSION_AUTHENTICATE_ACCOUNTS permission.
account - The account for which to peek the requested authentication token.authTokenType - Type of the requested authentication token to peek.null if there has not been stored token for the requested
type yet or the token has been invalidated.setAccountAuthToken(Account, String, String),
invalidateAccountAuthToken(Account, String),
AccountManager.peekAuthToken(Account, String)public void invalidateAccountAuthToken(android.accounts.Account account,
String authToken)
This method requires the caller to hold PERMISSION_MANAGE_ACCOUNTS permission.
account - The account for which to invalidate the authentication token.authToken - The token that should be invalidated.peekAccountAuthToken(Account, String),
AccountManager.invalidateAuthToken(String, String)public void setAccountPassword(android.accounts.Account account,
String password)
This method requires the caller to hold PERMISSION_AUTHENTICATE_ACCOUNTS permission.
account - The account for which to update its password.password - The desired password to be stored.getAccountPassword(Account),
clearAccountPassword(Account),
AccountManager.setPassword(Account, String)public String getAccountPassword(android.accounts.Account account)
This method requires the caller to hold PERMISSION_AUTHENTICATE_ACCOUNTS permission.
account - The account for which to obtain the requested password.null if there has not been stored any password for the
account yet.setAccountPassword(Account, String),
clearAccountPassword(Account),
AccountManager.getPassword(Account)public void clearAccountPassword(android.accounts.Account account)
This method requires the caller to hold PERMISSION_MANAGE_ACCOUNTS permission.
account - The account for which to clear its password.AccountManager.clearPassword(Account)public void setAccountData(android.accounts.Account account,
String key,
String value)
This method requires the caller to hold PERMISSION_AUTHENTICATE_ACCOUNTS permission.
account - The account for which to update its data.key - The key under which will be the desired value stored.value - The desired data value to be stored.getAccountData(Account, String),
setAccountDataBundle(Account, Bundle),
AccountManager.setUserData(Account, String, String)public String getAccountData(android.accounts.Account account, String key)
This method requires the caller to hold PERMISSION_AUTHENTICATE_ACCOUNTS permission.
account - The account for which to obtain its data.key - The key for which to obtain the requested account data.null if there are no data stored for the requested
key.setAccountData(Account, String, String),
AccountManager.getUserData(Account, String)public void setAccountDataBundle(android.accounts.Account account,
android.os.Bundle dataBundle)
This method requires the caller to hold PERMISSION_AUTHENTICATE_ACCOUNTS permission.
account - The account for which to update its data bundle.dataBundle - Bundle with the desired data.getAccountDataBundle(Account, String...),
setAccountData(Account, String, String)public android.os.Bundle getAccountDataBundle(android.accounts.Account account,
String... keys)
This method requires the caller to hold PERMISSION_AUTHENTICATE_ACCOUNTS permission.
account - The account for which to obtain its data bundle.keys - Set of keys for which to obtain the desired data.setAccountDataBundle(Account, Bundle),
getAccountData(Account, String)public void deleteAccountAsync(A userAccount)
deleteAccount(UserAccount) where deletion of the given userAccount
will be executed asynchronously using AsyncTask. When the deletion process is
finished the current AccountWatchers (if any) will be notified through
UserAccountManager.AccountWatcher.onAccountDeleted(UserAccount) callback.
This method requires the caller to hold PERMISSION_GET_ACCOUNTS along with
PERMISSION_AUTHENTICATE_ACCOUNTS permissions.
userAccount - The desired user account for which to delete the corresponding Android
Account.registerWatcher(AccountWatcher),
deleteAccount(UserAccount),
createAccountAsync(UserAccount)public boolean deleteAccount(A userAccount)
Account for the given userAccount.
This method requires the caller to hold PERMISSION_GET_ACCOUNTS along with
PERMISSION_AUTHENTICATE_ACCOUNTS permissions.
Note, that this method may be only invoked from a background thread. If invoked from the
main UI thread an exception will be thrown. For none-blocking call use deleteAccountAsync(UserAccount)
instead.
userAccount - The desired user account for which to delete the corresponding Android Account.True if the account has been deleted, false otherwise.deleteAccountAsync(UserAccount),
createAccount(UserAccount),
AccountManager.removeAccount(Account, AccountManagerCallback, Handler),
AccountManager.invalidateAuthToken(String, String)protected boolean onDeleteAccount(A userAccount)
createAccount(UserAccount) or createAccountAsync(UserAccount)
is called to create new Android Account.
Note, that invocation of this method need to be always on a background thread otherwise an exception will be thrown.
Current implementation returns true whenever there has been found Android account to
be deleted for the given user account, false otherwise.
This method requires the caller to hold PERMISSION_GET_ACCOUNTS along with
PERMISSION_AUTHENTICATE_ACCOUNTS permissions.
userAccount - The desired user account for which to delete the corresponding Android
Account.True if account has been deleted, false otherwise.onCreateAccount(UserAccount)protected android.accounts.Account findAccountForUser(A userAccount)
Account associated with the given userAccount.
Default implementation retrieves all current accounts by the account type specified for this
manager and searches for one that has same name specified as the given user account. See
UserAccount.getName() and Account.name for additional information.
This method requires the caller to hold PERMISSION_GET_ACCOUNTS permission.
userAccount - The user account for which to find the corresponding Android account.null if there is no account created for the requested user account.