From db3495a33d27401fb0e23f142335ff2c6f17b7f8 Mon Sep 17 00:00:00 2001 From: Paul Cartier Date: Tue, 28 Apr 2020 16:38:54 +0200 Subject: [PATCH] refresh friends from fastaddressbook when going from BG to FG --- Classes/Contact.h | 2 ++ Classes/Contact.m | 32 ++++++++++++++++++++++++++++++++ Classes/LinphoneAppDelegate.m | 9 +++++---- Classes/Utils/FastAddressBook.h | 4 +++- Classes/Utils/FastAddressBook.m | 14 ++++++++++++++ 5 files changed, 56 insertions(+), 5 deletions(-) diff --git a/Classes/Contact.h b/Classes/Contact.h index 180154225..2d5529ff4 100644 --- a/Classes/Contact.h +++ b/Classes/Contact.h @@ -43,6 +43,8 @@ - (instancetype)initWithCNContact:(CNContact *)contact; - (instancetype)initWithFriend:(LinphoneFriend *) friend; +- (void)reloadFriend; +- (void)clearFriend; - (BOOL)setSipAddress:(NSString *)sip atIndex:(NSInteger)index; - (BOOL)setEmail:(NSString *)email atIndex:(NSInteger)index; diff --git a/Classes/Contact.m b/Classes/Contact.m index 0a77fccf0..c6dc9ae29 100644 --- a/Classes/Contact.m +++ b/Classes/Contact.m @@ -455,4 +455,36 @@ _emails = [[NSMutableArray alloc] init]; } +- (void)reloadFriend { + const char *key = [NSString stringWithFormat:@"ab%@", _person.identifier].UTF8String; + // try to find friend associated with that person + _friend = linphone_friend_list_find_friend_by_ref_key(linphone_core_get_default_friend_list(LC), key); + if (!_friend) { + _friend = linphone_friend_ref(linphone_core_create_friend(LC)); + linphone_friend_set_ref_key(_friend, key); + linphone_friend_set_name(_friend, [NSString stringWithFormat:@"%@%@", _firstName ? _firstName : @"", _lastName ? [_firstName ? @" " : @"" stringByAppendingString:_lastName] : @""] .UTF8String); + for (NSString *sipAddr in _sipAddresses) { + LinphoneAddress *addr = linphone_core_interpret_url(LC, sipAddr.UTF8String); + if (addr) { + linphone_address_set_display_name(addr, [self displayName].UTF8String); + linphone_friend_add_address(_friend, addr); + linphone_address_destroy(addr); + } + } + for (NSString *phone in _phones) { + linphone_friend_add_phone_number(_friend, phone.UTF8String); + } + if (_friend) { + linphone_friend_enable_subscribes(_friend, FALSE); + linphone_friend_set_inc_subscribe_policy(_friend, LinphoneSPDeny); + linphone_core_add_friend(LC, _friend); + } + } + linphone_friend_ref(_friend); +} + +- (void)clearFriend { + _friend = NULL; +} + @end diff --git a/Classes/LinphoneAppDelegate.m b/Classes/LinphoneAppDelegate.m index 8068ac8d0..9565118fe 100644 --- a/Classes/LinphoneAppDelegate.m +++ b/Classes/LinphoneAppDelegate.m @@ -58,12 +58,13 @@ - (void)applicationDidEnterBackground:(UIApplication *)application { LOGI(@"%@", NSStringFromSelector(_cmd)); [LinphoneManager.instance enterBackgroundMode]; - + [LinphoneManager.instance.fastAddressBook clearFriends]; [CoreManager.instance stopLinphoneCore]; } - (void)applicationWillEnterForeground:(UIApplication *)application { [LinphoneManager.instance startLinphoneCore]; + [LinphoneManager.instance.fastAddressBook reloadFriends]; [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneMessageReceived object:nil]; } @@ -100,9 +101,9 @@ if (PhoneMainView.instance.currentView == ContactsListView.compositeViewDescription || PhoneMainView.instance.currentView == ContactDetailsView.compositeViewDescription) { [PhoneMainView.instance changeCurrentView:DialerView.compositeViewDescription]; } - [instance.fastAddressBook fetchContactsInBackGroundThread]; - instance.fastAddressBook.needToUpdate = FALSE; - } + [instance.fastAddressBook fetchContactsInBackGroundThread]; + instance.fastAddressBook.needToUpdate = FALSE; + } LinphoneCall *call = linphone_core_get_current_call(LC); diff --git a/Classes/Utils/FastAddressBook.h b/Classes/Utils/FastAddressBook.h index 60ecb78e9..3b6decef5 100644 --- a/Classes/Utils/FastAddressBook.h +++ b/Classes/Utils/FastAddressBook.h @@ -28,12 +28,14 @@ @property(readonly, nonatomic) NSMutableDictionary *addressBookMap; @property BOOL needToUpdate; -- (void) fetchContactsInBackGroundThread; +- (void)fetchContactsInBackGroundThread; - (BOOL)deleteContact:(Contact *)contact; - (BOOL)deleteCNContact:(CNContact *)CNContact; - (BOOL)deleteAllContacts; - (BOOL)saveContact:(Contact *)contact; - (BOOL)saveCNContact:(CNContact *)CNContact contact:(Contact *)Contact; +- (void)reloadFriends; +- (void)clearFriends; - (void)dumpContactsDisplayNamesToUserDefaults; - (void)removeContactFromUserDefaults:(Contact *)contact; diff --git a/Classes/Utils/FastAddressBook.m b/Classes/Utils/FastAddressBook.m index cde589599..25958cc8f 100644 --- a/Classes/Utils/FastAddressBook.m +++ b/Classes/Utils/FastAddressBook.m @@ -498,6 +498,20 @@ } } +- (void)reloadFriends { + dispatch_async(dispatch_get_main_queue(), ^{ + [_addressBookMap enumerateKeysAndObjectsUsingBlock:^(NSString *name, Contact *contact, BOOL *stop) { + [contact reloadFriend]; + }]; + }); +} + +- (void)clearFriends { + [_addressBookMap enumerateKeysAndObjectsUsingBlock:^(NSString *name, Contact *contact, BOOL *stop) { + [contact clearFriend]; + }]; +} + - (void)dumpContactsDisplayNamesToUserDefaults { LOGD(@"dumpContactsDisplayNamesToUserDefaults"); NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:kLinphoneMsgNotificationAppGroupId];