From 4c75c4222d1fd78c119d2de79bc59e241f327b0a Mon Sep 17 00:00:00 2001 From: QuentinArguillere Date: Mon, 26 Sep 2022 10:46:58 +0200 Subject: [PATCH] =?UTF-8?q?Add=20=E2=80=9Cfetch=5Fnative=5Fcontacts?= =?UTF-8?q?=E2=80=9D=20parameters=20to=20the=20rc-factory.=20If=20set=20to?= =?UTF-8?q?=200,=20we=20won=E2=80=99t=20ask=20for=20permission=20to=20use?= =?UTF-8?q?=20the=20phone=20native=20contacts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Classes/Utils/FastAddressBook.m | 124 ++++++++++++++++++-------------- Resources/linphonerc-factory | 1 + 2 files changed, 72 insertions(+), 53 deletions(-) diff --git a/Classes/Utils/FastAddressBook.m b/Classes/Utils/FastAddressBook.m index fa2c99cf0..3576d3405 100644 --- a/Classes/Utils/FastAddressBook.m +++ b/Classes/Utils/FastAddressBook.m @@ -146,7 +146,7 @@ } + (BOOL)isAuthorized { - return [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]; + return ![LinphoneManager.instance lpConfigBoolForKey:@"fetch_native_contacts"] || [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]; } - (FastAddressBook *)init { @@ -172,62 +172,80 @@ return self; } +- (void) loadLinphoneFriends { + // 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; + } + [self dumpContactsDisplayNamesToUserDefaults]; + + [NSNotificationCenter.defaultCenter + postNotificationName:kLinphoneAddressBookUpdate + object:self]; +} + - (void) fetchContactsInBackGroundThread{ [_addressBookMap removeAllObjects]; _addressBookMap = [NSMutableDictionary dictionary]; - CNEntityType entityType = CNEntityTypeContacts; - [store requestAccessForEntityType:entityType completionHandler:^(BOOL granted, NSError *_Nullable error) { - BOOL success = FALSE; - if(granted){ - LOGD(@"CNContactStore authorization granted"); - - 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, CNContactOrganizationNameKey - ]; - 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; + + LinphoneFriend *testFriend = linphone_core_create_friend(LC); + linphone_friend_set_name(testFriend, "Moi Maisentest"); + linphone_friend_set_native_uri(testFriend, "sip:pouetpouet@sip.linphone.org"); + const MSList *lists = linphone_core_get_friends_lists(LC); + LinphoneFriendList *fl = lists->data; + linphone_friend_list_add_friend(fl, testFriend); + const MSList *friends = linphone_friend_list_get_friends(fl); + + if ([LinphoneManager.instance lpConfigBoolForKey:@"fetch_native_contacts"]) { + CNEntityType entityType = CNEntityTypeContacts; + [store requestAccessForEntityType:entityType completionHandler:^(BOOL granted, NSError *_Nullable error) { + BOOL success = FALSE; + if(granted){ + LOGD(@"CNContactStore authorization granted"); + + 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, CNContactOrganizationNameKey + ]; + 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]; + } + }]; } - linphone_friend_list_update_subscriptions(fl); - lists = lists->next; - } - [self dumpContactsDisplayNamesToUserDefaults]; - - [NSNotificationCenter.defaultCenter - postNotificationName:kLinphoneAddressBookUpdate - object:self]; - }]; + [self loadLinphoneFriends]; + }]; + } else { + [self loadLinphoneFriends]; + } + + } -(void) updateAddressBook:(NSNotification*) notif { diff --git a/Resources/linphonerc-factory b/Resources/linphonerc-factory index d1c8531d2..5468a9c0d 100644 --- a/Resources/linphonerc-factory +++ b/Resources/linphonerc-factory @@ -27,6 +27,7 @@ use_callkit=1 accept_early_media=0 +fetch_native_contacts=1 [sip] deliver_imdn=1