Example usage for android.accounts AccountManager KEY_INTENT

List of usage examples for android.accounts AccountManager KEY_INTENT

Introduction

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

Prototype

String KEY_INTENT

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

Click Source Link

Document

Bundle key used for an Intent in results from methods that may require the caller to interact with the user.

Usage

From source file:com.example.jumpnote.android.jsonrpc.AuthenticatedJsonRpcJavaClient.java

public void blockingAuthenticateAccount(final Account account, final int needAuthAction,
        boolean forceReauthenticate) throws AuthenticationException, OperationCanceledException,
        RequestedUserAuthenticationException, InvalidAuthTokenException {

    String existingToken = mTokenStoreHelper.getToken(account);
    if (!forceReauthenticate && existingToken != null) {
        BasicClientCookie c = new BasicClientCookie("ACSID", existingToken);
        try {/* ww  w  .  j av a 2s.  c  o m*/
            c.setDomain(new URI(Config.SERVER_BASE_URL).getHost());
            mHttpClient.getCookieStore().addCookie(c);
            return;
        } catch (URISyntaxException e) {
        }
    }

    // Get an auth token for this account.
    AccountManager am = AccountManager.get(mContext);
    Bundle authBundle = null;
    String authToken = null;

    // Block on getting the auth token result.
    try {
        authBundle = am.getAuthToken(account, APPENGINE_SERVICE_NAME, needAuthAction == NEED_AUTH_NOTIFICATION,
                null, null).getResult();
    } catch (IOException e) {
        throw new AuthenticationException("IOException while getting auth token.", e);
    } catch (AuthenticatorException e) {
        throw new AuthenticationException("AuthenticatorException while getting auth token.", e);
    }

    if (authBundle.containsKey(AccountManager.KEY_INTENT) && needAuthAction == NEED_AUTH_INTENT) {
        Intent authRequestIntent = (Intent) authBundle.get(AccountManager.KEY_INTENT);
        mContext.startActivity(authRequestIntent);
        throw new RequestedUserAuthenticationException();
    } else if (authBundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
        authToken = authBundle.getString(AccountManager.KEY_AUTHTOKEN);
    }

    if (authToken == null) {
        throw new AuthenticationException("Retrieved auth token was null.");
    }

    try {
        blockingAuthenticateWithToken(account, authToken);
    } catch (InvalidAuthTokenException e) {
        am.invalidateAuthToken(account.type, authToken);
        throw e;
    }
}

From source file:com.samsung.android.remindme.jsonrpc.AuthenticatedJsonRpcJavaClient.java

public void blockingAuthenticateAccount(final Account account, final int needAuthAction,
        boolean forceReauthenticate) throws AuthenticationException, OperationCanceledException,
        RequestedUserAuthenticationException, InvalidAuthTokenException {

    String existingToken = mTokenStoreHelper.getToken(account);
    if (!forceReauthenticate && existingToken != null) {
        BasicClientCookie c = new BasicClientCookie("ACSID", existingToken);
        try {/*from   w  ww.  ja v a 2  s .c o m*/
            c.setDomain(new URI(Config.SERVER_BASE_URL).getHost());
            mHttpClient.getCookieStore().addCookie(c);
            return;
        } catch (URISyntaxException e) {
        }
    }

    // Get an auth token for this account.
    AccountManager am = AccountManager.get(mContext);
    Bundle authBundle = null;
    String authToken = null;

    // Block on getting the auth token result.
    try {
        authBundle = am.getAuthToken(account, APPENGINE_SERVICE_NAME, needAuthAction == NEED_AUTH_NOTIFICATION,
                null, null).getResult();
    } catch (IOException e) {
        throw new AuthenticationException("IOException while getting auth token.", e);
    } catch (AuthenticatorException e) {
        throw new AuthenticationException("AuthenticatorException while getting auth token.", e);
    }

    if (authBundle.containsKey(AccountManager.KEY_INTENT) && needAuthAction == NEED_AUTH_INTENT) {
        Intent authRequestIntent = (Intent) authBundle.get(AccountManager.KEY_INTENT);
        mContext.startActivity(authRequestIntent);
        throw new RequestedUserAuthenticationException();
    } else if (authBundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
        authToken = authBundle.getString(AccountManager.KEY_AUTHTOKEN);
        System.out.println(authToken);
        System.out.println(AccountManager.KEY_AUTHTOKEN);
    }

    if (authToken == null) {
        throw new AuthenticationException("Retrieved auth token was null.");
    }

    try {
        blockingAuthenticateWithToken(account, authToken);
    } catch (InvalidAuthTokenException e) {
        am.invalidateAuthToken(account.type, authToken);
        throw e;
    }
}

From source file:com.noswap.keyring.MainActivity.java

public void doGCMStuff() {
    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);

    final String regId = GCMRegistrar.getRegistrationId(this);
    if (regId.equals("")) {
        Log.v(TAG, "Registering");
        GCMRegistrar.register(this, SENDER_ID);
    } else {/*from w  ww .ja v  a2 s. c  o m*/
        Log.v(TAG, "Already registered: " + regId);
    }

    AccountManager am = AccountManager.get(this);
    Account[] accounts = am.getAccountsByType("com.google");
    for (Account account : accounts) {
        Log.v(TAG, "Account: " + account.name + " (" + account.type + ")");
    }

    if (accounts.length > 0) {
        Account account = accounts[0];
        Bundle options = new Bundle();
        am.getAuthToken(account, "Keyring", options, this, new AccountManagerCallback<Bundle>() {
            @Override
            public void run(AccountManagerFuture<Bundle> result) {
                try {
                    Bundle bundle = result.getResult();
                    String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                    Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
                    if (intent != null) {
                        startActivityForResult(intent, 0);
                        return;
                    }
                    Log.v(TAG, "onTokenAcquired: " + token);
                } catch (Exception e) {
                    Log.v(TAG, "onTokenAcquired exception: " + e.toString());
                }
            }
        }, new Handler() {
        });

    }
}

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

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

    Log.d(TAG, "Get Auth Token");
    // If the caller requested an authToken type we don't support, then
    // return an error
    if (!authTokenType.equals(GMSApplication.AUTHTOKEN_TYPE)) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
        return result;
    }// w  w  w . j a v  a 2  s.  c o m

    // Extract the username and password from the Account Manager, and ask
    // the server for an appropriate AuthToken.
    final String password = AccountManager.get(mContext).getPassword(account);
    if (!TextUtils.isEmpty(password)) {
        try {
            final String authToken = NetworkUtilities.authenticate(account.name, password);
            if (!TextUtils.isEmpty(authToken)) {
                final Bundle result = new Bundle();
                result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
                result.putString(AccountManager.KEY_ACCOUNT_TYPE, GMSApplication.ACCOUNT_TYPE);
                result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
                return result;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    // 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 panel.
    final Intent intent = new Intent(mContext, GMSAuthenticatorActivity.class);
    intent.putExtra(GMSAuthenticatorActivity.PARAM_USERNAME, account.name);
    intent.putExtra(GMSAuthenticatorActivity.PARAM_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.arthackday.killerapp.util.Util.java

public String getGoogleAuth(String type) {
    AccountManager mgr = AccountManager.get(activity);
    Account[] accts = mgr.getAccountsByType("com.google");

    if (accts.length == 0) {
        return null;
    }/* w  ww.  ja  v a 2  s  .  c o m*/

    try {
        Account acct = accts[0];
        Log.d(LOG_TAG, "acct name=" + acct.name);
        AccountManagerFuture<Bundle> accountManagerFuture = mgr.getAuthToken(acct, type, null, activity, null,
                null);

        Bundle authTokenBundle = accountManagerFuture.getResult();

        if (authTokenBundle.containsKey(AccountManager.KEY_INTENT)) {
            Intent authRequestIntent = (Intent) authTokenBundle.get(AccountManager.KEY_INTENT);
            activity.startActivity(authRequestIntent);
        }

        return authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
    } catch (OperationCanceledException e) {
        e.printStackTrace();
    } catch (AuthenticatorException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

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

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

    if (!authTokenType.equals(AuthenticatorActivity.PARAM_AUTHTOKEN_TYPE)) {

        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");

        return result;
    }/*  w  ww  . j  av a 2 s.com*/

    final AccountManager am = AccountManager.get(mContext);
    final String password = am.getPassword(account);

    if (password != null) {
        boolean verified = false;

        String loginResponse = null;
        try {
            loginResponse = LoginServiceImpl.sendCredentials(account.name, password);
            verified = LoginServiceImpl.hasLoggedIn(loginResponse);
        } catch (AndroidHacksException e) {
            verified = false;
        }

        if (verified) {
            final Bundle result = new Bundle();
            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, AuthenticatorActivity.PARAM_ACCOUNT_TYPE);

            return result;
        }
    }
    // Password is missing or incorrect
    final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
    intent.putExtra(AuthenticatorActivity.PARAM_USER, account.name);
    intent.putExtra(AuthenticatorActivity.PARAM_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.clearcenter.mobile_demo.mdAuthenticator.java

public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
        Bundle loginOptions) throws NetworkErrorException {
    Log.v(TAG, "getAuthToken()");

    // If the caller requested an authToken type we don't support, then
    // return an error
    if (!authTokenType.equals(mdConstants.AUTHTOKEN_TYPE)) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
        return result;
    }/*from www.  j  av a 2  s.  c o m*/

    // Extract the username and password from the Account Manager, and ask
    // the server for an appropriate AuthToken.
    final AccountManager am = AccountManager.get(ctx);
    final String password = am.getPassword(account);
    final String hostname = am.getUserData(account, "hostname");
    final String username = am.getUserData(account, "username");

    if (password != null) {
        try {
            mdSSLUtil.DisableSecurity();

            final String authToken = mdRest.Login(hostname, username, null, password);
            if (!TextUtils.isEmpty(authToken)) {
                final Bundle result = new Bundle();
                result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
                result.putString(AccountManager.KEY_ACCOUNT_TYPE, mdConstants.ACCOUNT_TYPE);
                result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
                return result;
            }
        } catch (JSONException e) {
            Log.e(TAG, "JSONException", e);
        } catch (GeneralSecurityException e) {
            Log.e(TAG, "GeneralSecurityException", e);
        }
    }

    Log.v(TAG, "Asking for password again...");

    // 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 mdAuthenticatorActivity panel.
    final Intent intent = new Intent(ctx, mdAuthenticatorActivity.class);
    intent.putExtra(mdAuthenticatorActivity.PARAM_NICKNAME, account.name);
    intent.putExtra(mdAuthenticatorActivity.PARAM_USERNAME, username);
    intent.putExtra(mdAuthenticatorActivity.PARAM_HOSTNAME, hostname);
    intent.putExtra(mdAuthenticatorActivity.PARAM_AUTHTOKEN_TYPE, authTokenType);
    intent.putExtra(mdAuthenticatorActivity.PARAM_CONFIRM_CREDENTIALS, true);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);

    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);

    return bundle;
}

From source file:eu.masconsult.bgbanking.accounts.AccountAuthenticator.java

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
        Bundle options) throws NetworkErrorException {
    Log.v(TAG, "getAuthToken(account: " + account + ", authTokenType: " + authTokenType + ")");

    Bank bank = Bank.fromAccountType(context, account.type);
    if (bank == null) {
        throw new IllegalArgumentException("unsupported account type " + account.type);
    }//from w  w  w.j a  v  a 2  s . c  o  m
    if (!Constants.getAuthorityType(context).equals(authTokenType)) {
        throw new IllegalArgumentException("unsupported authTOkenType " + authTokenType);
    }

    final Intent intent = new Intent(context, LoginActivity.class);
    intent.putExtra(KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    intent.putExtra(KEY_ACCOUNT_NAME, account.name);
    intent.putExtra(KEY_ACCOUNT_TYPE, account.type);

    String password = accountManager.getPassword(account);
    try {
        String authToken = bank.getClient().authenticate(account.name, password);
        Log.v(TAG, "obtained auth token " + authToken);

        if (authToken == null) {
            throw new AuthenticationException("no authToken");
        }

        // store the new auth token and return it
        accountManager.setAuthToken(account, authTokenType, authToken);
        intent.putExtra(KEY_AUTHTOKEN, authToken);
        return intent.getExtras();
    } catch (ParseException e) {
        Log.w(TAG, "ParseException", e);
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_ERROR_CODE, 1);
        bundle.putString(KEY_ERROR_MESSAGE, e.getMessage());
        return bundle;
    } catch (IOException e) {
        Log.w(TAG, "IOException", e);
        throw new NetworkErrorException(e);
    } catch (CaptchaException e) {
        Log.w(TAG, "CaptchaException", e);
        // We need human to verify captcha
        final Bundle bundle = new Bundle();
        bundle.putParcelable(AccountManager.KEY_INTENT, intent);
        intent.putExtra(KEY_CAPTCHA_URI, e.getCaptchaUri());
        return bundle;
    } catch (AuthenticationException e) {
        Log.w(TAG, "AuthenticationException", e);
        // we need new credentials
        final Bundle bundle = new Bundle();
        bundle.putParcelable(AccountManager.KEY_INTENT, intent);
        return bundle;
    }
}

From source file:com.notifry.android.remote.BackendClient.java

private String getAuthToken(Context context, Account account) throws PendingAuthException {
    String authToken = null;/* w  ww.  j  a v  a2  s.c o  m*/
    AccountManager accountManager = AccountManager.get(context);
    try {
        AccountManagerFuture<Bundle> future = accountManager.getAuthToken(account, "ah", false, null, null);
        Bundle bundle = future.getResult();
        authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
        // User will be asked for "App Engine" permission.
        if (authToken == null) {
            // No auth token - will need to ask permission from user.
            Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
            if (intent != null) {
                // User input required
                context.startActivity(intent);
                throw new PendingAuthException("Asking user for permission.");
            }
        }
    } catch (OperationCanceledException e) {
        Log.w(TAG, e.getMessage());
    } catch (AuthenticatorException e) {
        Log.w(TAG, e.getMessage());
    } catch (IOException e) {
        Log.w(TAG, e.getMessage());
    }

    return authToken;
}

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

/**
 * {@inheritDoc}/*from ww w  . j  a v a  2 s.  c  o 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;
}