From 15481cf227468f4ae97f63d9ae990b0f41d6c9fe Mon Sep 17 00:00:00 2001 From: Benoit Martins Date: Fri, 14 Apr 2023 17:25:30 +0200 Subject: [PATCH] Add delegate presence (callback) Set Consolidated presence of core when app enter in background or foreground --- Classes/ChatsListTableView.m | 18 +++++++++ Classes/LinphoneAppDelegate.m | 5 +++ Classes/Swift/Voip/Widgets/Avatar.swift | 54 +++++++++++++++++++++++-- Classes/Utils/FastAddressBook.m | 2 +- linphone.xcodeproj/project.pbxproj | 8 ++-- 5 files changed, 78 insertions(+), 9 deletions(-) diff --git a/Classes/ChatsListTableView.m b/Classes/ChatsListTableView.m index 4dfcd0888..5d0807849 100644 --- a/Classes/ChatsListTableView.m +++ b/Classes/ChatsListTableView.m @@ -53,6 +53,24 @@ self.tableView.accessibilityIdentifier = @"Chat list"; [self loadData]; _chatRooms = NULL; + + NSDictionary* userInfo; + [NSNotificationCenter.defaultCenter addObserver:self + selector: @selector(receiveTestNotification:) + name: @"LinphoneFriendPresenceUpdate" + object: userInfo]; +} + +-(void) receiveTestNotification:(NSNotification*)notification +{ + if ([notification.name isEqualToString:@"LinphoneFriendPresenceUpdate"]) + { + NSDictionary* userInfo = notification.userInfo; + NSString* friend = (NSString*)userInfo[@"friend"]; + BOOL friendIsOnline = (BOOL)userInfo[@"isOnline"]; + + //NSLog(@"Successfully received test notification! %@ %d", friend, friendIsOnline); + } } - (void)viewDidAppear:(BOOL)animated { diff --git a/Classes/LinphoneAppDelegate.m b/Classes/LinphoneAppDelegate.m index 5899553d0..8e256d7ed 100644 --- a/Classes/LinphoneAppDelegate.m +++ b/Classes/LinphoneAppDelegate.m @@ -75,6 +75,9 @@ // To avoid crash [PhoneMainView.instance changeCurrentView:DialerView.compositeViewDescription]; } + + linphone_core_set_consolidated_presence(LC, LinphoneConsolidatedPresenceBusy); + [CallManager.instance stopLinphoneCore]; } [SwiftUtil resetCachedAsset]; @@ -86,6 +89,8 @@ [LinphoneManager.instance startLinphoneCore]; [LinphoneManager.instance.fastAddressBook reloadFriends]; + linphone_core_set_consolidated_presence(LC, LinphoneConsolidatedPresenceOnline); + [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneMessageReceived object:nil]; } diff --git a/Classes/Swift/Voip/Widgets/Avatar.swift b/Classes/Swift/Voip/Widgets/Avatar.swift index 365bfd0a7..af23e991d 100644 --- a/Classes/Swift/Voip/Widgets/Avatar.swift +++ b/Classes/Swift/Voip/Widgets/Avatar.swift @@ -114,7 +114,30 @@ class Avatar : UIView { let contactAddress = Address.getSwiftObject(cObject: address).contact() var iconPresenceView = UIImageView() if contactAddress != nil { - iconPresenceView = updatePresenceImage(contact: Address.getSwiftObject(cObject: address).contact()!) + iconPresenceView = updatePresenceImage(contact: contactAddress!) + + + + + let friend = Friend.getSwiftObject(cObject: (contactAddress?.friend)!) + + let friendDelegate = FriendDelegateStub( + onPresenceReceived: { (linphoneFriend: Friend) -> Void in + let presenceModel = linphoneFriend.getPresenceModelForUriOrTel(uriOrTel: (linphoneFriend.address?.asStringUriOnly())!) + print("Successfully received test notification linphoneFriend \(linphoneFriend.address?.displayName)") + print("Successfully received test notification friend \(friend.address?.displayName)") + print("Successfully received test notification consolidatedPresence \(presenceModel!.consolidatedPresence)") + print("Successfully received test notification consolidatedPresence \(friend.consolidatedPresence)") + print("Successfully received test notification isOnline \(presenceModel!.isOnline)") + print("Successfully received test notification friend \(friend.isPresenceReceived)") + NotificationCenter.default.post(name: Notification.Name("LinphoneFriendPresenceUpdate"), object: nil, userInfo: ["friend": linphoneFriend.address?.asStringUriOnly(), "isOnline": presenceModel!.isOnline]) + } + ) + + friend.addDelegate(delegate: friendDelegate) + + + } avatarWithPresence.addSubview(avatarImageWihtoutPresence) @@ -124,8 +147,7 @@ class Avatar : UIView { return avatarWithPresence.toImage() } - @objc static func imageForInitials(contact:Contact) -> UIImage? { - let displayName: String = contact.displayName + @objc static func imageForInitials(contact:Contact, displayName:String) -> UIImage? { if (shared == nil) { prepareIt() } @@ -139,6 +161,30 @@ class Avatar : UIView { let avatarWithPresence = UIView(frame: CGRect(x: 0, y: 0, width: size, height: size)) let avatarImageWihtoutPresence = UIImageView(image: shared?.toImage()) let iconPresenceView = updatePresenceImage(contact: contact) + + + + + let friend = Friend.getSwiftObject(cObject: contact.friend) + + let friendDelegate = FriendDelegateStub( + onPresenceReceived: { (linphoneFriend: Friend) -> Void in + let presenceModel = linphoneFriend.getPresenceModelForUriOrTel(uriOrTel: (linphoneFriend.address?.asStringUriOnly())!) + print("Successfully received test notification linphoneFriend displayName \(linphoneFriend.address?.displayName)") + print("Successfully received test notification friend displayName \(friend.address?.displayName)") + print("Successfully received test notification presenceModel consolidatedPresence \(presenceModel!.consolidatedPresence)") + print("Successfully received test notification friend consolidatedPresence \(friend.consolidatedPresence)") + print("Successfully received test notification presenceModel isOnline \(presenceModel!.isOnline)") + print("Successfully received test notification friend isPresenceReceived \(friend.isPresenceReceived)") + NotificationCenter.default.post(name: Notification.Name("LinphoneFriendPresenceUpdate"), object: nil, userInfo: ["friend": linphoneFriend.address?.asStringUriOnly(), "isOnline": presenceModel!.isOnline]) + } + ) + + friend.addDelegate(delegate: friendDelegate) + + + + avatarWithPresence.addSubview(avatarImageWihtoutPresence) avatarWithPresence.addSubview(iconPresenceView) @@ -166,7 +212,7 @@ class Avatar : UIView { } else { imageName = "led_inprogress"; } - + return UIImageView(image: UIImage(named:imageName)) } } diff --git a/Classes/Utils/FastAddressBook.m b/Classes/Utils/FastAddressBook.m index 66ee7c873..6ebaa08a8 100644 --- a/Classes/Utils/FastAddressBook.m +++ b/Classes/Utils/FastAddressBook.m @@ -36,7 +36,7 @@ if (retImage == nil) { retImage = contact.friend && linphone_friend_get_addresses(contact.friend) ? [AvatarBridge imageForAddressWithAddress:linphone_friend_get_addresses(contact.friend)->data] : - [AvatarBridge imageForInitialsWithContact:contact]; + [AvatarBridge imageForInitialsWithContact:contact displayName:[contact displayName]]; } if (retImage.size.width != retImage.size.height) { retImage = [retImage squareCrop]; diff --git a/linphone.xcodeproj/project.pbxproj b/linphone.xcodeproj/project.pbxproj index 92416ff7d..771defd30 100644 --- a/linphone.xcodeproj/project.pbxproj +++ b/linphone.xcodeproj/project.pbxproj @@ -606,7 +606,7 @@ 63E27A321C4FECD000D332AE /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 63E27A311C4FECD000D332AE /* LaunchScreen.xib */; }; 63E27A521C50EDB000D332AE /* hold.mkv in Resources */ = {isa = PBXBuildFile; fileRef = 63E27A511C50EB2700D332AE /* hold.mkv */; }; 63E59A3F1ADE70D900646FB3 /* InAppProductsManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E59A3E1ADE70D900646FB3 /* InAppProductsManager.m */; }; - 63E802DB1C625AEF000D5509 /* BuildFile in Resources */ = {isa = PBXBuildFile; }; + 63E802DB1C625AEF000D5509 /* (null) in Resources */ = {isa = PBXBuildFile; }; 63EC8D391D7438660066547B /* AssistantLinkView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 63EC8D3B1D7438660066547B /* AssistantLinkView.xib */; }; 63F1DF441BCE618E00EDED90 /* UIAddressTextField.m in Sources */ = {isa = PBXBuildFile; fileRef = 63F1DF431BCE618E00EDED90 /* UIAddressTextField.m */; }; 63FB30351A680E73008CA393 /* UIRoundedImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 63FB30341A680E73008CA393 /* UIRoundedImageView.m */; }; @@ -2551,7 +2551,7 @@ path = LinphoneUI; sourceTree = ""; }; - 29B97314FDCFA39411CA2CEA = { + 29B97314FDCFA39411CA2CEA /* CustomTemplate */ = { isa = PBXGroup; children = ( 8C23BCB71D82AAC3005F19BB /* linphone.entitlements */, @@ -3975,7 +3975,7 @@ fr, hu, ); - mainGroup = 29B97314FDCFA39411CA2CEA; + mainGroup = 29B97314FDCFA39411CA2CEA /* CustomTemplate */; productRefGroup = 19C28FACFE9D520D11CA2CBB /* Products */; projectDirPath = ""; projectRoot = ""; @@ -4046,7 +4046,7 @@ 633FEEE01D3CD55A0014B822 /* numpad_8_over~ipad@2x.png in Resources */, 633FEDDC1D3CD5590014B822 /* call_start_body_disabled~ipad.png in Resources */, C63F72FF285A31DA0066163B /* Roboto-Bold.ttf in Resources */, - 63E802DB1C625AEF000D5509 /* BuildFile in Resources */, + 63E802DB1C625AEF000D5509 /* (null) in Resources */, 633FEE2E1D3CD5590014B822 /* color_F.png in Resources */, 633FEDC51D3CD5590014B822 /* call_hangup_disabled@2x.png in Resources */, 633FEEDF1D3CD55A0014B822 /* numpad_8_over~ipad.png in Resources */,