Fixed a race condition where multiple syncs could be started simultaneously

pull/14/head
Sam Bosley 14 years ago
parent 2863a2810e
commit 1afe1a51ff

@ -1,5 +1,7 @@
package com.todoroo.astrid.sync; package com.todoroo.astrid.sync;
import java.util.concurrent.atomic.AtomicBoolean;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
@ -45,12 +47,15 @@ abstract public class SyncBackgroundService extends Service {
DependencyInjectionService.getInstance().inject(this); DependencyInjectionService.getInstance().inject(this);
} }
private final AtomicBoolean started = new AtomicBoolean(false);
/** Receive the alarm - start the synchronize service! */ /** Receive the alarm - start the synchronize service! */
@Override @Override
public void onStart(Intent intent, int startId) { public void onStart(Intent intent, int startId) {
try { try {
if(intent != null) if(intent != null && !started.getAndSet(true)) {
startSynchronization(this); startSynchronization(this);
}
} catch (Exception e) { } catch (Exception e) {
exceptionService.reportError(getSyncUtilities().getIdentifier() + "-bg-sync", e); //$NON-NLS-1$ exceptionService.reportError(getSyncUtilities().getIdentifier() + "-bg-sync", e); //$NON-NLS-1$
} }
@ -67,8 +72,6 @@ abstract public class SyncBackgroundService extends Service {
return; return;
getSyncProvider().synchronize(context); getSyncProvider().synchronize(context);
stopSelf();
} }
@Override @Override
@ -76,6 +79,11 @@ abstract public class SyncBackgroundService extends Service {
return null; return null;
} }
public synchronized void stop() {
started.set(false);
stopSelf();
}
// --- alarm management // --- alarm management
/** /**

@ -11,7 +11,6 @@ import java.util.HashMap;
import android.app.Activity; import android.app.Activity;
import android.app.Notification; import android.app.Notification;
import android.app.Service;
import android.content.Context; import android.content.Context;
import android.widget.Toast; import android.widget.Toast;
@ -162,7 +161,7 @@ public abstract class SyncProvider<TYPE extends SyncContainer> {
}); });
} }
initiateManual((Activity)context); initiateManual((Activity)context);
} else if(context instanceof Service) { } else if(context instanceof SyncBackgroundService) {
// display notification // display notification
final int notificationId = updateNotification(context, notification); final int notificationId = updateNotification(context, notification);
final NotificationManager nm = new NotificationManager.AndroidNotificationManager(context); final NotificationManager nm = new NotificationManager.AndroidNotificationManager(context);
@ -175,6 +174,7 @@ public abstract class SyncProvider<TYPE extends SyncContainer> {
initiateBackground(); initiateBackground();
} finally { } finally {
nm.cancel(notificationId); nm.cancel(notificationId);
((SyncBackgroundService)context).stop();
} }
} }
}).start(); }).start();

Loading…
Cancel
Save