Removed static properties from Synchronizer class so controller overrides are not propagated.

pull/14/head
Tim Su 17 years ago
parent ff9fc72156
commit 18adc8799d

@ -37,6 +37,7 @@ import android.widget.ViewFlipper;
import com.timsu.astrid.R; import com.timsu.astrid.R;
import com.timsu.astrid.data.tag.TagController; import com.timsu.astrid.data.tag.TagController;
import com.timsu.astrid.data.task.TaskController; import com.timsu.astrid.data.task.TaskController;
import com.timsu.astrid.sync.SynchronizationService;
import com.timsu.astrid.sync.Synchronizer; import com.timsu.astrid.sync.Synchronizer;
import com.timsu.astrid.utilities.Constants; import com.timsu.astrid.utilities.Constants;
import com.timsu.astrid.utilities.StartupReceiver; import com.timsu.astrid.utilities.StartupReceiver;
@ -109,8 +110,6 @@ public class TaskList extends Activity {
taskController.open(); taskController.open();
tagController = new TagController(this); tagController = new TagController(this);
tagController.open(); tagController.open();
Synchronizer.setTagController(tagController);
Synchronizer.setTaskController(taskController);
setupUIComponents(); setupUIComponents();
@ -128,7 +127,12 @@ public class TaskList extends Activity {
// sync now if requested // sync now if requested
if(synchronizeNow) { if(synchronizeNow) {
synchronizeNow = false; synchronizeNow = false;
Synchronizer.synchronize(this, false, null); Synchronizer sync = new Synchronizer(false);
sync.setTagController(tagController);
sync.setTaskController(taskController);
sync.synchronize(this, null);
} else {
SynchronizationService.start();
} }
// if we have no filter tag, we're not on the last task // if we have no filter tag, we're not on the last task
@ -344,7 +348,5 @@ public class TaskList extends Activity {
super.onDestroy(); super.onDestroy();
taskController.close(); taskController.close();
tagController.close(); tagController.close();
Synchronizer.setTagController(null);
Synchronizer.setTaskController(null);
} }
} }

@ -806,9 +806,9 @@ public class TaskListSubActivity extends SubActivity {
syncPreferencesOpened = false; syncPreferencesOpened = false;
// stop & start synchronization service // stop & start synchronization service
Intent service = new Intent(getParent(), SynchronizationService.class); SynchronizationService.stop();
getParent().stopService(service); SynchronizationService.start();
getParent().startService(service);
} else if (context.taskArray != null && } else if (context.taskArray != null &&
context.taskArray.size() > 0 && context.taskArray.size() > 0 &&
context.taskArray.size() < AUTO_REFRESH_MAX_LIST_SIZE) { context.taskArray.size() < AUTO_REFRESH_MAX_LIST_SIZE) {
@ -826,7 +826,10 @@ public class TaskListSubActivity extends SubActivity {
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == Constants.RESULT_SYNCHRONIZE) { if(resultCode == Constants.RESULT_SYNCHRONIZE) {
Synchronizer.synchronize(getParent(), false, new SynchronizerListener() { Synchronizer sync = new Synchronizer(false);
sync.setTagController(getTagController());
sync.setTaskController(getTaskController());
sync.synchronize(getParent(), new SynchronizerListener() {
public void onSynchronizerFinished(int numServicesSynced) { public void onSynchronizerFinished(int numServicesSynced) {
if(numServicesSynced == 0) { if(numServicesSynced == 0) {
DialogUtilities.okDialog(getParent(), getResources().getString( DialogUtilities.okDialog(getParent(), getResources().getString(

@ -83,7 +83,7 @@ public class RTMSyncProvider extends SynchronizationProvider {
public void clearPersonalData(Context context) { public void clearPersonalData(Context context) {
Preferences.setSyncRTMToken(context, null); Preferences.setSyncRTMToken(context, null);
Preferences.setSyncRTMLastSync(context, null); Preferences.setSyncRTMLastSync(context, null);
Synchronizer.getSyncController(context).deleteAllMappings(getId()); synchronizer.getSyncController(context).deleteAllMappings(getId());
} }
// --- authentication // --- authentication
@ -239,7 +239,7 @@ public class RTMSyncProvider extends SynchronizationProvider {
Preferences.setSyncRTMLastSync(context, syncTime); Preferences.setSyncRTMLastSync(context, syncTime);
// on with the synchronization // on with the synchronization
Synchronizer.continueSynchronization(context); synchronizer.continueSynchronization(context);
} catch (Exception e) { } catch (Exception e) {
showError(context, e, null); showError(context, e, null);

@ -55,15 +55,19 @@ public abstract class SynchronizationProvider {
private int id; private int id;
static ProgressDialog progressDialog; static ProgressDialog progressDialog;
private Handler syncHandler; private Handler syncHandler;
private boolean backgroundSync; protected Synchronizer synchronizer;
public SynchronizationProvider(int id) { public SynchronizationProvider(int id) {
this.id = id; this.id = id;
} }
// called off the UI thread. does some setup /** Called off the UI thread. does some setup and then invokes implemented
void synchronizeService(final Context activity, boolean isBackgroundSync) { * synchronize method
this.backgroundSync = isBackgroundSync; * @param activity
* @param caller
*/
void synchronizeService(final Context activity, Synchronizer caller) {
this.synchronizer = caller;
if(!isBackgroundService()) { if(!isBackgroundService()) {
syncHandler = new Handler(); syncHandler = new Handler();
@ -101,7 +105,7 @@ public abstract class SynchronizationProvider {
* @return true if it's running as a background service * @return true if it's running as a background service
*/ */
protected boolean isBackgroundService() { protected boolean isBackgroundService() {
return backgroundSync; return synchronizer.isService();
} }
/** Utility method for showing synchronization errors. If message is null, /** Utility method for showing synchronization errors. If message is null,
@ -194,10 +198,10 @@ public abstract class SynchronizationProvider {
final SyncStats stats = new SyncStats(); final SyncStats stats = new SyncStats();
final StringBuilder log = new StringBuilder(); final StringBuilder log = new StringBuilder();
SyncDataController syncController = Synchronizer.getSyncController(context); SyncDataController syncController = synchronizer.getSyncController(context);
TaskController taskController = Synchronizer.getTaskController(context); TaskController taskController = synchronizer.getTaskController(context);
TagController tagController = Synchronizer.getTagController(context); TagController tagController = synchronizer.getTagController(context);
AlertController alertController = Synchronizer.getAlertController(context); AlertController alertController = synchronizer.getAlertController(context);
SyncData data = new SyncData(context, remoteTasks); SyncData data = new SyncData(context, remoteTasks);
// 1. CREATE: grab tasks without a sync mapping and create them remotely // 1. CREATE: grab tasks without a sync mapping and create them remotely
@ -437,10 +441,10 @@ public abstract class SynchronizationProvider {
public SyncData(Context context, LinkedList<TaskProxy> remoteTasks) { public SyncData(Context context, LinkedList<TaskProxy> remoteTasks) {
// 1. get data out of the database // 1. get data out of the database
mappings = Synchronizer.getSyncController(context).getSyncMapping(getId()); mappings = synchronizer.getSyncController(context).getSyncMapping(getId());
activeTasks = Synchronizer.getTaskController(context).getActiveTaskIdentifiers(); activeTasks = synchronizer.getTaskController(context).getActiveTaskIdentifiers();
allTasks = Synchronizer.getTaskController(context).getAllTaskIdentifiers(); allTasks = synchronizer.getTaskController(context).getAllTaskIdentifiers();
tags = Synchronizer.getTagController(context).getAllTagsAsMap(); tags = synchronizer.getTagController(context).getAllTagsAsMap();
// 2. build helper data structures // 2. build helper data structures
remoteIdToSyncMapping = new HashMap<String, SyncMapping>(); remoteIdToSyncMapping = new HashMap<String, SyncMapping>();

@ -25,6 +25,23 @@ public class SynchronizationService extends Service {
SynchronizationService.context = context; SynchronizationService.context = context;
} }
// --- utility methods
public static void start() {
if(Preferences.getSyncAutoSyncFrequency(context) == null)
return;
Intent service = new Intent(context, SynchronizationService.class);
context.stopService(service);
}
public static void stop() {
Intent service = new Intent(context, SynchronizationService.class);
context.stopService(service);
}
// ---
@Override @Override
public IBinder onBind(Intent arg0) { public IBinder onBind(Intent arg0) {
return null; // unused return null; // unused
@ -48,6 +65,9 @@ public class SynchronizationService extends Service {
/** Start the timer that runs the service */ /** Start the timer that runs the service */
private void startService() { private void startService() {
if(context == null)
return;
// figure out synchronization frequency // figure out synchronization frequency
Integer syncFrequencySeconds = Preferences.getSyncAutoSyncFrequency(context); Integer syncFrequencySeconds = Preferences.getSyncAutoSyncFrequency(context);
if(syncFrequencySeconds == null) { if(syncFrequencySeconds == null) {
@ -97,6 +117,7 @@ public class SynchronizationService extends Service {
Log.i("astrid", "Automatic Synchronize Initiated."); Log.i("astrid", "Automatic Synchronize Initiated.");
Preferences.setSyncLastSyncAttempt(context, new Date()); Preferences.setSyncLastSyncAttempt(context, new Date());
Synchronizer.synchronize(context, true, null); Synchronizer sync = new Synchronizer(true);
sync.synchronize(context, null);
} }
} }

@ -33,28 +33,46 @@ import com.timsu.astrid.data.tag.TagController;
import com.timsu.astrid.data.task.TaskController; import com.timsu.astrid.data.task.TaskController;
import com.timsu.astrid.utilities.Preferences; import com.timsu.astrid.utilities.Preferences;
/**
* Synchronizer is a class that manages a synchronization lifecycle. You would
* use it as follows.
* <p>
* <pre>Synchronizer synchronizer = new Synchronizer(...);
* synchronizer.synchronize();</pre>
*
* @author Tim Su
*
*/
public class Synchronizer { public class Synchronizer {
/** identifier for the RTM sync provider */
private static final int SYNC_ID_RTM = 1; private static final int SYNC_ID_RTM = 1;
// --- public interface // --- public interface
public Synchronizer(boolean isService) {
this.isService = isService;
}
public interface SynchronizerListener { public interface SynchronizerListener {
void onSynchronizerFinished(int numServicesSynced); void onSynchronizerFinished(int numServicesSynced);
} }
/** Synchronize all activated sync services */ /** Synchronize all activated sync services */
public synchronized static void synchronize(Context context, boolean isAutoSync, public synchronized void synchronize(Context context, SynchronizerListener listener) {
SynchronizerListener listener) {
currentStep = ServiceWrapper._FIRST_SERVICE.ordinal(); currentStep = ServiceWrapper._FIRST_SERVICE.ordinal();
servicesSynced = 0; servicesSynced = 0;
autoSync = isAutoSync;
callback = listener; callback = listener;
// if we're not the autosync service, stop it
if(!isService)
SynchronizationService.stop();
continueSynchronization(context); continueSynchronization(context);
} }
/** Clears tokens if services are disabled */ /** Clears tokens of activated services */
public static void clearUserData(Activity activity) { public static void clearUserData(Activity activity) {
for(ServiceWrapper serviceWrapper : ServiceWrapper.values()) { for(ServiceWrapper serviceWrapper : ServiceWrapper.values()) {
if(serviceWrapper.isActivated(activity)) { if(serviceWrapper.isActivated(activity)) {
@ -104,18 +122,22 @@ public class Synchronizer {
// Internal state for the synchronization process // Internal state for the synchronization process
/** Current step in the sync process */ /** Current step in the sync process */
private static int currentStep; private int currentStep;
/** # of services synchronized */ /** # of services synchronized */
private static int servicesSynced; private int servicesSynced;
/** On finished callback */ /** On finished callback */
private static SynchronizerListener callback; private SynchronizerListener callback;
private static boolean autoSync; private boolean isService;
boolean isService() {
return isService;
}
/** Called to do the next step of synchronization. */ /** Called to do the next step of synchronization. */
static void continueSynchronization(Context context) { void continueSynchronization(Context context) {
ServiceWrapper serviceWrapper = ServiceWrapper serviceWrapper =
ServiceWrapper.values()[currentStep]; ServiceWrapper.values()[currentStep];
currentStep++; currentStep++;
@ -126,7 +148,7 @@ public class Synchronizer {
case RTM: case RTM:
if(serviceWrapper.isActivated(context)) { if(serviceWrapper.isActivated(context)) {
servicesSynced++; servicesSynced++;
serviceWrapper.service.synchronizeService(context, autoSync); serviceWrapper.service.synchronizeService(context, this);
} else { } else {
continueSynchronization(context); continueSynchronization(context);
} }
@ -137,15 +159,27 @@ public class Synchronizer {
} }
/** Called at the end of sync. */ /** Called at the end of sync. */
private static void finishSynchronization(final Context context) { private void finishSynchronization(final Context context) {
closeControllers(); closeControllers();
Preferences.setSyncLastSync(context, new Date()); Preferences.setSyncLastSync(context, new Date());
if(callback != null) if(callback != null)
callback.onSynchronizerFinished(servicesSynced); callback.onSynchronizerFinished(servicesSynced);
if(!isService)
SynchronizationService.start();
} }
// --- controller stuff // --- controller stuff
private ControllerWrapper<SyncDataController> syncController =
new ControllerWrapper<SyncDataController>(SyncDataController.class);
private ControllerWrapper<TaskController> taskController =
new ControllerWrapper<TaskController>(TaskController.class);
private ControllerWrapper<TagController> tagController =
new ControllerWrapper<TagController>(TagController.class);
private ControllerWrapper<AlertController> alertController =
new ControllerWrapper<AlertController>(AlertController.class);
private static class ControllerWrapper<TYPE extends AbstractController> { private static class ControllerWrapper<TYPE extends AbstractController> {
TYPE controller; TYPE controller;
Class<TYPE> typeClass; Class<TYPE> typeClass;
@ -180,6 +214,7 @@ public class Synchronizer {
} }
public void set(TYPE newController) { public void set(TYPE newController) {
if(controller != null && !override)
close(); close();
override = newController != null; override = newController != null;
@ -194,40 +229,31 @@ public class Synchronizer {
} }
} }
private static ControllerWrapper<SyncDataController> syncController = SyncDataController getSyncController(Context context) {
new ControllerWrapper<SyncDataController>(SyncDataController.class);
private static ControllerWrapper<TaskController> taskController =
new ControllerWrapper<TaskController>(TaskController.class);
private static ControllerWrapper<TagController> tagController =
new ControllerWrapper<TagController>(TagController.class);
private static ControllerWrapper<AlertController> alertController =
new ControllerWrapper<AlertController>(AlertController.class);
static SyncDataController getSyncController(Context context) {
return syncController.get(context); return syncController.get(context);
} }
static TaskController getTaskController(Context context) { TaskController getTaskController(Context context) {
return taskController.get(context); return taskController.get(context);
} }
static TagController getTagController(Context context) { TagController getTagController(Context context) {
return tagController.get(context); return tagController.get(context);
} }
static AlertController getAlertController(Context context) { AlertController getAlertController(Context context) {
return alertController.get(context); return alertController.get(context);
} }
public static void setTaskController(TaskController taskController) { public void setTaskController(TaskController taskController) {
Synchronizer.taskController.set(taskController); this.taskController.set(taskController);
} }
public static void setTagController(TagController tagController) { public void setTagController(TagController tagController) {
Synchronizer.tagController.set(tagController); this.tagController.set(tagController);
} }
private static void closeControllers() { private void closeControllers() {
syncController.close(); syncController.close();
taskController.close(); taskController.close();
tagController.close(); tagController.close();

Loading…
Cancel
Save