Inject GtasksTokenValidator

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

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

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

@ -15,26 +15,37 @@ import android.os.Bundle;
import com.google.api.client.googleapis.extensions.android2.auth.GoogleAccountManager; import com.google.api.client.googleapis.extensions.android2.auth.GoogleAccountManager;
import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.gtasks.GtasksPreferenceService; import com.todoroo.astrid.gtasks.GtasksPreferenceService;
import com.todoroo.astrid.gtasks.api.GoogleTasksException; import com.todoroo.astrid.gtasks.api.GoogleTasksException;
import com.todoroo.astrid.gtasks.api.GtasksInvoker; import com.todoroo.astrid.gtasks.api.GtasksInvoker;
import org.tasks.R; import org.tasks.R;
import org.tasks.preferences.Preferences;
import java.io.IOException; import java.io.IOException;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class GtasksTokenValidator { public class GtasksTokenValidator {
private static final String TOKEN_INTENT_RECEIVED = "intent!"; //$NON-NLS-1$ private static final String TOKEN_INTENT_RECEIVED = "intent!"; //$NON-NLS-1$
private static final int REVALIDATION_TRIES = 4; 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 * 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 * Shouldn't be called from the main thread--will block on network calls
* @return valid token on success, null on failure * @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()); GoogleAccountManager accountManager = new GoogleAccountManager(ContextManager.getContext());
if(testToken(token)) { if(testToken(token)) {
@ -42,7 +53,7 @@ public class GtasksTokenValidator {
} }
// If fail, token may have expired -- get a new one and return that // 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); Account a = accountManager.getAccountByName(accountName);
if (a == null) { if (a == null) {
throw new GoogleTasksException(c.getString(R.string.gtasks_error_accountNotFound, accountName), "account-not-found"); 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"); throw new GoogleTasksException(c.getString(R.string.gtasks_error_authRefresh), "auth-token-refresh");
} }
private static boolean testToken(String token) { private boolean testToken(String token) {
GtasksInvoker testService = new GtasksInvoker(token); GtasksInvoker testService = new GtasksInvoker(this, token);
try { try {
testService.ping(); testService.ping();
return true; 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 { throws GoogleTasksException {
Bundle result; Bundle result;
try { 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.GtasksApiUtilities;
import com.todoroo.astrid.gtasks.api.GtasksInvoker; import com.todoroo.astrid.gtasks.api.GtasksInvoker;
import com.todoroo.astrid.gtasks.api.MoveRequest; 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.MetadataService;
import com.todoroo.astrid.service.TaskService; import com.todoroo.astrid.service.TaskService;
@ -45,14 +46,18 @@ public class GtasksSyncService {
private final GtasksMetadataService gtasksMetadataService; private final GtasksMetadataService gtasksMetadataService;
private final TaskDao taskDao; private final TaskDao taskDao;
private final GtasksPreferenceService gtasksPreferenceService; private final GtasksPreferenceService gtasksPreferenceService;
private final GtasksTokenValidator gtasksTokenValidator;
@Inject @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.metadataService = metadataService;
this.metadataDao = metadataDao; this.metadataDao = metadataDao;
this.gtasksMetadataService = gtasksMetadataService; this.gtasksMetadataService = gtasksMetadataService;
this.taskDao = taskDao; this.taskDao = taskDao;
this.gtasksPreferenceService = gtasksPreferenceService; this.gtasksPreferenceService = gtasksPreferenceService;
this.gtasksTokenValidator = gtasksTokenValidator;
} }
private final LinkedBlockingQueue<SyncOnSaveOperation> operationQueue = new LinkedBlockingQueue<>(); private final LinkedBlockingQueue<SyncOnSaveOperation> operationQueue = new LinkedBlockingQueue<>();
@ -150,7 +155,7 @@ public class GtasksSyncService {
continue; continue;
} }
try { try {
GtasksInvoker invoker = new GtasksInvoker(gtasksPreferenceService.getToken()); GtasksInvoker invoker = new GtasksInvoker(gtasksTokenValidator, gtasksPreferenceService.getToken());
op.op(invoker); op.op(invoker);
} catch (IOException e) { } catch (IOException e) {
Log.w("gtasks-sync-error", "Sync on save failed", 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 GtasksTaskListUpdater gtasksTaskListUpdater;
private final Context context; private final Context context;
private final Preferences preferences; private final Preferences preferences;
private final GtasksTokenValidator gtasksTokenValidator;
@Inject @Inject
public GtasksSyncV2Provider(TaskService taskService, MetadataService metadataService, StoreObjectDao storeObjectDao, GtasksPreferenceService gtasksPreferenceService, public GtasksSyncV2Provider(TaskService taskService, MetadataService metadataService, StoreObjectDao storeObjectDao, GtasksPreferenceService gtasksPreferenceService,
GtasksSyncService gtasksSyncService, GtasksListService gtasksListService, GtasksMetadataService gtasksMetadataService, 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.taskService = taskService;
this.metadataService = metadataService; this.metadataService = metadataService;
this.storeObjectDao = storeObjectDao; this.storeObjectDao = storeObjectDao;
@ -81,6 +83,7 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
this.gtasksTaskListUpdater = gtasksTaskListUpdater; this.gtasksTaskListUpdater = gtasksTaskListUpdater;
this.context = context; this.context = context;
this.preferences = preferences; this.preferences = preferences;
this.gtasksTokenValidator = gtasksTokenValidator;
} }
@Override @Override
@ -118,7 +121,7 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
@Override @Override
public void run() { public void run() {
String authToken = getValidatedAuthToken(); String authToken = getValidatedAuthToken();
final GtasksInvoker invoker = new GtasksInvoker(authToken); final GtasksInvoker invoker = new GtasksInvoker(gtasksTokenValidator, authToken);
try { try {
gtasksListService.updateLists(invoker.allGtaskLists()); gtasksListService.updateLists(invoker.allGtaskLists());
} catch (GoogleTasksException e) { } catch (GoogleTasksException e) {
@ -200,7 +203,7 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
try { try {
String authToken = getValidatedAuthToken(); String authToken = getValidatedAuthToken();
gtasksSyncService.waitUntilEmpty(); gtasksSyncService.waitUntilEmpty();
final GtasksInvoker service = new GtasksInvoker(authToken); final GtasksInvoker service = new GtasksInvoker(gtasksTokenValidator, authToken);
synchronizeListHelper(gtasksList, service, manual, null, isImport); synchronizeListHelper(gtasksList, service, manual, null, isImport);
} finally { } finally {
callback.finished(); callback.finished();
@ -212,7 +215,7 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
private String getValidatedAuthToken() { private String getValidatedAuthToken() {
String authToken = gtasksPreferenceService.getToken(); String authToken = gtasksPreferenceService.getToken();
try { try {
authToken = GtasksTokenValidator.validateAuthToken(context, authToken); authToken = gtasksTokenValidator.validateAuthToken(context, authToken);
if (authToken != null) { if (authToken != null) {
gtasksPreferenceService.setToken(authToken); gtasksPreferenceService.setToken(authToken);
} }
@ -222,7 +225,6 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
return authToken; return authToken;
} }
private synchronized void synchronizeListHelper(StoreObject list, GtasksInvoker invoker, private synchronized void synchronizeListHelper(StoreObject list, GtasksInvoker invoker,
boolean manual, SyncExceptionHandler errorHandler, boolean isImport) { boolean manual, SyncExceptionHandler errorHandler, boolean isImport) {
String listId = list.getValue(GtasksList.REMOTE_ID); String listId = list.getValue(GtasksList.REMOTE_ID);

Loading…
Cancel
Save