linphone-ios/Classes/LinphoneAppDelegate.m

762 lines
33 KiB
Objective-C

/* LinphoneAppDelegate.m
*
* Copyright (C) 2009 Belledonne Comunications, Grenoble, France
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import "PhoneMainView.h"
#import "ContactsListView.h"
#import "ShopView.h"
#import "linphoneAppDelegate.h"
#import "AddressBook/ABPerson.h"
#import "CoreTelephony/CTCallCenter.h"
#import "CoreTelephony/CTCall.h"
#import "LinphoneCoreSettingsStore.h"
#include "LinphoneManager.h"
#include "linphone/linphonecore.h"
@implementation LinphoneAppDelegate
@synthesize configURL;
@synthesize window;
#pragma mark - Lifecycle Functions
- (id)init {
self = [super init];
if (self != nil) {
startedInBackground = FALSE;
}
return self;
}
#pragma mark -
- (void)applicationDidEnterBackground:(UIApplication *)application {
LOGI(@"%@", NSStringFromSelector(_cmd));
if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_9_x_Max && (linphone_core_get_calls_nb(LC) == 0)) {
linphone_core_set_network_reachable(LC, FALSE);
LinphoneManager.instance.connectivity = none;
}
[LinphoneManager.instance enterBackgroundMode];
}
- (void)applicationWillResignActive:(UIApplication *)application {
LOGI(@"%@", NSStringFromSelector(_cmd));
LinphoneCall *call = linphone_core_get_current_call(LC);
if (call) {
/* save call context */
LinphoneManager *instance = LinphoneManager.instance;
instance->currentCallContextBeforeGoingBackground.call = call;
instance->currentCallContextBeforeGoingBackground.cameraIsEnabled = linphone_call_camera_enabled(call);
const LinphoneCallParams *params = linphone_call_get_current_params(call);
if (linphone_call_params_video_enabled(params)) {
linphone_call_enable_camera(call, false);
}
}
if (![LinphoneManager.instance resignActive]) {
}
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
LOGI(@"%@", NSStringFromSelector(_cmd));
if (startedInBackground) {
startedInBackground = FALSE;
[PhoneMainView.instance startUp];
[PhoneMainView.instance updateStatusBar:nil];
}
LinphoneManager *instance = LinphoneManager.instance;
[instance becomeActive];
if (instance.fastAddressBook.needToUpdate) {
//Update address book for external changes
if (PhoneMainView.instance.currentView == ContactsListView.compositeViewDescription) {
[PhoneMainView.instance changeCurrentView:DialerView.compositeViewDescription];
}
[instance.fastAddressBook reload];
instance.fastAddressBook.needToUpdate = FALSE;
const MSList *lists = linphone_core_get_friends_lists(LC);
while (lists) {
linphone_friend_list_update_subscriptions(lists->data);
lists = lists->next;
}
}
LinphoneCall *call = linphone_core_get_current_call(LC);
if (call) {
if (call == instance->currentCallContextBeforeGoingBackground.call) {
const LinphoneCallParams *params = linphone_call_get_current_params(call);
if (linphone_call_params_video_enabled(params)) {
linphone_call_enable_camera(call, instance->currentCallContextBeforeGoingBackground.cameraIsEnabled);
}
instance->currentCallContextBeforeGoingBackground.call = 0;
} else if (linphone_call_get_state(call) == LinphoneCallIncomingReceived) {
[PhoneMainView.instance displayIncomingCall:call];
// in this case, the ringing sound comes from the notification.
// To stop it we have to do the iOS7 ring fix...
[self fixRing];
}
}
[LinphoneManager.instance.iapManager check];
}
#pragma deploymate push "ignored-api-availability"
- (UIUserNotificationCategory *)getMessageNotificationCategory {
NSArray *actions;
if ([[UIDevice.currentDevice systemVersion] floatValue] < 9 ||
[LinphoneManager.instance lpConfigBoolForKey:@"show_msg_in_notif"] == NO) {
UIMutableUserNotificationAction *reply = [[UIMutableUserNotificationAction alloc] init];
reply.identifier = @"reply";
reply.title = NSLocalizedString(@"Reply", nil);
reply.activationMode = UIUserNotificationActivationModeForeground;
reply.destructive = NO;
reply.authenticationRequired = YES;
UIMutableUserNotificationAction *mark_read = [[UIMutableUserNotificationAction alloc] init];
mark_read.identifier = @"mark_read";
mark_read.title = NSLocalizedString(@"Mark Read", nil);
mark_read.activationMode = UIUserNotificationActivationModeBackground;
mark_read.destructive = NO;
mark_read.authenticationRequired = NO;
actions = @[ mark_read, reply ];
} else {
// iOS 9 allows for inline reply. We don't propose mark_read in this case
UIMutableUserNotificationAction *reply_inline = [[UIMutableUserNotificationAction alloc] init];
reply_inline.identifier = @"reply_inline";
reply_inline.title = NSLocalizedString(@"Reply", nil);
reply_inline.activationMode = UIUserNotificationActivationModeBackground;
reply_inline.destructive = NO;
reply_inline.authenticationRequired = NO;
reply_inline.behavior = UIUserNotificationActionBehaviorTextInput;
actions = @[ reply_inline ];
}
UIMutableUserNotificationCategory *localRingNotifAction = [[UIMutableUserNotificationCategory alloc] init];
localRingNotifAction.identifier = @"incoming_msg";
[localRingNotifAction setActions:actions forContext:UIUserNotificationActionContextDefault];
[localRingNotifAction setActions:actions forContext:UIUserNotificationActionContextMinimal];
return localRingNotifAction;
}
- (UIUserNotificationCategory *)getCallNotificationCategory {
UIMutableUserNotificationAction *answer = [[UIMutableUserNotificationAction alloc] init];
answer.identifier = @"answer";
answer.title = NSLocalizedString(@"Answer", nil);
answer.activationMode = UIUserNotificationActivationModeForeground;
answer.destructive = NO;
answer.authenticationRequired = YES;
UIMutableUserNotificationAction *decline = [[UIMutableUserNotificationAction alloc] init];
decline.identifier = @"decline";
decline.title = NSLocalizedString(@"Decline", nil);
decline.activationMode = UIUserNotificationActivationModeBackground;
decline.destructive = YES;
decline.authenticationRequired = NO;
NSArray *localRingActions = @[ decline, answer ];
UIMutableUserNotificationCategory *localRingNotifAction = [[UIMutableUserNotificationCategory alloc] init];
localRingNotifAction.identifier = @"incoming_call";
[localRingNotifAction setActions:localRingActions forContext:UIUserNotificationActionContextDefault];
[localRingNotifAction setActions:localRingActions forContext:UIUserNotificationActionContextMinimal];
return localRingNotifAction;
}
- (UIUserNotificationCategory *)getAccountExpiryNotificationCategory {
UIMutableUserNotificationCategory *expiryNotification = [[UIMutableUserNotificationCategory alloc] init];
expiryNotification.identifier = @"expiry_notification";
return expiryNotification;
}
- (void)registerForNotifications:(UIApplication *)app {
LinphoneManager *instance = [LinphoneManager instance];
if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_8_0) {
//[app unregisterForRemoteNotifications];
// iOS8 and more : PushKit
self.voipRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
self.voipRegistry.delegate = self;
// Initiate registration.
self.voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
} else {
/* iOS7 and below */
self.voipRegistry.delegate = NULL;
if (!instance.isTesting) {
NSUInteger notifTypes =
UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge;
[app registerForRemoteNotificationTypes:notifTypes];
}
}
}
#pragma deploymate pop
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIApplication *app = [UIApplication sharedApplication];
UIApplicationState state = app.applicationState;
LinphoneManager *instance = [LinphoneManager instance];
BOOL background_mode = [instance lpConfigBoolForKey:@"backgroundmode_preference"];
BOOL start_at_boot = [instance lpConfigBoolForKey:@"start_at_boot_preference"];
[self registerForNotifications:app];
if (state == UIApplicationStateBackground) {
// we've been woken up directly to background;
if (!start_at_boot || !background_mode) {
// autoboot disabled or no background, and no push: do nothing and wait for a real launch
//output a log with NSLog, because the ortp logging system isn't activated yet at this time
NSLog(@"Linphone launch doing nothing because start_at_boot or background_mode are not activated.", NULL);
return YES;
}
}
bgStartId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
LOGW(@"Background task for application launching expired.");
[[UIApplication sharedApplication] endBackgroundTask:bgStartId];
}];
[LinphoneManager.instance startLinphoneCore];
LinphoneManager.instance.iapManager.notificationCategory = @"expiry_notification";
// initialize UI
[self.window makeKeyAndVisible];
[RootViewManager setupWithPortrait:(PhoneMainView *)self.window.rootViewController];
[PhoneMainView.instance startUp];
[PhoneMainView.instance updateStatusBar:nil];
if (floor(NSFoundationVersionNumber) < NSFoundationVersionNumber_iOS_8_0) {
NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
LOGI(@"PushNotification from launch received.");
[self processRemoteNotification:remoteNotif];
}
}
if (bgStartId != UIBackgroundTaskInvalid)
[[UIApplication sharedApplication] endBackgroundTask:bgStartId];
//Enable all notification type. VoIP Notifications don't present a UI but we will use this to show local nofications later
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert| UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
//register the notification settings
[application registerUserNotificationSettings:notificationSettings];
//output what state the app is in. This will be used to see when the app is started in the background
LOGI(@"app launched with state : %li", (long)application.applicationState);
LOGI(@"FINISH LAUNCHING WITH OPTION : %@", launchOptions.description);
return YES;
}
- (void)applicationWillTerminate:(UIApplication *)application {
LOGI(@"%@", NSStringFromSelector(_cmd));
linphone_core_terminate_all_calls(LC);
// destroyLinphoneCore automatically unregister proxies but if we are using
// remote push notifications, we want to continue receiving them
if (LinphoneManager.instance.pushNotificationToken != nil) {
// trick me! setting network reachable to false will avoid sending unregister
const MSList *proxies = linphone_core_get_proxy_config_list(LC);
BOOL pushNotifEnabled = NO;
while (proxies) {
const char *refkey = linphone_proxy_config_get_ref_key(proxies->data);
pushNotifEnabled = pushNotifEnabled || (refkey && strcmp(refkey, "push_notification") == 0);
proxies = proxies->next;
}
// but we only want to hack if at least one proxy config uses remote push..
if (pushNotifEnabled) {
linphone_core_set_network_reachable(LC, FALSE);
}
}
[LinphoneManager.instance destroyLinphoneCore];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
NSString *scheme = [[url scheme] lowercaseString];
if ([scheme isEqualToString:@"linphone-config"] || [scheme isEqualToString:@"linphone-config"]) {
NSString *encodedURL =
[[url absoluteString] stringByReplacingOccurrencesOfString:@"linphone-config://" withString:@""];
self.configURL = [encodedURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Remote configuration", nil)
message:NSLocalizedString(@"This operation will load a remote configuration. Continue ?", nil)
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"No", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
UIAlertAction* yesAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Yes", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self showWaitingIndicator];
[self attemptRemoteConfiguration];
}];
[errView addAction:defaultAction];
[errView addAction:yesAction];
[PhoneMainView.instance presentViewController:errView animated:YES completion:nil];
} else {
if ([[url scheme] isEqualToString:@"sip"]) {
// remove "sip://" from the URI, and do it correctly by taking resourceSpecifier and removing leading and
// trailing "/"
NSString *sipUri = [[url resourceSpecifier]
stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
[VIEW(DialerView) setAddress:sipUri];
}
}
return YES;
}
- (void)fixRing {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
// iOS7 fix for notification sound not stopping.
// see http://stackoverflow.com/questions/19124882/stopping-ios-7-remote-notification-sound
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
}
- (void)processRemoteNotification:(NSDictionary *)userInfo {
NSDictionary *aps = [userInfo objectForKey:@"aps"];
if (aps != nil) {
NSDictionary *alert = [aps objectForKey:@"alert"];
if (alert != nil) {
NSString *loc_key = [alert objectForKey:@"loc-key"];
/*if we receive a remote notification, it is probably because our TCP background socket was no more working.
As a result, break it and refresh registers in order to make sure to receive incoming INVITE or MESSAGE*/
if (linphone_core_get_calls(LC) == NULL) { // if there are calls, obviously our TCP socket shall be working
//linphone_core_set_network_reachable(LC, FALSE);
if (!linphone_core_is_network_reachable(LC)) {
LinphoneManager.instance.connectivity = none; //Force connectivity to be discovered again
[LinphoneManager.instance setupNetworkReachabilityCallback];
}
if (loc_key != nil) {
NSString *callId = [userInfo objectForKey:@"call-id"];
if (callId != nil) {
[LinphoneManager.instance addPushCallId:callId];
} else {
LOGE(@"PushNotification: does not have call-id yet, fix it !");
}
if ([loc_key isEqualToString:@"IC_MSG"]) {
[self fixRing];
}
}
}
}
}
LOGI(@"Notification %@ processed", userInfo.description);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
LOGI(@"%@ : %@", NSStringFromSelector(_cmd), userInfo);
[self processRemoteNotification:userInfo];
}
- (LinphoneChatRoom *)findChatRoomForContact:(NSString *)contact {
const MSList *rooms = linphone_core_get_chat_rooms(LC);
const char *from = [contact UTF8String];
while (rooms) {
const LinphoneAddress *room_from_address = linphone_chat_room_get_peer_address((LinphoneChatRoom *)rooms->data);
char *room_from = linphone_address_as_string_uri_only(room_from_address);
if (room_from && strcmp(from, room_from) == 0) {
return rooms->data;
}
rooms = rooms->next;
}
return NULL;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
LOGI(@"%@ - state = %ld", NSStringFromSelector(_cmd), (long)application.applicationState);
if ([notification.category isEqual:LinphoneManager.instance.iapManager.notificationCategory]){
[PhoneMainView.instance changeCurrentView:ShopView.compositeViewDescription];
return;
}
[self fixRing];
if ([notification.userInfo objectForKey:@"callId"] != nil) {
BOOL bypass_incoming_view = TRUE;
// some local notifications have an internal timer to relaunch themselves at specified intervals
if ([[notification.userInfo objectForKey:@"timer"] intValue] == 1) {
[LinphoneManager.instance cancelLocalNotifTimerForCallId:[notification.userInfo objectForKey:@"callId"]];
bypass_incoming_view = [LinphoneManager.instance lpConfigBoolForKey:@"autoanswer_notif_preference"];
}
if (bypass_incoming_view) {
[LinphoneManager.instance acceptCallForCallId:[notification.userInfo objectForKey:@"callId"]];
}
} else if ([notification.userInfo objectForKey:@"from_addr"] != nil) {
NSString *remoteContact = (NSString *)[notification.userInfo objectForKey:@"from_addr"];
LinphoneChatRoom *room = [self findChatRoomForContact:remoteContact];
ChatConversationView *view = VIEW(ChatConversationView);
[view setChatRoom:room];
[PhoneMainView.instance changeCurrentView:view.compositeViewDescription];
} else if ([notification.userInfo objectForKey:@"callLog"] != nil) {
NSString *callLog = (NSString *)[notification.userInfo objectForKey:@"callLog"];
HistoryDetailsView *view = VIEW(HistoryDetailsView);
[view setCallLogId:callLog];
[PhoneMainView.instance changeCurrentView:view.compositeViewDescription];
}
}
// this method is implemented for iOS7. It is invoked when receiving a push notification for a call and it has
// "content-available" in the aps section.
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
LOGI(@"%@ : %@", NSStringFromSelector(_cmd), userInfo);
LinphoneManager *lm = LinphoneManager.instance;
// save the completion handler for later execution.
// 2 outcomes:
// - if a new call/message is received, the completion handler will be called with "NEWDATA"
// - if nothing happens for 15 seconds, the completion handler will be called with "NODATA"
lm.silentPushCompletion = completionHandler;
[NSTimer scheduledTimerWithTimeInterval:15.0
target:lm
selector:@selector(silentPushFailed:)
userInfo:nil
repeats:FALSE];
// If no call is yet received at this time, then force Linphone to drop the current socket and make new one to
// register, so that we get
// a better chance to receive the INVITE.
if (linphone_core_get_calls(LC) == NULL) {
linphone_core_set_network_reachable(LC, FALSE);
lm.connectivity = none; /*force connectivity to be discovered again*/
[lm refreshRegisters];
}
}
#pragma mark - PushNotification Functions
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
LOGI(@"%@ : %@", NSStringFromSelector(_cmd), deviceToken);
[LinphoneManager.instance setPushNotificationToken:deviceToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
LOGI(@"%@ : %@", NSStringFromSelector(_cmd), [error localizedDescription]);
[LinphoneManager.instance setPushNotificationToken:nil];
}
#pragma mark - PushKit Functions
- (void)pushRegistry:(PKPushRegistry *)registry
didInvalidatePushTokenForType:(NSString *)type {
LOGI(@"PushKit Token invalidated");
dispatch_async(dispatch_get_main_queue(), ^{[LinphoneManager.instance setPushNotificationToken:nil];});
}
- (void)pushRegistry:(PKPushRegistry *)registry
didReceiveIncomingPushWithPayload:(PKPushPayload *)payload
forType:(NSString *)type {
LOGI(@"PushKit received with payload : %@", payload.description);
LOGI(@"incoming voip notfication: %@ ", payload.dictionaryPayload);
if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_x_Max) {
//Call category
UNNotificationAction* act_ans = [UNNotificationAction actionWithIdentifier:@"Answer" title:@"Answer" options:UNNotificationActionOptionForeground];
UNNotificationAction* act_dec = [UNNotificationAction actionWithIdentifier:@"Decline" title:@"Decline" options:UNNotificationActionOptionNone];
UNNotificationCategory* cat_call = [UNNotificationCategory categoryWithIdentifier:@"call_cat" actions:[NSArray arrayWithObjects:act_ans, act_dec, nil] intentIdentifiers:[[NSMutableArray alloc] init] options:UNNotificationCategoryOptionCustomDismissAction];
//Msg category
UNTextInputNotificationAction* act_reply = [UNTextInputNotificationAction actionWithIdentifier:@"Reply" title:@"Reply" options:UNNotificationActionOptionNone];
UNNotificationAction* act_seen = [UNNotificationAction actionWithIdentifier:@"Seen" title:@"Mark as seen" options:UNNotificationActionOptionNone];
UNNotificationCategory* cat_msg = [UNNotificationCategory categoryWithIdentifier:@"msg_cat" actions:[NSArray arrayWithObjects:act_reply, act_seen, nil] intentIdentifiers:[[NSMutableArray alloc] init] options:UNNotificationCategoryOptionCustomDismissAction];
//UNUserNotificationCenter* notifCenter = [UNUserNotificationCenter currentNotificationCenter];
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
if (error) {
LOGD(error.description);
}
}];
NSSet* categories = [NSSet setWithObjects:cat_call, cat_msg, nil];
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories];
}
dispatch_async(dispatch_get_main_queue(), ^{[self processRemoteNotification:payload.dictionaryPayload];});
}
- (void)pushRegistry:(PKPushRegistry *)registry
didUpdatePushCredentials:(PKPushCredentials *)credentials
forType:(NSString *)type {
LOGI(@"PushKit credentials updated");
LOGI(@"voip token: %@", (credentials.token));
LOGI(@"%@ : %@", NSStringFromSelector(_cmd), credentials.token);
dispatch_async(dispatch_get_main_queue(), ^{[LinphoneManager.instance setPushNotificationToken:credentials.token];});
}
#pragma mark - UserNotifications Framework
- (void) userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionAlert);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)())completionHandler {
LOGD(@"UN : response recieved");
LOGD(response.description);
LinphoneCall* call = linphone_core_get_current_call(LC);
if (call) {
LinphoneCallAppData *data = (__bridge LinphoneCallAppData *)linphone_call_get_user_data(call);
if (data->timer) {
[data->timer invalidate];
data->timer = nil;
}
}
if ([response.actionIdentifier isEqual:@"Answer"]) {
[PhoneMainView.instance changeCurrentView:CallView.compositeViewDescription];
[LinphoneManager.instance acceptCallForCallId:[response.notification.request.content.userInfo objectForKey:@"callId"]];
} else if ([response.actionIdentifier isEqual:@"Decline"]) {
linphone_core_decline_call(LC, call, LinphoneReasonDeclined);
} else if ([response.actionIdentifier isEqual:@"Reply"]) {
LinphoneCore *lc = [LinphoneManager getLc];
NSString *replyText = [(UNTextInputNotificationResponse*)response userText];
NSString *from = [response.notification.request.content.userInfo objectForKey:@"from_addr"];
LinphoneChatRoom *room = linphone_core_get_chat_room_from_uri(lc, [from UTF8String]);
if (room) {
LinphoneChatMessage *msg = linphone_chat_room_create_message(room, replyText.UTF8String);
linphone_chat_room_send_chat_message(room, msg);
linphone_chat_room_mark_as_read(room);
[PhoneMainView.instance updateApplicationBadgeNumber];
}
} else if ([response.actionIdentifier isEqual:@"Seen"]) {
NSString *from = [response.notification.request.content.userInfo objectForKey:@"from_addr"];
LinphoneChatRoom *room = linphone_core_get_chat_room_from_uri(LC, [from UTF8String]);
if (room) {
linphone_chat_room_mark_as_read(room);
TabBarView *tab = (TabBarView *)[PhoneMainView.instance.mainViewController
getCachedController:NSStringFromClass(TabBarView.class)];
[tab update:YES];
[PhoneMainView.instance updateApplicationBadgeNumber];
}
} else { //in this case the value is : com.apple.UNNotificationDefaultActionIdentifier
if ([response.notification.request.content.categoryIdentifier isEqual:@"call_cat"]) {
} else if ([response.notification.request.content.categoryIdentifier isEqual:@"msg_cat"]) {
[PhoneMainView.instance changeCurrentView:ChatsListView.compositeViewDescription];
} else { //Missed call
[PhoneMainView.instance changeCurrentView:HistoryListView.compositeViewDescription];
}
}
}
#pragma mark - User notifications
- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forLocalNotification:(UILocalNotification *)notification
completionHandler:(void (^)())completionHandler {
LinphoneCall* call = linphone_core_get_current_call(LC);
if (call) {
LinphoneCallAppData *data = (__bridge LinphoneCallAppData *)linphone_call_get_user_data(call);
if (data->timer) {
[data->timer invalidate];
data->timer = nil;
}
}
LOGI(@"%@", NSStringFromSelector(_cmd));
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8) {
LOGI(@"%@", NSStringFromSelector(_cmd));
if ([notification.category isEqualToString:@"incoming_call"]) {
if ([identifier isEqualToString:@"answer"]) {
// use the standard handler
[self application:application didReceiveLocalNotification:notification];
} else if ([identifier isEqualToString:@"decline"]) {
LinphoneCall *call = linphone_core_get_current_call(LC);
if (call)
linphone_core_decline_call(LC, call, LinphoneReasonDeclined);
}
} else if ([notification.category isEqualToString:@"incoming_msg"]) {
if ([identifier isEqualToString:@"reply"]) {
// use the standard handler
[self application:application didReceiveLocalNotification:notification];
} else if ([identifier isEqualToString:@"mark_read"]) {
NSString *from = [notification.userInfo objectForKey:@"from_addr"];
LinphoneChatRoom *room = linphone_core_get_chat_room_from_uri(LC, [from UTF8String]);
if (room) {
linphone_chat_room_mark_as_read(room);
TabBarView *tab = (TabBarView *)[PhoneMainView.instance.mainViewController
getCachedController:NSStringFromClass(TabBarView.class)];
[tab update:YES];
[PhoneMainView.instance updateApplicationBadgeNumber];
}
}
}
}
completionHandler();
}
- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forLocalNotification:(UILocalNotification *)notification
withResponseInfo:(NSDictionary *)responseInfo
completionHandler:(void (^)())completionHandler {
LinphoneCall* call = linphone_core_get_current_call(LC);
if (call) {
LinphoneCallAppData *data = (__bridge LinphoneCallAppData *)linphone_call_get_user_data(call);
if (data->timer) {
[data->timer invalidate];
data->timer = nil;
}
}
if ([notification.category isEqualToString:@"incoming_call"]) {
if ([identifier isEqualToString:@"answer"]) {
// use the standard handler
[self application:application didReceiveLocalNotification:notification];
} else if ([identifier isEqualToString:@"decline"]) {
LinphoneCall *call = linphone_core_get_current_call(LC);
if (call)
linphone_core_decline_call(LC, call, LinphoneReasonDeclined);
}
} else if ([notification.category isEqualToString:@"incoming_msg"] &&
[identifier isEqualToString:@"reply_inline"]) {
LinphoneCore *lc = [LinphoneManager getLc];
NSString *replyText = [responseInfo objectForKey:UIUserNotificationActionResponseTypedTextKey];
NSString *from = [notification.userInfo objectForKey:@"from_addr"];
LinphoneChatRoom *room = linphone_core_get_chat_room_from_uri(lc, [from UTF8String]);
if (room) {
LinphoneChatMessage *msg = linphone_chat_room_create_message(room, replyText.UTF8String);
linphone_chat_room_send_chat_message(room, msg);
linphone_chat_room_mark_as_read(room);
[PhoneMainView.instance updateApplicationBadgeNumber];
}
}
completionHandler();
}
- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)userInfo
completionHandler:(void (^)())completionHandler {
LOGI(@"%@", NSStringFromSelector(_cmd));
completionHandler();
}
#pragma deploymate pop
#pragma mark - Remote configuration Functions (URL Handler)
- (void)ConfigurationStateUpdateEvent:(NSNotification *)notif {
LinphoneConfiguringState state = [[notif.userInfo objectForKey:@"state"] intValue];
if (state == LinphoneConfiguringSuccessful) {
[NSNotificationCenter.defaultCenter removeObserver:self name:kLinphoneConfiguringStateUpdate object:nil];
[_waitingIndicator dismissViewControllerAnimated:YES completion:nil];
UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Success", nil)
message:NSLocalizedString(@"Remote configuration successfully fetched and applied.", nil)
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[errView addAction:defaultAction];
[PhoneMainView.instance presentViewController:errView animated:YES completion:nil];
[PhoneMainView.instance startUp];
}
if (state == LinphoneConfiguringFailed) {
[NSNotificationCenter.defaultCenter removeObserver:self name:kLinphoneConfiguringStateUpdate object:nil];
[_waitingIndicator dismissViewControllerAnimated:YES completion:nil];
UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Failure", nil)
message:NSLocalizedString(@"Failed configuring from the specified URL.", nil)
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[errView addAction:defaultAction];
[PhoneMainView.instance presentViewController:errView animated:YES completion:nil];
}
}
- (void)showWaitingIndicator {
_waitingIndicator = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Fetching remote configuration...", nil)
message:@""
preferredStyle:UIAlertControllerStyleAlert];
UIActivityIndicatorView *progress = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 60, 30, 30)];
progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[_waitingIndicator setValue:progress forKey:@"accessoryView"];
[progress setColor:[UIColor blackColor]];
[progress startAnimating];
[PhoneMainView.instance presentViewController:_waitingIndicator animated:YES completion:nil];
}
- (void)attemptRemoteConfiguration {
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(ConfigurationStateUpdateEvent:)
name:kLinphoneConfiguringStateUpdate
object:nil];
linphone_core_set_provisioning_uri(LC, [configURL UTF8String]);
[LinphoneManager.instance destroyLinphoneCore];
[LinphoneManager.instance startLinphoneCore];
}
#pragma mark - Prevent ImagePickerView from rotating
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if ([[(PhoneMainView*)self.window.rootViewController currentView] equal:ImagePickerView.compositeViewDescription])
{
//Prevent rotation of camera
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
return UIInterfaceOrientationMaskPortrait;
}
else return UIInterfaceOrientationMaskAll;
}
@end