public static void Unregister(Service service)
        {
            if (!IsRegistered(service))
            {
                throw new ServiceSchedulerException("Service " + service.Information.ServiceName + "  is not registered!");
            }

            lock (ServicesBindings) ServicesBindings.Remove(ServicesBindings.FirstOrDefault(s => s.Service == service));
            ServiceEvents.RaiseServiceUnRegistered(service);
            SettingsChangedManager.SetNeedsContactSync(service, true);
        }
Beispiel #2
0
        public static void OnPhoneContactsUpdated()
        {
            Task.Factory.StartNew(() =>
            {
                if (_isUpdating)
                {
                    Utils.DebugPrint("Contacts are already updating... Returning...");
                    return;
                }

                Action action = () =>
                {
                    lock (PhoneBookContactsLock)
                    {
                        using (new PhoneContactsUpdatingDisposable())
                        {
                            if (_phoneBookContacts == null)
                            {
                                Utils.DebugPrint("Phone contacts have not yet been used by the framework/services. Therefore, and update doesn't make sense. Skipping.");
                                return;
                            }

                            Utils.DebugPrint("Phone contacts have been updated! Querying phone contacts...");

                            var updatedPhoneContacts = Platform.GetPhoneBookContacts();

                            Utils.DebugPrint("Checking if any numbers/names have been updated...");

                            var updatedPhoneContactsNumbers =
                                (from phoneBookRelation in updatedPhoneContacts
                                 from phoneNumber in phoneBookRelation.PhoneNumbers
                                 select phoneNumber.Number).ToList();

                            var phoneBookContactsNumbers =
                                (from phoneBookRelation in _phoneBookContacts
                                 from phoneNumber in phoneBookRelation.PhoneNumbers
                                 select phoneNumber.Number).ToList();

                            var updatedPhoneContactsFullName =
                                (from phoneBookRelation in updatedPhoneContacts
                                 select phoneBookRelation.FullName).ToList();

                            var phoneContactsFullName =
                                (from phoneBookRelation in _phoneBookContacts
                                 select phoneBookRelation.FullName).ToList();

                            var phoneBooksEqual =
                                updatedPhoneContactsNumbers.Intersect(phoneBookContactsNumbers).Count() ==
                                updatedPhoneContactsNumbers.Union(phoneBookContactsNumbers).Count() &&
                                updatedPhoneContactsFullName.Intersect(phoneContactsFullName).Count() ==
                                updatedPhoneContactsFullName.Union(phoneContactsFullName).Count();

                            if (phoneBooksEqual)
                            {
                                Utils.DebugPrint(
                                    "Phone books are equal. No need to update contacts!");
                                return;
                            }

                            _phoneBookContacts = updatedPhoneContacts;

                            Utils.DebugPrint("Got phone contacts... updating running services...");

                            var notRunningServices = ServiceManager.AllNoUnified.ToList();

                            foreach (
                                var service in
                                ServiceManager.RunningNoUnified)
                            {
                                try
                                {
                                    service.RefreshPhoneBookContacts();
                                    BubbleGroupUpdater.Update(service);
                                    ServiceEvents.RaiseContactsUpdated(service);
                                    notRunningServices.Remove(service);
                                }
                                catch (Exception ex)
                                {
                                    Utils.DebugPrint("Service " + service.Information.ServiceName +
                                                     " failed to refresh it contacts. " + ex.Message);
                                }
                            }

                            if (notRunningServices.Any())
                            {
                                SettingsChangedManager.SetNeedsContactSync(notRunningServices.ToArray(), true);
                            }
                            SaveCachedPhoneBookContacts(updatedPhoneContacts);
                        }
                    }
                };

                using (Platform.AquireWakeLock("DisaContactsUpdate"))
                {
                    action();
                }
            });
        }