AccountManagerService
is the actual system service that manages accounts, whereas AccountManager
is just a proxy that hides all bound-services-related stuff under the hood.
The below source code of addAccountInternal
method from AccountManagerService
is pretty much self-explanatory, except that if you pass null
for account
then IllegalArgumentException
will be thrown instead of execution of this method:
private boolean addAccountInternal(UserAccounts accounts, Account account, String password,
Bundle extras, boolean restricted, int callingUid) {
if (account == null) {
return false;
}
synchronized (accounts.cacheLock) {
final SQLiteDatabase db = accounts.openHelper.getWritableDatabase();
db.beginTransaction();
try {
long numMatches = DatabaseUtils.longForQuery(db,
"select count(*) from " + TABLE_ACCOUNTS
+ " WHERE " + ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?",
new String[]{account.name, account.type});
if (numMatches > 0) {
Log.w(TAG, "insertAccountIntoDatabase: " + account
+ ", skipping since the account already exists");
return false;
}
ContentValues values = new ContentValues();
values.put(ACCOUNTS_NAME, account.name);
values.put(ACCOUNTS_TYPE, account.type);
values.put(ACCOUNTS_PASSWORD, password);
values.put(ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS, System.currentTimeMillis());
long accountId = db.insert(TABLE_ACCOUNTS, ACCOUNTS_NAME, values);
if (accountId < 0) {
Log.w(TAG, "insertAccountIntoDatabase: " + account
+ ", skipping the DB insert failed");
return false;
}
if (extras != null) {
for (String key : extras.keySet()) {
final String value = extras.getString(key);
if (insertExtraLocked(db, accountId, key, value) < 0) {
Log.w(TAG, "insertAccountIntoDatabase: " + account
+ ", skipping since insertExtra failed for key " + key);
return false;
}
}
}
db.setTransactionSuccessful();
logRecord(db, DebugDbHelper.ACTION_ACCOUNT_ADD, TABLE_ACCOUNTS, accountId,
accounts, callingUid);
insertAccountIntoCacheLocked(accounts, account);
} finally {
db.endTransaction();
}
sendAccountsChangedBroadcast(accounts.userId);
}
if (accounts.userId == UserHandle.USER_OWNER) {
addAccountToLimitedUsers(account);
}
return true;
}
Bottom line: addAccountExplicitly
will return false
if either the required account already exists, or some SQLite database error prevented storage of account related information in database.