Set GtasksInvoker username after logging in

pull/384/head
Alex Baker 9 years ago
parent 2113558770
commit 6eeeceef4e

@ -25,6 +25,7 @@ import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton;
/** /**
* Wrapper around the official Google Tasks API to simplify common operations. In the case * 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 * @author Sam Bosley
*/ */
@Singleton
public class GtasksInvoker { public class GtasksInvoker {
private static final Logger log = LoggerFactory.getLogger(GtasksInvoker.class); private static final Logger log = LoggerFactory.getLogger(GtasksInvoker.class);
@ -43,13 +45,17 @@ public class GtasksInvoker {
@Inject @Inject
public GtasksInvoker(@ForApplication Context context, GtasksPreferenceService preferenceService, AccountManager accountManager) { public GtasksInvoker(@ForApplication Context context, GtasksPreferenceService preferenceService, AccountManager accountManager) {
this.accountManager = accountManager; this.accountManager = accountManager;
credential = GoogleAccountCredential.usingOAuth2(context, Collections.singletonList(TasksScopes.TASKS)) credential = GoogleAccountCredential.usingOAuth2(context, Collections.singletonList(TasksScopes.TASKS));
.setSelectedAccountName(preferenceService.getUserName()); setUserName(preferenceService.getUserName());
service = new Tasks.Builder(new NetHttpTransport(), new JacksonFactory(), credential) service = new Tasks.Builder(new NetHttpTransport(), new JacksonFactory(), credential)
.setApplicationName(String.format("Tasks/%s", BuildConfig.VERSION_NAME)) .setApplicationName(String.format("Tasks/%s", BuildConfig.VERSION_NAME))
.build(); .build();
} }
public void setUserName(String username) {
credential.setSelectedAccountName(username);
}
//If we get a 401 or 403, try revalidating the auth token before bailing //If we get a 401 or 403, try revalidating the auth token before bailing
private synchronized void handleException(IOException e) throws IOException { private synchronized void handleException(IOException e) throws IOException {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
@ -118,7 +124,7 @@ public class GtasksInvoker {
.delete(listId, taskId)); .delete(listId, taskId));
} }
private <T> T execute(TasksRequest<T> request) throws IOException { private synchronized <T> T execute(TasksRequest<T> request) throws IOException {
String caller = getCaller(); String caller = getCaller();
log.debug("{} request: {}", caller, request); log.debug("{} request: {}", caller, request);
T response; T response;

@ -13,6 +13,7 @@ import android.widget.Toast;
import com.todoroo.andlib.utility.DialogUtilities; import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.astrid.gtasks.GtasksPreferenceService; import com.todoroo.astrid.gtasks.GtasksPreferenceService;
import com.todoroo.astrid.gtasks.api.GtasksInvoker;
import org.tasks.AccountManager; import org.tasks.AccountManager;
import org.tasks.R; import org.tasks.R;
@ -36,6 +37,7 @@ public class GtasksLoginActivity extends InjectingAppCompatActivity implements A
@Inject GtasksPreferenceService gtasksPreferenceService; @Inject GtasksPreferenceService gtasksPreferenceService;
@Inject DialogBuilder dialogBuilder; @Inject DialogBuilder dialogBuilder;
@Inject AccountManager accountManager; @Inject AccountManager accountManager;
@Inject GtasksInvoker gtasksInvoker;
private String accountName; private String accountName;
@ -72,6 +74,7 @@ public class GtasksLoginActivity extends InjectingAppCompatActivity implements A
@Override @Override
public void authenticationSuccessful(String accountName, String authToken) { public void authenticationSuccessful(String accountName, String authToken) {
gtasksPreferenceService.setUserName(accountName); gtasksPreferenceService.setUserName(accountName);
gtasksInvoker.setUserName(accountName);
setResult(RESULT_OK); setResult(RESULT_OK);
finish(); finish();
DialogUtilities.dismissDialog(GtasksLoginActivity.this, pd); DialogUtilities.dismissDialog(GtasksLoginActivity.this, pd);

Loading…
Cancel
Save