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: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 {//  w  ww  . j a  v  a2s . co 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:org.birthdayadapter.util.AccountHelper.java

/**
 * Remove account from Android system//from www .  j  a v a  2 s .co m
 */
public boolean removeAccount() {
    Log.d(Constants.TAG, "Removing account...");

    AccountManager am = AccountManager.get(mContext);

    // remove account
    AccountManagerFuture<Boolean> future = am.removeAccount(Constants.ACCOUNT, null, null);
    if (future.isDone()) {
        try {
            future.getResult();

            return true;
        } catch (Exception e) {
            Log.e(Constants.TAG, "Problem while removing account!", e);
            return false;
        }
    } else {
        return false;
    }
}

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

private String getAuthToken(Context context, Account account) throws PendingAuthException {
    String authToken = null;//from   w  w  w  . j a  va 2 s .c  om
    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:com.example.ami1.CheckActivity.java

@Override
public void onResume() {
    super.onResume();
    // Get the account list, and pick the first one
    AccountManager.get(this).getAccountsByTypeAndFeatures(ACCOUNT_TYPE_GOOGLE, FEATURES_MAIL,
            new AccountManagerCallback<Account[]>() {
                @Override//from w  w w .j  a v  a 2  s  . co m
                public void run(AccountManagerFuture<Account[]> future) {
                    Account[] accounts = null;
                    try {
                        accounts = future.getResult();
                    } catch (OperationCanceledException oce) {
                        Log.e(TAG, "Got OperationCanceledException", oce);
                    } catch (IOException ioe) {
                        Log.e(TAG, "Got OperationCanceledException", ioe);
                    } catch (AuthenticatorException ae) {
                        Log.e(TAG, "Got OperationCanceledException", ae);
                    }
                    onAccountResults(accounts);
                }
            }, null /* handler */);
}

From source file:com.browsertophone.AppEngineClient.java

private String getAuthToken(Context context, Account account) {
    String authToken = null;/* w  ww  .  j av  a  2s. c  om*/
    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(SetupActivity.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:com.android.volley.toolbox.AndroidAuthenticator.java

@SuppressWarnings("deprecation")
@Override/*w  w  w .j  a v a  2  s.c  o  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.lvlstudios.android.gtmessage.AppEngineClient.java

private String getAuthToken(Context context, Account account) throws PendingAuthException {
    String authToken = null;//  w ww .ja  v  a  2 s.c  om
    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);
        if (authToken == null) {
            throw new PendingAuthException(bundle);
        }
    } catch (OperationCanceledException e) {
        Log.w(TAG, "AppEngineClient.getAuthToken " + e);
    } catch (AuthenticatorException e) {
        Log.w(TAG, "AppEngineClient.getAuthToken " + e);
    } catch (IOException e) {
        Log.w(TAG, "AppEngineClient.getAuthToken " + e);
    }
    return authToken;
}

From source file:com.google.android.apps.chrometophone.AppEngineClient.java

private String getAuthToken(Context context, Account account) throws PendingAuthException {
    String authToken = null;//from ww w.  jav  a2 s . 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);
        if (authToken == null) {
            throw new PendingAuthException(bundle);
        }
    } 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:com.agiro.scanner.android.AppEngineClient.java

private String getAuthToken(Context context, Account account) {
    String authToken = null;/*from  ww  w. j av a  2 s  .co  m*/
    AccountManager accountManager = AccountManager.get(context);
    try {
        AccountManagerFuture<Bundle> future = accountManager.getAuthToken(account, AUTH_TOKEN_TYPE, false, null,
                null);
        Bundle bundle = future.getResult();
        Account[] accs = accountManager.getAccounts();
        Log.v(TAG, "Account size = " + accs.length);
        Log.v(TAG, "Listing accounts");
        for (Account acc : accs) {
            Log.v(TAG, "Account: " + acc);
        }
        authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
        // User will be asked for "App Engine" permission.
        if (authToken == null) {
            Log.e(TAG, "No authToken");
            // No auth token - will need to ask permission from user.
            Intent intent = new Intent("com.google.ctp.AUTH_PERMISSION");
            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:cn.studyjams.s2.sj0132.bowenyan.mygirlfriend.nononsenseapps.notepad.ui.settings.ActivitySettings.java

/**
 * Called when the user has selected a Google account when pressing the enable Gtask switch.
 *///from w  w  w . j  a  v a2  s .  co m
@SuppressLint("CommitPrefEdits")
@Override
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 we are authorized by the user.

        if (token != null && !token.isEmpty()) {
            // Also mark enabled as true, as the dialog was shown from enable button
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
            sharedPreferences.edit().putBoolean(getString(R.string.const_preference_gtask_enabled_key), true)
                    .commit();

            // Set it syncable
            SyncGtaskHelper.toggleSync(this, sharedPreferences);
            // And schedule an immediate sync
            SyncGtaskHelper.requestSyncIf(this, SyncGtaskHelper.MANUAL);
        }
    } catch (OperationCanceledException | AuthenticatorException | IOException ignored) {
        // if the request was canceled for any reason, or something went wrong
        SyncGtaskHelper.disableSync(this);
    }
}