diff --git a/astrid/plugin-src/com/todoroo/astrid/gtasks/GtasksListAdder.java b/astrid/plugin-src/com/todoroo/astrid/gtasks/GtasksListAdder.java index 2c3517536..ee04cb543 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gtasks/GtasksListAdder.java +++ b/astrid/plugin-src/com/todoroo/astrid/gtasks/GtasksListAdder.java @@ -49,9 +49,9 @@ public class GtasksListAdder extends Activity { @Override public void run() { String token = gtasksPreferenceService.getToken(); - token = GtasksTokenValidator.validateAuthToken(token); - GtasksService service = new GtasksService(token); try { + token = GtasksTokenValidator.validateAuthToken(activity, token); + GtasksService service = new GtasksService(token); String title = editText.getText().toString(); if (TextUtils.isEmpty(title)) //Don't create a list without a title return; diff --git a/astrid/plugin-src/com/todoroo/astrid/gtasks/api/GtasksService.java b/astrid/plugin-src/com/todoroo/astrid/gtasks/api/GtasksService.java index d79802f81..550e7d570 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gtasks/api/GtasksService.java +++ b/astrid/plugin-src/com/todoroo/astrid/gtasks/api/GtasksService.java @@ -41,7 +41,7 @@ public class GtasksService { private static final String API_KEY = "AIzaSyCIYZTBo6haRHxmiplZsfYdagFEpaiFnAk"; // non-production API key - public static final String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/tasks"; + public static final String AUTH_TOKEN_TYPE = "Manage your tasks"; //"oauth2:https://www.googleapis.com/auth/tasks"; public GtasksService(String authToken) { DependencyInjectionService.getInstance().inject(this); @@ -65,7 +65,7 @@ public class GtasksService { int statusCode = h.getResponse().getStatusCode(); if (statusCode == 401 || statusCode == 403) { System.err.println("Encountered " + statusCode + " error"); - token = GtasksTokenValidator.validateAuthToken(token); + token = GtasksTokenValidator.validateAuthToken(ContextManager.getContext(), token); if (token != null) { accessProtectedResource.setAccessToken(token); } 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 2a3c97585..8d0482e8c 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gtasks/auth/GtasksTokenValidator.java +++ b/astrid/plugin-src/com/todoroo/astrid/gtasks/auth/GtasksTokenValidator.java @@ -5,12 +5,15 @@ import java.io.IOException; import android.accounts.Account; import android.accounts.AccountManager; import android.accounts.AccountManagerFuture; +import android.content.Context; import android.os.Bundle; import com.google.api.client.googleapis.extensions.android2.auth.GoogleAccountManager; +import com.timsu.astrid.R; import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.utility.Preferences; import com.todoroo.astrid.gtasks.GtasksPreferenceService; +import com.todoroo.astrid.gtasks.api.GoogleTasksException; import com.todoroo.astrid.gtasks.api.GtasksService; public class GtasksTokenValidator { @@ -21,7 +24,7 @@ public class GtasksTokenValidator { * @param token * @return valid token on success, null on failure */ - public static String validateAuthToken(String token) { + public static String validateAuthToken(Context c, String token) throws GoogleTasksException { GoogleAccountManager accountManager = new GoogleAccountManager(ContextManager.getContext()); GtasksService testService = new GtasksService(token); @@ -30,10 +33,9 @@ public class GtasksTokenValidator { 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(Preferences.getStringValue(GtasksPreferenceService.PREF_USER_NAME)); + Account a = accountManager.getAccountByName(accountName); if (a == null) { - System.err.println("Account for name: " + accountName + " not found"); - return null; + throw new GoogleTasksException(c.getString(R.string.gtasks_error_accountNotFound, accountName)); } accountManager.invalidateAuthToken(token); @@ -49,20 +51,39 @@ public class GtasksTokenValidator { return token; } catch (IOException i2) { i2.printStackTrace(); - return null; + String manufacturer = android.os.Build.MANUFACTURER.toLowerCase(); + if (!manufacturer.contains("samsung")) { // Try with the notifyAuthFailure set to true in case it was that that broke things + accountManager.invalidateAuthToken(token); + future = accountManager.manager.getAuthToken(a, GtasksService.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 GtasksService(token); + try { + testService.ping(); + return token; + } catch (IOException i3) { + i3.printStackTrace(); + throw new GoogleTasksException(c.getString(R.string.gtasks_error_authRefresh)); + } + } 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 { - System.err.println("Future did not have key for authtoken"); + throw new GoogleTasksException(c.getString(R.string.gtasks_error_accountManager)); } } catch (Exception e) { - e.printStackTrace(); - return null; + throw new GoogleTasksException(e.getLocalizedMessage()); } } - - System.err.println("Gtasks token validation fell through all logic"); - return null; } - } diff --git a/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncOnSaveService.java b/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncOnSaveService.java index 722be1d81..4243f0047 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncOnSaveService.java +++ b/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncOnSaveService.java @@ -9,6 +9,7 @@ import android.text.TextUtils; import com.todoroo.andlib.data.DatabaseDao.ModelUpdateListener; import com.todoroo.andlib.data.Property; import com.todoroo.andlib.service.Autowired; +import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.utility.AndroidUtilities; import com.todoroo.andlib.utility.DateUtilities; @@ -21,7 +22,6 @@ import com.todoroo.astrid.gtasks.GtasksMetadata; import com.todoroo.astrid.gtasks.GtasksMetadataService; import com.todoroo.astrid.gtasks.GtasksPreferenceService; import com.todoroo.astrid.gtasks.GtasksTaskListUpdater; -import com.todoroo.astrid.gtasks.api.GoogleTasksException; import com.todoroo.astrid.gtasks.api.GtasksApiUtilities; import com.todoroo.astrid.gtasks.api.GtasksService; import com.todoroo.astrid.gtasks.api.MoveRequest; @@ -158,10 +158,7 @@ public final class GtasksSyncOnSaveService { //Initialize the gtasks api service String token = gtasksPreferenceService.getToken(); - token = GtasksTokenValidator.validateAuthToken(token); - if (token == null) { - throw new GoogleTasksException("Failed to establish connection for sync on save"); //$NON-NLS-1$ - } + token = GtasksTokenValidator.validateAuthToken(ContextManager.getContext(), token); gtasksPreferenceService.setToken(token); GtasksService gtasksService = new GtasksService(token); @@ -265,10 +262,7 @@ public final class GtasksSyncOnSaveService { AndroidUtilities.sleepDeep(1000L); //Initialize the gtasks api service String token = gtasksPreferenceService.getToken(); - token = GtasksTokenValidator.validateAuthToken(token); - if (token == null) { - throw new GoogleTasksException("Failed to establish connection for sync on save"); //$NON-NLS-1$ - } + token = GtasksTokenValidator.validateAuthToken(ContextManager.getContext(), token); gtasksPreferenceService.setToken(token); GtasksService gtasksService = new GtasksService(token); diff --git a/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncProvider.java b/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncProvider.java index dc46d011d..a1fe4aed7 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncProvider.java +++ b/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncProvider.java @@ -121,10 +121,7 @@ public class GtasksSyncProvider extends SyncProvider { protected void initiateBackground() { try { String authToken = gtasksPreferenceService.getToken(); - authToken = GtasksTokenValidator.validateAuthToken(authToken); - if (authToken == null) { - throw new GoogleTasksException(ContextManager.getContext().getString(R.string.gtasks_GLA_errorAuth)); - } + authToken = GtasksTokenValidator.validateAuthToken(ContextManager.getContext(), authToken); gtasksPreferenceService.setToken(authToken); taskService = new GtasksService(authToken); diff --git a/astrid/res/values/strings-gtasks.xml b/astrid/res/values/strings-gtasks.xml index f2301dd3d..b764607fd 100644 --- a/astrid/res/values/strings-gtasks.xml +++ b/astrid/res/values/strings-gtasks.xml @@ -96,6 +96,15 @@ Google\'s Task API is in beta and has encountered an error. The service may be down, please try again later. + + + Account %s not found--please log out and log back in from the Google Tasks settings. + + + Unable to authenticate with Google Tasks. Please check your account password or try again later. + + + Error in your phone\'s account manager. Please log out and log back in from the Google Tasks settings. diff --git a/tests/src/com/todoroo/astrid/gtasks/GtasksApiTest.java b/tests/src/com/todoroo/astrid/gtasks/GtasksApiTest.java index 3c3708e34..5082465f5 100644 --- a/tests/src/com/todoroo/astrid/gtasks/GtasksApiTest.java +++ b/tests/src/com/todoroo/astrid/gtasks/GtasksApiTest.java @@ -38,7 +38,7 @@ public class GtasksApiTest extends DatabaseTestCase { Task newTask = new Task(); String title = "New task"; newTask.setTitle(title); - + service.createGtask(DEFAULT_LIST, newTask); assertTrue(taskWithTitleExists(title)); } @@ -279,7 +279,7 @@ public class GtasksApiTest extends DatabaseTestCase { } String authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN); - authToken = GtasksTokenValidator.validateAuthToken(authToken); + authToken = GtasksTokenValidator.validateAuthToken(getContext(), authToken); service = new GtasksService(authToken); diff --git a/tests/src/com/todoroo/astrid/gtasks/GtasksNewSyncTest.java b/tests/src/com/todoroo/astrid/gtasks/GtasksNewSyncTest.java index dfa4d02ff..a42f9ce19 100644 --- a/tests/src/com/todoroo/astrid/gtasks/GtasksNewSyncTest.java +++ b/tests/src/com/todoroo/astrid/gtasks/GtasksNewSyncTest.java @@ -519,7 +519,7 @@ public class GtasksNewSyncTest extends DatabaseTestCase { return; } String authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN); - authToken = GtasksTokenValidator.validateAuthToken(authToken); + authToken = GtasksTokenValidator.validateAuthToken(getContext(), authToken); gtasksPreferenceService.setToken(authToken); gtasksService = new GtasksService(authToken); diff --git a/tests/src/com/todoroo/astrid/gtasks/GtasksSyncOnSaveTest.java b/tests/src/com/todoroo/astrid/gtasks/GtasksSyncOnSaveTest.java index a315b7f16..24221ff7a 100644 --- a/tests/src/com/todoroo/astrid/gtasks/GtasksSyncOnSaveTest.java +++ b/tests/src/com/todoroo/astrid/gtasks/GtasksSyncOnSaveTest.java @@ -202,7 +202,7 @@ public class GtasksSyncOnSaveTest extends DatabaseTestCase { return; } String authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN); - authToken = GtasksTokenValidator.validateAuthToken(authToken); + authToken = GtasksTokenValidator.validateAuthToken(getContext(), authToken); gtasksPreferenceService.setToken(authToken); gtasksService = new GtasksService(authToken); diff --git a/tests/src/com/todoroo/astrid/sync/repeats/RepeatTestsGtasksSync.java b/tests/src/com/todoroo/astrid/sync/repeats/RepeatTestsGtasksSync.java index ea6c79ea5..9c849f241 100644 --- a/tests/src/com/todoroo/astrid/sync/repeats/RepeatTestsGtasksSync.java +++ b/tests/src/com/todoroo/astrid/sync/repeats/RepeatTestsGtasksSync.java @@ -136,7 +136,7 @@ public class RepeatTestsGtasksSync extends NewRepeatTests