better management of push notif mode versus bg mode/voip socket

This commit is contained in:
Jehan Monnier 2014-02-18 15:48:03 +01:00
parent 613b163b82
commit 0f08dc7f55
2 changed files with 69 additions and 56 deletions

View file

@ -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];
}

View file

@ -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 {