-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMobileDeviceAccessIPhone.m
178 lines (154 loc) · 4.12 KB
/
MobileDeviceAccessIPhone.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// MobileDeviceAccessIPhone.m
// Apps@HomeII
//
// Created by Jeff Laing on 7/11/09.
// Copyright 2009 Tristero Computer Systems. All rights reserved.
//
#import "MobileDeviceAccessIPhone.h"
static id _notificationHandlers = nil;
// we create an instance of these so that we can dispatch notifications
// to different objects.
@interface _handler : NSObject
{
NSObject *_target;
SEL _selector;
NSMutableDictionary *_dict;
NSString *_notification;
}
@end
@implementation _handler
static
void notificationDispatcher(
CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const void *alwaysZero1,
CFDictionaryRef alwaysZero2)
{
NSMutableDictionary *_notificationHandlers = (NSMutableDictionary*)observer;
if (_notificationHandlers) {
_handler *h = [_notificationHandlers objectForKey:(NSString*)name];
if (h) {
// NSLog(@"send %@ to %@",NSStringFromSelector(h->_selector),h->_target);
[h->_target performSelector:h->_selector];
}
}
}
- (id) initWithTarget:(NSObject*)target
andSelector:(SEL)selector
andNotification:(NSString*)notification
inDict:(NSMutableDictionary*)dict
{
if (self=[super init]) {
_target = target;
_selector = selector;
_notification = [notification copy];
_dict = dict;
[_dict setObject:self forKey:_notification];
CFNotificationCenterAddObserver(
CFNotificationCenterGetDarwinNotifyCenter(),
_dict,
notificationDispatcher,
(CFStringRef)_notification, // eg, @"com.myapp.notification"
NULL,
0 );
}
return self;
}
- (void)dealloc
{
CFNotificationCenterRemoveObserver(
CFNotificationCenterGetDarwinNotifyCenter(),
_dict,
(CFStringRef)_notification, // eg, @"com.myapp.notification"
NULL );
[_notification release];
[super dealloc];
}
@end
@implementation AMNotificationCenter
+ (void)initialize
{
_notificationHandlers = [NSMutableDictionary new];
}
+ (void)send:(SEL)message
to:(id)reciever
for:(NSString*)notification
{
_handler *h = [[_handler alloc]
initWithTarget:reciever
andSelector:message
andNotification:notification
inDict:_notificationHandlers];
// we can release now, the handler is retained by the dictionary
[h release];
}
+ (void)ignore:(NSString*)notification
{
_handler *h = [_notificationHandlers objectForKey:notification];
if (h) {
// removing it from the dictionary will release it, which
// will un-register the observer
[_notificationHandlers removeObject:h];
}
}
/// Send a notification
+ (void)postNotification:(NSString*)notification
{
CFNotificationCenterPostNotification (
CFNotificationCenterGetDarwinNotifyCenter(),
(CFStringRef)notification,
nil,
nil,
YES );
}
@end
@implementation MobileInstallationProxy
// defined in PrivateFrameworks/MobileInstallation.Framework, though not with these arguments
int MobileInstallationBrowse(NSDictionary *options, int (*mibcallback)(NSDictionary *dict, id usercon), id usercon);
static int browse_mibcallback(NSDictionary *dict, id result)
{
NSArray *currentlist = [dict objectForKey:@"CurrentList"];
if (currentlist) {
for (NSDictionary *appinfo in currentlist) {
[(NSMutableArray*)result addObject:[[appinfo copy] autorelease]];
}
}
return 0;
}
+ (id)browse:(NSString*)kind
{
if (!kind) kind=@"Any";
NSMutableArray *result = [NSMutableArray new];
MobileInstallationBrowse(
[NSDictionary dictionaryWithObject:kind forKey:@"ApplicationType"],
&browse_mibcallback,
result);
return [result autorelease];
}
static id foundEntry;
static int lookup_mibcallback(NSDictionary *dict, id soughtEntry)
{
NSArray *currentlist = [dict objectForKey:@"CurrentList"];
if (currentlist) {
for (NSDictionary *appinfo in currentlist) {
if ([[appinfo objectForKey:@"CFBundleIdentifier"] isEqual:soughtEntry]) {
foundEntry = [appinfo copy];
return;
}
}
}
return 0;
}
+ (id)lookup:(NSString*)identifier
{
NSMutableArray *result = [NSMutableArray new];
foundEntry = nil;
MobileInstallationBrowse(
[NSDictionary dictionaryWithObject:@"Any" forKey:@"ApplicationType"],
&lookup_mibcallback,
identifier);
return [foundEntry autorelease];
}
@end