From 6eeeceef4ec5b41088fd7f5b7a0c06fcf6fff3f5 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 10 Dec 2015 12:35:34 -0600 Subject: [PATCH] Set GtasksInvoker username after logging in --- .../com/todoroo/astrid/gtasks/api/GtasksInvoker.java | 12 +++++++++--- .../astrid/gtasks/auth/GtasksLoginActivity.java | 3 +++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/googleplay/java/com/todoroo/astrid/gtasks/api/GtasksInvoker.java b/src/googleplay/java/com/todoroo/astrid/gtasks/api/GtasksInvoker.java index 105374b66..cda59e9b7 100644 --- a/src/googleplay/java/com/todoroo/astrid/gtasks/api/GtasksInvoker.java +++ b/src/googleplay/java/com/todoroo/astrid/gtasks/api/GtasksInvoker.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.util.Collections; import javax.inject.Inject; +import javax.inject.Singleton; /** * Wrapper around the official Google Tasks API to simplify common operations. In the case @@ -32,6 +33,7 @@ import javax.inject.Inject; * * @author Sam Bosley */ +@Singleton public class GtasksInvoker { private static final Logger log = LoggerFactory.getLogger(GtasksInvoker.class); @@ -43,13 +45,17 @@ public class GtasksInvoker { @Inject public GtasksInvoker(@ForApplication Context context, GtasksPreferenceService preferenceService, AccountManager accountManager) { this.accountManager = accountManager; - credential = GoogleAccountCredential.usingOAuth2(context, Collections.singletonList(TasksScopes.TASKS)) - .setSelectedAccountName(preferenceService.getUserName()); + credential = GoogleAccountCredential.usingOAuth2(context, Collections.singletonList(TasksScopes.TASKS)); + setUserName(preferenceService.getUserName()); service = new Tasks.Builder(new NetHttpTransport(), new JacksonFactory(), credential) .setApplicationName(String.format("Tasks/%s", BuildConfig.VERSION_NAME)) .build(); } + public void setUserName(String username) { + credential.setSelectedAccountName(username); + } + //If we get a 401 or 403, try revalidating the auth token before bailing private synchronized void handleException(IOException e) throws IOException { log.error(e.getMessage(), e); @@ -118,7 +124,7 @@ public class GtasksInvoker { .delete(listId, taskId)); } - private T execute(TasksRequest request) throws IOException { + private synchronized T execute(TasksRequest request) throws IOException { String caller = getCaller(); log.debug("{} request: {}", caller, request); T response; diff --git a/src/googleplay/java/com/todoroo/astrid/gtasks/auth/GtasksLoginActivity.java b/src/googleplay/java/com/todoroo/astrid/gtasks/auth/GtasksLoginActivity.java index f7d0fccb9..81c46a120 100644 --- a/src/googleplay/java/com/todoroo/astrid/gtasks/auth/GtasksLoginActivity.java +++ b/src/googleplay/java/com/todoroo/astrid/gtasks/auth/GtasksLoginActivity.java @@ -13,6 +13,7 @@ import android.widget.Toast; import com.todoroo.andlib.utility.DialogUtilities; import com.todoroo.astrid.gtasks.GtasksPreferenceService; +import com.todoroo.astrid.gtasks.api.GtasksInvoker; import org.tasks.AccountManager; import org.tasks.R; @@ -36,6 +37,7 @@ public class GtasksLoginActivity extends InjectingAppCompatActivity implements A @Inject GtasksPreferenceService gtasksPreferenceService; @Inject DialogBuilder dialogBuilder; @Inject AccountManager accountManager; + @Inject GtasksInvoker gtasksInvoker; private String accountName; @@ -72,6 +74,7 @@ public class GtasksLoginActivity extends InjectingAppCompatActivity implements A @Override public void authenticationSuccessful(String accountName, String authToken) { gtasksPreferenceService.setUserName(accountName); + gtasksInvoker.setUserName(accountName); setResult(RESULT_OK); finish(); DialogUtilities.dismissDialog(GtasksLoginActivity.this, pd);