|
|
@ -19,7 +19,7 @@ import com.todoroo.astrid.gtasks.api.GtasksInvoker;
|
|
|
|
|
|
|
|
|
|
|
|
public class GtasksTokenValidator {
|
|
|
|
public class GtasksTokenValidator {
|
|
|
|
|
|
|
|
|
|
|
|
private static final String MANUFACTURER_SAMSUNG = "samsung"; //$NON-NLS-1$
|
|
|
|
private static final String TOKEN_INTENT_RECEIVED = "intent!"; //$NON-NLS-1$
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Invalidates and then revalidates the auth token for the currently logged in user
|
|
|
|
* Invalidates and then revalidates the auth token for the currently logged in user
|
|
|
@ -30,11 +30,10 @@ public class GtasksTokenValidator {
|
|
|
|
public static String validateAuthToken(Context c, String token) throws GoogleTasksException {
|
|
|
|
public static String validateAuthToken(Context c, String token) throws GoogleTasksException {
|
|
|
|
GoogleAccountManager accountManager = new GoogleAccountManager(ContextManager.getContext());
|
|
|
|
GoogleAccountManager accountManager = new GoogleAccountManager(ContextManager.getContext());
|
|
|
|
|
|
|
|
|
|
|
|
GtasksInvoker testService = new GtasksInvoker(token);
|
|
|
|
if(testToken(token))
|
|
|
|
try {
|
|
|
|
|
|
|
|
testService.ping();
|
|
|
|
|
|
|
|
return token;
|
|
|
|
return token;
|
|
|
|
} catch (IOException i) { //If fail, token may have expired -- get a new one and return that
|
|
|
|
|
|
|
|
|
|
|
|
// If fail, token may have expired -- get a new one and return that
|
|
|
|
String accountName = Preferences.getStringValue(GtasksPreferenceService.PREF_USER_NAME);
|
|
|
|
String accountName = Preferences.getStringValue(GtasksPreferenceService.PREF_USER_NAME);
|
|
|
|
Account a = accountManager.getAccountByName(accountName);
|
|
|
|
Account a = accountManager.getAccountByName(accountName);
|
|
|
|
if (a == null) {
|
|
|
|
if (a == null) {
|
|
|
@ -42,59 +41,54 @@ public class GtasksTokenValidator {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
accountManager.invalidateAuthToken(token);
|
|
|
|
accountManager.invalidateAuthToken(token);
|
|
|
|
AccountManagerFuture<Bundle> future = accountManager.manager.getAuthToken(a, GtasksInvoker.AUTH_TOKEN_TYPE, false, null, null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
// try with notify-auth-failure = false
|
|
|
|
if (future.getResult().containsKey(AccountManager.KEY_AUTHTOKEN)) {
|
|
|
|
AccountManagerFuture<Bundle> future = accountManager.manager.getAuthToken(a, GtasksInvoker.AUTH_TOKEN_TYPE, false, null, null);
|
|
|
|
Bundle result = future.getResult();
|
|
|
|
token = getTokenFromFuture(c, future);
|
|
|
|
token = result.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
if(TOKEN_INTENT_RECEIVED.equals(token))
|
|
|
|
testService = new GtasksInvoker(token);
|
|
|
|
return null;
|
|
|
|
try { //Make sure the new token works--if not, we may have network problems
|
|
|
|
else if(token != null)
|
|
|
|
testService.ping();
|
|
|
|
|
|
|
|
return token;
|
|
|
|
return token;
|
|
|
|
} catch (IOException i2) {
|
|
|
|
|
|
|
|
i2.printStackTrace();
|
|
|
|
throw new GoogleTasksException(c.getString(R.string.gtasks_error_authRefresh));
|
|
|
|
String manufacturer = android.os.Build.MANUFACTURER.toLowerCase();
|
|
|
|
}
|
|
|
|
if (!manufacturer.contains(MANUFACTURER_SAMSUNG)) { // Try with the notifyAuthFailure set to true in case it was that that broke things
|
|
|
|
|
|
|
|
accountManager.invalidateAuthToken(token);
|
|
|
|
private static boolean testToken(String token) {
|
|
|
|
future = accountManager.manager.getAuthToken(a, GtasksInvoker.AUTH_TOKEN_TYPE, true, null, null);
|
|
|
|
GtasksInvoker testService = new GtasksInvoker(token);
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (future.getResult().containsKey(AccountManager.KEY_AUTHTOKEN)) {
|
|
|
|
|
|
|
|
result = future.getResult();
|
|
|
|
|
|
|
|
token = result.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
|
|
|
|
testService = new GtasksInvoker(token);
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
testService.ping();
|
|
|
|
testService.ping();
|
|
|
|
return token;
|
|
|
|
return true;
|
|
|
|
} catch (IOException i3) {
|
|
|
|
} catch (IOException i) {
|
|
|
|
i3.printStackTrace();
|
|
|
|
return false;
|
|
|
|
throw new GoogleTasksException(c.getString(R.string.gtasks_error_authRefresh));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (future.getResult().containsKey(AccountManager.KEY_INTENT)) {
|
|
|
|
|
|
|
|
Intent intent = (Intent) future.getResult().get(AccountManager.KEY_INTENT);
|
|
|
|
|
|
|
|
c.startActivity(intent);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
throw new GoogleTasksException(c.getString(R.string.gtasks_error_accountManager));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static String getTokenFromFuture(Context c, AccountManagerFuture<Bundle> future)
|
|
|
|
|
|
|
|
throws GoogleTasksException {
|
|
|
|
|
|
|
|
Bundle result;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
result = future.getResult();
|
|
|
|
|
|
|
|
if(result == null)
|
|
|
|
|
|
|
|
throw new NullPointerException("Future result was null."); //$NON-NLS-1$
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new GoogleTasksException(e.getLocalizedMessage());
|
|
|
|
throw new GoogleTasksException(e.getLocalizedMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
throw new GoogleTasksException(c.getString(R.string.gtasks_error_authRefresh));
|
|
|
|
// check what kind of result was returned
|
|
|
|
}
|
|
|
|
String token;
|
|
|
|
}
|
|
|
|
if (result.containsKey(AccountManager.KEY_AUTHTOKEN)) {
|
|
|
|
} else if (future.getResult().containsKey(AccountManager.KEY_INTENT)) {
|
|
|
|
token = result.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
Intent intent = (Intent) future.getResult().get(AccountManager.KEY_INTENT);
|
|
|
|
} else if (result.containsKey(AccountManager.KEY_INTENT)) {
|
|
|
|
|
|
|
|
Intent intent = (Intent) result.get(AccountManager.KEY_INTENT);
|
|
|
|
c.startActivity(intent);
|
|
|
|
c.startActivity(intent);
|
|
|
|
return null;
|
|
|
|
return TOKEN_INTENT_RECEIVED;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw new GoogleTasksException(c.getString(R.string.gtasks_error_accountManager));
|
|
|
|
throw new GoogleTasksException(c.getString(R.string.gtasks_error_accountManager));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
throw new GoogleTasksException(e.getLocalizedMessage());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
if(testToken(token))
|
|
|
|
|
|
|
|
return token;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|