mirror of https://github.com/tasks/tasks
parent
2b68b887b5
commit
29afa0d804
@ -0,0 +1,12 @@
|
||||
package com.todoroo.astrid.gtasks;
|
||||
|
||||
import android.test.AndroidTestCase;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GtasksIndentActionTest extends AndroidTestCase {
|
||||
@Inject
|
||||
public GtasksIndentActionTest() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package com.todoroo.astrid.gtasks;
|
||||
|
||||
import android.test.AndroidTestCase;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GtasksListServiceTest extends AndroidTestCase {
|
||||
@Inject
|
||||
public GtasksListServiceTest() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package com.todoroo.astrid.gtasks;
|
||||
|
||||
import android.test.AndroidTestCase;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GtasksTaskListUpdaterTest extends AndroidTestCase {
|
||||
@Inject
|
||||
public GtasksTaskListUpdaterTest() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package com.todoroo.astrid.gtasks;
|
||||
|
||||
import android.test.AndroidTestCase;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GtasksTaskMovingTest extends AndroidTestCase {
|
||||
@Inject
|
||||
public GtasksTaskMovingTest() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package com.todoroo.astrid.gtasks;
|
||||
|
||||
import com.todoroo.astrid.api.Filter;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GtasksFilterExposer {
|
||||
@Inject
|
||||
public GtasksFilterExposer() {
|
||||
|
||||
}
|
||||
|
||||
public List<Filter> getFilters() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
package com.todoroo.astrid.gtasks;
|
||||
|
||||
import com.todoroo.astrid.activity.TaskListFragment;
|
||||
|
||||
public class GtasksListFragment extends TaskListFragment {
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package com.todoroo.astrid.gtasks;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GtasksListService {
|
||||
|
||||
@Inject
|
||||
public GtasksListService() {
|
||||
|
||||
}
|
||||
|
||||
public List<GtasksList> getLists() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package com.todoroo.astrid.gtasks;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GtasksMetadataService {
|
||||
@Inject
|
||||
public GtasksMetadataService() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package com.todoroo.astrid.gtasks;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GtasksPreferences {
|
||||
@Inject
|
||||
public GtasksPreferences() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package com.todoroo.astrid.gtasks;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GtasksTaskListUpdater {
|
||||
@Inject
|
||||
public GtasksTaskListUpdater() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package com.todoroo.astrid.gtasks.auth;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GtasksLoginActivity {
|
||||
@Inject
|
||||
public GtasksLoginActivity() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
package com.todoroo.astrid.gtasks.sync;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GtasksSyncService {
|
||||
@Inject
|
||||
public GtasksSyncService() {
|
||||
|
||||
}
|
||||
|
||||
public void initialize() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
package com.todoroo.astrid.gtasks.sync;
|
||||
|
||||
import com.todoroo.astrid.gtasks.GtasksList;
|
||||
import com.todoroo.astrid.sync.SyncResultCallback;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GtasksSyncV2Provider {
|
||||
|
||||
@Inject
|
||||
public GtasksSyncV2Provider() {
|
||||
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void synchronizeActiveTasks(SyncResultCallback callback) {
|
||||
|
||||
}
|
||||
|
||||
public void synchronizeList(GtasksList list, SyncResultCallback callback) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package org.tasks.activities;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class ClearGtaskDataActivity {
|
||||
@Inject
|
||||
public ClearGtaskDataActivity() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package org.tasks.dialogs;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class AccountSelectionDialog {
|
||||
@Inject
|
||||
public AccountSelectionDialog() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package org.tasks.scheduling;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GtasksBackgroundService {
|
||||
@Inject
|
||||
public GtasksBackgroundService() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,115 @@
|
||||
package org.tasks;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
|
||||
import com.google.android.gms.auth.GoogleAuthException;
|
||||
import com.google.android.gms.auth.GoogleAuthUtil;
|
||||
import com.google.android.gms.auth.UserRecoverableAuthException;
|
||||
import com.google.api.client.googleapis.extensions.android.accounts.GoogleAccountManager;
|
||||
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
|
||||
import com.google.api.services.tasks.TasksScopes;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Predicate;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tasks.injection.ForApplication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static com.google.common.collect.Iterables.tryFind;
|
||||
import static com.google.common.collect.Lists.transform;
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
public class AccountManager {
|
||||
|
||||
public void clearToken(GoogleAccountCredential credential) throws IOException {
|
||||
try {
|
||||
String token = credential.getToken();
|
||||
log.debug("Invalidating {}", token);
|
||||
GoogleAuthUtil.clearToken(context, token);
|
||||
GoogleAuthUtil.getTokenWithNotification(context, credential.getSelectedAccount(), "oauth2:" + TasksScopes.TASKS, null);
|
||||
} catch (GoogleAuthException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new IOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public interface AuthResultHandler {
|
||||
void authenticationSuccessful(String accountName, String authToken);
|
||||
void authenticationFailed(String message);
|
||||
}
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AccountManager.class);
|
||||
|
||||
public static final int REQUEST_AUTHORIZATION = 10987;
|
||||
|
||||
private final Context context;
|
||||
private final GoogleAccountManager googleAccountManager;
|
||||
|
||||
@Inject
|
||||
public AccountManager(@ForApplication Context context) {
|
||||
this.context = context;
|
||||
|
||||
googleAccountManager = new GoogleAccountManager(context);
|
||||
}
|
||||
|
||||
public List<String> getAccounts() {
|
||||
return transform(getAccountList(), new Function<Account, String>() {
|
||||
@Nullable
|
||||
@Override
|
||||
public String apply(Account account) {
|
||||
return account.name;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public boolean hasAccount(final String name) {
|
||||
return getAccount(name) != null;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return getAccounts().isEmpty();
|
||||
}
|
||||
|
||||
public void getAuthToken(final Activity activity, final String accountName, final AuthResultHandler handler) {
|
||||
final Account account = getAccount(accountName);
|
||||
if (account == null) {
|
||||
handler.authenticationFailed(activity.getString(R.string.gtasks_error_accountNotFound, accountName));
|
||||
} else {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
handler.authenticationSuccessful(accountName, GoogleAuthUtil.getToken(activity, account, "oauth2:" + TasksScopes.TASKS, null));
|
||||
} catch(UserRecoverableAuthException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
activity.startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
|
||||
} catch(GoogleAuthException | IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
handler.authenticationFailed(context.getString(R.string.gtasks_GLA_errorIOAuth));
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
|
||||
private List<Account> getAccountList() {
|
||||
return asList(googleAccountManager.getAccounts());
|
||||
}
|
||||
|
||||
public Account getAccount(final String name) {
|
||||
return tryFind(getAccountList(), new Predicate<Account>() {
|
||||
@Override
|
||||
public boolean apply(Account account) {
|
||||
return name.equalsIgnoreCase(account.name);
|
||||
}
|
||||
}).orNull();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<bool name="sync_enabled">true</bool>
|
||||
</resources>
|
||||
@ -1,119 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2012 Todoroo Inc
|
||||
*
|
||||
* See the file "LICENSE" for the full license governing this code.
|
||||
*/
|
||||
package com.todoroo.astrid.gtasks.auth;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountManager;
|
||||
import android.accounts.AccountManagerFuture;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.google.api.client.googleapis.extensions.android.accounts.GoogleAccountManager;
|
||||
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.injection.ForApplication;
|
||||
|
||||
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 Context context;
|
||||
private final GtasksPreferenceService preferences;
|
||||
|
||||
@Inject
|
||||
public GtasksTokenValidator(@ForApplication Context context, GtasksPreferenceService preferences) {
|
||||
this.context = context;
|
||||
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 synchronized String validateAuthToken(Context c, String token) throws GoogleTasksException {
|
||||
GoogleAccountManager accountManager = new GoogleAccountManager(context);
|
||||
|
||||
if(testToken(token)) {
|
||||
return token;
|
||||
}
|
||||
|
||||
// If fail, token may have expired -- get a new one and return that
|
||||
String accountName = preferences.getUserName();
|
||||
Account a = accountManager.getAccountByName(accountName);
|
||||
if (a == null) {
|
||||
throw new GoogleTasksException(c.getString(R.string.gtasks_error_accountNotFound, accountName));
|
||||
}
|
||||
|
||||
for (int i = 0; i < REVALIDATION_TRIES; i++) {
|
||||
accountManager.invalidateAuthToken(token);
|
||||
|
||||
// try with notify-auth-failure = false
|
||||
AccountManagerFuture<Bundle> future = accountManager.getAccountManager().getAuthToken(a, GtasksInvoker.AUTH_TOKEN_TYPE, false, null, null);
|
||||
token = getTokenFromFuture(c, future);
|
||||
|
||||
if(TOKEN_INTENT_RECEIVED.equals(token)) {
|
||||
return null;
|
||||
} else if(token != null && testToken(token)) {
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
throw new GoogleTasksException(c.getString(R.string.gtasks_error_authRefresh));
|
||||
}
|
||||
|
||||
private boolean testToken(String token) {
|
||||
GtasksInvoker testService = new GtasksInvoker(context, this, token);
|
||||
try {
|
||||
testService.ping();
|
||||
return true;
|
||||
} catch (IOException i) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private String getTokenFromFuture(Context c, AccountManagerFuture<Bundle> future)
|
||||
throws GoogleTasksException {
|
||||
Bundle result;
|
||||
try {
|
||||
result = future.getResult();
|
||||
if(result == null) {
|
||||
throw new NullPointerException("Future result was null."); //$NON-NLS-1$
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new GoogleTasksException(e.getLocalizedMessage());
|
||||
}
|
||||
|
||||
// check what kind of result was returned
|
||||
String token;
|
||||
if (result.containsKey(AccountManager.KEY_AUTHTOKEN)) {
|
||||
token = result.getString(AccountManager.KEY_AUTHTOKEN);
|
||||
} else if (result.containsKey(AccountManager.KEY_INTENT)) {
|
||||
Intent intent = (Intent) result.get(AccountManager.KEY_INTENT);
|
||||
if (c instanceof Activity) {
|
||||
c.startActivity(intent);
|
||||
} else {
|
||||
throw new GoogleTasksException(c.getString(R.string.gtasks_error_background_sync_auth));
|
||||
}
|
||||
return TOKEN_INTENT_RECEIVED;
|
||||
} else {
|
||||
throw new GoogleTasksException(c.getString(R.string.gtasks_error_accountManager));
|
||||
}
|
||||
return token;
|
||||
}
|
||||
}
|
||||
@ -1,110 +0,0 @@
|
||||
package org.tasks;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountManagerCallback;
|
||||
import android.accounts.AccountManagerFuture;
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.google.api.client.googleapis.extensions.android.accounts.GoogleAccountManager;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.todoroo.astrid.gtasks.api.GtasksInvoker;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static com.google.common.collect.Iterables.tryFind;
|
||||
import static com.google.common.collect.Lists.transform;
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
public class AccountManager {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AccountManager.class);
|
||||
|
||||
public interface AuthResultHandler {
|
||||
void authenticationSuccessful(String accountName, String authToken);
|
||||
|
||||
void authenticationFailed(String message);
|
||||
}
|
||||
|
||||
private GoogleAccountManager googleAccountManager;
|
||||
private Activity activity;
|
||||
|
||||
@Inject
|
||||
public AccountManager(Activity activity) {
|
||||
this.activity = activity;
|
||||
|
||||
googleAccountManager = new GoogleAccountManager(activity);
|
||||
}
|
||||
|
||||
public List<String> getAccounts() {
|
||||
return transform(getAccountList(), new Function<Account, String>() {
|
||||
@Nullable
|
||||
@Override
|
||||
public String apply(Account account) {
|
||||
return account.name;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public boolean hasAccount(final String name) {
|
||||
return getAccount(name) != null;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return getAccounts().isEmpty();
|
||||
}
|
||||
|
||||
public void getAuthToken(final String accountName, final AuthResultHandler handler) {
|
||||
Account account = getAccount(accountName);
|
||||
if (account == null) {
|
||||
handler.authenticationFailed(activity.getString(R.string.gtasks_error_accountNotFound, accountName));
|
||||
} else {
|
||||
googleAccountManager.getAccountManager().getAuthToken(account, GtasksInvoker.AUTH_TOKEN_TYPE, null, activity, new AccountManagerCallback<Bundle>() {
|
||||
@Override
|
||||
public void run(final AccountManagerFuture<Bundle> future) {
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Bundle bundle = future.getResult(30, TimeUnit.SECONDS);
|
||||
if (bundle.containsKey(android.accounts.AccountManager.KEY_AUTHTOKEN)) {
|
||||
handler.authenticationSuccessful(accountName, bundle.getString(android.accounts.AccountManager.KEY_AUTHTOKEN));
|
||||
} else {
|
||||
log.error("No auth token found in response bundle");
|
||||
handler.authenticationFailed(activity.getString(R.string.gtasks_error_accountNotFound, accountName));
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
handler.authenticationFailed(activity.getString(e instanceof IOException
|
||||
? R.string.gtasks_GLA_errorIOAuth
|
||||
: R.string.gtasks_GLA_errorAuth));
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
}, null);
|
||||
}
|
||||
}
|
||||
|
||||
private List<Account> getAccountList() {
|
||||
return asList(googleAccountManager.getAccounts());
|
||||
}
|
||||
|
||||
private Account getAccount(final String name) {
|
||||
return tryFind(getAccountList(), new Predicate<Account>() {
|
||||
@Override
|
||||
public boolean apply(Account account) {
|
||||
return name.equalsIgnoreCase(account.name);
|
||||
}
|
||||
}).orNull();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue