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.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 w  w.j  a  v  a  2  s  . 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:info.semanticsoftware.semassist.android.activity.AuthenticationActivity.java

/** Sends an authentication request when the save button is pushed.
 * @param v view //  w w w  . ja v a  2  s .c o  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:org.voidsink.anewjkuapp.activity.KusssAuthenticatorActivity.java

private void submit() {
    mSubmit.setEnabled(false);/*from   ww  w.j a  va 2s  .  com*/

    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();
}

From source file:com.ntsync.android.sync.activities.KeyPasswordActivity.java

/**
 * Called when KeyPassword was set./*from  ww  w  .  j  a v  a 2 s. c  o  m*/
 * 
 * @param result
 *            the confirmCredentials result.
 */
private void finishSetKeyPassword() {
    Log.i(TAG, "finishSetKeyPassword()");
    final Intent intent = new Intent();
    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
    setResult(RESULT_OK, intent);

    Account account = new Account(mUsername, Constants.ACCOUNT_TYPE);
    ContentResolver.requestSync(account, Constants.CONTACT_AUTHORITY, new Bundle());

    // Remove notification
    clearNotification(account.name);
    finish();
}

From source file:org.ohmage.auth.Authenticator.java

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
        Bundle loginOptions) throws NetworkErrorException {

    // Extract the email and refresh_token from the Account Manager, and ask
    // the server for a new refresh_token.
    String authToken = am.peekAuthToken(account, authTokenType);

    // Lets give another try to authenticate the user
    AccessToken token = null;//from  www .  j  a v  a  2 s. com
    UserRecoverableAuthException userRecoverableAuthException = null;

    if (TextUtils.isEmpty(authToken)) {
        final String refreshToken = am.getPassword(account);
        Log.i(TAG, "Auth token is null");
        if (refreshToken != null) {
            Log.i(TAG, "Refresh Token");
            try {
                // If the account credentials have not gone to the server yet we saved the
                // password for the user
                if (Boolean.parseBoolean(am.getUserData(account, USE_PASSWORD))) {
                    token = ohmageService.getAccessToken(account.name, refreshToken);
                    if (token != null) {
                        am.setUserData(account, Authenticator.USE_PASSWORD, String.valueOf(false));
                        am.setUserData(account, Authenticator.USER_ID, token.getUserId());
                    }
                } else {
                    // refresh token
                    if (Ohmage.USE_DSU_DATAPOINTS_API) {

                        Log.i(TAG, "Refresh Token with DSU");
                        token = ohmageService.refreshAccessToken(refreshToken, "refresh_token");
                    } else {
                        token = ohmageService.getAccessToken(refreshToken);
                    }
                }
            } catch (AuthenticationException e) {
                // This will happen if the refresh token was already used, or it was
                // invalidated or something

                // We can try getting the token from google
                String googleAccount = am.getUserData(account, USER_DATA_GOOGLE_ACCOUNT);
                if (googleAccount != null) {
                    try {
                        token = getTokenFromGoogle(googleAccount);
                        Log.i(TAG, token.toString());
                    } catch (UserRecoverableAuthException e1) {
                        userRecoverableAuthException = e1;
                    }
                }
            } catch (RetrofitError e) {
                if (e.getResponse() != null && e.getResponse().getStatus() == 409) {
                    // The user hasn't activated their account by clicking the link
                    final Bundle bundle = new Bundle();
                    bundle.putString(AccountManager.KEY_AUTH_FAILED_MESSAGE,
                            Ohmage.app().getString(R.string.account_not_activated));
                    bundle.putParcelable(AccountManager.KEY_INTENT,
                            new Intent(mContext, AccountNotActivatedDialog.class));
                    return bundle;
                } else {
                    throw new NetworkErrorException();
                }
            } catch (Exception e) {
                Log.e(TAG, "", e);
            }
        }
    }

    // If we get an authToken - we return it
    if (token != null) {
        am.setPassword(account, token.getRefreshToken());
        authToken = token.getAccessToken();
    }

    if (!TextUtils.isEmpty(authToken)) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
        return result;
    }

    // If we get here, then we couldn't access the user's password - so we
    // need to re-prompt them for their credentials. We do that by creating
    // an intent to display our AuthenticatorActivity.
    final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    intent.putExtra(AuthenticatorActivity.EXTRA_FROM_AUTHENTICATOR, true);
    intent.putExtra(AuthenticatorActivity.EXTRA_HANDLE_USER_RECOVERABLE_ERROR, userRecoverableAuthException);
    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    return bundle;
}

From source file:com.pindroid.authenticator.AuthenticatorActivity.java

/**
 * /*from  ww  w . jav a 2s.co  m*/
 * Called when response is received from the server for authentication
 * request. See onAuthenticationResult(). Sets the
 * AccountAuthenticatorResult which is sent back to the caller. Also sets
 * the authToken in AccountManager for this account.
 * 
 * @param the confirmCredentials result.
 */
protected void finishLogin(String authToken) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    final int synctime = Integer.parseInt(settings.getString("pref_synctime", "0"));

    final Account account = new Account(mUsername, Constants.ACCOUNT_TYPE);

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

        ContentResolver.setSyncAutomatically(account, BookmarkContentProvider.AUTHORITY, true);
        if (synctime != 0) {
            SyncUtils.addPeriodicSync(BookmarkContentProvider.AUTHORITY, Bundle.EMPTY, synctime, this);
        }
    }

    mAccountManager.setAuthToken(account, Constants.AUTHTOKEN_TYPE, authToken);

    final Intent intent = new Intent();

    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
    intent.putExtra(AccountManager.KEY_AUTHTOKEN, authToken);
    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);
    finish();
}

From source file:com.rukman.emde.smsgroups.authenticator.GMSAuthenticatorActivity.java

/**
 * Called when response is received from the server for authentication
 * request. See onAuthenticationResult(). Sets the
 * AccountAuthenticatorResult which is sent back to the caller. We store the
 * authToken that's returned from the server as the 'password' for this
 * account - so we're never storing the user's actual password locally.
 *
 * @param result the confirmCredentials result.
 */// w w  w .j ava  2 s  .co  m
private void finishLogin(String authToken) {

    Log.i(TAG, "finishLogin()");
    final Account account = new Account(mUsername, GMSApplication.ACCOUNT_TYPE);
    if (mIsUnknownAccountName) {
        mAccountManager.addAccountExplicitly(account, mPassword, null);
        // Set contacts sync for this account.
        ContentResolver.setIsSyncable(account, GMSProvider.AUTHORITY, 1);
        ContentResolver.setSyncAutomatically(account, GMSProvider.AUTHORITY, true);
    } else {
        mAccountManager.setPassword(account, mPassword);
    }
    final Intent intent = new Intent();
    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, GMSApplication.ACCOUNT_TYPE);
    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);
    finish();
}

From source file:org.klnusbaum.udj.auth.AuthActivity.java

/**
 * Called when response is received from the server for authentication
 * request. See onAuthenticationResult(). Sets the
 * AccountAuthenticatorResult which is sent back to the caller. We store the
 * authToken that's returned from the server as the 'password' for this
 * account - so we're never storing the user's actual password locally.
 *
 * @param result the confirmCredentials result.
 *///w  w w . java  2 s .com
private void finishLogin(ServerConnection.AuthResult authResult) {
    Log.i(TAG, "finishLogin()");
    final Account account = new Account(mUsername, Constants.ACCOUNT_TYPE);
    if (mRequestNewAccount) {
        mAccountManager.addAccountExplicitly(account, mPassword, null);
        ContentResolver.setIsSyncable(account, Constants.AUTHORITY, 0);
    } else {
        mAccountManager.setPassword(account, mPassword);
    }
    mAccountManager.setUserData(account, Constants.USER_ID_DATA, authResult.userId);
    mAccountManager.setUserData(account, Constants.LAST_PLAYER_ID_DATA, Constants.NO_PLAYER_ID);
    mAccountManager.setUserData(account, Constants.PLAYER_STATE_DATA,
            Integer.toString(Constants.NOT_IN_PLAYER));
    final Intent intent = new Intent();
    intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername);
    intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
    intent.putExtra(Constants.ACCOUNT_EXTRA, account);
    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);
    finish();
}

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

private void finishLogin(Intent intent) {
    String accountName = intent.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
    String accountPassword = intent.getStringExtra(PARAM_USER_PASS);
    String accountType = intent.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);

    Log.i(TAG, "finish login to " + accountName);

    Account account;//from   w ww .java 2  s.com
    boolean addNewAccount;

    final Account oldAccount = AppUtils.getAccount(this);
    if (oldAccount != null) {
        if (oldAccount.name.equals(accountName)) {
            account = oldAccount;
            addNewAccount = false;
        } else {
            AppUtils.removeAccout(mAccountManager, oldAccount);
            account = new Account(accountName, accountType);
            addNewAccount = true;
        }
    } else {
        account = new Account(accountName, accountType);
        addNewAccount = true;
    }

    if (addNewAccount) {
        String authtoken = intent.getStringExtra(AccountManager.KEY_AUTHTOKEN);
        String authtokenType = mAuthTokenType;

        // Creating the account on the device and setting the auth token we
        // got
        // (Not setting the auth token will cause another call to the server
        // to authenticate the user)
        mAccountManager.addAccountExplicitly(account, accountPassword, null);
        mAccountManager.setAuthToken(account, authtokenType, authtoken);
        mAccountManager.setPassword(account, accountPassword);

        // Turn on periodic syncing
        long interval = PreferenceWrapper.getSyncInterval(this) * 60 * 60;

        ContentResolver.addPeriodicSync(account, CalendarContractWrapper.AUTHORITY(), new Bundle(), interval);
        ContentResolver.addPeriodicSync(account, KusssContentContract.AUTHORITY, new Bundle(), interval);
        // Inform the system that this account supports sync
        ContentResolver.setIsSyncable(account, CalendarContractWrapper.AUTHORITY(), 1);
        ContentResolver.setIsSyncable(account, KusssContentContract.AUTHORITY, 1);
        // Inform the system that this account is eligible for auto sync
        // when the network is up
        ContentResolver.setSyncAutomatically(account, CalendarContractWrapper.AUTHORITY(), true);
        ContentResolver.setSyncAutomatically(account, KusssContentContract.AUTHORITY, true);
        // Recommend a schedule for automatic synchronization. The system
        // may modify this based
        // on other scheduled syncs and network utilization.
    } else {
        mAccountManager.setPassword(account, accountPassword);
    }

    // Kalender aktualisieren
    CalendarUtils.createCalendarsIfNecessary(this, account);

    // Sync NOW
    KusssAuthenticator.triggerSync(this);

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

    finish();
}

From source file:com.beem.project.beem.ui.wizard.AccountConfigureFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == SELECT_ACCOUNT_CODE && resultCode == Activity.RESULT_OK) {
        mSelectedAccountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
        mSelectedAccountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
        onDeviceAccountSelected(mSelectedAccountName, mSelectedAccountType);
    } else if (requestCode == MANUAL_CONFIGURATION_CODE) {
        String login = settings.getString(BeemApplication.ACCOUNT_USERNAME_KEY, "");
        String password = settings.getString(BeemApplication.ACCOUNT_PASSWORD_KEY, "");
        mAccountJID.setText(login);//  www .  j  a va2  s.c o m
        mAccountPassword.setText(password);
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}