Inject GtasksTokenValidator

pull/189/head
Alex Baker 10 years ago
parent 4fca2ef1eb
commit 37b7ee456a

@ -30,6 +30,7 @@ public class GtasksListAdder extends InjectingActivity {
@Inject GtasksPreferenceService gtasksPreferenceService;
@Inject GtasksListService gtasksListService;
@Inject GtasksTokenValidator gtasksTokenValidator;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -55,8 +56,8 @@ public class GtasksListAdder extends InjectingActivity {
public void run() {
String token = gtasksPreferenceService.getToken();
try {
token = GtasksTokenValidator.validateAuthToken(activity, token);
GtasksInvoker service = new GtasksInvoker(token);
token = gtasksTokenValidator.validateAuthToken(activity, token);
GtasksInvoker service = new GtasksInvoker(gtasksTokenValidator, token);
String title = editText.getText().toString();
if (TextUtils.isEmpty(title)) //Don't create a list without a title
{

@ -35,15 +35,13 @@ public class GtasksInvoker {
private Tasks service;
private GoogleAccessProtectedResource accessProtectedResource;
private final GtasksTokenValidator gtasksTokenValidator;
private String token;
public static final String AUTH_TOKEN_TYPE = "Manage your tasks"; //"oauth2:https://www.googleapis.com/auth/tasks";
public GtasksInvoker(String authToken) {
authenticate(authToken);
}
public void authenticate(String authToken) {
public GtasksInvoker(GtasksTokenValidator gtasksTokenValidator, String authToken) {
this.gtasksTokenValidator = gtasksTokenValidator;
this.token = authToken;
accessProtectedResource = new GoogleAccessProtectedResource(authToken);
@ -62,7 +60,7 @@ public class GtasksInvoker {
int statusCode = h.getResponse().getStatusCode();
if (statusCode == 401 || statusCode == 403) {
System.err.println("Encountered " + statusCode + " error");
token = GtasksTokenValidator.validateAuthToken(ContextManager.getContext(), token);
token = gtasksTokenValidator.validateAuthToken(ContextManager.getContext(), token);
if (token != null) {
accessProtectedResource.setAccessToken(token);
}

@ -15,26 +15,37 @@ import android.os.Bundle;
import com.google.api.client.googleapis.extensions.android2.auth.GoogleAccountManager;
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.GtasksInvoker;
import org.tasks.R;
import org.tasks.preferences.Preferences;
import java.io.IOException;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class GtasksTokenValidator {
private static final String TOKEN_INTENT_RECEIVED = "intent!"; //$NON-NLS-1$
private static final int REVALIDATION_TRIES = 4;
private final Preferences preferences;
@Inject
public GtasksTokenValidator(Preferences preferences) {
this.preferences = preferences;
}
/**
* Invalidates and then revalidates the auth token for the currently logged in user
* Shouldn't be called from the main thread--will block on network calls
* @return valid token on success, null on failure
*/
public static synchronized String validateAuthToken(Context c, String token) throws GoogleTasksException {
public synchronized String validateAuthToken(Context c, String token) throws GoogleTasksException {
GoogleAccountManager accountManager = new GoogleAccountManager(ContextManager.getContext());
if(testToken(token)) {
@ -42,7 +53,7 @@ public class GtasksTokenValidator {
}
// 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);
if (a == null) {
throw new GoogleTasksException(c.getString(R.string.gtasks_error_accountNotFound, accountName), "account-not-found");
@ -66,8 +77,8 @@ public class GtasksTokenValidator {
throw new GoogleTasksException(c.getString(R.string.gtasks_error_authRefresh), "auth-token-refresh");
}
private static boolean testToken(String token) {
GtasksInvoker testService = new GtasksInvoker(token);
private boolean testToken(String token) {
GtasksInvoker testService = new GtasksInvoker(this, token);
try {
testService.ping();
return true;
@ -76,7 +87,7 @@ public class GtasksTokenValidator {
}
}
private static String getTokenFromFuture(Context c, AccountManagerFuture<Bundle> future)
private String getTokenFromFuture(Context c, AccountManagerFuture<Bundle> future)
throws GoogleTasksException {
Bundle result;
try {

@ -25,6 +25,7 @@ import com.todoroo.astrid.gtasks.api.CreateRequest;
import com.todoroo.astrid.gtasks.api.GtasksApiUtilities;
import com.todoroo.astrid.gtasks.api.GtasksInvoker;
import com.todoroo.astrid.gtasks.api.MoveRequest;
import com.todoroo.astrid.gtasks.auth.GtasksTokenValidator;
import com.todoroo.astrid.service.MetadataService;
import com.todoroo.astrid.service.TaskService;
@ -45,14 +46,18 @@ public class GtasksSyncService {
private final GtasksMetadataService gtasksMetadataService;
private final TaskDao taskDao;
private final GtasksPreferenceService gtasksPreferenceService;
private final GtasksTokenValidator gtasksTokenValidator;
@Inject
public GtasksSyncService(MetadataService metadataService, MetadataDao metadataDao, GtasksMetadataService gtasksMetadataService, TaskDao taskDao, GtasksPreferenceService gtasksPreferenceService) {
public GtasksSyncService(MetadataService metadataService, MetadataDao metadataDao,
GtasksMetadataService gtasksMetadataService, TaskDao taskDao,
GtasksPreferenceService gtasksPreferenceService, GtasksTokenValidator gtasksTokenValidator) {
this.metadataService = metadataService;
this.metadataDao = metadataDao;
this.gtasksMetadataService = gtasksMetadataService;
this.taskDao = taskDao;
this.gtasksPreferenceService = gtasksPreferenceService;
this.gtasksTokenValidator = gtasksTokenValidator;
}
private final LinkedBlockingQueue<SyncOnSaveOperation> operationQueue = new LinkedBlockingQueue<>();
@ -150,7 +155,7 @@ public class GtasksSyncService {
continue;
}
try {
GtasksInvoker invoker = new GtasksInvoker(gtasksPreferenceService.getToken());
GtasksInvoker invoker = new GtasksInvoker(gtasksTokenValidator, gtasksPreferenceService.getToken());
op.op(invoker);
} catch (IOException e) {
Log.w("gtasks-sync-error", "Sync on save failed", e);

@ -66,11 +66,13 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
private final GtasksTaskListUpdater gtasksTaskListUpdater;
private final Context context;
private final Preferences preferences;
private final GtasksTokenValidator gtasksTokenValidator;
@Inject
public GtasksSyncV2Provider(TaskService taskService, MetadataService metadataService, StoreObjectDao storeObjectDao, GtasksPreferenceService gtasksPreferenceService,
GtasksSyncService gtasksSyncService, GtasksListService gtasksListService, GtasksMetadataService gtasksMetadataService,
GtasksTaskListUpdater gtasksTaskListUpdater, @ForApplication Context context, Preferences preferences) {
GtasksTaskListUpdater gtasksTaskListUpdater, @ForApplication Context context, Preferences preferences,
GtasksTokenValidator gtasksTokenValidator) {
this.taskService = taskService;
this.metadataService = metadataService;
this.storeObjectDao = storeObjectDao;
@ -81,6 +83,7 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
this.gtasksTaskListUpdater = gtasksTaskListUpdater;
this.context = context;
this.preferences = preferences;
this.gtasksTokenValidator = gtasksTokenValidator;
}
@Override
@ -118,7 +121,7 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
@Override
public void run() {
String authToken = getValidatedAuthToken();
final GtasksInvoker invoker = new GtasksInvoker(authToken);
final GtasksInvoker invoker = new GtasksInvoker(gtasksTokenValidator, authToken);
try {
gtasksListService.updateLists(invoker.allGtaskLists());
} catch (GoogleTasksException e) {
@ -200,7 +203,7 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
try {
String authToken = getValidatedAuthToken();
gtasksSyncService.waitUntilEmpty();
final GtasksInvoker service = new GtasksInvoker(authToken);
final GtasksInvoker service = new GtasksInvoker(gtasksTokenValidator, authToken);
synchronizeListHelper(gtasksList, service, manual, null, isImport);
} finally {
callback.finished();
@ -212,7 +215,7 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
private String getValidatedAuthToken() {
String authToken = gtasksPreferenceService.getToken();
try {
authToken = GtasksTokenValidator.validateAuthToken(context, authToken);
authToken = gtasksTokenValidator.validateAuthToken(context, authToken);
if (authToken != null) {
gtasksPreferenceService.setToken(authToken);
}
@ -222,7 +225,6 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
return authToken;
}
private synchronized void synchronizeListHelper(StoreObject list, GtasksInvoker invoker,
boolean manual, SyncExceptionHandler errorHandler, boolean isImport) {
String listId = list.getValue(GtasksList.REMOTE_ID);

Loading…
Cancel
Save