diff --git a/Classes/LinphoneAppDelegate.h b/Classes/LinphoneAppDelegate.h index cf01f937c..f13b8cb84 100644 --- a/Classes/LinphoneAppDelegate.h +++ b/Classes/LinphoneAppDelegate.h @@ -44,6 +44,10 @@ CTCallCenter* callCenter; } +- (void) loadDefaultSettings:(NSDictionary *) appDefaults; +-(void) setupUI; +-(void) setupGSMInteraction; + @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UITabBarController* myTabBarController; @property (nonatomic, retain) ABPeoplePickerNavigationController* myPeoplePickerController; diff --git a/Classes/LinphoneAppDelegate.m b/Classes/LinphoneAppDelegate.m index 8cbd45e2c..6e6653dbc 100644 --- a/Classes/LinphoneAppDelegate.m +++ b/Classes/LinphoneAppDelegate.m @@ -125,7 +125,7 @@ int __aeabi_idiv(int a, int b) { } } -- (void) loadDefaultSettings { +- (void) loadDefaultSettings:(NSDictionary *) appDefaults { NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"]; if(!settingsBundle) { @@ -149,33 +149,15 @@ int __aeabi_idiv(int a, int b) { [defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key]; } } - - NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys: - @"NO", @"enable_first_login_view_preference", // -#ifdef HAVE_AMR - @"YES",@"amr_8k_preference", // enable amr by default if compiled with -#endif -#ifdef HAVE_G729 - @"YES",@"g729_preference", // enable amr by default if compiled with -#endif - @"NO",@"debugenable_preference", - //@"+33",@"countrycode_preference", - nil]; - + [defaultsToRegister addEntriesFromDictionary:appDefaults]; [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister]; [defaultsToRegister release]; [[NSUserDefaults standardUserDefaults] synchronize]; - } -- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ - - /* - *Custumization - */ - [self loadDefaultSettings]; - //as defined in PhoneMainView.xib +-(void) setupUI { + //as defined in PhoneMainView.xib //dialer myPhoneViewController = (PhoneViewController*) [myTabBarController.viewControllers objectAtIndex: DIALER_TAB_INDEX]; myPhoneViewController.myTabBarController = myTabBarController; @@ -217,17 +199,44 @@ int __aeabi_idiv(int a, int b) { [window makeKeyAndVisible]; [[LinphoneManager instance] setCallDelegate:myPhoneViewController]; - [[LinphoneManager instance] startLibLinphone]; - - [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound]; +} +-(void) setupGSMInteraction { callCenter = [[CTCallCenter alloc] init]; callCenter.callEventHandler = ^(CTCall* call) { // post on main thread [self performSelectorOnMainThread:@selector(handleGSMCallInteration:) withObject:callCenter waitUntilDone:YES]; - }; + }; +} + +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ + NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys: + @"NO", @"enable_first_login_view_preference", // +#ifdef HAVE_AMR + @"YES",@"amr_8k_preference", // enable amr by default if compiled with +#endif +#ifdef HAVE_G729 + @"YES",@"g729_preference", // enable amr by default if compiled with +#endif + //@"+33",@"countrycode_preference", + nil]; + + /* explicitely instanciate LinphoneManager */ + LinphoneManager* lm = [[LinphoneManager alloc] init]; + assert(lm == [LinphoneManager instance]); + + [self loadDefaultSettings: appDefaults]; + + [self setupUI]; + + [[LinphoneManager instance] startLibLinphone]; + + [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound]; + + [self setupGSMInteraction]; + return YES; } diff --git a/Classes/LinphoneUI/LinphoneManager.h b/Classes/LinphoneUI/LinphoneManager.h index d31962ddb..f68b7739f 100644 --- a/Classes/LinphoneUI/LinphoneManager.h +++ b/Classes/LinphoneUI/LinphoneManager.h @@ -36,10 +36,16 @@ typedef struct _CallContext { bool_t cameraIsEnabled; } CallContext; +struct NetworkReachabilityContext { + bool_t testWifi, testWWan; + void (*networkStateChanged) (Connectivity newConnectivity); +}; + + @interface LinphoneManager : NSObject { -@private - SCNetworkReachabilityContext proxyReachabilityContext; +@protected SCNetworkReachabilityRef proxyReachability; +@private NSTimer* mIterateTimer; id mLogView; bool isbackgroundModeEnabled; @@ -76,6 +82,7 @@ typedef struct _CallContext { -(UIImage*) getImageFromAddressBook:(NSString*) number; -(BOOL) reconfigureLinphoneIfNeeded:(NSDictionary *)oldSettings; +-(void) setupNetworkReachabilityCallback: (const char*) nodeName withContext:(SCNetworkReachabilityContext*) ctx; @property (nonatomic, retain) id callDelegate; @property (nonatomic, retain) id registrationDelegate; @@ -84,4 +91,3 @@ typedef struct _CallContext { @property (readonly) const char* backCamId; @end - diff --git a/Classes/LinphoneUI/LinphoneManager.m b/Classes/LinphoneUI/LinphoneManager.m index be55db73f..adf367d07 100644 --- a/Classes/LinphoneUI/LinphoneManager.m +++ b/Classes/LinphoneUI/LinphoneManager.m @@ -57,14 +57,16 @@ extern void libmsbcg729_init(); @synthesize backCamId; -(id) init { + assert (!theLinphoneManager); if ((self= [super init])) { mFastAddressBook = [[FastAddressBook alloc] init]; + theLinphoneManager = self; } return self; } +(LinphoneManager*) instance { if (theLinphoneManager==nil) { - theLinphoneManager = [[LinphoneManager alloc] init]; + [[LinphoneManager alloc] init]; } return theLinphoneManager; } @@ -413,16 +415,21 @@ static LinphoneCoreVTable linphonec_vtable = { CFWriteStreamClose (writeStream); } -void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void * info) { +void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* nilCtx) { ms_message("Network connection flag [%x]",flags); - LinphoneManager* lLinphoneMgr = (LinphoneManager*)info; + LinphoneManager* lLinphoneMgr = [LinphoneManager instance]; if ([LinphoneManager getLc] != nil) { + struct NetworkReachabilityContext* ctx = nilCtx ? ((struct NetworkReachabilityContext*)nilCtx) : 0; if ((flags == 0) | (flags & (kSCNetworkReachabilityFlagsConnectionRequired |kSCNetworkReachabilityFlagsConnectionOnTraffic))) { [[LinphoneManager instance] kickOffNetworkConnection]; linphone_core_set_network_reachable([LinphoneManager getLc],false); - ((LinphoneManager*)info).connectivity = none; + lLinphoneMgr.connectivity = none; } else { - Connectivity newConnectivity = flags & kSCNetworkReachabilityFlagsIsWWAN ? wwan:wifi; + Connectivity newConnectivity; + if (!ctx || ctx->testWWan) + newConnectivity = flags & kSCNetworkReachabilityFlagsIsWWAN ? wwan:wifi; + else + newConnectivity = wifi; if (lLinphoneMgr.connectivity == none) { linphone_core_set_network_reachable([LinphoneManager getLc],true); } else if (lLinphoneMgr.connectivity != newConnectivity) { @@ -433,7 +440,9 @@ void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReach lLinphoneMgr.connectivity=newConnectivity; ms_message("new network connectivity of type [%s]",(newConnectivity==wifi?"wifi":"wwan")); } - + if (ctx && ctx->networkStateChanged) { + (*ctx->networkStateChanged)(lLinphoneMgr.connectivity); + } } } @@ -649,7 +658,7 @@ void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReach } bool enableSrtp = [[NSUserDefaults standardUserDefaults] boolForKey:@"enable_srtp_preference"]; linphone_core_set_media_encryption(theLinphoneCore, enableSrtp?LinphoneMediaEncryptionSRTP:LinphoneMediaEncryptionZRTP); - + NSString* stun_server = [[NSUserDefaults standardUserDefaults] stringForKey:@"stun_preference"]; if ([stun_server length]>0){ linphone_core_set_stun_server(theLinphoneCore,[stun_server cStringUsingEncoding:[NSString defaultCStringEncoding]]); @@ -706,7 +715,8 @@ void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReach linphone_core_destroy(theLinphoneCore); theLinphoneCore = nil; SCNetworkReachabilityUnscheduleFromRunLoop(proxyReachability, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); - CFRelease(proxyReachability); + if (proxyReachability) + CFRelease(proxyReachability); proxyReachability=nil; } @@ -761,7 +771,6 @@ void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReach [self destroyLibLinphone]; return NO; } - } @@ -771,7 +780,30 @@ void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReach } - +-(void) setupNetworkReachabilityCallback: (const char*) nodeName withContext:(SCNetworkReachabilityContext*) ctx { + if (proxyReachability) { + ms_message("Cancel old network reachability check"); + SCNetworkReachabilityUnscheduleFromRunLoop(proxyReachability, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); + CFRelease(proxyReachability); + proxyReachability = nil; + } + + proxyReachability = SCNetworkReachabilityCreateWithName(nil, nodeName); + + //initial state is network off should be done as soon as possible + SCNetworkReachabilityFlags flags; + if (!SCNetworkReachabilityGetFlags(proxyReachability, &flags)) { + ms_error("Cannot get reachability flags"); + }; + networkReachabilityCallBack(proxyReachability, flags, ctx ? ctx->info : 0); + + if (!SCNetworkReachabilitySetCallback(proxyReachability, (SCNetworkReachabilityCallBack)networkReachabilityCallBack, ctx)){ + ms_error("Cannot register reachability cb"); + }; + if(!SCNetworkReachabilityScheduleWithRunLoop(proxyReachability, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)){ + ms_error("Cannot register schedule reachability cb"); + }; +} /************* @@ -823,22 +855,8 @@ void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReach linphone_core_set_zrtp_secrets_file(theLinphoneCore, [zrtpSecretsFileName cStringUsingEncoding:[NSString defaultCStringEncoding]]); - proxyReachability=SCNetworkReachabilityCreateWithName(nil, "linphone.org"); - proxyReachabilityContext.info=self; - //initial state is network off should be done as soon as possible - SCNetworkReachabilityFlags flags; - if (!SCNetworkReachabilityGetFlags(proxyReachability, &flags)) { - ms_error("Cannot get reachability flags"); - }; - networkReachabilityCallBack(proxyReachability,flags,self); + [self setupNetworkReachabilityCallback: "linphone.org" withContext:nil]; - if (!SCNetworkReachabilitySetCallback(proxyReachability, (SCNetworkReachabilityCallBack)networkReachabilityCallBack,&proxyReachabilityContext)){ - ms_error("Cannot register reachability cb"); - }; - if(!SCNetworkReachabilityScheduleWithRunLoop(proxyReachability, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)){ - ms_error("Cannot register schedule reachability cb"); - }; - [self reconfigureLinphoneIfNeeded:nil]; // start scheduler