Example usage for android.accounts AccountManagerFuture getResult

List of usage examples for android.accounts AccountManagerFuture getResult

Introduction

In this page you can find the example usage for android.accounts AccountManagerFuture getResult.

Prototype

V getResult() throws OperationCanceledException, IOException, AuthenticatorException;

Source Link

Document

Accessor for the future result the AccountManagerFuture represents.

Usage

From source file:cl.chileagil.agileday2012.fragment.MainFragment.java

private void chooseAccount() {
    accountManager.getAccountManager().getAuthTokenByFeatures(GoogleAccountManager.ACCOUNT_TYPE,
            AUTH_TOKEN_TYPE, null, MainFragment.this, null, null, new AccountManagerCallback<Bundle>() {

                public void run(AccountManagerFuture<Bundle> future) {
                    Bundle bundle;//from ww w .  ja v a  2  s . c  om
                    try {
                        bundle = future.getResult();
                        setAccountName(bundle.getString(AccountManager.KEY_ACCOUNT_NAME));
                        setAuthToken(bundle.getString(AccountManager.KEY_AUTHTOKEN));
                        onAuthToken();
                    } catch (OperationCanceledException e) {
                        // user canceled
                    } catch (AuthenticatorException e) {
                        Log.e(TAG, e.getMessage(), e);
                    } catch (IOException e) {
                        Log.e(TAG, e.getMessage(), e);
                    }
                }
            }, null);
}

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

/**
 * Makes sure that we have a reference to a non-null
 * {@link CloudFoundryClient} object.// www  .j a va 2 s .c  o  m
 * 
 */
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: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.
 *//*from   w w  w . j a v a  2 s.  c  om*/
@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:be.evias.cloudLogin.cloudLoginMainActivity.java

private void logoutAccount(final Account account) {
    final AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(account,
            AccountBase.AUTHTOKEN_TYPE_FULL_ACCESS, null, this, null, null);

    new Thread(new Runnable() {
        @Override//from   www  . j a  v  a2 s.c  o m
        public void run() {
            try {
                Bundle bnd = future.getResult();

                final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
                mAccountManager.invalidateAuthToken(account.type, authtoken);

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

From source file:com.myandroidremote.AccountsActivity.java

/**
 * Registers for C2DM messaging with the given account name.
 * //from w w w .ja v  a2  s  .  com
 * @param accountName
 *            a String containing a Google account name
 */
private void register(final String accountName) {
    // Store the account name in shared preferences
    final SharedPreferences prefs = Util.getSharedPreferences(mContext);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(Util.ACCOUNT_NAME, accountName);
    editor.putString(Util.AUTH_COOKIE, null);
    editor.commit();

    // Obtain an auth token and register
    final AccountManager mgr = AccountManager.get(mContext);
    Account[] accts = mgr.getAccountsByType("com.google");
    for (Account acct : accts) {
        if (acct.name.equals(accountName)) {
            if (Util.isDebug(mContext)) {
                // Use a fake cookie for the dev mode app engine server
                // The cookie has the form email:isAdmin:userId
                // We set the userId to be the same as the account name
                String authCookie = "dev_appserver_login=" + accountName + ":false:" + accountName;
                boolean result = prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit();
                C2DMessaging.register(mContext, Setup.SENDER_ID);
            } else {
                // Get the auth token from the AccountManager and convert
                // it into a cookie for the appengine server
                mgr.getAuthToken(acct, "ah", null, this, new AccountManagerCallback<Bundle>() {
                    public void run(AccountManagerFuture<Bundle> future) {
                        try {
                            Bundle authTokenBundle = future.getResult();
                            String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
                            String authCookie = getAuthCookie(authToken);
                            if (authCookie == null) {
                                mgr.invalidateAuthToken("com.google", authToken);
                            }
                            prefs.edit().putString(Util.AUTH_COOKIE, authCookie).commit();

                            C2DMessaging.register(mContext, Setup.SENDER_ID);
                        } catch (AuthenticatorException e) {
                            Log.w(TAG, "Got AuthenticatorException " + e);
                            Log.w(TAG, Log.getStackTraceString(e));
                        } catch (IOException e) {
                            Log.w(TAG, "Got IOException " + Log.getStackTraceString(e));
                            Log.w(TAG, Log.getStackTraceString(e));
                        } catch (OperationCanceledException e) {
                            Log.w(TAG, "Got OperationCanceledException " + e);
                            Log.w(TAG, Log.getStackTraceString(e));
                        }
                    }
                }, null);
            }
            break;
        }
    }
}

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

/**
 * Add new account to the account manager for the cloudLogin
 * account type./*  www.  jav a  2s .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:uk.co.bubblebearapps.contactsintegration.MainActivity.java

/**
 * Add new account to the account manager
 *
 * @param accountType//  ww w .ja  v  a 2  s . c om
 * @param authTokenType
 */
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("Account was created");
                        Log.d("udinic", "AddNewAccount Bundle is " + bnd);

                    } catch (Exception e) {
                        e.printStackTrace();
                        showMessage(e.getMessage());
                    }
                }
            }, null);
}

From source file:com.sefford.beauthentic.activities.LoginActivity.java

void createGoogleAccount(final GoogleSignInAccount acct) {
    final Account account = new Account(acct.getDisplayName(), AuthenticAuthenticator.ACCOUNT_TYPE);
    final AccountManager am = AccountManager.get(this);
    final Bundle data = new Bundle();
    data.putInt(AuthenticAuthenticator.EXTRA_TYPE, AuthenticAuthenticator.Type.GOOGLE.ordinal());
    data.putString(AccountManager.KEY_ACCOUNT_NAME, acct.getDisplayName());
    data.putString(AccountManager.KEY_AUTHTOKEN, acct.getIdToken());
    am.confirmCredentials(account, data, null, new AccountManagerCallback<Bundle>() {
        @Override/* w  ww.  ja  v a2  s  . c  om*/
        public void run(AccountManagerFuture<Bundle> future) {
            try {
                final Bundle result = future.getResult();
                if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) {
                    Sessions.addAccount(am, account, "", Bundle.EMPTY);
                    am.setAuthToken(account, AuthenticAuthenticator.AUTHTOKEN_TYPE,
                            result.getString(AccountManager.KEY_AUTHTOKEN));
                    am.setUserData(account, AuthenticAuthenticator.EXTRA_TYPE,
                            Integer.toString(AuthenticAuthenticator.Type.GOOGLE.ordinal()));
                    notifyLoginToGCM(AuthenticAuthenticator.Type.GOOGLE.ordinal(), account.name, "",
                            result.getString(AccountManager.KEY_AUTHTOKEN));
                    googleApi.saveCredential(new Credential.Builder(acct.getEmail())
                            .setAccountType(IdentityProviders.GOOGLE).setName(acct.getDisplayName())
                            .setProfilePictureUri(acct.getPhotoUrl()).build(),
                            new SmartlockCredentialCallback());
                }
            } catch (OperationCanceledException e) {
                Snackbar.make(vLoginForm, R.string.error_operation_cancelled, Snackbar.LENGTH_LONG).show();
            } catch (IOException e) {
                Snackbar.make(vLoginForm, R.string.error_not_connected_to_internet, Snackbar.LENGTH_LONG)
                        .show();
            } catch (AuthenticatorException e) {
                Snackbar.make(vLoginForm, R.string.error_invalid_credentials, Snackbar.LENGTH_LONG).show();
            }
        }
    }, null);
}

From source file:com.sefford.beauthentic.activities.LoginActivity.java

void performLogin() {
    final AccountManager am = AccountManager.get(this);
    final Bundle data = new Bundle();
    data.putString(AuthenticAuthenticator.EXTRA_PASSWORD, etPassword.getText().toString());
    data.putInt(AuthenticAuthenticator.EXTRA_TYPE, AuthenticAuthenticator.Type.PASSWORD.ordinal());
    final Account account = new Account(etUsername.getText().toString(), AuthenticAuthenticator.ACCOUNT_TYPE);
    am.getAuthToken(account, "", data, true, new AccountManagerCallback<Bundle>() {
        @Override//w w  w  . j  ava2 s .  c o m
        public void run(AccountManagerFuture<Bundle> future) {
            try {
                final Bundle result = future.getResult();
                if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) {
                    Sessions.addAccount(am, account, etPassword.getText().toString(), Bundle.EMPTY);
                    am.setAuthToken(account, AuthenticAuthenticator.AUTHTOKEN_TYPE,
                            result.getString(AccountManager.KEY_AUTHTOKEN));
                    am.setUserData(account, AuthenticAuthenticator.EXTRA_TYPE,
                            Integer.toString(AuthenticAuthenticator.Type.PASSWORD.ordinal()));
                    notifyLoginToGCM(AuthenticAuthenticator.Type.PASSWORD.ordinal(), account.name,
                            etPassword.getText().toString(), result.getString(AccountManager.KEY_AUTHTOKEN));
                    googleApi
                            .saveCredential(
                                    new Credential.Builder(account.name)
                                            .setPassword(etPassword.getText().toString()).build(),
                                    new SmartlockCredentialCallback());
                } else {
                    Snackbar.make(vLoginForm, R.string.error_invalid_credentials, Snackbar.LENGTH_LONG).show();
                }
            } catch (OperationCanceledException e) {
                Snackbar.make(vLoginForm, R.string.error_operation_cancelled, Snackbar.LENGTH_LONG).show();
            } catch (IOException e) {
                Snackbar.make(vLoginForm, R.string.error_not_connected_to_internet, Snackbar.LENGTH_LONG)
                        .show();
            } catch (AuthenticatorException e) {
                Snackbar.make(vLoginForm, R.string.error_invalid_credentials, Snackbar.LENGTH_LONG).show();
            }
        }

    }, null);
}

From source file:com.dhara.googlecalendartrial.MainActivity.java

private void getAccounts() {
    accountManager = AccountManager.get(this.getBaseContext());
    Account[] accounts = accountManager.getAccountsByType("com.google");
    account = accounts[0];/*w  ww. j a  v  a  2 s .c o m*/
    Log.e("tag", "acc : " + account.name + " ");
    accountManager.getAuthToken(account, AUTH_TOKEN_TYPE, null, MainActivity.this,
            new AccountManagerCallback<Bundle>() {
                public void run(AccountManagerFuture<Bundle> future) {
                    try {
                        // If the user has authorized your application to use the tasks API
                        // a token is available.
                        String token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);
                        // Now you can use the Tasks API...
                        useCalendarAPI(token, account.name);
                    } catch (OperationCanceledException e) {
                        // TODO: The user has denied you access to the API, you should handle that
                    } catch (Exception e) {
                        e.printStackTrace();

                        t.send(new HitBuilders.ExceptionBuilder().setDescription(Utilities.getMessage(e))
                                //.setDescription(new StandardExceptionParser(MainActivity.this, null).getDescription(Thread.currentThread().getName(), e))
                                .setFatal(false).build());
                    }
                }
            }, null);

    // Set screen name.
    // Where path is a String representing the screen name.
    t.setScreenName(getString(R.string.path));

    // Send a screen view.
    t.send(new HitBuilders.AppViewBuilder().build());
}