forked from rangeles/tgCid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallManager.m
93 lines (76 loc) · 2.95 KB
/
CallManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#import "Debug.h"
#import "CallManager.h"
#import "IncomingCallView.h"
@implementation CallManager
+ (NSURLRequest *)createRequestForNumber:(NSString *)aNumber {
NSString *urlString = [NSString stringWithFormat:@"http://freecnam.org/dip?q=%@", aNumber];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:7];
return urlRequest;
}
+ (CallManager *)sharedManager {
static CallManager *sharedManager;
if (!sharedManager) {
sharedManager = [[CallManager alloc] init];
}
return sharedManager;
}
- (id)init {
if ((self = [super init])) {
resource = [[NSBundle alloc] initWithPath:@"/Library/Application Support/tgCid"];
queue = [[NSOperationQueue alloc] init];
}
return self;
}
- (void)dealloc {
[resource release];
[queue release];
[view release];
[request release];
[super dealloc];
}
- (void)setupForController:(id)aController withCall:(CTCallRef)call {
NSString *address = [CTCallCopyAddress(NULL, call) autorelease];
NSString *countryCode = [CTCallCopyCountryCode(NULL, call) autorelease];
if (address != nil && countryCode != nil && controller == nil) {
// U.S. MCCs = 310-316 (inclusive)
if ([countryCode integerValue] >= 310 && [countryCode integerValue] <= 316) {
request = [CallManager createRequestForNumber:address];
view = [[IncomingCallView alloc] initWithDefaultFrameAndBundle:resource];
controller = aController;
}
}
}
- (void)teardownForController:(id)aController {
if (controller == aController) {
[request release];
[view release];
controller = nil;
request = nil;
view = nil;
}
}
- (void)setForController:(id)aController ABUID:(NSNumber *)abuid {
if (abuid != nil) {
[self teardownForController:aController];
} else if (controller == aController) {
DEBUG_LOG((@"tgCid: Lookup request to be fetched: %@", request));
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *rData, NSError *rError){
NSString *caller = @"Unknown";
if (rData != nil && [rData length] > 0) {
caller = [[[NSString alloc] initWithData:rData encoding:NSUTF8StringEncoding] autorelease];
DEBUG_LOG((@"tgCid: Lookup request returned: %@", caller));
} else if (rError != nil) {
DEBUG_LOG((@"tgCid: Lookup request errored: %@", rError));
}
[view performSelectorOnMainThread:@selector(setCallerAndFinalize:) withObject:caller waitUntilDone:NO];
}];
}
}
- (UIView *)viewForController:(id)aController {
return controller == aController ? view : nil;
}
- (BOOL)hasViewForController:(id)aController {
return [self viewForController:aController] != nil;
}
@end