From e320e9ee6bdb1266a8337772d2b06a6ec224c3d1 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Tue, 13 Mar 2012 17:14:41 -0700 Subject: [PATCH] Refactored gtasks token validator --- .../gtasks/auth/GtasksTokenValidator.java | 118 +++++++++--------- 1 file changed, 56 insertions(+), 62 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/gtasks/auth/GtasksTokenValidator.java b/astrid/plugin-src/com/todoroo/astrid/gtasks/auth/GtasksTokenValidator.java index f89a3a6ac..462c4352e 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gtasks/auth/GtasksTokenValidator.java +++ b/astrid/plugin-src/com/todoroo/astrid/gtasks/auth/GtasksTokenValidator.java @@ -19,7 +19,7 @@ import com.todoroo.astrid.gtasks.api.GtasksInvoker; 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 @@ -30,71 +30,65 @@ public class GtasksTokenValidator { public static 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)); + } + + accountManager.invalidateAuthToken(token); + + // try with notify-auth-failure = false + AccountManagerFuture 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) + return token; + + throw new GoogleTasksException(c.getString(R.string.gtasks_error_authRefresh)); + } + + private static boolean testToken(String token) { GtasksInvoker testService = new GtasksInvoker(token); try { testService.ping(); - return token; - } catch (IOException i) { //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)); - } - - accountManager.invalidateAuthToken(token); - AccountManagerFuture future = accountManager.manager.getAuthToken(a, GtasksInvoker.AUTH_TOKEN_TYPE, false, null, null); - - try { - if (future.getResult().containsKey(AccountManager.KEY_AUTHTOKEN)) { - Bundle result = future.getResult(); - token = result.getString(AccountManager.KEY_AUTHTOKEN); - testService = new GtasksInvoker(token); - try { //Make sure the new token works--if not, we may have network problems - testService.ping(); - return token; - } catch (IOException i2) { - i2.printStackTrace(); - 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); - future = accountManager.manager.getAuthToken(a, GtasksInvoker.AUTH_TOKEN_TYPE, true, null, null); - try { - if (future.getResult().containsKey(AccountManager.KEY_AUTHTOKEN)) { - result = future.getResult(); - token = result.getString(AccountManager.KEY_AUTHTOKEN); - testService = new GtasksInvoker(token); - try { - testService.ping(); - return token; - } catch (IOException i3) { - i3.printStackTrace(); - 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)); - } - } catch (Exception e) { - throw new GoogleTasksException(e.getLocalizedMessage()); - } - } else { - 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)); - } - } catch (Exception e) { - throw new GoogleTasksException(e.getLocalizedMessage()); - } + return true; + } catch (IOException i) { + return false; + } + } + private static String getTokenFromFuture(Context c, AccountManagerFuture 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) { + throw new GoogleTasksException(e.getLocalizedMessage()); } + + // check what kind of result was returned + String token; + if (result.containsKey(AccountManager.KEY_AUTHTOKEN)) { + token = result.getString(AccountManager.KEY_AUTHTOKEN); + } else if (result.containsKey(AccountManager.KEY_INTENT)) { + Intent intent = (Intent) result.get(AccountManager.KEY_INTENT); + c.startActivity(intent); + return TOKEN_INTENT_RECEIVED; + } else { + throw new GoogleTasksException(c.getString(R.string.gtasks_error_accountManager)); + } + + if(testToken(token)) + return token; + + return null; } }