Example usage for android.accounts AccountManager KEY_ACCOUNT_NAME

List of usage examples for android.accounts AccountManager KEY_ACCOUNT_NAME

Introduction

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

Prototype

String KEY_ACCOUNT_NAME

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

Click Source Link

Document

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

Usage

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

@Override
public void onResume() {
    super.onResume();
    mContext = getActivity();//from w ww . j ava  2s.  co  m
    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:com.github.beardlybread.orgestrator.io.GoogleDriveApi.java

/** Hook into the Intents sent to initialize Google Drive.
 *
 * @param requestCode indicates the data source.
 * @param resultCode indicates whether the action was successful.
 * @param data holds necessary data returned from the action.
 *///from www  .j a  v  a  2s.com
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == GoogleDriveApi.REQUEST_GOOGLE_PLAY_SERVICES) {
        if (resultCode != Activity.RESULT_OK) {
            this.showErrorDialog(new Exception(GoogleDriveApi.ERROR_REQUEST_GOOGLE_PLAY));
        } else {
            this.initialize();
        }
    } else if (requestCode == GoogleDriveApi.REQUEST_ACCOUNT_PICKER) {
        if (resultCode == Activity.RESULT_OK && data != null && data.getExtras() != null) {
            String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
            if (accountName != null) {
                (getActivity().getPreferences(Context.MODE_PRIVATE)).edit()
                        .putString("accountName", accountName).apply();
                this.credential.setSelectedAccountName(accountName);
                this.initialize();
            }
        }
    } else if (requestCode == REQUEST_AUTHORIZATION) {
        if (resultCode == Activity.RESULT_OK) {
            if (this.getLastRequest().getRequest().isIncomplete())
                new MakeRequest(this.getLastRequest().getRequest()).execute();
        }
    }
}

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();// w w w  .  j a  va 2s . com
}

From source file:com.manning.androidhacks.hack023.authenticator.AuthenticatorActivity.java

private void finishLogin() {
    final Account account = new Account(mUsername, PARAM_ACCOUNT_TYPE);

    if (mRequestNewAccount) {
        mAccountManager.addAccountExplicitly(account, mPassword, null);

        Bundle bundle = new Bundle();
        ContentResolver.addPeriodicSync(account, TodoContentProvider.AUTHORITY, bundle, 300);

    } else {//from  w ww.j a  va  2s  .  c  o  m
        mAccountManager.setPassword(account, mPassword);
    }

    final Intent intent = new Intent();
    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, PARAM_ACCOUNT_TYPE);

    if (mAuthTokenType != null && mAuthTokenType.equals(PARAM_AUTHTOKEN_TYPE)) {
        intent.putExtra(AccountManager.KEY_AUTHTOKEN, mAuthToken);
    }

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

From source file:com.dealbreaker.cloud.backend.core.CloudBackendFragment.java

/**
 * Handles callback from Intents like authorization request or account
 * picking. If the hosting Activity also makes use of onActivityResult, make
 * sure the Activity calls super.onActivityResult to handle all
 * requestCodes, including the code here. Forgetting to call the super
 * method is a common mistake and is often cause for confusion.
 *//*from   www  .  j  av  a  2  s  .c  o  m*/
@Override
public final void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Log.i("result from activity", "resultcode: " + resultCode);
    switch (requestCode) {
    case REQUEST_ACCOUNT_PICKER:
        if (data != null && data.getExtras() != null) {

            // set the picked account name to the credential
            String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
            mCredential.setSelectedAccountName(accountName);

            // save account name to shared pref
            SharedPreferences.Editor e = mCloudBackend.getSharedPreferences().edit();
            e.putString(Consts.PREF_KEY_ACCOUNT_NAME, accountName);
            e.commit();
        }

        // post create initialization
        subscribeToBroadcast();
        break;
    }
}

From source file:info.semanticsoftware.semassist.android.activity.AuthenticationActivity.java

/** Sends an authentication request when the save button is pushed.
 * @param v view /*from   w w w  .  j a  v  a 2 s .co  m*/
 */
public void onSaveClick(View v) {
    TextView tvUsername = (TextView) this.findViewById(R.id.uc_txt_username);
    TextView tvPassword = (TextView) this.findViewById(R.id.uc_txt_password);

    //Qualified username, i.e, user@semanticassistants.com
    String qUsername = tvUsername.getText().toString();
    String username = null;
    if (qUsername.indexOf("@") > 0) {
        username = qUsername.substring(0, qUsername.indexOf("@"));
    } else {
        username = qUsername;
    }
    String password = tvPassword.getText().toString();

    //TODO do client-side validation like password length etc.
    String response = authenicate(username, password);
    if (!response.equals(Constants.AUTHENTICATION_FAIL)) {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        Editor editor = settings.edit();
        editor.putString("username", username);
        editor.putString("password", password);
        //FIXME replace this with a descent XML parser
        String acctype = "";
        int start = response.indexOf("<accType>");
        int end = response.indexOf("</accType>");
        if (start > -1 && end > -1) {
            acctype = response.substring(start + "<accType>".length(), end);
        }
        editor.putString("acctype", acctype);
        String sessionId = "";
        start = response.indexOf("<sessionId>");
        end = response.indexOf("</sessionId>");
        if (start > -1 && end > -1) {
            sessionId = response.substring(start + "<sessionId>".length(), end);
        }
        editor.putString("sessionId", sessionId);
        String reqNum = "";
        start = response.indexOf("<reqNum>");
        end = response.indexOf("</reqNum>");
        if (start > -1 && end > -1) {
            reqNum = response.substring(start + "<reqNum>".length(), end);
        }
        editor.putString("reqNum", reqNum);
        boolean result = editor.commit();
        if (result) {
            Toast.makeText(this, R.string.authenticationSuccess, Toast.LENGTH_LONG).show();
            String accountType = this.getIntent().getStringExtra("auth.token");
            if (accountType == null) {
                accountType = SemAssistAuthenticator.ACCOUNT_TYPE;
            }

            AccountManager accMgr = AccountManager.get(this);

            // Add the account to the Android Account Manager
            String accountName = username + "@semanticassistants.com";
            final Account account = new Account(accountName, accountType);
            accMgr.addAccountExplicitly(account, password, null);

            // Inform the caller (could be the Android Account Manager or the SA app) that the process was successful
            final Intent intent = new Intent();
            intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, accountName);
            intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, accountType);
            intent.putExtra(AccountManager.KEY_AUTHTOKEN, accountType);
            this.setAccountAuthenticatorResult(intent.getExtras());
            this.setResult(RESULT_OK, intent);
            this.finish();
        } else {
            Toast.makeText(this, "Could not write the preferences.", Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(this, R.string.authenticationFail, Toast.LENGTH_LONG).show();
    }
}

From source file:com.teocci.utubinbg.fragments.PlaylistsFragment.java

/**
 * Handles Google OAuth 2.0 authorization or account chosen result
 *
 * @param requestCode/*w w w. j a va2  s .  c  o m*/
 * @param resultCode
 * @param data
 */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case REQUEST_AUTHORIZATION:
        if (resultCode != Activity.RESULT_OK) {
            chooseAccount();
        }
        break;
    case REQUEST_ACCOUNT_PICKER:
        if (resultCode == Activity.RESULT_OK && data != null && data.getExtras() != null) {
            String accountName = data.getExtras().getString(AccountManager.KEY_ACCOUNT_NAME);
            if (accountName != null) {
                mChosenAccountName = accountName;
                youTubeSearch.setAuthSelectedAccountName(accountName);
                userNameTextView.setText(extractUserName(mChosenAccountName));
                Toast.makeText(getContext(), "Hi " + extractUserName(mChosenAccountName), Toast.LENGTH_SHORT)
                        .show();
                saveAccount();
            }

            youTubeSearch.searchPlaylists();
        }
        break;
    }
}

From source file:org.cloudfoundry.android.cfdroid.CloudFoundry.java

/**
 * Makes sure that we have a reference to a non-null
 * {@link CloudFoundryClient} object./*ww  w  .  j a va  2s . c  om*/
 * 
 */
private void ensureClient() {
    if (cache.client != null) {
        return;
    }
    try {
        AccountManagerFuture<Bundle> future = accountManager.getAuthTokenByFeatures(Accounts.ACCOUNT_TYPE,
                Accounts.ACCOUNT_TYPE, new String[0], activity, null, null, null, null);
        Bundle bundle = future.getResult();
        String targetURL = Accounts.extractTarget(bundle.getString(AccountManager.KEY_ACCOUNT_NAME));
        cache.token = bundle.getString(AccountManager.KEY_AUTHTOKEN);

        cache.client = new CloudFoundryClient(cache.token, targetURL);
    } catch (Exception e) {
        Ln.e(e, "Logged from here");
    }
}

From source file:pt.up.mobile.authenticator.AuthenticatorActivity.java

/**
 * Called when response is received from the server for confirm credentials
 * request. See onAuthenticationResult(). Sets the
 * AccountAuthenticatorResult which is sent back to the caller.
 * /*from  w w w. ja  v a 2s .  c om*/
 * @param result
 *            the confirmCredentials result.
 */
private void finishConfirmCredentials(boolean result) {
    Log.i(TAG, "finishConfirmCredentials()");
    final Account account = new Account(mUsername, Constants.ACCOUNT_TYPE);
    mAccountManager.setPassword(account, mPassword);
    final Intent intent = new Intent();
    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername);
    intent.putExtra(AccountManager.KEY_BOOLEAN_RESULT, result);
    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);
    finish();
}

From source file:org.voidsink.anewjkuapp.activity.KusssAuthenticatorActivity.java

private void submit() {
    mSubmit.setEnabled(false);/*  www  .java  2  s  .  c om*/

    final String userName = ((TextView) findViewById(R.id.accountName)).getText().toString();
    final String userPass = ((TextView) findViewById(R.id.accountPassword)).getText().toString();

    final String accountType = getIntent().getStringExtra(KusssAuthenticator.ARG_ACCOUNT_TYPE);

    new AsyncTask<String, Void, Intent>() {
        private ProgressDialog progressDialog;
        private Context mContext;

        @Override
        protected void onPreExecute() {
            mContext = KusssAuthenticatorActivity.this;

            progressDialog = ProgressDialog.show(KusssAuthenticatorActivity.this,
                    mContext.getString(R.string.progress_title), mContext.getString(R.string.progress_login),
                    true);
        }

        @Override
        protected Intent doInBackground(String... params) {
            Bundle data = new Bundle();
            try {
                final String authtoken = KusssHandler.getInstance().login(KusssAuthenticatorActivity.this,
                        userName, userPass);

                KusssHandler.getInstance().logout(KusssAuthenticatorActivity.this);

                data.putString(AccountManager.KEY_ACCOUNT_NAME, userName);
                data.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType);
                data.putString(AccountManager.KEY_AUTHTOKEN, authtoken);
                data.putString(PARAM_USER_PASS, userPass);

            } catch (Exception e) {
                data.putString(KEY_ERROR_MESSAGE, e.getMessage());
            }

            final Intent res = new Intent();
            res.putExtras(data);
            return res;
        }

        @Override
        protected void onPostExecute(Intent intent) {
            if (intent.hasExtra(KEY_ERROR_MESSAGE)) {
                Toast.makeText(getBaseContext(), intent.getStringExtra(KEY_ERROR_MESSAGE), Toast.LENGTH_SHORT)
                        .show();
            } else if (intent.hasExtra(AccountManager.KEY_AUTHTOKEN)) {
                String authToken = intent.getStringExtra(AccountManager.KEY_AUTHTOKEN);
                if ((authToken != null) && !TextUtils.isEmpty(authToken)) {
                    finishLogin(intent);
                } else {
                    Toast.makeText(getBaseContext(),
                            mContext.getString(R.string.account_login_failed_wrong_pwd), Toast.LENGTH_SHORT)
                            .show();
                }
            } else {
                Toast.makeText(getBaseContext(),
                        mContext.getString(R.string.account_login_failed_wrong_auth_token), Toast.LENGTH_SHORT)
                        .show();
            }

            progressDialog.dismiss();

            mSubmit.setEnabled(true);

            mContext = null;
        }
    }.execute();
}