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:saschpe.birthdays.helper.AccountHelper.java

/**
 * Remove account from Android system/* w  w  w.j a v a2  s .c o  m*/
 */
public static boolean removeAccount(Context context) {
    Log.d(TAG, "Removing account...");
    AccountManager manager = AccountManager.get(context);
    final Account account = new Account(context.getString(R.string.app_name),
            context.getString(R.string.account_type));
    AccountManagerFuture<Boolean> future = manager.removeAccount(account, null, null);
    if (future.isDone()) {
        try {
            future.getResult();
            return true;
        } catch (Exception e) {
            Log.e(TAG, "Problem while removing account!", e);
            return false;
        }
    } else {
        return false;
    }
}

From source file:net.heroicefforts.viable.android.rep.it.auth.Authenticate.java

/**
 * Attempts to authenticate the user using a pre-existing stored authentication token.  If an account exists, but no such token 
 * exists, then the user will be prompted by the account authenticator to re-enter their Google credentials to generate the new token.
 * /*from   w w  w  .  j a v a  2s  .  c om*/
 * @param act the calling activity
 * @return the authentication token for the requested service or null if there is no Google Account.
 * @throws AuthenticatorException if an error occurs during authentication.
 * @throws OperationCanceledException
 * @throws IOException
 */
public static String authenticate(Activity act, String serviceCode)
        throws AuthenticatorException, OperationCanceledException, IOException {
    AccountManager mgr = AccountManager.get(act);
    Account[] accts = mgr.getAccountsByType(GCLAccountAuthenticator.ACCT_TYPE);
    if (accts.length > 0) {
        Account acct = accts[0];
        AccountManagerFuture<Bundle> accountManagerFuture = mgr.getAuthToken(acct, serviceCode, null, act, null,
                null);
        Bundle authTokenBundle = accountManagerFuture.getResult();
        String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();

        return authToken;
    } else {
        Log.e(TAG, "No google accounts registered for this device.");
        return null;
    }
}

From source file:sg.macbuntu.android.pushcontacts.DeviceRegistrar.java

private static String getAuthToken(Context context, Account account) {
    String authToken = null;//from   w w  w . j a  v  a2s .  c o  m
    AccountManager accountManager = AccountManager.get(context);
    try {
        AccountManagerFuture<Bundle> future = accountManager.getAuthToken(account, AUTH_TOKEN_TYPE, 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 = new Intent(ActivityUI.AUTH_PERMISSION_ACTION);
            intent.putExtra("AccountManagerBundle", bundle);
            context.sendBroadcast(intent);
        }
    } 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:Main.java

public static String getAuthToken(Activity activity, String name, String googleApi) {
    String authToken = null;/*from   w  w  w.  j av  a2s.c  o m*/
    final Account account;
    AccountManagerFuture<Bundle> accountFuture;

    account = new Account(name, GOOGLE_ACCOUNT_TYPE);
    accountFuture = AccountManager.get(activity).getAuthToken(account, googleApi, null, activity, null, null);

    try {
        authToken = accountFuture.getResult().get(AccountManager.KEY_AUTHTOKEN).toString();
        // invalidate the retrieved token and get a fresh one
        AccountManager.get(activity).invalidateAuthToken(GOOGLE_ACCOUNT_TYPE, authToken);
        accountFuture = AccountManager.get(activity).getAuthToken(account, googleApi, null, activity, null,
                null);
        authToken = accountFuture.getResult().get(AccountManager.KEY_AUTHTOKEN).toString();
    } catch (OperationCanceledException e) {
        Log.e(TAG, e.toString());
    } catch (AuthenticatorException e) {
        Log.e(TAG, e.toString());
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    }
    return authToken;
}

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

public static void ensureHasTokenWithUI(Activity activity, Account account,
        final EnsureHasTokenWithUICallback callback) {
    AccountManager am = AccountManager.get(activity);
    am.getAuthToken(account, APPENGINE_SERVICE_NAME, null, activity, new AccountManagerCallback<Bundle>() {
        public void run(AccountManagerFuture<Bundle> authBundleFuture) {
            Bundle authBundle = null;/*from  ww  w  .j  av a2  s . c  o m*/
            try {
                authBundle = authBundleFuture.getResult();
            } catch (OperationCanceledException e) {
                callback.onAuthDenied();
                return;
            } catch (AuthenticatorException e) {
                callback.onError(e);
                return;
            } catch (IOException e) {
                callback.onError(e);
                return;
            }

            if (authBundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
                callback.onHasToken((String) authBundle.get(AccountManager.KEY_AUTHTOKEN));
            } else {
                callback.onError(
                        new IllegalStateException("No auth token available, but operation not canceled."));
            }
        }
    }, null);
}

From source file:com.owncloud.android.network.OwnCloudClientUtils.java

public static WebdavClient createOwnCloudClient(Account account, Context appContext, Activity currentActivity)
        throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException {
    Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account));
    AccountManager am = AccountManager.get(appContext);
    boolean isOauth2 = am.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here
    boolean isSamlSso = am.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_SAML_WEB_SSO) != null;
    WebdavClient client = createOwnCloudClient(uri, appContext, !isSamlSso);

    if (isOauth2) { // TODO avoid a call to getUserData here
        AccountManagerFuture<Bundle> future = am.getAuthToken(account,
                AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN, null, currentActivity, null, null);
        Bundle result = future.getResult();
        String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
        if (accessToken == null)
            throw new AuthenticatorException("WTF!");
        client.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token

    } else if (isSamlSso) { // TODO avoid a call to getUserData here
        AccountManagerFuture<Bundle> future = am.getAuthToken(account,
                AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE, null, currentActivity, null,
                null);/*  w  ww  . j  av  a  2 s . co  m*/
        Bundle result = future.getResult();
        String accessToken = result.getString(AccountManager.KEY_AUTHTOKEN);
        if (accessToken == null)
            throw new AuthenticatorException("WTF!");
        client.setSsoSessionCookie(accessToken);

    } else {
        String username = account.name.substring(0, account.name.lastIndexOf('@'));
        //String password = am.getPassword(account);
        //String password = am.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD, false);
        AccountManagerFuture<Bundle> future = am.getAuthToken(account,
                AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD, null, currentActivity, null, null);
        Bundle result = future.getResult();
        String password = result.getString(AccountManager.KEY_AUTHTOKEN);
        client.setBasicCredentials(username, password);
    }

    return client;
}

From source file:Main.java

public static String refreshAuthToken(Activity activity, String token, String name, String googleApi) {
    String authToken = null;//from  w ww  .ja  v a2 s .c  o  m
    final Account account;
    AccountManagerFuture<Bundle> accountFuture;

    account = new Account(name, GOOGLE_ACCOUNT_TYPE);
    try {
        // invalidate the retrieved token and get a fresh one
        AccountManager.get(activity).invalidateAuthToken(GOOGLE_ACCOUNT_TYPE, token);
        accountFuture = AccountManager.get(activity).getAuthToken(account, googleApi, null, activity, null,
                null);
        authToken = accountFuture.getResult().get(AccountManager.KEY_AUTHTOKEN).toString();
    } catch (OperationCanceledException e) {
        Log.e(TAG, e.toString());
    } catch (AuthenticatorException e) {
        Log.e(TAG, e.toString());
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    }
    return authToken;
}

From source file:com.hemou.android.account.AccountUtils.java

private static Account[] getAccounts(final AccountManager manager)
        throws OperationCanceledException, AuthenticatorException, IOException {
    final AccountManagerFuture<Account[]> future = manager.getAccountsByTypeAndFeatures(ACCOUNT_TYPE, null,
            null, null);/*from  ww  w. ja v  a 2 s  .co  m*/
    final Account[] accounts = future.getResult();
    if (accounts != null && accounts.length > 0)
        return getPasswordAccessibleAccounts(manager, accounts);
    else
        return new Account[0];
}

From source file:com.murrayc.galaxyzoo.app.LoginUtils.java

/**
 * This returns null if there is no account (not even an anonymous account).
 * Don't call this from the main thread - use an AsyncTask, for instance.
 *
 * @param context/*from  ww  w  . ja v a 2  s.c  o  m*/
 * @return
 */
@Nullable
public static LoginDetails getAccountLoginDetails(final Context context) {
    final AccountManager mgr = AccountManager.get(context);
    if (mgr == null) {
        Log.error(
                "getAccountLoginDetails(): getAccountLoginDetails() failed because AccountManager.get() returned null.");
        return null;
    }

    final Account account = getAccount(mgr);
    if (account == null) {
        Log.error(
                "getAccountLoginDetails(): getAccountLoginDetails() failed because getAccount() returned null. ");
        return null;
    }

    //Make sure that this has not been unset somehow:
    setAutomaticAccountSync(context, account);

    final LoginDetails result = new LoginDetails();

    //Avoid showing our anonymous account name in the UI.
    //Also, an anonymous account never has an auth_api_key.
    result.isAnonymous = TextUtils.equals(account.name, ACCOUNT_NAME_ANONYMOUS);
    if (result.isAnonymous) {
        return result; //Return a mostly-empty empty (but not null) LoginDetails.
    }

    result.name = account.name;

    //Note that this requires the USE_CREDENTIALS permission on
    //SDK <=22.
    final AccountManagerFuture<Bundle> response = mgr.getAuthToken(account, ACCOUNT_AUTHTOKEN_TYPE, null, null,
            null, null);
    try {
        final Bundle bundle = response.getResult();
        if (bundle == null) {
            //TODO: Let the caller catch this?
            Log.error(
                    "getAccountLoginDetails(): getAccountLoginDetails() failed because getAuthToken() returned a null response result bundle.");
            return null;
        }

        result.authApiKey = bundle.getString(AccountManager.KEY_AUTHTOKEN);
        return result;
    } catch (final OperationCanceledException e) {
        //TODO: Let the caller catch this?
        Log.error("getAccountLoginDetails(): getAccountLoginDetails() failed", e);
        return null;
    } catch (final AuthenticatorException e) {
        //TODO: Let the caller catch this?
        Log.error("getAccountLoginDetails(): getAccountLoginDetails() failed", e);
        return null;
    } catch (final IOException e) {
        //TODO: Let the caller catch this?
        Log.error("getAccountLoginDetails(): getAccountLoginDetails() failed", e);
        return null;
    }
}

From source file:org.voidsink.anewjkuapp.utils.AppUtils.java

public static String getAccountAuthToken(Context context, Account account) {
    if (account == null) {
        return null;
    }//  w  w w.  j  av a2 s .  c o  m

    AccountManager am = AccountManager.get(context);
    AccountManagerFuture<Bundle> response = am.getAuthToken(account,
            KusssAuthenticator.AUTHTOKEN_TYPE_READ_ONLY, null, true, null, null);

    if (response == null)
        return null;

    try {
        return response.getResult().getString(AccountManager.KEY_AUTHTOKEN);
    } catch (OperationCanceledException | AuthenticatorException | IOException e) {
        Log.e(TAG, "getAccountAuthToken", e);
        return null;
    }
}