forked from rpetrich/AppList
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALApplicationList.m
255 lines (217 loc) · 8.63 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
#import "ALApplicationList.h"
#import <ImageIO/ImageIO.h>
#import <UIKit/UIKit.h>
#import <SpringBoard/SpringBoard.h>
#import <CaptainHook/CaptainHook.h>
#import <AppSupport/AppSupport.h>
NSString *const ALIconLoadedNotification = @"ALIconLoadedNotification";
NSString *const ALDisplayIdentifierKey = @"ALDisplayIdentifier";
NSString *const ALIconSizeKey = @"ALIconSize";
@interface SBIconModel ()
- (SBApplicationIcon *)applicationIconForDisplayIdentifier:(NSString *)displayIdentifier;
@end
@interface UIImage (iOS40)
+ (UIImage *)imageWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(int)orientation;
@end
@interface ALApplicationList ()
@property (nonatomic, readonly) CPDistributedMessagingCenter *messagingCenter;
@end
@interface ALApplicationListImpl : ALApplicationList {
}
@end
static ALApplicationList *sharedApplicationList;
@implementation ALApplicationList
+ (id)sharedApplicationList
{
return sharedApplicationList;
}
- (id)init
{
if ((self = [super init])) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
messagingCenter = [[CPDistributedMessagingCenter centerNamed:@"applist.springboardCenter"] retain];
cachedIcons = [[NSMutableDictionary alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
[pool drain];
}
return self;
}
@synthesize messagingCenter;
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[cachedIcons release];
[messagingCenter release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
OSSpinLockLock(&spinLock);
[cachedIcons removeAllObjects];
OSSpinLockUnlock(&spinLock);
}
- (NSDictionary *)applications
{
return [messagingCenter sendMessageAndReceiveReplyName:@"applications" userInfo:nil];
}
- (NSDictionary *)applicationsFilteredUsingPredicate:(NSPredicate *)predicate
{
if (!predicate)
return [self applications];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:predicate];
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:data forKey:@"predicate"];
return [messagingCenter sendMessageAndReceiveReplyName:@"_remoteApplicationsFilteredForMessage:userInfo:" userInfo:userInfo];
}
- (void)postNotificationWithUserInfo:(NSDictionary *)userInfo
{
[[NSNotificationCenter defaultCenter] postNotificationName:ALIconLoadedNotification object:self userInfo:userInfo];
}
- (CGImageRef)copyIconOfSize:(ALApplicationIconSize)iconSize forDisplayIdentifier:(NSString *)displayIdentifier
{
NSString *key = [displayIdentifier stringByAppendingFormat:@"#%f", 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];
NSDictionary *serialized = [messagingCenter sendMessageAndReceiveReplyName:@"_remoteGetIconForMessage:userInfo:" userInfo:userInfo];
NSData *data = [serialized objectForKey:@"result"];
if (!data)
return NULL;
CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)data, NULL);
result = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
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", 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
- (id)init
{
if ((self = [super init])) {
CPDistributedMessagingCenter *center = [self messagingCenter];
[center runServerOnCurrentThread];
[center registerForMessageName:@"applications" target:self selector:@selector(applications)];
[center registerForMessageName:@"_remoteApplicationsFilteredForMessage:userInfo:" target:self selector:@selector(_remoteApplicationsFilteredForMessage:userInfo:)];
[center registerForMessageName:@"_remoteGetIconForMessage:userInfo:" target:self selector:@selector(_remoteGetIconForMessage:userInfo:)];
}
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 *)_remoteApplicationsFilteredForMessage:(NSString *)message userInfo:(NSDictionary *)userInfo
{
NSPredicate *predicate = [NSKeyedUnarchiver unarchiveObjectWithData:[userInfo objectForKey:@"predicate"]];
return [self applicationsFilteredUsingPredicate:predicate];
}
- (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;
}
- (NSDictionary *)_remoteGetIconForMessage:(NSString *)message userInfo:(NSDictionary *)userInfo
{
CGImageRef image = [self copyIconOfSize:[[userInfo objectForKey:@"iconSize"] unsignedIntegerValue] forDisplayIdentifier:[userInfo objectForKey:@"displayIdentifier"]];
if (!image)
return [NSDictionary dictionary];
NSMutableData *result = [NSMutableData data];
CGImageDestinationRef dest = CGImageDestinationCreateWithData((CFMutableDataRef)result, CFSTR("public.png"), 1, NULL);
CGImageDestinationAddImage(dest, image, NULL);
CGImageRelease(image);
CGImageDestinationFinalize(dest);
CFRelease(dest);
return [NSDictionary dictionaryWithObject:result forKey:@"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;
image = [UIImage imageWithContentsOfFile:[app pathForSmallIcon]];
if (image)
goto finish;
}
image = getIconImage ? [icon getIconImage:1] : [icon icon];
if (image)
goto finish;
image = [UIImage imageWithContentsOfFile:[app pathForIcon]];
finish:
return CGImageRetain([image CGImage]);
}
@end
CHConstructor
{
CHAutoreleasePoolForScope();
if (CHLoadLateClass(SBIconModel)) {
CHLoadLateClass(SBApplicationController);
sharedApplicationList = [[ALApplicationListImpl alloc] init];
} else {
sharedApplicationList = [[ALApplicationList alloc] init];
}
}