diff --git a/Classes/AssistantLinkView.m b/Classes/AssistantLinkView.m index 1b0bad163..06a1d7082 100644 --- a/Classes/AssistantLinkView.m +++ b/Classes/AssistantLinkView.m @@ -199,10 +199,10 @@ void assistant_activate_phone_number_link(LinphoneAccountCreator *creator, Linph } [PhoneMainView.instance popToView:DialerView.compositeViewDescription]; [[NSNotificationCenter defaultCenter] postNotificationName:kLinphoneAddressBookUpdate object:NULL]; - [LinphoneManager.instance.fastAddressBook reloadAllContacts]; - } else { - [thiz showErrorPopup:resp]; - } + [LinphoneManager.instance.fastAddressBook fetchContactsInBackGroundThread]; + } else { + [thiz showErrorPopup:resp]; + } } #pragma mark - other diff --git a/Classes/AssistantView.m b/Classes/AssistantView.m index 7048d0d39..823c86255 100644 --- a/Classes/AssistantView.m +++ b/Classes/AssistantView.m @@ -412,7 +412,7 @@ static UICompositeViewDescription *compositeDescription = nil; linphone_core_set_default_proxy_config(LC, new_config); // reload address book to prepend proxy config domain to contacts' phone number // todo: STOP doing that! - [[LinphoneManager.instance fastAddressBook] reloadAllContacts]; + [[LinphoneManager.instance fastAddressBook] fetchContactsInBackGroundThread]; } else { [self displayAssistantConfigurationError]; } @@ -1369,17 +1369,14 @@ void assistant_is_account_linked(LinphoneAccountCreator *creator, LinphoneAccoun linphone_core_set_default_proxy_config(LC, config); // reload address book to prepend proxy config domain to contacts' phone number // todo: STOP doing that! - [[LinphoneManager.instance fastAddressBook] - reloadAllContacts]; - [PhoneMainView.instance - changeCurrentView: - DialerView.compositeViewDescription]; - } else { - [self displayAssistantConfigurationError]; - } - } else { - [self displayAssistantConfigurationError]; - } + [[LinphoneManager.instance fastAddressBook] fetchContactsInBackGroundThread]; + [PhoneMainView.instance changeCurrentView:DialerView.compositeViewDescription]; + } else { + [self displayAssistantConfigurationError]; + } + } else { + [self displayAssistantConfigurationError]; + } }); } diff --git a/Classes/LinphoneAppDelegate.m b/Classes/LinphoneAppDelegate.m index 4e56fbac1..7f0d024a0 100644 --- a/Classes/LinphoneAppDelegate.m +++ b/Classes/LinphoneAppDelegate.m @@ -91,7 +91,7 @@ if (PhoneMainView.instance.currentView == ContactsListView.compositeViewDescription || PhoneMainView.instance.currentView == ContactDetailsView.compositeViewDescription) { [PhoneMainView.instance changeCurrentView:DialerView.compositeViewDescription]; } - [instance.fastAddressBook reloadAllContacts]; + [instance.fastAddressBook fetchContactsInBackGroundThread]; instance.fastAddressBook.needToUpdate = FALSE; const MSList *lists = linphone_core_get_friends_lists(LC); while (lists) { @@ -968,7 +968,7 @@ didInvalidatePushTokenForType:(NSString *)type { linphone_core_set_provisioning_uri(LC, [configURL UTF8String]); [LinphoneManager.instance destroyLinphoneCore]; [LinphoneManager.instance startLinphoneCore]; - [LinphoneManager.instance.fastAddressBook reloadAllContacts]; + [LinphoneManager.instance.fastAddressBook fetchContactsInBackGroundThread]; } #pragma mark - Prevent ImagePickerView from rotating diff --git a/Classes/LinphoneCoreSettingsStore.m b/Classes/LinphoneCoreSettingsStore.m index c0d7e4d75..3d4659bc6 100644 --- a/Classes/LinphoneCoreSettingsStore.m +++ b/Classes/LinphoneCoreSettingsStore.m @@ -636,7 +636,7 @@ } } // reload address book to prepend proxy config domain to contacts' phone number - [[LinphoneManager.instance fastAddressBook] reloadAllContacts]; + [[LinphoneManager.instance fastAddressBook] fetchContactsInBackGroundThread]; } - (void)synchronizeCodecs:(const MSList *)codecs { diff --git a/Classes/LinphoneManager.m b/Classes/LinphoneManager.m index a1fb3e75e..04559532c 100644 --- a/Classes/LinphoneManager.m +++ b/Classes/LinphoneManager.m @@ -2181,7 +2181,7 @@ void popup_link_account_cb(LinphoneAccountCreator *creator, LinphoneAccountCreat [self destroyLinphoneCore]; [self createLinphoneCore]; // reload friends - [self.fastAddressBook reloadAllContacts]; + [self.fastAddressBook fetchContactsInBackGroundThread]; // reset network state to trigger a new network connectivity assessment linphone_core_set_network_reachable(theLinphoneCore, FALSE); diff --git a/Classes/LinphoneUI/UIContactDetailsCell.m b/Classes/LinphoneUI/UIContactDetailsCell.m index 76326c098..39b25a77d 100644 --- a/Classes/LinphoneUI/UIContactDetailsCell.m +++ b/Classes/LinphoneUI/UIContactDetailsCell.m @@ -128,7 +128,6 @@ - (IBAction)onCallClick:(id)event { LinphoneAddress *addr = [LinphoneUtils normalizeSipOrPhoneAddress:_addressLabel.text]; - LOGE(@"=====>>>> onCallClick() : %@ ", [NSString stringWithUTF8String:linphone_address_as_string(addr)] ); [LinphoneManager.instance call:addr]; if (addr) linphone_address_destroy(addr); diff --git a/Classes/Utils/FastAddressBook.h b/Classes/Utils/FastAddressBook.h index 606cf1576..2c7224830 100644 --- a/Classes/Utils/FastAddressBook.h +++ b/Classes/Utils/FastAddressBook.h @@ -28,8 +28,7 @@ @property(readonly, nonatomic) NSMutableDictionary *addressBookMap; @property BOOL needToUpdate; -- (BOOL)reloadAllContacts; -//- (void)saveAddressBook; +- (void) fetchContactsInBackGroundThread; - (BOOL)deleteContact:(Contact *)contact; - (BOOL)deleteCNContact:(CNContact *)CNContact; - (BOOL)deleteAllContacts; diff --git a/Classes/Utils/FastAddressBook.m b/Classes/Utils/FastAddressBook.m index 8a302140f..23a47cb51 100644 --- a/Classes/Utils/FastAddressBook.m +++ b/Classes/Utils/FastAddressBook.m @@ -192,68 +192,6 @@ self.needToUpdate = TRUE; } -- (BOOL)reloadAllContacts { - BOOL success = FALSE; - if ([CNContactStore class]) { - [_addressBookMap removeAllObjects]; - // iOS 9 or later - NSError *contactError; - CNContactStore* store = [[CNContactStore alloc] init]; - [store - containersMatchingPredicate:[CNContainer - predicateForContainersWithIdentifiers:@[ - store.defaultContainerIdentifier - ]] - error:&contactError]; - NSArray *keysToFetch = @[ - CNContactEmailAddressesKey, CNContactPhoneNumbersKey, - CNContactFamilyNameKey, CNContactGivenNameKey, CNContactNicknameKey, - CNContactPostalAddressesKey, CNContactIdentifierKey, - CNInstantMessageAddressUsernameKey, CNContactInstantMessageAddressesKey, - CNInstantMessageAddressUsernameKey, CNContactImageDataKey - ]; - CNContactFetchRequest *request = - [[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch]; - - success = - [store enumerateContactsWithFetchRequest:request - error:&contactError - usingBlock:^(CNContact *__nonnull contact, - BOOL *__nonnull stop) { - if (contactError) { - NSLog(@"error fetching contacts %@", - contactError); - } else { - Contact *newContact = [[Contact alloc] - initWithCNContact:contact]; - [self registerAddrsFor:newContact]; - } - }]; - // load Linphone friends - const MSList *lists = linphone_core_get_friends_lists(LC); - while (lists) { - LinphoneFriendList *fl = lists->data; - const MSList *friends = linphone_friend_list_get_friends(fl); - while (friends) { - LinphoneFriend *f = friends->data; - // only append friends that are not native contacts (already added - // above) - if (linphone_friend_get_ref_key(f) == NULL) { - Contact *contact = [[Contact alloc] initWithFriend:f]; - [self registerAddrsFor:contact]; - } - friends = friends->next; - } - linphone_friend_list_update_subscriptions(fl); - lists = lists->next; - } - } - [NSNotificationCenter.defaultCenter - postNotificationName:kLinphoneAddressBookUpdate - object:self]; - return success; -} - - (void)registerAddrsFor:(Contact *)contact { Contact* mContact = contact; for (NSString *phone in mContact.phones) { @@ -374,44 +312,45 @@ } - (BOOL)deleteCNContact:(CNContact *)contact { - CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init]; - [saveRequest deleteContact:[contact mutableCopy]]; - @try { - NSLog(@"Success %d", [store executeSaveRequest:saveRequest error:nil]); - [self fetchContactsInBackGroundThread]; - } @catch (NSException *exception) { - NSLog(@"description = %@", [exception description]); - return FALSE; - } - return TRUE; + CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init]; + [saveRequest deleteContact:[contact mutableCopy]]; + @try { + BOOL success = [store executeSaveRequest:saveRequest error:nil]; + NSLog(@"Success %d", success); + if(success) + [self fetchContactsInBackGroundThread]; + } @catch (NSException *exception) { + NSLog(@"description = %@", [exception description]); + return FALSE; + } + return TRUE; } - (BOOL)deleteAllContacts { NSArray *keys = @[ CNContactPhoneNumbersKey ]; NSString *containerId = store.defaultContainerIdentifier; - NSPredicate *predicate = - [CNContact predicateForContactsInContainerWithIdentifier:containerId]; + NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId]; NSError *error; NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys - error:&error]; - if (error) { - NSLog(@"error fetching contacts %@", error); - return FALSE; - } else { - CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init]; - for (CNContact *contact in cnContacts) { - [saveRequest deleteContact:[contact mutableCopy]]; - } - @try { - NSLog(@"Success %d", [store executeSaveRequest:saveRequest error:nil]); - } @catch (NSException *exception) { - NSLog(@"description = %@", [exception description]); - return FALSE; - } + error:&error]; + if (error) { + NSLog(@"error fetching contacts %@", error); + return FALSE; + } else { + CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init]; + for (CNContact *contact in cnContacts) { + [saveRequest deleteContact:[contact mutableCopy]]; + } + @try { + NSLog(@"Success %d", [store executeSaveRequest:saveRequest error:nil]); + } @catch (NSException *exception) { + NSLog(@"description = %@", [exception description]); + return FALSE; + } NSLog(@"Deleted contacts %lu", (unsigned long)cnContacts.count); - } - return TRUE; + } + return TRUE; } - (BOOL)saveContact:(Contact *)contact { @@ -446,14 +385,12 @@ } NSError *saveError; @try { - NSLog(@"Success %d", - [store executeSaveRequest:saveRequest error:&saveError]); - [self updateFriend:contact]; + NSLog(@"Success %d", [store executeSaveRequest:saveRequest error:&saveError]); + [self updateFriend:contact]; [LinphoneManager.instance setContactsUpdated:TRUE]; } @catch (NSException *exception) { - NSLog(@"=====>>>>> CNContact SaveRequest failed : description = %@", - [exception description]); - return FALSE; + NSLog(@"=====>>>>> CNContact SaveRequest failed : description = %@", [exception description]); + return FALSE; } [self fetchContactsInBackGroundThread]; return TRUE; diff --git a/TestsUI/LinphoneTestCase.m b/TestsUI/LinphoneTestCase.m index 8c82a871f..5ca6f0da5 100644 --- a/TestsUI/LinphoneTestCase.m +++ b/TestsUI/LinphoneTestCase.m @@ -150,14 +150,11 @@ linphone_core_set_file_transfer_server(lc, "https://www.linphone.org:444/lft.php"); // reload address book to prepend proxy config domain to contacts' phone number - [[[LinphoneManager instance] fastAddressBook] - reloadAllContacts]; + [[[LinphoneManager instance] fastAddressBook] fetchContactsInBackGroundThread]; - [self waitForRegistration]; - [[LinphoneManager instance] - lpConfigSetInt:NO - forKey:@"animations_preference"]; - } + [self waitForRegistration]; + [[LinphoneManager instance] lpConfigSetInt:NO forKey:@"animations_preference"]; + } } - (UITableView *)findTableView:(NSString *)table {