Multi-threading google tasks sync

pull/14/head
Tim Su 13 years ago
parent 200e8fbf9b
commit 1cf26c4ca6

@ -8,6 +8,7 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.concurrent.Semaphore;
import org.json.JSONException; import org.json.JSONException;
@ -204,7 +205,7 @@ 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!!!!"); System.err.println("- -------- SYNC STARTED");
createdWithoutId = new ArrayList<GtasksTaskContainer>(); createdWithoutId = new ArrayList<GtasksTaskContainer>();
try { try {
@ -231,7 +232,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!!!!"); System.err.println("- ------ SYNC FINISHED");
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// occurs when application was closed // occurs when application was closed
} catch (Exception e) { } catch (Exception e) {
@ -261,11 +262,7 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
throws IOException { throws IOException {
// first, pull all tasks. then we can write them // first, pull all tasks. then we can write them
// include deleted tasks so we can delete them in astrid // include deleted tasks so we can delete them in astrid
try { data.remoteUpdated = readAllRemoteTasks(true);
data.remoteUpdated = readAllRemoteTasks(true);
} catch (JSONException e) {
throw new GoogleTasksException(e);
}
// match remote tasks to locally created tasks // match remote tasks to locally created tasks
HashMap<String, GtasksTaskContainer> locals = new HashMap<String, GtasksTaskContainer>(); HashMap<String, GtasksTaskContainer> locals = new HashMap<String, GtasksTaskContainer>();
@ -345,17 +342,36 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
// ------------------------------------------------- create / push / pull // ------------------------------------------------- create / push / pull
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
private ArrayList<GtasksTaskContainer> readAllRemoteTasks(boolean includeDeleted) private ArrayList<GtasksTaskContainer> readAllRemoteTasks(final boolean includeDeleted) {
throws JSONException, IOException, GoogleLoginException { final ArrayList<GtasksTaskContainer> remoteTasks = new ArrayList<GtasksTaskContainer>();
ArrayList<GtasksTaskContainer> remoteTasks = new ArrayList<GtasksTaskContainer>(); final Semaphore listsFinished = new Semaphore(0);
for(StoreObject dashboard : gtasksListService.getLists()) {
String listId = dashboard.getValue(GtasksList.REMOTE_ID); // launch threads
StoreObject[] lists = gtasksListService.getLists();
System.err.println("ACTION: getTasks, " + listId); for(final StoreObject dashboard : lists) {
GetTasksAction action = new GetTasksAction(listId, includeDeleted); new Thread(new Runnable() {
taskService.executeActions(action); @Override
List<GoogleTaskTask> list = action.getGoogleTasks(); public void run() {
addRemoteTasksToList(list, remoteTasks); try {
String listId = dashboard.getValue(GtasksList.REMOTE_ID);
System.err.println("ACTION: getTasks, " + listId);
GetTasksAction action = new GetTasksAction(listId, includeDeleted);
taskService.executeActions(action);
List<GoogleTaskTask> list = action.getGoogleTasks();
addRemoteTasksToList(list, remoteTasks);
} catch (Exception e) {
handleException("read-remotes", e, false);
} finally {
listsFinished.release();
}
}
}).start();
}
try {
listsFinished.acquire(lists.length);
} catch (InterruptedException e) {
handleException("wait-for-remotes", e, false);
} }
return remoteTasks; return remoteTasks;
} }
@ -430,24 +446,14 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
return local; return local;
} }
private String updateTaskHelper(GtasksTaskContainer local, private void updateTaskHelper(final GtasksTaskContainer local,
GtasksTaskContainer remote, TaskBuilder<?> builder) throws IOException { final GtasksTaskContainer remote, TaskBuilder<?> builder) throws IOException {
String idTask = local.gtaskMetadata.getValue(GtasksMetadata.ID); final String idTask = local.gtaskMetadata.getValue(GtasksMetadata.ID);
String idList = local.gtaskMetadata.getValue(GtasksMetadata.LIST_ID); final String idList = local.gtaskMetadata.getValue(GtasksMetadata.LIST_ID);
try { try {
// set properties
// moving between lists
if(remote != null && !idList.equals(remote.gtaskMetadata.getValue(
GtasksMetadata.LIST_ID))) {
System.err.println("ACTION: moveTask(5), " + idTask + ", " + idList + " to " +
remote.gtaskMetadata.getValue(GtasksMetadata.LIST_ID));
taskService.executeActions(a.moveTask(idTask, idList,
remote.gtaskMetadata.getValue(GtasksMetadata.LIST_ID), null));
}
// other properties
if(shouldTransmit(local, Task.DUE_DATE, remote)) if(shouldTransmit(local, Task.DUE_DATE, remote))
builder.taskDate(local.task.getValue(Task.DUE_DATE)); builder.taskDate(local.task.getValue(Task.DUE_DATE));
if(shouldTransmit(local, Task.COMPLETION_DATE, remote)) if(shouldTransmit(local, Task.COMPLETION_DATE, remote))
@ -457,10 +463,8 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
if(shouldTransmit(local, Task.NOTES, remote)) if(shouldTransmit(local, Task.NOTES, remote))
builder.notes(local.task.getValue(Task.NOTES)); builder.notes(local.task.getValue(Task.NOTES));
String id = idList;
// write task (and perform move action if requested) // write task (and perform move action if requested)
ListAction action; final ListAction action;
if(builder instanceof TaskModifier) { if(builder instanceof TaskModifier) {
System.err.println("ACTION: task edit (6), " + idTask); System.err.println("ACTION: task edit (6), " + idTask);
action = ((TaskModifier) builder).done(); action = ((TaskModifier) builder).done();
@ -470,15 +474,35 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
} else } else
throw new GoogleTasksException("Unknown builder " + builder.getClass()); throw new GoogleTasksException("Unknown builder " + builder.getClass());
if(idTask != null && local.parentId != null && (remote == null || local.parentId != remote.parentId || new Thread(new Runnable() {
local.priorSiblingId != remote.priorSiblingId)) { @Override
System.err.println("ACTION: move(1) - " + idTask + ", " + local.parentId + ", " + local.priorSiblingId); public void run() {
ListAction moveAction = l.move(idTask, local.parentId, local.priorSiblingId); try {
taskService.executeListActions(idList, action, moveAction); if(idTask != null && local.parentId != null && (remote == null || local.parentId != remote.parentId ||
} else if(action.toJson(idList).getJSONObject("entity_delta").length() > 0) local.priorSiblingId != remote.priorSiblingId)) {
taskService.executeListActions(idList, action); System.err.println("ACTION: move(1) - " + idTask + ", " + local.parentId + ", " + local.priorSiblingId);
ListAction moveAction = l.move(idTask, local.parentId, local.priorSiblingId);
taskService.executeListActions(idList, action, moveAction);
} else if(action.toJson(idList).getJSONObject("entity_delta").length() > 0) {
taskService.executeListActions(idList, action);
}
// moving between lists
if(remote != null && !idList.equals(remote.gtaskMetadata.getValue(
GtasksMetadata.LIST_ID))) {
System.err.println("ACTION: moveTask(5), " + idTask + ", " + idList + " to " +
remote.gtaskMetadata.getValue(GtasksMetadata.LIST_ID));
taskService.executeActions(a.moveTask(idTask, idList,
remote.gtaskMetadata.getValue(GtasksMetadata.LIST_ID), null));
}
} catch (IOException e) {
handleException("update-task", e, false);
} catch (JSONException e) {
handleException("update-task-json", e, false);
}
}
}).start();
return id;
} catch (JSONException e) { } catch (JSONException e) {
throw new GoogleTasksException(e); throw new GoogleTasksException(e);
} }

Loading…
Cancel
Save