|
|
@ -53,15 +53,16 @@ import com.todoroo.astrid.utility.Constants;
|
|
|
|
import com.todoroo.gtasks.GoogleConnectionManager;
|
|
|
|
import com.todoroo.gtasks.GoogleConnectionManager;
|
|
|
|
import com.todoroo.gtasks.GoogleLoginException;
|
|
|
|
import com.todoroo.gtasks.GoogleLoginException;
|
|
|
|
import com.todoroo.gtasks.GoogleTaskService;
|
|
|
|
import com.todoroo.gtasks.GoogleTaskService;
|
|
|
|
import com.todoroo.gtasks.GoogleTaskService.ConvenientTaskCreator;
|
|
|
|
|
|
|
|
import com.todoroo.gtasks.GoogleTaskTask;
|
|
|
|
import com.todoroo.gtasks.GoogleTaskTask;
|
|
|
|
import com.todoroo.gtasks.GoogleTaskView;
|
|
|
|
import com.todoroo.gtasks.GoogleTaskView;
|
|
|
|
import com.todoroo.gtasks.GoogleTasksException;
|
|
|
|
import com.todoroo.gtasks.GoogleTasksException;
|
|
|
|
|
|
|
|
import com.todoroo.gtasks.actions.Action;
|
|
|
|
import com.todoroo.gtasks.actions.Actions;
|
|
|
|
import com.todoroo.gtasks.actions.Actions;
|
|
|
|
import com.todoroo.gtasks.actions.GetTasksAction;
|
|
|
|
import com.todoroo.gtasks.actions.GetTasksAction;
|
|
|
|
import com.todoroo.gtasks.actions.ListAction;
|
|
|
|
import com.todoroo.gtasks.actions.ListAction;
|
|
|
|
import com.todoroo.gtasks.actions.ListActions;
|
|
|
|
import com.todoroo.gtasks.actions.ListActions;
|
|
|
|
import com.todoroo.gtasks.actions.ListActions.TaskBuilder;
|
|
|
|
import com.todoroo.gtasks.actions.ListActions.TaskBuilder;
|
|
|
|
|
|
|
|
import com.todoroo.gtasks.actions.ListActions.TaskCreator;
|
|
|
|
import com.todoroo.gtasks.actions.ListActions.TaskModifier;
|
|
|
|
import com.todoroo.gtasks.actions.ListActions.TaskModifier;
|
|
|
|
import com.todoroo.gtasks.actions.ListCreationAction;
|
|
|
|
import com.todoroo.gtasks.actions.ListCreationAction;
|
|
|
|
|
|
|
|
|
|
|
@ -78,6 +79,8 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
|
|
|
|
private static final Actions a = new Actions();
|
|
|
|
private static final Actions a = new Actions();
|
|
|
|
private static final ListActions l = new ListActions();
|
|
|
|
private static final ListActions l = new ListActions();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ArrayList<GtasksTaskContainer> createdWithoutId;
|
|
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
static {
|
|
|
|
AstridDependencyInjector.initialize();
|
|
|
|
AstridDependencyInjector.initialize();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -201,6 +204,8 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
|
|
|
|
protected void performSync() {
|
|
|
|
protected void performSync() {
|
|
|
|
StatisticsService.reportEvent("gtasks-started");
|
|
|
|
StatisticsService.reportEvent("gtasks-started");
|
|
|
|
gtasksPreferenceService.recordSyncStart();
|
|
|
|
gtasksPreferenceService.recordSyncStart();
|
|
|
|
|
|
|
|
System.err.println("- -------- SYNC START!!!!");
|
|
|
|
|
|
|
|
createdWithoutId = new ArrayList<GtasksTaskContainer>();
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
GoogleTaskView taskView = taskService.getTaskView();
|
|
|
|
GoogleTaskView taskView = taskService.getTaskView();
|
|
|
@ -211,9 +216,7 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
|
|
|
|
gtasksTaskListUpdater.createParentSiblingMaps();
|
|
|
|
gtasksTaskListUpdater.createParentSiblingMaps();
|
|
|
|
|
|
|
|
|
|
|
|
// read non-deleted tasks for each list
|
|
|
|
// read non-deleted tasks for each list
|
|
|
|
ArrayList<GtasksTaskContainer> remoteTasks = readAllRemoteTasks(false);
|
|
|
|
SyncData<GtasksTaskContainer> syncData = populateSyncData();
|
|
|
|
|
|
|
|
|
|
|
|
SyncData<GtasksTaskContainer> syncData = populateSyncData(remoteTasks);
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
synchronizeTasks(syncData);
|
|
|
|
synchronizeTasks(syncData);
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
@ -228,6 +231,7 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
|
|
|
|
|
|
|
|
|
|
|
|
Intent broadcastIntent = new Intent(AstridApiConstants.BROADCAST_EVENT_REFRESH);
|
|
|
|
Intent broadcastIntent = new Intent(AstridApiConstants.BROADCAST_EVENT_REFRESH);
|
|
|
|
ContextManager.getContext().sendBroadcast(broadcastIntent, AstridApiConstants.PERMISSION_READ);
|
|
|
|
ContextManager.getContext().sendBroadcast(broadcastIntent, AstridApiConstants.PERMISSION_READ);
|
|
|
|
|
|
|
|
System.err.println("- ------ SYNC FINITO!!!!");
|
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
// occurs when application was closed
|
|
|
|
// occurs when application was closed
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
@ -242,6 +246,7 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
|
|
|
|
listId = taskView.getActiveTaskList().getInfo().getId();
|
|
|
|
listId = taskView.getActiveTaskList().getInfo().getId();
|
|
|
|
else if(taskView.getAllLists().length == 0) {
|
|
|
|
else if(taskView.getAllLists().length == 0) {
|
|
|
|
ListCreationAction createList = a.createList(0, ContextManager.getString(R.string.app_name));
|
|
|
|
ListCreationAction createList = a.createList(0, ContextManager.getString(R.string.app_name));
|
|
|
|
|
|
|
|
System.err.println("ACTION: createList(4)");
|
|
|
|
taskService.executeActions(createList);
|
|
|
|
taskService.executeActions(createList);
|
|
|
|
listId = createList.getNewId();
|
|
|
|
listId = createList.getNewId();
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -262,6 +267,41 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
|
|
|
|
throw new GoogleTasksException(e);
|
|
|
|
throw new GoogleTasksException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// match remote tasks to locally created tasks
|
|
|
|
|
|
|
|
HashMap<String, GtasksTaskContainer> locals = new HashMap<String, GtasksTaskContainer>();
|
|
|
|
|
|
|
|
for(GtasksTaskContainer task : createdWithoutId) {
|
|
|
|
|
|
|
|
locals.put(task.task.getValue(Task.TITLE), task);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayList<Action> moveActions = new ArrayList<Action>();
|
|
|
|
|
|
|
|
for(GtasksTaskContainer remote : data.remoteUpdated) {
|
|
|
|
|
|
|
|
if(remote.task.getId() < 1) {
|
|
|
|
|
|
|
|
GtasksTaskContainer local = locals.get(remote.task.getValue(Task.TITLE));
|
|
|
|
|
|
|
|
if(local != null) {
|
|
|
|
|
|
|
|
System.err.println("FOUND LOCAL - " + remote.task.getValue(Task.TITLE));
|
|
|
|
|
|
|
|
remote.task.setId(local.task.getId());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String remoteList = remote.gtaskMetadata.getValue(GtasksMetadata.LIST_ID);
|
|
|
|
|
|
|
|
String localList = local.gtaskMetadata.getValue(GtasksMetadata.LIST_ID);
|
|
|
|
|
|
|
|
if(!remoteList.equals(localList)) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
System.err.println("MOVE: " + remoteList + " to " + localList);
|
|
|
|
|
|
|
|
moveActions.add(a.moveTask(remote.gtaskMetadata.getValue(GtasksMetadata.ID),
|
|
|
|
|
|
|
|
remoteList, localList, local.parentId));
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
|
|
Log.e("gtasks-sync", "Error Moving Local Task", e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(moveActions.size() > 0) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
taskService.executeActions(moveActions.toArray(new Action[moveActions.size()]));
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
|
|
Log.e("gtasks-sync", "Error Running Local Action", e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
super.readRemotelyUpdated(data);
|
|
|
|
super.readRemotelyUpdated(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -286,7 +326,12 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
|
|
|
|
* Populate SyncData data structure
|
|
|
|
* Populate SyncData data structure
|
|
|
|
* @throws JSONException
|
|
|
|
* @throws JSONException
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private SyncData<GtasksTaskContainer> populateSyncData(ArrayList<GtasksTaskContainer> remoteTasks) throws JSONException {
|
|
|
|
private SyncData<GtasksTaskContainer> populateSyncData() throws JSONException,
|
|
|
|
|
|
|
|
GoogleLoginException, IOException {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// fetch remote tasks
|
|
|
|
|
|
|
|
ArrayList<GtasksTaskContainer> remoteTasks = readAllRemoteTasks(false);
|
|
|
|
|
|
|
|
|
|
|
|
// fetch locally created tasks
|
|
|
|
// fetch locally created tasks
|
|
|
|
TodorooCursor<Task> localCreated = gtasksMetadataService.getLocallyCreated(PROPERTIES);
|
|
|
|
TodorooCursor<Task> localCreated = gtasksMetadataService.getLocallyCreated(PROPERTIES);
|
|
|
|
|
|
|
|
|
|
|
@ -306,6 +351,7 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
|
|
|
|
for(StoreObject dashboard : gtasksListService.getLists()) {
|
|
|
|
for(StoreObject dashboard : gtasksListService.getLists()) {
|
|
|
|
String listId = dashboard.getValue(GtasksList.REMOTE_ID);
|
|
|
|
String listId = dashboard.getValue(GtasksList.REMOTE_ID);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.err.println("ACTION: getTasks, " + listId);
|
|
|
|
GetTasksAction action = new GetTasksAction(listId, includeDeleted);
|
|
|
|
GetTasksAction action = new GetTasksAction(listId, includeDeleted);
|
|
|
|
taskService.executeActions(action);
|
|
|
|
taskService.executeActions(action);
|
|
|
|
List<GoogleTaskTask> list = action.getGoogleTasks();
|
|
|
|
List<GoogleTaskTask> list = action.getGoogleTasks();
|
|
|
@ -369,20 +415,18 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
|
|
|
|
if(local.gtaskMetadata.containsNonNullValue(GtasksMetadata.LIST_ID))
|
|
|
|
if(local.gtaskMetadata.containsNonNullValue(GtasksMetadata.LIST_ID))
|
|
|
|
list = local.gtaskMetadata.getValue(GtasksMetadata.LIST_ID);
|
|
|
|
list = local.gtaskMetadata.getValue(GtasksMetadata.LIST_ID);
|
|
|
|
gtasksTaskListUpdater.updateParentAndSibling(local);
|
|
|
|
gtasksTaskListUpdater.updateParentAndSibling(local);
|
|
|
|
|
|
|
|
local.gtaskMetadata.setValue(GtasksMetadata.ID, null);
|
|
|
|
|
|
|
|
local.gtaskMetadata.setValue(GtasksMetadata.LIST_ID, list);
|
|
|
|
|
|
|
|
|
|
|
|
ConvenientTaskCreator createdTask;
|
|
|
|
createdWithoutId.add(local);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
createdTask = taskService.createTask(list, local.task.getValue(Task.TITLE));
|
|
|
|
TaskCreator createdTask = l.createTask(local.task.getValue(Task.TITLE));
|
|
|
|
createdTask.parentId(local.parentId);
|
|
|
|
createdTask.parentId(local.parentId);
|
|
|
|
|
|
|
|
updateTaskHelper(local, null, createdTask);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
} catch (JSONException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
throw new GoogleTasksException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String remoteId = updateTaskHelper(local, null, createdTask);
|
|
|
|
|
|
|
|
gtasksTaskListUpdater.addRemoteTaskMapping(local.task.getId(), remoteId);
|
|
|
|
|
|
|
|
local.gtaskMetadata.setValue(GtasksMetadata.ID, remoteId);
|
|
|
|
|
|
|
|
local.gtaskMetadata.setValue(GtasksMetadata.LIST_ID, list);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return local;
|
|
|
|
return local;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -397,6 +441,8 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
|
|
|
|
// moving between lists
|
|
|
|
// moving between lists
|
|
|
|
if(remote != null && !idList.equals(remote.gtaskMetadata.getValue(
|
|
|
|
if(remote != null && !idList.equals(remote.gtaskMetadata.getValue(
|
|
|
|
GtasksMetadata.LIST_ID))) {
|
|
|
|
GtasksMetadata.LIST_ID))) {
|
|
|
|
|
|
|
|
System.err.println("ACTION: moveTask(5), " + idTask + ", " + idList + " to " +
|
|
|
|
|
|
|
|
remote.gtaskMetadata.getValue(GtasksMetadata.LIST_ID));
|
|
|
|
taskService.executeActions(a.moveTask(idTask, idList,
|
|
|
|
taskService.executeActions(a.moveTask(idTask, idList,
|
|
|
|
remote.gtaskMetadata.getValue(GtasksMetadata.LIST_ID), null));
|
|
|
|
remote.gtaskMetadata.getValue(GtasksMetadata.LIST_ID), null));
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -414,18 +460,23 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
|
|
|
|
String id = idList;
|
|
|
|
String id = idList;
|
|
|
|
|
|
|
|
|
|
|
|
// write task (and perform move action if requested)
|
|
|
|
// write task (and perform move action if requested)
|
|
|
|
|
|
|
|
ListAction action;
|
|
|
|
if(builder instanceof TaskModifier) {
|
|
|
|
if(builder instanceof TaskModifier) {
|
|
|
|
|
|
|
|
System.err.println("ACTION: task edit (6), " + idTask);
|
|
|
|
|
|
|
|
action = ((TaskModifier) builder).done();
|
|
|
|
|
|
|
|
} else if(builder instanceof TaskCreator) {
|
|
|
|
|
|
|
|
System.err.println("ACTION: task create (7), " + local.task.getValue(Task.TITLE));
|
|
|
|
|
|
|
|
action = ((TaskCreator) builder).done();
|
|
|
|
|
|
|
|
} else
|
|
|
|
|
|
|
|
throw new GoogleTasksException("Unknown builder " + builder.getClass());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(idTask != null && local.parentId != null && (remote == null || local.parentId != remote.parentId ||
|
|
|
|
|
|
|
|
local.priorSiblingId != remote.priorSiblingId)) {
|
|
|
|
|
|
|
|
System.err.println("ACTION: move(1) - " + idTask + ", " + local.parentId + ", " + local.priorSiblingId);
|
|
|
|
ListAction moveAction = l.move(idTask, local.parentId, local.priorSiblingId);
|
|
|
|
ListAction moveAction = l.move(idTask, local.parentId, local.priorSiblingId);
|
|
|
|
ListAction action = ((TaskModifier) builder).done();
|
|
|
|
taskService.executeListActions(idList, action, moveAction);
|
|
|
|
if(remote == null || local.parentId != remote.parentId || local.priorSiblingId != remote.priorSiblingId)
|
|
|
|
} else if(action.toJson(idList).getJSONObject("entity_delta").length() > 0)
|
|
|
|
taskService.executeListActions(idList, action, moveAction);
|
|
|
|
taskService.executeListActions(idList, action);
|
|
|
|
else if(action.toJson(idList).getJSONObject("entity_delta").length() > 0)
|
|
|
|
|
|
|
|
taskService.executeListActions(idList, action);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
id = ((ConvenientTaskCreator)builder).go();
|
|
|
|
|
|
|
|
ListAction moveAction = l.move(id, local.parentId, local.priorSiblingId);
|
|
|
|
|
|
|
|
taskService.executeListActions(idList, moveAction);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return id;
|
|
|
|
return id;
|
|
|
|
} catch (JSONException e) {
|
|
|
|
} catch (JSONException e) {
|
|
|
@ -476,6 +527,7 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
|
|
|
|
gtasksTaskListUpdater.updateParentAndSibling(local);
|
|
|
|
gtasksTaskListUpdater.updateParentAndSibling(local);
|
|
|
|
|
|
|
|
|
|
|
|
String id = local.gtaskMetadata.getValue(GtasksMetadata.ID);
|
|
|
|
String id = local.gtaskMetadata.getValue(GtasksMetadata.ID);
|
|
|
|
|
|
|
|
System.err.println("ACTION: modifyTask(3) - " + id);
|
|
|
|
TaskModifier modifyTask = l.modifyTask(id);
|
|
|
|
TaskModifier modifyTask = l.modifyTask(id);
|
|
|
|
if(shouldTransmit(local, Task.TITLE, remote))
|
|
|
|
if(shouldTransmit(local, Task.TITLE, remote))
|
|
|
|
modifyTask.name(local.task.getValue(Task.TITLE));
|
|
|
|
modifyTask.name(local.task.getValue(Task.TITLE));
|
|
|
|