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:de.msal.shoutemo.connector.GetPostsService.java

@Override
public void onCreate() {
    super.onCreate();

    broadcaster = LocalBroadcastManager.getInstance(this);

    mAccountManager = AccountManager.get(this);
    Account[] acc = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);

    /* No account; push the user into adding one */
    if (acc.length == 0) {
        Log.v(TAG, "No suitable account found, directing user to add one.");
        mAccountManager.addAccount(AccountAuthenticator.ACCOUNT_TYPE, null, null, new Bundle(), null,
                new AccountManagerCallback<Bundle>() {
                    @Override/*www .j ava2 s .c  om*/
                    public void run(AccountManagerFuture<Bundle> result) {
                        Bundle bundle;
                        try {
                            bundle = result.getResult();
                        } catch (OperationCanceledException e) {
                            e.printStackTrace();
                            return;
                        } catch (AuthenticatorException e) {
                            e.printStackTrace();
                            return;
                        } catch (IOException e) {
                            e.printStackTrace();
                            return;
                        }

                        /* no accounts saved, yet; ask the user for credentials */
                        Intent launch = bundle.getParcelable(AccountManager.KEY_INTENT);
                        if (launch != null) {
                            launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(launch);
                            return;
                        }

                        mAccount = new Account(bundle.getString(AccountManager.KEY_ACCOUNT_NAME),
                                bundle.getString(AccountManager.KEY_ACCOUNT_TYPE));
                        Log.v(TAG, "Added account " + mAccount.name + "; now fetching new posts.");
                        startGetPostsTask();
                    }
                }, null);
    } else {
        mAccount = acc[0];
        startGetPostsTask();
    }
}

From source file:com.kinvey.sample.signin.GoogleLoginActivity.java

private void finishLogin(String authToken, String password) {
    final Account account = new Account(authToken, UserLogin.ACCOUNT_TYPE);
    Bundle userData = new Bundle();
    userData.putString(UserLogin.LOGIN_TYPE_KEY, PARAM_LOGIN_TYPE_GOOGLE);
    mAccountManager.addAccountExplicitly(account, password, userData);

    final Intent intent = new Intent();
    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, authToken);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, UserLogin.ACCOUNT_TYPE);
    setResult(RESULT_OK, intent);/*w  ww.  j a v a2  s  . c o  m*/
    finish();
}

From source file:be.evias.cloudLogin.cloudLoginRunPointActivity.java

/**
 * Add new account to the account manager for the cloudLogin
 * account type.//from   w  w  w  .j  a  v a2s . co  m
 *
 * @param accountType   String
 * @param authTokenType String
 */
private void addNewAccount(String accountType, String authTokenType) {
    final AccountManagerFuture<Bundle> future = mAccountManager.addAccount(accountType, authTokenType, null,
            null, this, new AccountManagerCallback<Bundle>() {
                @Override
                public void run(AccountManagerFuture<Bundle> future) {
                    try {
                        Bundle bnd = future.getResult();
                        showMessage(getBaseContext().getString(R.string.message_account_created),
                                Toast.LENGTH_SHORT);
                        Log.d("cloudLogin", "AddNewAccount Bundle is " + bnd);

                        final Account account = new Account(bnd.getString(AccountManager.KEY_ACCOUNT_NAME),
                                bnd.getString(AccountManager.KEY_ACCOUNT_TYPE));

                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putBoolean("cloudlogin_active_account", true);
                        editor.putString("cloudlogin_active_account_name", account.name);
                        editor.commit();

                        displayNavigationDrawer(bnd, account);
                    } catch (Exception e) {
                        e.printStackTrace();
                        showMessage(e.getMessage(), Toast.LENGTH_LONG);

                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putBoolean("cloudlogin_active_account", false);
                        editor.commit();
                    }
                }
            }, null);
}

From source file:edu.mit.mobile.android.locast.accounts.Authenticator.java

/**
 * {@inheritDoc}/*from   w ww.  j  av a  2s. co  m*/
 */
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
        Bundle loginOptions) {
    if (!authTokenType.equals(AuthenticationService.AUTHTOKEN_TYPE)) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
        return result;
    }
    final AccountManager am = AccountManager.get(mContext);
    final String password = am.getPassword(account);
    if (password != null) {
        final Bundle accountData = onlineConfirmPassword(account, password);
        if (accountData != null) {
            final Bundle result = new Bundle();

            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, AuthenticationService.ACCOUNT_TYPE);
            result.putString(AccountManager.KEY_AUTHTOKEN, password);
            return result;
        }
    }
    // the password was missing or incorrect, return an Intent to an
    // Activity that will prompt the user for the password.
    final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
    intent.putExtra(AuthenticatorActivity.EXTRA_USERNAME, account.name);
    intent.putExtra(AuthenticatorActivity.EXTRA_AUTHTOKEN_TYPE, authTokenType);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    return bundle;
}

From source file:edu.mit.mobile.android.locast.accounts.AbsLocastAuthenticator.java

/**
 * {@inheritDoc}//from   w  w  w . j  a  v a  2 s. co m
 */
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
        Bundle loginOptions) {
    if (!authTokenType.equals(getAuthTokenType())) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
        return result;
    }
    final AccountManager am = AccountManager.get(mContext);
    final String password = am.getPassword(account);
    if (password != null) {
        final Bundle accountData = onlineConfirmPassword(account, password);
        if (accountData != null) {
            final Bundle result = new Bundle();

            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, getAccountType());
            result.putString(AccountManager.KEY_AUTHTOKEN, password);
            return result;
        }
    }
    // the password was missing or incorrect, return an Intent to an
    // Activity that will prompt the user for the password.
    final Intent intent = getAuthenticator(mContext);
    intent.putExtra(AbsLocastAuthenticatorActivity.EXTRA_USERNAME, account.name);
    intent.putExtra(AbsLocastAuthenticatorActivity.EXTRA_AUTHTOKEN_TYPE, authTokenType);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    return bundle;
}

From source file:com.nextgis.maplibui.activity.NGWLoginActivity.java

@Override
public void onAddAccount(Account account, String token, boolean accountAdded) {
    if (null != account) {
        mResultBundle = new Bundle();

        if (accountAdded) {
            mResultBundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            mResultBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
            mResultBundle.putString(AccountManager.KEY_AUTHTOKEN, token);
        } else {//from   w  w  w  . j a  va  2 s.co  m
            mResultBundle.putString(AccountManager.KEY_ERROR_MESSAGE,
                    getString(R.string.account_already_exists));
        }
    }

    setResult(RESULT_OK);
    finish();
}

From source file:dev.drsoran.moloko.auth.AuthenticatorActivity.java

/**
 * Response is received from the server for authentication request. Sets the AccountAuthenticatorResult which is sent
 * back to the caller. Also sets the authToken in AccountManager for this account.
 *//*from  w  w  w  .  j  av a  2  s  . c o m*/
@Override
public void onAuthenticationFinished(RtmAuth rtmAuth) {
    final Account account = new Account(rtmAuth.getUser().getUsername(), Constants.ACCOUNT_TYPE);
    try {
        boolean ok = true;

        if (isNewAccount) {
            ok = accountManager.addAccountExplicitly(account, rtmAuth.getToken(), null);
            if (ok) {
                ContentResolver.setSyncAutomatically(account, Rtm.AUTHORITY, true);
            }
        }

        if (ok) {
            accountManager.setUserData(account, Constants.FEAT_API_KEY, MolokoApp.getRtmApiKey(this));
            accountManager.setUserData(account, Constants.FEAT_SHARED_SECRET,
                    MolokoApp.getRtmSharedSecret(this));
            accountManager.setUserData(account, Constants.FEAT_PERMISSION, rtmAuth.getPerms().toString());
            accountManager.setUserData(account, Constants.ACCOUNT_USER_ID, rtmAuth.getUser().getId());
            accountManager.setUserData(account, Constants.ACCOUNT_FULLNAME, rtmAuth.getUser().getFullname());

            final Intent intent = new Intent();

            intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, rtmAuth.getUser().getUsername());
            // We store the authToken as password
            intent.putExtra(AccountManager.KEY_PASSWORD, rtmAuth.getToken());
            intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
            intent.putExtra(AccountManager.KEY_AUTHTOKEN, rtmAuth.getToken());

            intent.putExtra(AccountManager.KEY_BOOLEAN_RESULT, true);

            setAccountAuthenticatorResult(intent.getExtras());
            setResult(RESULT_OK, intent);
        }
    } catch (SecurityException e) {
        MolokoApp.Log.e(getClass(), e.getLocalizedMessage());
        onAuthenticationFailed(getString(R.string.auth_err_cause_scurity));
    } finally {
        finish();
    }
}

From source file:me.philio.ghost.ui.LoginActivity.java

@Override
public void onSuccess(String email, String password, Token token, User user) {
    // Create the account
    Account account = new Account(AccountUtils.getName(mBlogUrl, email), getString(R.string.account_type));
    Bundle userdata = new Bundle();
    userdata.putString(KEY_BLOG_URL, mBlogUrl);
    userdata.putString(KEY_EMAIL, email);
    userdata.putString(KEY_ACCESS_TOKEN_TYPE, token.tokenType);
    userdata.putString(KEY_ACCESS_TOKEN_EXPIRES,
            Long.toString(System.currentTimeMillis() + (token.expires * 1000)));

    // Add account to the system
    AccountManager accountManager = AccountManager.get(this);
    accountManager.addAccountExplicitly(account, password, userdata);

    // Set the account auth tokens
    accountManager.setAuthToken(account, TOKEN_TYPE_ACCESS, token.accessToken);
    accountManager.setAuthToken(account, TOKEN_TYPE_REFRESH, token.refreshToken);

    // Create initial database records
    Blog blog = new Blog();
    blog.url = mBlogUrl;//from  w w w . j  a  v a  2  s .  c o  m
    blog.email = email;
    user.blog = blog;
    ActiveAndroid.beginTransaction();
    try {
        blog.save();
        user.save();
        ActiveAndroid.setTransactionSuccessful();
    } finally {
        ActiveAndroid.endTransaction();
    }

    // Enable sync for the account
    ContentResolver.setSyncAutomatically(account, getString(R.string.content_authority), true);
    SyncHelper.requestSync(account, getString(R.string.content_authority));

    // Set response intent
    Intent intent = new Intent();
    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, account.type);
    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);
    finish();
}

From source file:org.creativecommons.thelist.fragments.AccountFragment.java

@Override
public void onResume() {
    super.onResume();
    mContext = getActivity();/*from   w ww. ja  va  2 s.  c om*/
    mMessageHelper = new MessageHelper(mContext);

    //Get account information
    String accountName = getActivity().getIntent().getStringExtra(ARG_ACCOUNT_NAME);
    final String accountType = getActivity().getIntent().getStringExtra(ARG_ACCOUNT_TYPE);
    mAuthTokenType = getActivity().getIntent().getStringExtra(ARG_AUTH_TYPE);
    if (mAuthTokenType == null)
        mAuthTokenType = AccountGeneral.AUTHTOKEN_TYPE_FULL_ACCESS;

    if (accountName != null) {
        ((EditText) getView().findViewById(R.id.accountName)).setText(accountName);
    }

    //UI Elements
    final Button cancelButton = (Button) getView().findViewById(R.id.cancelButton);
    final Button loginButton = (Button) getView().findViewById(R.id.loginButton);
    final Button signUpButton = (Button) getView().findViewById(R.id.signUpButton);
    final EditText accountEmailField = (EditText) getView().findViewById(R.id.accountName);
    final EditText accountPasswordField = (EditText) getView().findViewById(R.id.accountPassword);
    accountPasswordField.setTypeface(Typeface.DEFAULT);
    final TextView newAccountButton = (TextView) getView().findViewById(R.id.signUp);

    //Try Login on Click
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String accountEmail = accountEmailField.getText().toString().trim();
            final String accountPassword = accountPasswordField.getText().toString().trim();

            if (accountEmail.isEmpty() || accountPassword.isEmpty()) {
                mMessageHelper.showDialog(mContext, getString(R.string.login_error_title),
                        getString(R.string.login_error_message));
            } else {
                ListUser mCurrentUser = new ListUser(getActivity());
                try {
                    mCurrentUser.userSignIn(accountEmail, accountPassword, mAuthTokenType,
                            new ListUser.AuthCallback() {
                                @Override
                                public void onSuccess(String authtoken) {
                                    //TODO: authtoken stuff
                                    Bundle data = new Bundle();

                                    data.putString(AccountManager.KEY_ACCOUNT_NAME, accountEmail);
                                    data.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType);
                                    data.putString(AccountManager.KEY_AUTHTOKEN, authtoken);
                                    data.putString(PARAM_USER_PASS, accountPassword);

                                    //Create Bundle to create Account
                                    mCallback.onUserSignedIn(data);
                                }
                            });
                } catch (Exception e) {
                    Log.d("LoginFragment", e.getMessage());
                    //data.putString(KEY_ERROR_MESSAGE, e.getMessage());
                }
            }
        }
    });

    //Actually I need an account --> show user Sign Up Button
    if (newAccountButton != null) {
        newAccountButton.setMovementMethod(LinkMovementMethod.getInstance());
    }
    //TODO: hide loginButton and show signUpButton
    //        newAccountButton.setOnClickListener(new View.OnClickListener() {
    //            @Override
    //            public void onClick(View v) {
    //
    //                //loginButton.setVisibility(View.GONE);
    //                //signUpButton.setVisibility(View.VISIBLE);
    //            }
    //        });

    //Cancel Activity
    cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCallback.onCancelLogin();
        }
    });

    //TODO: do when we have register user
    //        signUpButton.setOnClickListener(new View.OnClickListener() {
    //            @Override
    //            public void onClick(View v) {
    //                //TODO:userSignUp
    //                final String accountEmail = accountEmailField.getText().toString().trim();
    //                final String accountPassword = accountPasswordField.getText().toString().trim();
    //
    //                if (accountEmail.isEmpty() || accountPassword.isEmpty()) {
    //                    mMessageHelper.showDialog(mContext, getString(R.string.login_error_title),
    //                            getString(R.string.login_error_message));
    //                } else {
    //                    //TODO: Login User + save to sharedPreferences
    //                    ListUser mCurrentUser = new ListUser(getActivity());
    //                    try {
    //                        mCurrentUser.userSignUp(accountEmail, accountPassword, mAuthTokenType, new ListUser.AuthCallback() {
    //                            @Override
    //                            public void onSuccess(String authtoken) {
    //
    //                                Bundle data = new Bundle();
    //
    //                                data.putString(AccountManager.KEY_ACCOUNT_NAME, accountEmail);
    //                                data.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType);
    //                                data.putString(AccountManager.KEY_AUTHTOKEN, authtoken);
    //                                data.putString(PARAM_USER_PASS, accountPassword);
    //
    //                                mCallback.onUserSignedUp(data);
    //                            }
    //                        });
    //                    } catch (Exception e) {
    //                        Log.d("LoginFragment", e.getMessage());
    //                    }
    //                }
    //            }
    //        });

}

From source file:moose.com.ac.LoginActivity.java

private void addAccount(String username, String password) {
    Account account = new Account(username, BuildConfig.APPLICATION_ID);
    accountManager.addAccountExplicitly(account, password, null);
    accountManager.setPassword(account, password); // for re-login with updated password
    Bundle bundle = new Bundle();
    bundle.putString(AccountManager.KEY_ACCOUNT_NAME, username);
    bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, BuildConfig.APPLICATION_ID);
    SettingPreferences.setUsername(this, username);
    finish();//from w  w  w. j a  v a  2s. c  o  m
}