forked from rpetrich/AppList
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALApplicationList.m
298 lines (263 loc) · 10.9 KB
/
ALApplicationList.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#import "ALApplicationList.h"
#import <ImageIO/ImageIO.h>
#import <UIKit/UIKit.h>
#import <SpringBoard/SpringBoard.h>
#import <CaptainHook/CaptainHook.h>
#import <dlfcn.h>
NSString *const ALIconLoadedNotification = @"ALIconLoadedNotification";
NSString *const ALDisplayIdentifierKey = @"ALDisplayIdentifier";
NSString *const ALIconSizeKey = @"ALIconSize";
enum {
ALMessageIdGetApplications,
ALMessageIdIconForSize
};
static CFMessagePortRef messagePort;
static inline CFDataRef SendMessage(SInt32 messageId, CFDataRef data)
{
if (!CFMessagePortIsValid(messagePort))
messagePort = CFMessagePortCreateRemote(kCFAllocatorDefault, CFSTR("applist.datasource"));
CFDataRef outData = NULL;
CFMessagePortSendRequest(messagePort, messageId, data, 45.0, 45.0, CFSTR("applist.waiting-on-datasource"), &outData);
return outData;
}
@interface SBIconModel ()
- (SBApplicationIcon *)applicationIconForDisplayIdentifier:(NSString *)displayIdentifier;
@end
@interface UIImage (iOS40)
+ (UIImage *)imageWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(int)orientation;
@end
__attribute__((visibility("hidden")))
@interface ALApplicationListImpl : ALApplicationList
@end
static ALApplicationList *sharedApplicationList;
// Can't late-bind and still support iOS3.0 :(
static bool (*_CGImageDestinationFinalize)(CGImageDestinationRef idst);
static CGImageDestinationRef (*_CGImageDestinationCreateWithData)(CFMutableDataRef data, CFStringRef type, size_t count, CFDictionaryRef options);
static void (*_CGImageDestinationAddImage)(CGImageDestinationRef idst, CGImageRef image, CFDictionaryRef properties);
static CGImageSourceRef (*_CGImageSourceCreateWithData)(CFDataRef data, CFDictionaryRef options);
static CGImageRef (*_CGImageSourceCreateImageAtIndex)(CGImageSourceRef isrc, size_t index, CFDictionaryRef options);
@implementation ALApplicationList
+ (ALApplicationList *)sharedApplicationList
{
return sharedApplicationList;
}
- (id)init
{
if ((self = [super init])) {
if (sharedApplicationList) {
[self release];
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Only one instance of ALApplicationList is permitted at a time! Use [ALApplicationList sharedApplicationList] instead." userInfo:nil];
}
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
cachedIcons = [[NSMutableDictionary alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
[pool drain];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[cachedIcons release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
OSSpinLockLock(&spinLock);
[cachedIcons removeAllObjects];
OSSpinLockUnlock(&spinLock);
}
- (NSDictionary *)applications
{
return [self applicationsFilteredUsingPredicate:nil];
}
- (NSDictionary *)applicationsFilteredUsingPredicate:(NSPredicate *)predicate
{
CFDataRef data = SendMessage(ALMessageIdGetApplications, (CFDataRef)[NSKeyedArchiver archivedDataWithRootObject:predicate]);
if (data) {
NSDictionary *result = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
CFRelease(data);
if ([result isKindOfClass:[NSDictionary class]]) {
return result;
}
}
return nil;
}
- (void)postNotificationWithUserInfo:(NSDictionary *)userInfo
{
[[NSNotificationCenter defaultCenter] postNotificationName:ALIconLoadedNotification object:self userInfo:userInfo];
}
- (CGImageRef)copyIconOfSize:(ALApplicationIconSize)iconSize forDisplayIdentifier:(NSString *)displayIdentifier
{
NSString *key = [displayIdentifier stringByAppendingFormat:@"#%f", (CGFloat)iconSize];
OSSpinLockLock(&spinLock);
CGImageRef result = (CGImageRef)[cachedIcons objectForKey:key];
if (result) {
result = CGImageRetain(result);
OSSpinLockUnlock(&spinLock);
return result;
}
OSSpinLockUnlock(&spinLock);
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInteger:iconSize], @"iconSize", displayIdentifier, @"displayIdentifier", nil];
CFDataRef data = SendMessage(ALMessageIdIconForSize, (CFDataRef)[NSPropertyListSerialization dataFromPropertyList:userInfo format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL]);
if (!data)
return NULL;
CGImageSourceRef imageSource = _CGImageSourceCreateWithData(data, NULL);
result = _CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
CFRelease(data);
if (result) {
OSSpinLockLock(&spinLock);
[cachedIcons setObject:(id)result forKey:key];
OSSpinLockUnlock(&spinLock);
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInteger:iconSize], ALIconSizeKey,
displayIdentifier, ALDisplayIdentifierKey,
nil];
if ([NSThread isMainThread])
[self postNotificationWithUserInfo:userInfo];
else
[self performSelectorOnMainThread:@selector(postNotificationWithUserInfo:) withObject:userInfo waitUntilDone:YES];
}
CFRelease(imageSource);
return result;
}
- (UIImage *)iconOfSize:(ALApplicationIconSize)iconSize forDisplayIdentifier:(NSString *)displayIdentifier
{
CGImageRef image = [self copyIconOfSize:iconSize forDisplayIdentifier:displayIdentifier];
if (!image)
return nil;
UIImage *result;
if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) {
CGFloat scale = (CGImageGetWidth(image) + CGImageGetHeight(image)) / (CGFloat)(iconSize + iconSize);
result = [UIImage imageWithCGImage:image scale:scale orientation:0];
} else {
result = [UIImage imageWithCGImage:image];
}
CGImageRelease(image);
return result;
}
- (BOOL)hasCachedIconOfSize:(ALApplicationIconSize)iconSize forDisplayIdentifier:(NSString *)displayIdentifier
{
NSString *key = [displayIdentifier stringByAppendingFormat:@"#%f", (CGFloat)iconSize];
OSSpinLockLock(&spinLock);
id result = [cachedIcons objectForKey:key];
OSSpinLockUnlock(&spinLock);
return result != nil;
}
@end
CHDeclareClass(SBApplicationController);
CHDeclareClass(SBIconModel);
@interface SBIcon ()
- (UIImage *)getIconImage:(NSInteger)sizeIndex;
@end
@implementation ALApplicationListImpl
static CFDataRef messageServerCallback(CFMessagePortRef local, SInt32 messageId, CFDataRef data, void *info)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData *resultData = nil;
switch (messageId) {
case ALMessageIdGetApplications: {
NSPredicate *predicate = [NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)data];
NSDictionary *result = [predicate isKindOfClass:[NSPredicate class]] ? [sharedApplicationList applicationsFilteredUsingPredicate:predicate] : [sharedApplicationList applications];
resultData = [NSPropertyListSerialization dataFromPropertyList:result format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL];
break;
}
case ALMessageIdIconForSize: {
NSDictionary *params = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
CGImageRef result = [sharedApplicationList copyIconOfSize:[[params objectForKey:@"iconSize"] floatValue] forDisplayIdentifier:[params objectForKey:@"displayIdentifier"]];
if (result) {
resultData = [NSMutableData data];
CGImageDestinationRef dest = _CGImageDestinationCreateWithData((CFMutableDataRef)resultData, CFSTR("public.png"), 1, NULL);
_CGImageDestinationAddImage(dest, result, NULL);
CGImageRelease(result);
_CGImageDestinationFinalize(dest);
CFRelease(dest);
}
break;
}
}
if (resultData) {
resultData = [resultData retain];
}
[pool drain];
return (CFDataRef)resultData;
}
- (id)init
{
if ((self = [super init])) {
messagePort = CFMessagePortCreateLocal(kCFAllocatorDefault, CFSTR("applist.datasource"), messageServerCallback, NULL, NULL);
CFRunLoopSourceRef source = CFMessagePortCreateRunLoopSource(kCFAllocatorDefault, messagePort, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
}
return self;
}
- (NSDictionary *)applications
{
NSMutableDictionary *result = [NSMutableDictionary dictionary];
for (SBApplication *app in [CHSharedInstance(SBApplicationController) allApplications])
[result setObject:[app displayName] forKey:[app displayIdentifier]];
return result;
}
- (NSDictionary *)applicationsFilteredUsingPredicate:(NSPredicate *)predicate
{
NSMutableDictionary *result = [NSMutableDictionary dictionary];
NSArray *apps = [CHSharedInstance(SBApplicationController) allApplications];
if (predicate)
apps = [apps filteredArrayUsingPredicate:predicate];
for (SBApplication *app in apps)
[result setObject:[app displayName] forKey:[app displayIdentifier]];
return result;
}
- (CGImageRef)copyIconOfSize:(ALApplicationIconSize)iconSize forDisplayIdentifier:(NSString *)displayIdentifier
{
SBIcon *icon;
SBIconModel *iconModel = CHSharedInstance(SBIconModel);
if ([iconModel respondsToSelector:@selector(applicationIconForDisplayIdentifier:)])
icon = [iconModel applicationIconForDisplayIdentifier:displayIdentifier];
else if ([iconModel respondsToSelector:@selector(iconForDisplayIdentifier:)])
icon = [iconModel iconForDisplayIdentifier:displayIdentifier];
else
return NULL;
BOOL getIconImage = [icon respondsToSelector:@selector(getIconImage:)];
SBApplication *app = [CHSharedInstance(SBApplicationController) applicationWithDisplayIdentifier:displayIdentifier];
UIImage *image;
if (iconSize <= ALApplicationIconSizeSmall) {
image = getIconImage ? [icon getIconImage:0] : [icon smallIcon];
if (image)
goto finish;
if ([app respondsToSelector:@selector(pathForSmallIcon)]) {
image = [UIImage imageWithContentsOfFile:[app pathForSmallIcon]];
if (image)
goto finish;
}
}
image = getIconImage ? [icon getIconImage:(kCFCoreFoundationVersionNumber >= 675.0) ? 2 : 1] : [icon icon];
if (image)
goto finish;
if ([app respondsToSelector:@selector(pathForIcon)])
image = [UIImage imageWithContentsOfFile:[app pathForIcon]];
if (!image)
return NULL;
finish:
return CGImageRetain([image CGImage]);
}
@end
CHConstructor
{
CHAutoreleasePoolForScope();
void *handle = dlopen("/System/Library/Frameworks/ImageIO.framework/ImageIO", RTLD_LAZY) ?: dlopen("/System/Library/PrivateFrameworks/ImageIO.framework/ImageIO", RTLD_LAZY);
if (!handle)
return;
if (CHLoadLateClass(SBIconModel)) {
CHLoadLateClass(SBApplicationController);
_CGImageDestinationCreateWithData = dlsym(handle, "CGImageDestinationCreateWithData");
_CGImageDestinationAddImage = dlsym(handle, "CGImageDestinationAddImage");
_CGImageDestinationFinalize = dlsym(handle, "CGImageDestinationFinalize");
sharedApplicationList = [[ALApplicationListImpl alloc] init];
} else {
_CGImageSourceCreateWithData = dlsym(handle, "CGImageSourceCreateWithData");
_CGImageSourceCreateImageAtIndex = dlsym(handle, "CGImageSourceCreateImageAtIndex");
messagePort = CFMessagePortCreateRemote(kCFAllocatorDefault, CFSTR("applist.datasource"));
sharedApplicationList = [[ALApplicationList alloc] init];
}
}