From 0f08dc7f55880e602224e367802c5e24b206dcae Mon Sep 17 00:00:00 2001 From: Jehan Monnier Date: Tue, 18 Feb 2014 15:48:03 +0100 Subject: [PATCH] better management of push notif mode versus bg mode/voip socket --- Classes/LinphoneAppDelegate.m | 16 ++--- Classes/LinphoneManager.m | 109 ++++++++++++++++++++-------------- 2 files changed, 69 insertions(+), 56 deletions(-) diff --git a/Classes/LinphoneAppDelegate.m b/Classes/LinphoneAppDelegate.m index f42b55f08..28daf08e5 100644 --- a/Classes/LinphoneAppDelegate.m +++ b/Classes/LinphoneAppDelegate.m @@ -60,10 +60,6 @@ - (void)applicationDidEnterBackground:(UIApplication *)application{ [LinphoneLogger logc:LinphoneLoggerLog format:"applicationDidEnterBackground"]; -#ifdef DEBUG_PUSH - // simulate a lost socket when going in background - linphone_core_set_network_reachable([LinphoneManager getLc], FALSE); -#endif if(![LinphoneManager isLcReady]) return; [[LinphoneManager instance] enterBackgroundMode]; } @@ -101,10 +97,6 @@ [instance becomeActive]; -#ifdef DEBUG_PUSH - linphone_core_set_network_reachable([LinphoneManager getLc], FALSE); -#endif - LinphoneCore* lc = [LinphoneManager getLc]; LinphoneCall* call = linphone_core_get_current_call(lc); @@ -216,8 +208,9 @@ /*if we receive a remote notification, it is 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*/ LinphoneCore *lc = [LinphoneManager getLc]; - linphone_core_set_network_reachable(lc, FALSE); - linphone_core_set_network_reachable(lc, TRUE); + linphone_core_set_network_reachable([LinphoneManager getLc], FALSE); + [LinphoneManager instance].connectivity=none; /*force connectivity to be discovered again*/ + //linphone_core_set_network_reachable([LinphoneManager getLc], TRUE); if(loc_key != nil) { if([loc_key isEqualToString:@"IM_MSG"]) { [[PhoneMainView instance] addInhibitedEvent:kLinphoneTextReceived]; @@ -280,7 +273,8 @@ // Force Linphone to drop the current socket, this will trigger a refresh registers linphone_core_set_network_reachable([LinphoneManager getLc], FALSE); - linphone_core_set_network_reachable([LinphoneManager getLc], TRUE); + lm.connectivity=none; /*force connectivity to be discovered again*/ + //linphone_core_set_network_reachable([LinphoneManager getLc], TRUE); [lm refreshRegisters]; } diff --git a/Classes/LinphoneManager.m b/Classes/LinphoneManager.m index ec055ff1f..f02444f81 100644 --- a/Classes/LinphoneManager.m +++ b/Classes/LinphoneManager.m @@ -414,7 +414,7 @@ static void linphone_iphone_display_status(struct _LinphoneCore * lc, const char // we were woken up by a silent push. Call the completion handler with NEWDATA // so that the push is notified to the user - NSLog(@"onCall - handler %p", silentPushCompletion); + [LinphoneLogger log:LinphoneLoggerLog format:@"onCall - handler %p", silentPushCompletion]; silentPushCompletion(UIBackgroundFetchResultNewData); silentPushCompletion = nil; } @@ -1093,55 +1093,74 @@ static int comp_call_state_paused (const LinphoneCall* call, const void* param) } - (BOOL)enterBackgroundMode { LinphoneProxyConfig* proxyCfg; - linphone_core_get_default_proxy(theLinphoneCore, &proxyCfg); + linphone_core_get_default_proxy(theLinphoneCore, &proxyCfg); + BOOL shouldEnterBgMode=FALSE; - - if ((proxyCfg || linphone_core_get_calls_nb(theLinphoneCore) > 0) && - [[NSUserDefaults standardUserDefaults] boolForKey:@"backgroundmode_preference"]) { - - if(proxyCfg != NULL) { - //For registration register - [self refreshRegisters]; - //wait for registration answer - int i=0; - while (!linphone_proxy_config_is_registered(proxyCfg) && i++<40 ) { - linphone_core_iterate(theLinphoneCore); - usleep(100000); - } - } - //register keepalive - if ([[UIApplication sharedApplication] setKeepAliveTimeout:600/*(NSTimeInterval)linphone_proxy_config_get_expires(proxyCfg)*/ - handler:^{ - [LinphoneLogger logc:LinphoneLoggerWarning format:"keepalive handler"]; - if (theLinphoneCore == nil) { - [LinphoneLogger logc:LinphoneLoggerWarning format:"It seems that Linphone BG mode was deactivated, just skipping"]; - return; + //handle proxy config if any + if (proxyCfg) { + if ([[LinphoneManager instance] lpConfigBoolForKey:@"backgroundmode_preference"] + || + [[LinphoneManager instance] lpConfigBoolForKey:@"pushnotification_preference"]) { + + //For registration register + [self refreshRegisters]; + //wait for registration answer + int i=0; + while (!linphone_proxy_config_is_registered(proxyCfg) && i++<40 ) { + linphone_core_iterate(theLinphoneCore); + usleep(100000); + } + } + + if ([[LinphoneManager instance] lpConfigBoolForKey:@"backgroundmode_preference"]) { + + //register keepalive + if ([[UIApplication sharedApplication] setKeepAliveTimeout:600/*(NSTimeInterval)linphone_proxy_config_get_expires(proxyCfg)*/ + handler:^{ + [LinphoneLogger logc:LinphoneLoggerWarning format:"keepalive handler"]; + if (theLinphoneCore == nil) { + [LinphoneLogger logc:LinphoneLoggerWarning format:"It seems that Linphone BG mode was deactivated, just skipping"]; + return; + } + //kick up network cnx, just in case + [self refreshRegisters]; + linphone_core_iterate(theLinphoneCore); } - //kick up network cnx, just in case - [self refreshRegisters]; - linphone_core_iterate(theLinphoneCore); - } - ]) { - - - [LinphoneLogger logc:LinphoneLoggerLog format:"keepalive handler succesfully registered"]; - } else { - [LinphoneLogger logc:LinphoneLoggerLog format:"keepalive handler cannot be registered"]; + ]) { + + + [LinphoneLogger logc:LinphoneLoggerLog format:"keepalive handler succesfully registered"]; + } else { + [LinphoneLogger logc:LinphoneLoggerLog format:"keepalive handler cannot be registered"]; + } + shouldEnterBgMode=TRUE; } - LinphoneCall* currentCall = linphone_core_get_current_call(theLinphoneCore); - const MSList* callList = linphone_core_get_calls(theLinphoneCore); - if (!currentCall //no active call - && callList // at least one call in a non active state - && ms_list_find_custom((MSList*)callList, (MSCompareFunc) comp_call_state_paused, NULL)) { - [self startCallPausedLongRunningTask]; - } - return YES; } - else { - [LinphoneLogger logc:LinphoneLoggerLog format:"Entering lite bg mode"]; + + LinphoneCall* currentCall = linphone_core_get_current_call(theLinphoneCore); + const MSList* callList = linphone_core_get_calls(theLinphoneCore); + if (!currentCall //no active call + && callList // at least one call in a non active state + && ms_list_find_custom((MSList*)callList, (MSCompareFunc) comp_call_state_paused, NULL)) { + [self startCallPausedLongRunningTask]; + shouldEnterBgMode=TRUE; + } + + [LinphoneLogger logc:LinphoneLoggerLog format:"Entering [%s] bg mode",shouldEnterBgMode?"normal":"lite"]; + + if (!shouldEnterBgMode ) { + if([[LinphoneManager instance] lpConfigBoolForKey:@"pushnotification_preference"]) { + [LinphoneLogger logc:LinphoneLoggerLog format:"Keeping lc core to handle push"]; + /*destroy voip socket if any and reset connectivity mode*/ + connectivity=none; + linphone_core_set_network_reachable(theLinphoneCore, FALSE); + return YES; + } [self destroyLibLinphone]; - return NO; - } + return NO; + + } else + return YES; } - (void)becomeActive {