private void gotAccount(boolean tokenExpired) {
SharedPreferences settings = getSharedPreferences(PREF, 0);
String accountName = settings.getString("accountName", null);
if (accountName != null) {
AccountManager manager = AccountManager.get(this);
Account[] accounts = manager.getAccountsByType("com.google");
int size = accounts.length;
for (int i = 0; i < size; i++) {
Account account = accounts[i];
if (accountName.equals(account.name)) {
if (tokenExpired) {
manager.invalidateAuthToken("com.google", this.authToken);
}
gotAccount(manager, account);
return;
}
}
}
showDialog(DIALOG_ACCOUNTS);
}
private void gotAccount(final AccountManager manager, final Account account) {
SharedPreferences settings = getSharedPreferences(PREF, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("accountName", account.name);
editor.commit();
new Thread() {
@Override
public void run() {
try {
final Bundle bundle =
manager.getAuthToken(account, AUTH_TOKEN_TYPE, true, null, null)
.getResult();
runOnUiThread(new Runnable() {
public void run() {
try {
if (bundle.containsKey(AccountManager.KEY_INTENT)) {
Intent intent =
bundle.getParcelable(AccountManager.KEY_INTENT);
int flags = intent.getFlags();
flags &= ~Intent.FLAG_ACTIVITY_NEW_TASK;
intent.setFlags(flags);
startActivityForResult(intent, REQUEST_AUTHENTICATE);
} else if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
authenticatedClientLogin(
bundle.getString(AccountManager.KEY_AUTHTOKEN));
}
} catch (Exception e) {
handleException(e);
}
}
});
} catch (Exception e) {
handleException(e);
}
}
}.start();
}