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.newtifry.android.remote.BackendClient.java

private String getAuthToken(Context context, Account account) throws PendingAuthException {
    String authToken = null;/*from  www  . j a  v a  2 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 authorization 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.d(TAG, e.getMessage());
    } catch (AuthenticatorException e) {
        Log.d(TAG, e.getMessage());
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
    }

    return authToken;
}

From source file:org.pixmob.appengine.client.AppEngineClient.java

private String getAuthToken() throws AppEngineAuthenticationException {
    // get an authentication token from the AccountManager:
    // this call is asynchronous, as the user may not respond immediately
    final AccountManagerFuture<Bundle> futureBundle = accountManager.getAuthToken(account, "ah", true, null,
            null);//  w  w  w.j av a2  s .c  o  m
    final Bundle authBundle;
    try {
        authBundle = futureBundle.getResult();
    } catch (OperationCanceledException e) {
        throw new AppEngineAuthenticationException(AUTHENTICATION_UNAVAILABLE, e);
    } catch (AuthenticatorException e) {
        throw new AppEngineAuthenticationException(AUTHENTICATION_UNAVAILABLE, e);
    } catch (IOException e) {
        throw new AppEngineAuthenticationException(AUTHENTICATION_UNAVAILABLE, e);
    }

    final String authToken = authBundle.getString(AccountManager.KEY_AUTHTOKEN);
    if (authToken == null) {
        // no authentication token was given: the user should give its
        // permission through an item in the notification bar
        Log.i(TAG, "Authentication permission is required");

        final Intent authPermIntent = (Intent) authBundle.get(AccountManager.KEY_INTENT);
        int flags = authPermIntent.getFlags();
        flags &= ~Intent.FLAG_ACTIVITY_NEW_TASK;
        authPermIntent.setFlags(flags);

        throw new AppEngineAuthenticationException(AUTHENTICATION_PENDING, authPermIntent);
    }

    return authToken;
}

From source file:com.mobiperf.speedometer.AccountSelector.java

private void getAuthToken(AccountManagerFuture<Bundle> result) {
    Logger.i("getAuthToken() called, result " + result);
    String errMsg = "Failed to get login cookie. ";
    Bundle bundle;//from   w ww .  j  av  a 2 s  .  co  m
    try {
        bundle = result.getResult();
        Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
        if (intent != null) {
            // User input required. (A UI will pop up for user's consent to
            // allow
            // this app access account information.)
            Logger.i("Starting account manager activity");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } else {
            Logger.i("Executing getCookie task");
            synchronized (this) {
                this.authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                this.checkinFuture = checkinExecutor.submit(new GetCookieTask());
            }
        }
    } catch (OperationCanceledException e) {
        Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    } catch (AuthenticatorException e) {
        Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    } catch (IOException e) {
        Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    }
}

From source file:com.mobiperf.AccountSelector.java

private void getAuthToken(AccountManagerFuture<Bundle> result) {
    Logger.i("getAuthToken() called, result " + result);
    String errMsg = "Failed to get login cookie. ";
    Bundle bundle;// w  w w .j a  va 2s.  co  m
    try {
        bundle = result.getResult();
        Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
        if (intent != null) {
            // User input required. (A UI will pop up for user's consent to allow
            // this app access account information.)
            Logger.i("Starting account manager activity");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } else {
            Logger.i("Executing getCookie task");
            synchronized (this) {
                this.authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                this.checkinFuture = checkinExecutor.submit(new GetCookieTask());
            }
        }
    } catch (OperationCanceledException e) {
        Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    } catch (AuthenticatorException e) {
        Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    } catch (IOException e) {
        Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    }
}

From source file:com.num.mobiperf.AccountSelector.java

private void getAuthToken(AccountManagerFuture<Bundle> result) {
    //Logger.i("getAuthToken() called, result " + result);
    String errMsg = "Failed to get login cookie. ";
    Bundle bundle;//w ww.j a v  a 2 s . c om
    try {
        bundle = result.getResult();
        Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
        if (intent != null) {
            // User input required. (A UI will pop up for user's consent to allow
            // this app access account information.)
            //Logger.i("Starting account manager activity");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } else {
            //Logger.i("Executing getCookie task");
            synchronized (this) {
                this.authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                this.checkinFuture = checkinExecutor.submit(new GetCookieTask());
            }
        }
    } catch (OperationCanceledException e) {
        //Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    } catch (AuthenticatorException e) {
        //Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    } catch (IOException e) {
        //Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    }
}

From source file:com.mobilyzer.AccountSelector.java

private void getAuthToken(AccountManagerFuture<Bundle> result) {
    Logger.i("getAuthToken() called, result " + result);
    String errMsg = "Failed to get login cookie. ";
    Bundle bundle;/*from www.  j  a  va  2 s . c  om*/
    try {
        bundle = result.getResult();
        Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
        if (intent != null) {
            // User input required. (A UI will pop up for user's consent to allow
            // this app access account information.)
            Logger.i("Starting account manager activity");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            context.startActivity(intent);
        } else {
            Logger.i("Executing getCookie task");
            synchronized (this) {
                this.authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                this.checkinFuture = checkinExecutor.submit(new GetCookieTask());
            }
        }
    } catch (OperationCanceledException e) {
        Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    } catch (AuthenticatorException e) {
        Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    } catch (IOException e) {
        Logger.e(errMsg, e);
        throw new RuntimeException("Can't get login cookie", e);
    }
}

From source file:de.unclenet.dehabewe.CalendarActivity.java

private void gotAccount(final AccountManager manager, final Account account) {
    SharedPreferences settings = getSharedPreferences(PREF, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("accountName", account.name);
    editor.commit();//from  w w w.j a va 2s .c o  m
    new Thread() {

        @Override
        public void run() {
            try {
                final Bundle bundle = manager.getAuthToken(account, AUTH_TOKEN_TYPE, true, null, null)
                        .getResult();
                runOnUiThread(new Runnable() {

                    public void run() {
                        try {
                            if (bundle.containsKey(AccountManager.KEY_INTENT)) {
                                Intent intent = bundle.getParcelable(AccountManager.KEY_INTENT);
                                int flags = intent.getFlags();
                                flags &= ~Intent.FLAG_ACTIVITY_NEW_TASK;
                                intent.setFlags(flags);
                                startActivityForResult(intent, REQUEST_AUTHENTICATE);
                            } else if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
                                authenticatedClientLogin(bundle.getString(AccountManager.KEY_AUTHTOKEN));
                            }
                        } catch (Exception e) {
                            handleException(e);
                        }
                    }
                });
            } catch (Exception e) {
                handleException(e);
            }
        }
    }.start();
}

From source file:eu.trentorise.smartcampus.ac.authenticator.AMSCAccessProvider.java

@Override
public String promote(final Activity activity, String inAuthority, final String token) {
    final String authority = inAuthority == null ? Constants.AUTHORITY_DEFAULT : inAuthority;
    final AccountManager am = AccountManager.get(activity);
    //      Bundle options = new Bundle();
    //      if (token != null) {
    //         options.putString(Constants.PROMOTION_TOKEN, token);
    //      }//from  www . ja  v a2  s .  co  m
    final Account a = new Account(Constants.getAccountName(activity), Constants.getAccountType(activity));
    final String userDataString = am.getUserData(a, AccountManager.KEY_USERDATA);

    invalidateToken(activity, authority);

    am.getAuthToken(new Account(Constants.getAccountName(activity), Constants.getAccountType(activity)),
            authority, null, null, new AccountManagerCallback<Bundle>() {
                @Override
                public void run(AccountManagerFuture<Bundle> result) {
                    Bundle bundle = null;
                    try {
                        bundle = result.getResult();
                        Intent launch = (Intent) bundle.get(AccountManager.KEY_INTENT);
                        if (launch != null) {
                            launch.putExtra(Constants.KEY_AUTHORITY, authority);
                            launch.putExtra(Constants.PROMOTION_TOKEN, token);
                            launch.putExtra(Constants.OLD_DATA, userDataString);
                            activity.startActivityForResult(launch, SC_AUTH_ACTIVITY_REQUEST_CODE);
                        } else if (bundle.getString(AccountManager.KEY_AUTHTOKEN) != null) {
                            //                         am.setAuthToken(a, authority, bundle.getString(AccountManager.KEY_AUTHTOKEN));
                            //                         am.addAccountExplicitly(a, null, null);
                            // no token acquired
                        } else {
                            storeAnonymousToken(token, authority, am, a);
                        }
                    } catch (Exception e) {
                        // revert the invalidated token
                        storeAnonymousToken(token, authority, am, a);
                        return;
                    }
                }
            }, null);
    return null;
}

From source file:com.google.ipc.invalidation.ticl.android.AndroidChannel.java

/**
 * Initiates acquisition of an authentication token that can be used with channel HTTP requests.
 * Android token acquisition is asynchronous since it may require HTTP interactions with the
 * ClientLogin servers to obtain the token.
 */// w w  w. j  a va2  s  .  c o m
@SuppressWarnings("deprecation")

synchronized void requestAuthToken(final CompletionCallback callback) {
    // If there is currently no token and no pending request, initiate one.
    if (disableAccountManager) {
        logger.fine("Not requesting auth token since account manager disabled");
        return;
    }
    if (authToken == null) {
        // Ask the AccountManager for the token, with a pending future to store it on the channel
        // once available.
        final AndroidChannel theChannel = this;
        AccountManager accountManager = AccountManager.get(proxy.getService());
        accountManager.getAuthToken(proxy.getAccount(), proxy.getAuthType(), true,
                new AccountManagerCallback<Bundle>() {
                    @Override
                    public void run(AccountManagerFuture<Bundle> future) {
                        try {
                            Bundle result = future.getResult();
                            if (result.containsKey(AccountManager.KEY_INTENT)) {
                                // TODO: Handle case where there are no authentication
                                // credentials associated with the client account
                                logger.severe("Token acquisition requires user login");
                                callback.success(); // No further retries.
                            }
                            setAuthToken(result.getString(AccountManager.KEY_AUTHTOKEN));
                        } catch (OperationCanceledException exception) {
                            logger.warning("Auth cancelled", exception);
                            // TODO: Send error to client
                        } catch (AuthenticatorException exception) {
                            logger.warning("Auth error acquiring token", exception);
                            callback.failure();
                        } catch (IOException exception) {
                            logger.warning("IO Exception acquiring token", exception);
                            callback.failure();
                        }
                    }
                }, null);
    } else {
        logger.fine("Auth token request already pending");
        callback.success();
    }
}

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

private void verifyNotifyUserBundle(Bundle bundle) {
    Intent intent = bundle.getParcelable(AccountManager.KEY_INTENT);
    assertNotNull(intent);//from  w  w w.  ja  v a2  s .  co  m
    assertEquals(intent.getComponent().getClassName(), AuthenticatorActivity.class.getName());
    assertTrue(intent.getBooleanExtra(AuthenticatorActivity.EXTRA_FROM_AUTHENTICATOR, false));
}