Code example for AccountManager

Methods: getAccountsByType, removeAccount

0
 
    /** 
     * @return The first account found with the application account type. 
     */ 
    public Account getAccount() {
        Account[] accounts = accountManager.getAccountsByType(getAccountType());
        if (accounts == null || accounts.length == 0)
            return null; 
        return accounts[0];
    } 
 
    /** 
     * @param name The name associated with the account 
     * @return The account with the application account type and the given name. 
     */ 
    public Account getAccountByName(String name) {
        Account[] accounts = accountManager.getAccountsByType(getAccountType());
        if (accounts != null) {
            for (Account account : accounts) {
                if (account.name.equals(name)) {
                    return account;
                } 
            } 
        } 
        return null; 
    } 
 
    /** 
     * @return All of the accounts found for this application account type. 
     */ 
    public Account[] getAccounts() {
        return accountManager.getAccountsByType(getAccountType());
    } 
 
    /** 
     * Remove all of the accounts passed in. 
     * @param accounts The array of accounts to remove. 
     */ 
    public void removeAccounts(Account[] accounts) {
        List<AccountManagerFuture<Boolean>> removalFutures = new ArrayList<AccountManagerFuture<Boolean>>();
        for (Account a : accounts)
            removalFutures.add(accountManager.removeAccount(a, null, null));
 
        for (AccountManagerFuture<Boolean> f : removalFutures) {
            try { 
                f.getResult();
            } catch (Exception ex) {
                Log.w("ClientManager:removeAccounts", "Exception removing old account", ex);
            } 
        } 
    } 
 
    /** 
     * Create a new account and return the details of the new account in a bundle. 
     * @param accountName 
     * @param username 
     * @param refreshToken 
     * @param authToken 
     * @param instanceUrl 
     * @param loginUrl 
     * @param idUrl 
     * @param clientId 
     * @param orgId 
     * @param userId 
     * @param passcodeHash 
     * @return 
     */ 
    public Bundle createNewAccount(String accountName, String username, String refreshToken, String authToken,
                                   String instanceUrl, String loginUrl, String idUrl, String clientId, String orgId, String userId, String passcodeHash) {
        return createNewAccount(accountName, username, refreshToken, authToken,
                                instanceUrl, loginUrl, idUrl, clientId, orgId, userId, passcodeHash, null);
    } 
 
    public Bundle createNewAccount(String accountName, String username, String refreshToken, String authToken,
                                   String instanceUrl, String loginUrl, String idUrl, String clientId, String orgId, String userId, String passcodeHash,
            String clientSecret) {
        Bundle extras = new Bundle();
        extras.putString(AccountManager.KEY_ACCOUNT_NAME, accountName);
        extras.putString(AccountManager.KEY_ACCOUNT_TYPE, getAccountType());
        extras.putString(AuthenticatorService.KEY_USERNAME, SalesforceSDKManager.encryptWithPasscode(username, passcodeHash));
        extras.putString(AuthenticatorService.KEY_LOGIN_URL, SalesforceSDKManager.encryptWithPasscode(loginUrl, passcodeHash));
        extras.putString(AuthenticatorService.KEY_ID_URL, SalesforceSDKManager.encryptWithPasscode(idUrl, passcodeHash));
        extras.putString(AuthenticatorService.KEY_INSTANCE_URL, SalesforceSDKManager.encryptWithPasscode(instanceUrl, passcodeHash));
        extras.putString(AuthenticatorService.KEY_CLIENT_ID, SalesforceSDKManager.encryptWithPasscode(clientId, passcodeHash));
        extras.putString(AuthenticatorService.KEY_ORG_ID, SalesforceSDKManager.encryptWithPasscode(orgId, passcodeHash));
        extras.putString(AuthenticatorService.KEY_USER_ID, SalesforceSDKManager.encryptWithPasscode(userId, passcodeHash));
        if (clientSecret != null) {
            extras.putString(AuthenticatorService.KEY_CLIENT_SECRET, SalesforceSDKManager.encryptWithPasscode(clientSecret, passcodeHash));
        } 
        extras.putString(AccountManager.KEY_AUTHTOKEN, SalesforceSDKManager.encryptWithPasscode(authToken, passcodeHash));
        Account acc = new Account(accountName, getAccountType());
        accountManager.addAccountExplicitly(acc, SalesforceSDKManager.encryptWithPasscode(refreshToken, passcodeHash), extras);
        accountManager.setAuthToken(acc, AccountManager.KEY_AUTHTOKEN, authToken);
        return extras;
    } 
 
    /** 
     * Should match the value in authenticator.xml.12 
     * @return The account type for this application. 
     */ 
    public String getAccountType() {
        return accountType;
    } 
 
    /** 
     * Changes the passcode to a new value and re-encrypts the account manager data with the new passcode. 
     * 
     * @param oldPass Old passcode. 
     * @param newPass New passcode. 
     */ 
    public static synchronized void changePasscode(String oldPass, String newPass) {
 
        // Update data stored in AccountManager with new encryption key. 
        final AccountManager acctManager = AccountManager.get(SalesforceSDKManager.getInstance().getAppContext());
        if (acctManager != null) {
            final Account[] accounts = acctManager.getAccountsByType(SalesforceSDKManager.getInstance().getAccountType());
            if (accounts != null && accounts.length > 0) {
                final Account account = accounts[0];
 
                // Grab existing data stored in AccountManager. 
                final String authToken = SalesforceSDKManager.decryptWithPasscode(acctManager.getUserData(account, AccountManager.KEY_AUTHTOKEN), oldPass);
                final String refreshToken = SalesforceSDKManager.decryptWithPasscode(acctManager.getPassword(account), oldPass);
                final String loginServer = SalesforceSDKManager.decryptWithPasscode(acctManager.getUserData(account, AuthenticatorService.KEY_LOGIN_URL), oldPass);
                final String idUrl = SalesforceSDKManager.decryptWithPasscode(acctManager.getUserData(account, AuthenticatorService.KEY_ID_URL), oldPass);
                final String instanceServer = SalesforceSDKManager.decryptWithPasscode(acctManager.getUserData(account, AuthenticatorService.KEY_INSTANCE_URL), oldPass);
                final String orgId = SalesforceSDKManager.decryptWithPasscode(acctManager.getUserData(account, AuthenticatorService.KEY_ORG_ID), oldPass);
                final String userId = SalesforceSDKManager.decryptWithPasscode(acctManager.getUserData(account, AuthenticatorService.KEY_USER_ID), oldPass);
                final String username = SalesforceSDKManager.decryptWithPasscode(acctManager.getUserData(account, AuthenticatorService.KEY_USERNAME), oldPass);
                final String clientId = SalesforceSDKManager.decryptWithPasscode(acctManager.getUserData(account, AuthenticatorService.KEY_CLIENT_ID), oldPass);
 
                // Encrypt data with new hash and put it back in AccountManager. 
                acctManager.setUserData(account, AccountManager.KEY_AUTHTOKEN, SalesforceSDKManager.encryptWithPasscode(authToken, newPass));
                acctManager.setPassword(account, SalesforceSDKManager.encryptWithPasscode(refreshToken, newPass));
                acctManager.setUserData(account, AuthenticatorService.KEY_LOGIN_URL, SalesforceSDKManager.encryptWithPasscode(loginServer, newPass));
                acctManager.setUserData(account, AuthenticatorService.KEY_ID_URL, SalesforceSDKManager.encryptWithPasscode(idUrl, newPass));
                acctManager.setUserData(account, AuthenticatorService.KEY_INSTANCE_URL, SalesforceSDKManager.encryptWithPasscode(instanceServer, newPass));
                acctManager.setUserData(account, AuthenticatorService.KEY_ORG_ID, SalesforceSDKManager.encryptWithPasscode(orgId, newPass));
                acctManager.setUserData(account, AuthenticatorService.KEY_USER_ID, SalesforceSDKManager.encryptWithPasscode(userId, newPass));
                acctManager.setUserData(account, AuthenticatorService.KEY_USERNAME, SalesforceSDKManager.encryptWithPasscode(username, newPass));
                acctManager.setUserData(account, AuthenticatorService.KEY_CLIENT_ID, SalesforceSDKManager.encryptWithPasscode(clientId, newPass));
                acctManager.setAuthToken(account, AccountManager.KEY_AUTHTOKEN, authToken);
            } 
        } 
    } 
 
    /** 
     * @return The AccountManager for the application. 
     */ 
    public AccountManager getAccountManager() {
        return accountManager;
    } 
 
    /** 
     * Removes the user account from the account manager.  This is an 
     * asynchronous process: the callback will be called on completion, if 
     * specified. 
     * @param callback The callback to call when the account removal completes. 
     */ 
    public void removeAccountAsync(AccountManagerCallback<Boolean> callback) {
        Account acc = getAccount();
        if (acc != null)
            accountManager.removeAccount(acc, callback, null);
    } 
 
 
    /** 
     * Callback from either user account creation, or a call to getAuthToken, used