Example usage for android.accounts AccountManager KEY_ACCOUNT_TYPE

List of usage examples for android.accounts AccountManager KEY_ACCOUNT_TYPE

Introduction

In this page you can find the example usage for android.accounts AccountManager KEY_ACCOUNT_TYPE.

Prototype

String KEY_ACCOUNT_TYPE

To view the source code for android.accounts AccountManager KEY_ACCOUNT_TYPE.

Click Source Link

Document

Bundle key used for the String account type in results from methods which return information about a particular account.

Usage

From source file:com.cerema.cloud2.authentication.AuthenticatorActivity.java

/**
 * Creates a new account through the Account Authenticator that started this activity. 
 * //w w w.jav  a2 s.  com
 * This makes the account permanent.
 * 
 * TODO Decide how to name the OAuth accounts
 */
private boolean createAccount(RemoteOperationResult authResult) {
    /// create and save new ownCloud account
    boolean isOAuth = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType())
            .equals(mAuthTokenType);
    boolean isSaml = AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType())
            .equals(mAuthTokenType);

    String lastPermanentLocation = authResult.getLastPermanentLocation();
    if (lastPermanentLocation != null) {
        mServerInfo.mBaseUrl = AccountUtils.trimWebdavSuffix(lastPermanentLocation);
    }

    Uri uri = Uri.parse(mServerInfo.mBaseUrl);
    String username = mUsernameInput.getText().toString().trim();
    if (isOAuth) {
        username = "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong();
    }
    String accountName = com.cerema.cloud2.lib.common.accounts.AccountUtils.buildAccountName(uri, username);
    Account newAccount = new Account(accountName, MainApp.getAccountType());
    if (AccountUtils.exists(newAccount, getApplicationContext())) {
        // fail - not a new account, but an existing one; disallow
        RemoteOperationResult result = new RemoteOperationResult(ResultCode.ACCOUNT_NOT_NEW);
        updateAuthStatusIconAndText(result);
        showAuthStatus();
        Log_OC.d(TAG, result.getLogMessage());
        return false;

    } else {
        mAccount = newAccount;

        if (isOAuth || isSaml) {
            // with external authorizations, the password is never input in the app
            mAccountMgr.addAccountExplicitly(mAccount, "", null);
        } else {
            mAccountMgr.addAccountExplicitly(mAccount, mPasswordInput.getText().toString(), null);
        }

        // include account version with the new account
        mAccountMgr.setUserData(mAccount, Constants.KEY_OC_ACCOUNT_VERSION,
                Integer.toString(AccountUtils.ACCOUNT_VERSION));

        /// add the new account as default in preferences, if there is none already
        Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);
        if (defaultAccount == null) {
            SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
            editor.putString("select_oc_account", accountName);
            editor.commit();
        }

        /// prepare result to return to the Authenticator
        //  TODO check again what the Authenticator makes with it; probably has the same 
        //  effect as addAccountExplicitly, but it's not well done
        final Intent intent = new Intent();
        intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, MainApp.getAccountType());
        intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mAccount.name);
        intent.putExtra(AccountManager.KEY_USERDATA, username);
        if (isOAuth || isSaml) {
            mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);
        }
        /// add user data to the new account; TODO probably can be done in the last parameter 
        //      addAccountExplicitly, or in KEY_USERDATA
        mAccountMgr.setUserData(mAccount, Constants.KEY_OC_VERSION, mServerInfo.mVersion.getVersion());
        mAccountMgr.setUserData(mAccount, Constants.KEY_OC_BASE_URL, mServerInfo.mBaseUrl);

        if (isSaml) {
            mAccountMgr.setUserData(mAccount, Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE");
        } else if (isOAuth) {
            mAccountMgr.setUserData(mAccount, Constants.KEY_SUPPORTS_OAUTH2, "TRUE");
        }

        setAccountAuthenticatorResult(intent.getExtras());
        setResult(RESULT_OK, intent);

        return true;
    }
}