/** * Invalidates and then revalidates the auth token for the currently logged in user Shouldn't be * called from the main thread--will block on network calls * * @return valid token on success, null on failure */ public static synchronized String validateAuthToken(Context c, String token) throws GoogleTasksException { GoogleAccountManager accountManager = new GoogleAccountManager(ContextManager.getContext()); if (testToken(token)) { return token; } // If fail, token may have expired -- get a new one and return that String accountName = Preferences.getStringValue(GtasksPreferenceService.PREF_USER_NAME); Account a = accountManager.getAccountByName(accountName); if (a == null) { throw new GoogleTasksException( c.getString(R.string.gtasks_error_accountNotFound, accountName), "account-not-found"); } for (int i = 0; i < REVALIDATION_TRIES; i++) { accountManager.invalidateAuthToken(token); // try with notify-auth-failure = false AccountManagerFuture<Bundle> future = accountManager.manager.getAuthToken(a, GtasksInvoker.AUTH_TOKEN_TYPE, false, null, null); token = getTokenFromFuture(c, future); if (TOKEN_INTENT_RECEIVED.equals(token)) { return null; } else if (token != null && testToken(token)) { return token; } } throw new GoogleTasksException( c.getString(R.string.gtasks_error_authRefresh), "auth-token-refresh"); }
public void loadAccount(boolean tokenExpired) { // Load account from preferences SharedPreferences settings = getSharedPreferences(PREF_FILE, 0); String accountName = settings.getString("accountName", null); Account account = accountManager.getAccountByName(accountName); if (account != null) { if (tokenExpired) { accountManager.invalidateAuthToken(accessProtectedResource.getAccessToken()); accessProtectedResource.setAccessToken(null); } setAccount(account); } else showDialog(DIALOG_ACCOUNTS); }
private AlertDialog showDialogAccounts() { final Account[] accounts = accountManager.getAccounts(); final int size = accounts.length; String[] names = new String[size]; for (int i = 0; i < size; i++) { names[i] = accounts[i].name; } AlertDialog.Builder dialogAccounts = new AlertDialog.Builder(this) .setTitle(getString(R.string.select_account)) .setItems( names, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { setAccount(accounts[which]); } }); return dialogAccounts.create(); }