Example usage for android.accounts AccountManagerFuture isCancelled

List of usage examples for android.accounts AccountManagerFuture isCancelled

Introduction

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

Prototype

boolean isCancelled();

Source Link

Document

Returns true if this task was cancelled before it completed normally.

Usage

From source file:com.android.volley.toolbox.AndroidAuthenticator.java

@SuppressWarnings("deprecation")
@Override//from   ww  w .j a  va2s  .co m
public String getAuthToken() throws AuthFailureError {
    AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(mAccount, mAuthTokenType,
            mNotifyAuthFailure, null, null);
    Bundle result;
    try {
        result = future.getResult();
    } catch (Exception e) {
        throw new AuthFailureError("Error while retrieving auth token", e);
    }
    String authToken = null;
    if (future.isDone() && !future.isCancelled()) {
        if (result.containsKey(AccountManager.KEY_INTENT)) {
            Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
            throw new AuthFailureError(intent);
        }
        authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
    }
    if (authToken == null) {
        throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType);
    }

    return authToken;
}

From source file:com.microsoft.windowsazure.mobileservices.MobileServiceClient.java

/**
 * Invokes Microsoft Azure Mobile Service authentication using a the Google
 * account registered in the device// w  ww .  ja v  a  2s . c o  m
 *
 * @param activity The activity that triggered the authentication
 * @param account  The account used for the login operation
 * @param scopes   The scopes used as authentication token type for login
 */
public ListenableFuture<MobileServiceUser> loginWithGoogleAccount(Activity activity, Account account,
        String scopes) {
    final SettableFuture<MobileServiceUser> future = SettableFuture.create();

    try {
        if (account == null) {
            throw new IllegalArgumentException("account");
        }

        final MobileServiceClient client = this;

        AccountManagerCallback<Bundle> authCallback = new AccountManagerCallback<Bundle>() {

            @Override
            public void run(AccountManagerFuture<Bundle> futureBundle) {
                try {
                    if (futureBundle.isCancelled()) {
                        future.setException(new MobileServiceException("User cancelled"));
                        // callback.onCompleted(null, new
                        // MobileServiceException("User cancelled"), null);
                    } else {
                        Bundle bundle = futureBundle.getResult();

                        String token = (String) (bundle.get(AccountManager.KEY_AUTHTOKEN));

                        JsonObject json = new JsonObject();
                        json.addProperty("access_token", token);

                        ListenableFuture<MobileServiceUser> loginFuture = client
                                .login(MobileServiceAuthenticationProvider.Google, json);

                        Futures.addCallback(loginFuture, new FutureCallback<MobileServiceUser>() {
                            @Override
                            public void onFailure(Throwable e) {
                                future.setException(e);
                            }

                            @Override
                            public void onSuccess(MobileServiceUser user) {
                                future.set(user);
                            }
                        });
                    }
                } catch (Exception e) {
                    future.setException(e);
                }
            }
        };

        AccountManager acMgr = AccountManager.get(activity.getApplicationContext());
        acMgr.getAuthToken(account, scopes, null, activity, authCallback, null);

    } catch (Exception e) {
        future.setException(e);
    }

    return future;
}