/**
  * Gets the auth token synchronously.
  *
  * <p>- Assumes that the account is a valid account. - Should not be called on the main thread.
  */
 @Deprecated
 public String getAuthTokenFromBackground(Account account, String authTokenType) {
   AccountManagerFuture<Bundle> future =
       mAccountManager.getAuthToken(account, authTokenType, false, null, null);
   AtomicBoolean errorEncountered = new AtomicBoolean(false);
   return getAuthTokenInner(future, errorEncountered);
 }
 /** @return Whether or not there is an account authenticator for Google accounts. */
 public boolean hasGoogleAccountAuthenticator() {
   AuthenticatorDescription[] descs = mAccountManager.getAuthenticatorTypes();
   for (AuthenticatorDescription desc : descs) {
     if (GOOGLE_ACCOUNT_TYPE.equals(desc.type)) return true;
   }
   return false;
 }
 public List<String> getGoogleAccountNames() {
   List<String> accountNames = new ArrayList<String>();
   Account[] accounts = mAccountManager.getAccountsByType(GOOGLE_ACCOUNT_TYPE);
   for (Account account : accounts) {
     accountNames.add(account.name);
   }
   return accountNames;
 }
 /** Returns the account if it exists, null otherwise. */
 public Account getAccountFromName(String accountName) {
   Account[] accounts = mAccountManager.getAccountsByType(GOOGLE_ACCOUNT_TYPE);
   for (Account account : accounts) {
     if (account.name.equals(accountName)) {
       return account;
     }
   }
   return null;
 }
 /**
  * Invalidates the old token (if non-null/non-empty) and asynchronously generates a new one.
  *
  * <p>- Assumes that the account is a valid account.
  */
 public void getNewAuthTokenFromForeground(
     Account account, String authToken, String authTokenType, GetAuthTokenCallback callback) {
   if (authToken != null && !authToken.isEmpty()) {
     mAccountManager.invalidateAuthToken(GOOGLE_ACCOUNT_TYPE, authToken);
   }
   AtomicInteger numTries = new AtomicInteger(0);
   AtomicBoolean errorEncountered = new AtomicBoolean(false);
   getAuthTokenAsynchronously(
       null, account, authTokenType, callback, numTries, errorEncountered, null);
 }
  /**
   * Invalidates the old token (if non-null/non-empty) and synchronously generates a new one. Also
   * notifies the user (via status bar) if any user action is required. The method will return null
   * if any user action is required to generate the new token.
   *
   * <p>- Assumes that the account is a valid account. - Should not be called on the main thread.
   */
  @Deprecated
  public String getNewAuthToken(Account account, String authToken, String authTokenType) {
    // TODO(dsmyers): consider reimplementing using an AccountManager function with an
    // explicit timeout.
    // Bug: https://code.google.com/p/chromium/issues/detail?id=172394.
    if (authToken != null && !authToken.isEmpty()) {
      mAccountManager.invalidateAuthToken(GOOGLE_ACCOUNT_TYPE, authToken);
    }

    try {
      return mAccountManager.blockingGetAuthToken(account, authTokenType, true);
    } catch (OperationCanceledException e) {
      Log.w(TAG, "Auth token - operation cancelled", e);
    } catch (AuthenticatorException e) {
      Log.w(TAG, "Auth token - authenticator exception", e);
    } catch (IOException e) {
      Log.w(TAG, "Auth token - IO exception", e);
    }
    return null;
  }
  private void getAuthTokenAsynchronously(
      @Nullable Activity activity,
      final Account account,
      final String authTokenType,
      final GetAuthTokenCallback callback,
      final AtomicInteger numTries,
      final AtomicBoolean errorEncountered,
      final ConnectionRetry retry) {
    AccountManagerFuture<Bundle> future;
    if (numTries.get() == 0 && activity != null) {
      future = mAccountManager.getAuthToken(account, authTokenType, null, activity, null, null);
    } else {
      future = mAccountManager.getAuthToken(account, authTokenType, false, null, null);
    }
    final AccountManagerFuture<Bundle> finalFuture = future;
    errorEncountered.set(false);

    // On ICS onPostExecute is never called when running an AsyncTask from a different thread
    // than the UI thread.
    if (ThreadUtils.runningOnUiThread()) {
      new AsyncTask<Void, Void, String>() {
        @Override
        public String doInBackground(Void... params) {
          return getAuthTokenInner(finalFuture, errorEncountered);
        }

        @Override
        public void onPostExecute(String authToken) {
          onGotAuthTokenResult(
              account, authTokenType, authToken, callback, numTries, errorEncountered, retry);
        }
      }.execute();
    } else {
      String authToken = getAuthTokenInner(finalFuture, errorEncountered);
      onGotAuthTokenResult(
          account, authTokenType, authToken, callback, numTries, errorEncountered, retry);
    }
  }
 /** Removes an auth token from the AccountManager's cache. */
 public void invalidateAuthToken(String authToken) {
   mAccountManager.invalidateAuthToken(GOOGLE_ACCOUNT_TYPE, authToken);
 }
 public Account[] getGoogleAccounts() {
   return mAccountManager.getAccountsByType(GOOGLE_ACCOUNT_TYPE);
 }