private void Finish() { if (!IsRunning && !IsCancelled) { throw new InvalidOperationException("Finish can only be called when the sync is running."); } IsRunning = false; IsFinished = true; if (subscriptionSyncFinishedMessage != null) { var bus = ServiceContainer.Resolve <MessageBus> (); bus.Unsubscribe(subscriptionSyncFinishedMessage); subscriptionSyncFinishedMessage = null; } foreach (var intent in intents) { GcmBroadcastReceiver.CompleteWakefulIntent(intent); } intents.Clear(); if (service != null) { foreach (var commandId in commandIds) { service.StopSelf(commandId); } } commandIds.Clear(); }
private void ClearWakelockIntent() { if (wakelockIntent != null) { GcmBroadcastReceiver.CompleteWakefulIntent(wakelockIntent); wakelockIntent = null; } }
public override void OnStart(Intent intent, int startId) { ((AndroidApp)Application).InitializeComponents(); try { var extras = intent.Extras; var entryId = Convert.ToInt64(extras.GetString("task_id", String.Empty)); // updated_at is null (usually) when the time entry was just created, in that // case we assign modifiedAt the default DateTime value (start of time) var modifiedAt = ParseDate(extras.GetString("updated_at", String.Empty)); var entry = Model.ByRemoteId <TimeEntryModel> (entryId); if (entry != null && modifiedAt <= entry.ModifiedAt) { // We already have the latest data, can skip this: GcmBroadcastReceiver.CompleteWakefulIntent(intent); StopSelf(startId); return; } // Purge finished syncs: schedule.RemoveAll((s) => s.IsFinished); // See if we can add this task to running sync: var running = schedule.FirstOrDefault((s) => s.IsRunning); if (running != null && running.StartTime >= modifiedAt) { running.AddCommand(intent, startId); return; } // Finally add it to the new sync: var upcoming = schedule.FirstOrDefault((s) => !s.IsCancelled && !s.IsRunning && !s.IsFinished); if (upcoming == null) { upcoming = new ScheduledSync(this); schedule.Add(upcoming); } upcoming.AddCommand(intent, startId); } catch (Exception exc) { // Something went wrong, recover gracefully GcmBroadcastReceiver.CompleteWakefulIntent(intent); StopSelf(startId); // Log errors var log = ServiceContainer.Resolve <Logger> (); log.Error(Tag, exc, "Failed to process pushed message."); } }