Example usage for android.accounts Account Account

List of usage examples for android.accounts Account Account

Introduction

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

Prototype

public Account(@NonNull Account other, @NonNull String accessId) 

Source Link

Usage

From source file:com.owncloud.android.authentication.AuthenticatorActivity.java

/**
 * Creates a new account through the Account Authenticator that started this
 * activity./*from   ww w .  ja v  a2 s  .c om*/
 * 
 * This makes the account permanent.
 * 
 * TODO Decide how to name the OAuth accounts
 */
private boolean createAccount() {
    // / create and save new ownCloud account
    boolean isOAuth = AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN.equals(mAuthTokenType);
    boolean isSaml = AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(mAuthTokenType);

    Uri uri = Uri.parse(mHostBaseUrl);
    String username = mUsernameInput.getText().toString().trim();
    username = username + "@" + location;

    if (isSaml) {
        username = getUserNameForSamlSso();

    } else if (isOAuth) {
        username = "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong();
    }
    String accountName = username + "@" + uri.getHost();
    if (uri.getPort() >= 0) {
        accountName += ":" + uri.getPort();
    }

    mAccount = new Account(accountName, AccountAuthenticator.ACCOUNT_TYPE);
    if (AccountUtils.exists(mAccount, 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 {

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

        // / 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, AccountAuthenticator.ACCOUNT_TYPE);
        intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mAccount.name);
        /*
         * if (!isOAuth) intent.putExtra(AccountManager.KEY_AUTHTOKEN,
         * AccountAuthenticator.ACCOUNT_TYPE);
         */
        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, AccountAuthenticator.KEY_OC_VERSION, mDiscoveredVersion.toString());
        mAccountMgr.setUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL, mHostBaseUrl);
        if (isSaml) {
            mAccountMgr.setUserData(mAccount, AccountAuthenticator.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE");
        } else if (isOAuth) {
            mAccountMgr.setUserData(mAccount, AccountAuthenticator.KEY_SUPPORTS_OAUTH2, "TRUE");
        }

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

        // / immediately request for the synchronization of the new account
        Bundle bundle = new Bundle();
        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        ContentResolver.requestSync(mAccount, AccountAuthenticator.AUTHORITY, bundle);
        syncAccount();
        // Bundle bundle = new Bundle();
        // bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        // ContentResolver.requestSync(mAccount,
        // AccountAuthenticator.AUTHORITY, bundle);
        return true;
    }
}

From source file:com.digitalarx.android.authentication.AuthenticatorActivity.java

/**
 * Creates a new account through the Account Authenticator that started this activity. 
 * /*from  w  w  w . j a  v  a2s. c o m*/
 * This makes the account permanent.
 * 
 * TODO Decide how to name the OAuth accounts
 */
private boolean createAccount() {
    /// create and save new ownCloud account
    boolean isOAuth = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType())
            .equals(mAuthTokenType);
    boolean isSaml = AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType())
            .equals(mAuthTokenType);

    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.owncloud.android.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) {
            mAccountMgr.addAccountExplicitly(mAccount, "", null); // with external authorizations, the password is never input in the app
        } else {
            mAccountMgr.addAccountExplicitly(mAccount, mPasswordInput.getText().toString(), null);
        }

        /// 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);
        /*if (!isOAuth)
        intent.putExtra(AccountManager.KEY_AUTHTOKEN,   MainApp.getAccountType()); */
        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;
    }
}

From source file:com.synox.android.authentication.AuthenticatorActivity.java

/**
 * Creates a new account through the Account Authenticator that started this activity. 
 * // w w w .  j  av  a 2 s  .  c  om
 * 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.synox.android.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;
    }
}

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

/**
 * Creates a new account through the Account Authenticator that started this activity. 
 * //from w  w w  .  j  a  v  a 2 s.c  o  m
 * 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;
    }
}

From source file:com.android.contacts.quickcontact.QuickContactActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_star:
        toggleStar(item);//from   ww w. j  a  va  2s.  c  om
        return true;
    case R.id.menu_edit:
        if (DirectoryContactUtil.isDirectoryContact(mContactData)) {
            // This action is used to launch the contact selector, with the option of
            // creating a new contact. Creating a new contact is an INSERT, while selecting
            // an exisiting one is an edit. The fields in the edit screen will be
            // prepopulated with data.

            final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
            intent.setType(Contacts.CONTENT_ITEM_TYPE);

            ArrayList<ContentValues> values = mContactData.getContentValues();

            // Only pre-fill the name field if the provided display name is an nickname
            // or better (e.g. structured name, nickname)
            if (mContactData.getDisplayNameSource() >= DisplayNameSources.NICKNAME) {
                intent.putExtra(Intents.Insert.NAME, mContactData.getDisplayName());
            } else if (mContactData.getDisplayNameSource() == DisplayNameSources.ORGANIZATION) {
                // This is probably an organization. Instead of copying the organization
                // name into a name entry, copy it into the organization entry. This
                // way we will still consider the contact an organization.
                final ContentValues organization = new ContentValues();
                organization.put(Organization.COMPANY, mContactData.getDisplayName());
                organization.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
                values.add(organization);
            }

            // Last time used and times used are aggregated values from the usage stat
            // table. They need to be removed from data values so the SQL table can insert
            // properly
            for (ContentValues value : values) {
                value.remove(Data.LAST_TIME_USED);
                value.remove(Data.TIMES_USED);
            }
            intent.putExtra(Intents.Insert.DATA, values);

            // If the contact can only export to the same account, add it to the intent.
            // Otherwise the ContactEditorFragment will show a dialog for selecting an
            // account.
            if (mContactData.getDirectoryExportSupport() == Directory.EXPORT_SUPPORT_SAME_ACCOUNT_ONLY) {
                intent.putExtra(Intents.Insert.EXTRA_ACCOUNT, new Account(
                        mContactData.getDirectoryAccountName(), mContactData.getDirectoryAccountType()));
                intent.putExtra(Intents.Insert.EXTRA_DATA_SET,
                        mContactData.getRawContacts().get(0).getDataSet());
            }

            // Add this flag to disable the delete menu option on directory contact joins
            // with local contacts. The delete option is ambiguous when joining contacts.
            intent.putExtra(ContactEditorFragment.INTENT_EXTRA_DISABLE_DELETE_MENU_OPTION, true);

            startActivityForResult(intent, REQUEST_CODE_CONTACT_SELECTION_ACTIVITY);
        } else if (InvisibleContactUtil.isInvisibleAndAddable(mContactData, this)) {
            InvisibleContactUtil.addToDefaultGroup(mContactData, this);
        } else if (isContactEditable()) {
            editContact();
        }
        return true;
    case R.id.menu_delete:
        if (isContactEditable()) {
            deleteContact();
        }
        return true;
    case R.id.menu_share:
        if (isContactShareable()) {
            shareContact();
        }
        return true;
    case R.id.menu_create_contact_shortcut:
        if (isShortcutCreatable()) {
            createLauncherShortcutWithContact();
        }
        return true;
    case R.id.menu_help:
        HelpUtils.launchHelpAndFeedbackForContactScreen(this);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}