Sync queue initialization

pull/14/head
Sam Bosley 12 years ago
parent 78cf95f767
commit b6218cd4b4

@ -1,9 +1,9 @@
package com.todoroo.astrid.actfm.sync;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import org.json.JSONArray;
import org.json.JSONObject;
@ -17,13 +17,17 @@ import com.todoroo.astrid.actfm.sync.messages.BriefMe;
import com.todoroo.astrid.actfm.sync.messages.ChangesHappened;
import com.todoroo.astrid.actfm.sync.messages.ClientToServerMessage;
import com.todoroo.astrid.actfm.sync.messages.ServerToClientMessage;
import com.todoroo.astrid.dao.TagDataDao;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.data.Task;
public class ActFmSyncThread {
private final Queue<Pair<Long, ModelType>> changesQueue;
private static final String ERROR_TAG = "actfm-sync-thread"; //$NON-NLS-1$
private final List<Pair<Long, ModelType>> changesQueue;
private final Object monitor;
private Thread thread;
@ -40,7 +44,19 @@ public class ActFmSyncThread {
TYPE_TAG
}
public ActFmSyncThread(Queue<Pair<Long, ModelType>> queue, Object syncMonitor) {
public static ActFmSyncThread initializeSyncComponents(TaskDao taskDao, TagDataDao tagDataDao) {
List<Pair<Long, ModelType>> syncQueue = Collections.synchronizedList(new LinkedList<Pair<Long, ModelType>>());
ActFmSyncMonitor monitor = ActFmSyncMonitor.getInstance();
taskDao.addListener(new SyncDatabaseListener<Task>(syncQueue, monitor, ModelType.TYPE_TASK));
tagDataDao.addListener(new SyncDatabaseListener<TagData>(syncQueue, monitor, ModelType.TYPE_TAG));
ActFmSyncThread thread = new ActFmSyncThread(syncQueue, monitor);
thread.startSyncThread();
return thread;
}
public ActFmSyncThread(List<Pair<Long, ModelType>> queue, Object syncMonitor) {
DependencyInjectionService.getInstance().inject(this);
this.changesQueue = queue;
this.monitor = syncMonitor;
@ -76,7 +92,7 @@ public class ActFmSyncThread {
// Stuff in the document
while (messages.size() < batchSize && !changesQueue.isEmpty()) {
Pair<Long, ModelType> tuple = changesQueue.poll();
Pair<Long, ModelType> tuple = changesQueue.remove(0);
if (tuple != null) {
ChangesHappened<?, ?> changes = ClientToServerMessage.instantiateChangesHappened(tuple.getLeft(), tuple.getRight());
if (changes != null)
@ -106,8 +122,11 @@ public class ActFmSyncThread {
JSONObject serverMessageJson = serverMessagesJson.optJSONObject(i);
if (serverMessageJson != null) {
ServerToClientMessage serverMessage = ServerToClientMessage.instantiateMessage(serverMessageJson);
if (serverMessage != null)
if (serverMessage != null) {
serverMessage.processMessage();
} else {
Log.e(ERROR_TAG, "Unable to instantiate message " + serverMessageJson.toString());
}
}
}
}
@ -121,7 +140,7 @@ public class ActFmSyncThread {
}
} catch (Exception e) {
// In the worst case, restart thread if something goes wrong
Log.e("actfm-sync", "Unexpected sync thread exception", e);
Log.e(ERROR_TAG, "Unexpected sync thread exception", e);
thread = null;
startSyncThread();
}

@ -1,6 +1,6 @@
package com.todoroo.astrid.actfm.sync;
import java.util.Queue;
import java.util.List;
import com.todoroo.andlib.data.DatabaseDao.ModelUpdateListener;
import com.todoroo.andlib.utility.Pair;
@ -9,11 +9,11 @@ import com.todoroo.astrid.data.RemoteModel;
public class SyncDatabaseListener<MTYPE extends RemoteModel> implements ModelUpdateListener<MTYPE> {
private final Queue<Pair<Long, ModelType>> queue;
private final List<Pair<Long, ModelType>> queue;
private final Object monitor;
private final ModelType modelType;
public SyncDatabaseListener(Queue<Pair<Long, ModelType>> queue, Object syncMonitor, ModelType modelType) {
public SyncDatabaseListener(List<Pair<Long, ModelType>> queue, Object syncMonitor, ModelType modelType) {
this.queue = queue;
this.monitor = syncMonitor;
this.modelType = modelType;

@ -40,13 +40,15 @@ import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.actfm.sync.ActFmPreferenceService;
import com.todoroo.astrid.actfm.sync.ActFmSyncService;
import com.todoroo.astrid.actfm.sync.ActFmSyncThread;
import com.todoroo.astrid.activity.BeastModePreferences;
import com.todoroo.astrid.backup.BackupConstants;
import com.todoroo.astrid.backup.BackupService;
import com.todoroo.astrid.backup.TasksXmlImporter;
import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria;
import com.todoroo.astrid.dao.TagDataDao;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.gcal.CalendarStartupReceiver;
@ -92,12 +94,14 @@ public class StartupService {
@Autowired TaskService taskService;
@Autowired TaskDao taskDao;
@Autowired TagDataDao tagDataDao;
@Autowired MetadataService metadataService;
@Autowired Database database;
@Autowired ActFmSyncService actFmSyncService;
@Autowired GtasksPreferenceService gtasksPreferenceService;
@Autowired ActFmPreferenceService actFmPreferenceService;
@ -209,6 +213,8 @@ public class StartupService {
abTestInvoker.reportAcquisition();
ActFmSyncThread.initializeSyncComponents(taskDao, tagDataDao);
// perform startup activities in a background thread
new Thread(new Runnable() {
public void run() {
@ -233,7 +239,6 @@ public class StartupService {
// perform initialization
ReminderStartupReceiver.startReminderSchedulingService(context);
BackupService.scheduleService(context);
actFmSyncService.initialize();
gtasksSyncService.initialize();

Loading…
Cancel
Save