add display name lookup into locakl address book

This commit is contained in:
Jehan Monnier 2011-05-26 17:31:54 +02:00
parent 0e0918acac
commit ba8e0e9902
5 changed files with 96 additions and 8 deletions

View file

@ -67,6 +67,7 @@
#ifdef HAVE_AMR
@"YES",@"amr_8k_preference", // enable amr by default if compiled with
#endif
//@"+33",@"countrycode_preference",
nil];
[defaultsToRegister addEntriesFromDictionary:appDefaults];

View file

@ -54,6 +54,7 @@ typedef enum _Connectivity {
-(void) enterBackgroundMode;
-(void) becomeActive;
-(void) kickOffNetworkConnection;
-(NSString*) getDisplayNameFromAddressBook:(NSString*) number andUpdateCallLog:(LinphoneCallLog*)log;
@property (nonatomic, retain) id<LinphoneUICallDelegate> callDelegate;
@property (nonatomic, retain) id<LinphoneUIRegistrationDelegate> registrationDelegate;

View file

@ -25,6 +25,8 @@
#include <netdb.h>
#import <AVFoundation/AVAudioSession.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AddressBook/AddressBook.h>
static LinphoneCore* theLinphoneCore=nil;
static LinphoneManager* theLinphoneManager=nil;
@ -44,11 +46,87 @@ extern void libmsamr_init();
}
return theLinphoneManager;
}
-(NSString*) appendCountryCodeIfPossible:(NSString*) number {
if (![number hasPrefix:@"+"] && ![number hasPrefix:@"00"]) {
NSString* lCountryCode = [[NSUserDefaults standardUserDefaults] stringForKey:@"countrycode_preference"];
if (lCountryCode && [lCountryCode length]>0) {
//append country code
return [lCountryCode stringByAppendingString:number];
}
}
return number;
}
-(NSString*) getDisplayNameFromAddressBook:(NSString*) number andUpdateCallLog:(LinphoneCallLog*)log {
ABAddressBookRef lAddressBook = ABAddressBookCreate();
NSArray *lContacts = (NSArray *)ABAddressBookCopyArrayOfAllPeople(lAddressBook);
for (id lContact in lContacts) {
ABMutableMultiValueRef lPhoneNumbers = ABRecordCopyValue((ABRecordRef)lContact, kABPersonPhoneProperty);
for ( int i=0; i<ABMultiValueGetCount(lPhoneNumbers); i++) {
CFStringRef lLabel = ABMultiValueCopyLabelAtIndex(lPhoneNumbers,i);
CFStringRef lValue = ABMultiValueCopyValueAtIndex(lPhoneNumbers,i);
CFStringRef lLocalizedLabel = ABAddressBookCopyLocalizedLabel(lLabel);
NSString* lNormalizedNumber = [(NSString*)lValue stringByReplacingOccurrencesOfString:@" " withString:@""];
lNormalizedNumber = [lNormalizedNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
lNormalizedNumber = [lNormalizedNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
lNormalizedNumber = [lNormalizedNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
lNormalizedNumber = [self appendCountryCodeIfPossible:lNormalizedNumber];
number = [self appendCountryCodeIfPossible:number];
if([lNormalizedNumber isEqualToString:number]) {
CFStringRef lDisplayName = ABRecordCopyCompositeName(lContact);
if (log) {
//add phone type
char ltmpString[256];
CFStringRef lFormatedString = CFStringCreateWithFormat(NULL,NULL,CFSTR("phone_type:%@;"),lLocalizedLabel);
CFStringGetCString(lFormatedString, ltmpString,sizeof(ltmpString), kCFStringEncodingUTF8);
linphone_call_log_set_ref_key(log, ltmpString);
CFRelease(lFormatedString);
}
return (NSString*)lDisplayName;
}
CFRelease(lLabel);
CFRelease(lValue);
CFRelease(lLocalizedLabel);
}
}
return nil;
}
-(void) updateCallWithAddressBookData:(LinphoneCall*) call {
//1 copy adress book
LinphoneCallLog* lLog = linphone_call_get_call_log(call);
LinphoneAddress* lAddress;
if (lLog->dir == LinphoneCallIncoming) {
lAddress=lLog->from;
} else {
lAddress=lLog->to;
}
const char* lUserName = linphone_address_get_username(lAddress);
if (!lUserName) {
//just return
return;
}
NSString* lE164Number = [[NSString alloc] initWithCString:lUserName encoding:[NSString defaultCStringEncoding]];
NSString* lDisplayName = [self getDisplayNameFromAddressBook:lE164Number andUpdateCallLog:lLog];
if(lDisplayName) {
linphone_address_set_display_name(lAddress, [lDisplayName cStringUsingEncoding:[NSString defaultCStringEncoding]]);
} else {
ms_message("No contact entry found for [%s] in address book",lUserName);
}
return;
}
-(void) onCall:(LinphoneCall*) currentCall StateChanged: (LinphoneCallState) new_state withMessage: (const char *) message {
const char* lUserNameChars=linphone_address_get_username(linphone_call_get_remote_address(currentCall));
NSString* lUserName = lUserNameChars?[[NSString alloc] initWithCString:lUserNameChars]:NSLocalizedString(@"Unknown",nil);
const char* lDisplayNameChars = linphone_address_get_display_name(linphone_call_get_remote_address(currentCall));
const char* lUserNameChars=linphone_address_get_username(linphone_call_get_remote_address(currentCall));
NSString* lUserName = lUserNameChars?[[NSString alloc] initWithCString:lUserNameChars]:NSLocalizedString(@"Unknown",nil);
if (new_state == LinphoneCallIncomingReceived) {
[self updateCallWithAddressBookData:currentCall]; // display name is updated
}
const char* lDisplayNameChars = linphone_address_get_display_name(linphone_call_get_remote_address(currentCall));
NSString* lDisplayName = lDisplayNameChars?[[NSString alloc] initWithCString:lDisplayNameChars]:@"";
switch (new_state) {
@ -628,8 +706,7 @@ void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReach
}
-(void) becomeActive {
/*IOS specific*/
linphone_core_start_dtmf_stream(theLinphoneCore);
if (theLinphoneCore == nil) {
//back from standby and background mode is disabled
[self startLibLinphone];
@ -638,7 +715,9 @@ void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReach
linphone_core_refresh_registers(theLinphoneCore);//just to make sure REGISTRATION is up to date
}
/*IOS specific*/
linphone_core_start_dtmf_stream(theLinphoneCore);
LCSipTransports transportValue;
if (linphone_core_get_sip_transports(theLinphoneCore, &transportValue)) {
ms_error("cannot get current transport");
@ -662,4 +741,5 @@ void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReach
-(void) registerLogView:(id<LogView>) view {
mLogView = view;
}
@end

View file

@ -51,6 +51,12 @@
} else {
char normalizedUserName[256];
NSString* toUserName = [NSString stringWithString:[mAddress text]];
if ([mDisplayName.text length] <=0) {
NSString* lDisplayName = [[LinphoneManager instance] getDisplayNameFromAddressBook:toUserName andUpdateCallLog:nil];
if (lDisplayName) {
mDisplayName.text = lDisplayName;
}
}
linphone_proxy_config_normalize_number(proxyCfg,[toUserName cStringUsingEncoding:[NSString defaultCStringEncoding]],normalizedUserName,sizeof(normalizedUserName));
LinphoneAddress* tmpAddress = linphone_address_new(linphone_core_get_identity([LinphoneManager getLc]));
linphone_address_set_username(tmpAddress,normalizedUserName);

View file

@ -244,14 +244,14 @@
if (notif)
{
notif.repeatInterval = 0;
notif.alertBody =[NSString stringWithFormat:NSLocalizedString(@" %@ is calling you",nil),username];
notif.alertBody =[NSString stringWithFormat:NSLocalizedString(@" %@ is calling you",nil),displayName?displayName:username];
notif.alertAction = @"Answer";
notif.soundName = @"oldphone-mono-30s.caf";
[[UIApplication sharedApplication] presentLocalNotificationNow:notif];
}
} else {
mIncomingCallActionSheet = [[UIActionSheet alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@" %@ is calling you",nil),username]
mIncomingCallActionSheet = [[UIActionSheet alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@" %@ is calling you",nil),displayName?displayName:username]
delegate:self
cancelButtonTitle:NSLocalizedString(@"Decline",nil)
destructiveButtonTitle:NSLocalizedString(@"Answer",nil)