-
Notifications
You must be signed in to change notification settings - Fork 74
/
ALApplicationList.x
750 lines (679 loc) · 25.2 KB
/
ALApplicationList.x
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
#import "ALApplicationList-private.h"
#import <ImageIO/ImageIO.h>
#import <UIKit/UIKit.h>
#import <CaptainHook/CaptainHook.h>
#import <dlfcn.h>
#define ROCKETBOOTSTRAP_LOAD_DYNAMIC
#import "LightMessaging/LightMessaging.h"
#import "unfair_lock.h"
#import "SpringBoard.h"
@interface UIImage (Private)
+ (UIImage *)_applicationIconImageForBundleIdentifier:(NSString *)bundleIdentifier format:(int)format scale:(CGFloat)scale;
+ (UIImage *)_applicationIconImageForBundleIdentifier:(NSString *)bundleIdentifier roleIdentifier:(NSString *)roleIdentifier format:(int)format scale:(CGFloat)scale;
@end
NSString *const ALIconLoadedNotification = @"ALIconLoadedNotification";
NSString *const ALDisplayIdentifierKey = @"ALDisplayIdentifier";
NSString *const ALIconSizeKey = @"ALIconSize";
enum {
ALMessageIdGetApplications,
ALMessageIdIconForSize,
ALMessageIdValueForKey,
ALMessageIdValueForKeyPath,
ALMessageIdGetApplicationCount,
ALMessageIdGetVisibleApplications,
ALMessageIdApplicationIsHidden,
};
static LMConnection connection = {
MACH_PORT_NULL,
"applist.datasource"
};
static NSMutableDictionary *cachedIcons;
static unfair_lock spinLock;
__attribute__((visibility("hidden")))
@interface ALApplicationListImpl : ALApplicationList
@end
static ALApplicationList *sharedApplicationList;
typedef enum {
LADirectAPINone,
LADirectAPIApplicationIconImageForBundleIdentifier,
LADirectAPIApplicationIconImageForBundleIdentifierRoleIdentifier,
} LADirectAPI;
static LADirectAPI supportedDirectAPI;
@implementation ALApplicationList
+ (void)initialize
{
if (self == [ALApplicationList class] && !%c(SBIconModel)) {
sharedApplicationList = [[self alloc] init];
}
}
+ (ALApplicationList *)sharedApplicationList
{
return sharedApplicationList;
}
extern CFTypeRef MGCopyAnswer(CFStringRef query) __attribute__((weak_import));
static BOOL IsIpad(void)
{
BOOL result = NO;
if (&MGCopyAnswer != NULL) {
CFNumberRef answer = MGCopyAnswer(CFSTR("ipad"));
if (answer) {
result = [(id)answer boolValue];
CFRelease(answer);
}
}
return result;
}
- (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];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
[pool drain];
if ([UIImage respondsToSelector:@selector(_applicationIconImageForBundleIdentifier:format:scale:)]) {
// Workaround iOS 7's fake retina mode bugs on iPad
if ((kCFCoreFoundationVersionNumber < 800.00) || ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) || !IsIpad())
supportedDirectAPI = LADirectAPIApplicationIconImageForBundleIdentifier;
} else if ([UIImage respondsToSelector:@selector(_applicationIconImageForBundleIdentifier:roleIdentifier:format:scale:)]) {
supportedDirectAPI = LADirectAPIApplicationIconImageForBundleIdentifierRoleIdentifier;
}
}
return self;
}
#if 0
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[cachedIcons release];
[super dealloc];
}
#endif
- (NSInteger)applicationCount
{
LMResponseBuffer buffer;
if (LMConnectionSendTwoWay(&connection, ALMessageIdGetApplicationCount, NULL, 0, &buffer))
return 0;
return LMResponseConsumeInteger(&buffer);
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<ALApplicationList: %p applicationCount=%ld>", self, (long)self.applicationCount];
}
- (void)didReceiveMemoryWarning
{
unfair_lock_lock(&spinLock);
NSDictionary *oldCachedIcons = cachedIcons;
cachedIcons = nil;
unfair_lock_unlock(&spinLock);
[oldCachedIcons release];
}
- (NSDictionary *)applications
{
return [self applicationsFilteredUsingPredicate:nil];
}
- (NSDictionary *)applicationsFilteredUsingPredicate:(NSPredicate *)predicate
{
LMResponseBuffer buffer;
if (LMConnectionSendTwoWayData(&connection, ALMessageIdGetApplications, (CFDataRef)[NSKeyedArchiver archivedDataWithRootObject:predicate], &buffer))
return nil;
id result = LMResponseConsumePropertyList(&buffer);
return [result isKindOfClass:[NSDictionary class]] ? result : nil;
}
static NSInteger DictionaryTextComparator(id a, id b, void *context)
{
return [[(NSDictionary *)context objectForKey:a] localizedCaseInsensitiveCompare:[(NSDictionary *)context objectForKey:b]];
}
- (NSDictionary *)applicationsFilteredUsingPredicate:(NSPredicate *)predicate onlyVisible:(BOOL)onlyVisible titleSortedIdentifiers:(NSArray **)outSortedByTitle
{
LMResponseBuffer buffer;
if (LMConnectionSendTwoWayData(&connection, onlyVisible ? ALMessageIdGetVisibleApplications : ALMessageIdGetApplications, (CFDataRef)[NSKeyedArchiver archivedDataWithRootObject:predicate], &buffer))
return nil;
NSDictionary *result = LMResponseConsumePropertyList(&buffer);
if (![result isKindOfClass:[NSDictionary class]])
return nil;
if (outSortedByTitle) {
// Generate a sorted list of apps
*outSortedByTitle = [[result allKeys] sortedArrayUsingFunction:DictionaryTextComparator context:result];
}
return result;
}
- (id)valueForKeyPath:(NSString *)keyPath forDisplayIdentifier:(NSString *)displayIdentifier
{
if (!keyPath || !displayIdentifier)
return nil;
LMResponseBuffer buffer;
if (LMConnectionSendTwoWayPropertyList(&connection, ALMessageIdValueForKeyPath, [NSDictionary dictionaryWithObjectsAndKeys:keyPath, @"key", displayIdentifier, @"displayIdentifier", nil], &buffer))
return nil;
return LMResponseConsumePropertyList(&buffer);
}
- (id)valueForKey:(NSString *)key forDisplayIdentifier:(NSString *)displayIdentifier
{
if (!key || !displayIdentifier)
return nil;
LMResponseBuffer buffer;
if (LMConnectionSendTwoWayPropertyList(&connection, ALMessageIdValueForKey, [NSDictionary dictionaryWithObjectsAndKeys:key, @"key", displayIdentifier, @"displayIdentifier", nil], &buffer))
return nil;
return LMResponseConsumePropertyList(&buffer);
}
- (BOOL)applicationWithDisplayIdentifierIsHidden:(NSString *)displayIdentifier
{
if (!displayIdentifier)
return NO;
LMResponseBuffer buffer;
if (LMConnectionSendTwoWayData(&connection, ALMessageIdApplicationIsHidden, (CFDataRef)[displayIdentifier dataUsingEncoding:NSUTF8StringEncoding], &buffer))
return NO;
return LMResponseConsumeInteger(&buffer) != 0;
}
- (void)postNotificationWithUserInfo:(NSDictionary *)userInfo
{
[[NSNotificationCenter defaultCenter] postNotificationName:ALIconLoadedNotification object:self userInfo:userInfo];
}
- (CGImageRef)copyIconOfSize:(ALApplicationIconSize)iconSize forDisplayIdentifier:(NSString *)displayIdentifier
{
if (iconSize <= 0)
return NULL;
NSString *key = [displayIdentifier stringByAppendingFormat:@"#%f", (CGFloat)iconSize];
unfair_lock_lock(&spinLock);
CGImageRef result = (CGImageRef)[cachedIcons objectForKey:key];
if (result) {
result = CGImageRetain(result);
unfair_lock_unlock(&spinLock);
return result;
}
unfair_lock_unlock(&spinLock);
if (iconSize == ALApplicationIconSizeSmall) {
switch (supportedDirectAPI) {
case LADirectAPINone:
break;
case LADirectAPIApplicationIconImageForBundleIdentifier:
result = [UIImage _applicationIconImageForBundleIdentifier:displayIdentifier format:0 scale:[UIScreen mainScreen].scale].CGImage;
if (result)
goto skip;
break;
case LADirectAPIApplicationIconImageForBundleIdentifierRoleIdentifier:
result = [UIImage _applicationIconImageForBundleIdentifier:displayIdentifier roleIdentifier:nil format:0 scale:[UIScreen mainScreen].scale].CGImage;
if (result)
goto skip;
break;
}
}
LMResponseBuffer buffer;
if (LMConnectionSendTwoWayPropertyList(&connection, ALMessageIdIconForSize, [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInteger:iconSize], @"iconSize", displayIdentifier, @"displayIdentifier", nil], &buffer))
return NULL;
result = [LMResponseConsumeImage(&buffer) CGImage];
if (!result)
return NULL;
skip:
unfair_lock_lock(&spinLock);
if (!cachedIcons) {
cachedIcons = [[NSMutableDictionary alloc] init];
}
[cachedIcons setObject:(id)result forKey:key];
unfair_lock_unlock(&spinLock);
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInteger:iconSize], ALIconSizeKey,
displayIdentifier, ALDisplayIdentifierKey,
nil];
if ([NSThread isMainThread])
[self performSelector:@selector(postNotificationWithUserInfo:) withObject:userInfo afterDelay:0.0];
else
[self performSelectorOnMainThread:@selector(postNotificationWithUserInfo:) withObject:userInfo waitUntilDone:NO];
return CGImageRetain(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];
unfair_lock_lock(&spinLock);
id result = [cachedIcons objectForKey:key];
unfair_lock_unlock(&spinLock);
return result != nil;
}
@end
@implementation ALApplicationListImpl
static void processMessage(SInt32 messageId, mach_port_t replyPort, CFDataRef data)
{
switch (messageId) {
case ALMessageIdGetApplications: {
NSDictionary *result;
if (data && CFDataGetLength(data)) {
NSPredicate *predicate = [NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)data];
@try {
result = [predicate isKindOfClass:[NSPredicate class]] ? [sharedApplicationList applicationsFilteredUsingPredicate:predicate] : [sharedApplicationList applications];
}
@catch (NSException *exception) {
NSLog(@"AppList: In call to applicationsFilteredUsingPredicate:%@ trapped %@", predicate, exception);
break;
}
} else {
result = [sharedApplicationList applications];
}
LMSendPropertyListReply(replyPort, result);
return;
}
case ALMessageIdGetVisibleApplications: {
NSDictionary *result;
if (data && CFDataGetLength(data)) {
NSPredicate *predicate = [NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)data];
@try {
result = [predicate isKindOfClass:[NSPredicate class]] ? [sharedApplicationList applicationsFilteredUsingPredicate:predicate onlyVisible:YES titleSortedIdentifiers:NULL] : [sharedApplicationList applications];
}
@catch (NSException *exception) {
NSLog(@"AppList: In call to applicationsFilteredUsingPredicate:%@ onlyVisible:YES titleSortedIdentifiers:NULL trapped %@", predicate, exception);
break;
}
} else {
result = [sharedApplicationList applications];
}
LMSendPropertyListReply(replyPort, result);
return;
}
case ALMessageIdIconForSize: {
if (!data)
break;
NSDictionary *params = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
if (![params isKindOfClass:[NSDictionary class]])
break;
id iconSize = [params objectForKey:@"iconSize"];
if (![iconSize respondsToSelector:@selector(floatValue)])
break;
NSString *displayIdentifier = [params objectForKey:@"displayIdentifier"];
if (![displayIdentifier isKindOfClass:[NSString class]])
break;
CGImageRef result = [sharedApplicationList copyIconOfSize:[iconSize floatValue] forDisplayIdentifier:displayIdentifier];
if (result) {
LMSendImageReply(replyPort, [UIImage imageWithCGImage:result]);
CGImageRelease(result);
return;
}
break;
}
case ALMessageIdValueForKeyPath:
case ALMessageIdValueForKey: {
if (!data)
break;
NSDictionary *params = [NSPropertyListSerialization propertyListFromData:(NSData *)data mutabilityOption:0 format:NULL errorDescription:NULL];
if (![params isKindOfClass:[NSDictionary class]])
break;
NSString *key = [params objectForKey:@"key"];
Class stringClass = [NSString class];
if (![key isKindOfClass:stringClass])
break;
NSString *displayIdentifier = [params objectForKey:@"displayIdentifier"];
if (![displayIdentifier isKindOfClass:stringClass])
break;
id result;
@try {
result = messageId == ALMessageIdValueForKeyPath ? [sharedApplicationList valueForKeyPath:key forDisplayIdentifier:displayIdentifier] : [sharedApplicationList valueForKey:key forDisplayIdentifier:displayIdentifier];
}
@catch (NSException *exception) {
NSLog(@"AppList: In call to valueForKey%s:%@ forDisplayIdentifier:%@ trapped %@", messageId == ALMessageIdValueForKeyPath ? "Path" : "", key, displayIdentifier, exception);
break;
}
LMSendPropertyListReply(replyPort, result);
return;
}
case ALMessageIdGetApplicationCount: {
LMSendIntegerReply(replyPort, [sharedApplicationList applicationCount]);
return;
}
case ALMessageIdApplicationIsHidden: {
LMSendIntegerReply(replyPort, [sharedApplicationList applicationWithDisplayIdentifierIsHidden:[[[NSString alloc] initWithData:(NSData *)data encoding:NSUTF8StringEncoding] autorelease]]);
return;
}
}
LMSendReply(replyPort, NULL, 0);
}
static void machPortCallback(CFMachPortRef port, void *bytes, CFIndex size, void *info)
{
LMMessage *request = bytes;
if (size < sizeof(LMMessage)) {
LMSendReply(request->head.msgh_remote_port, NULL, 0);
LMResponseBufferFree(bytes);
return;
}
// Send Response
const void *data = LMMessageGetData(request);
size_t length = LMMessageGetDataLength(request);
mach_port_t replyPort = request->head.msgh_remote_port;
CFDataRef cfdata = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, data ?: &data, length, kCFAllocatorNull);
processMessage(request->head.msgh_id, replyPort, cfdata);
if (cfdata)
CFRelease(cfdata);
LMResponseBufferFree(bytes);
}
- (id)init
{
if ((self = [super init])) {
kern_return_t err = LMStartService(connection.serverName, CFRunLoopGetCurrent(), machPortCallback);
if (err) {
NSLog(@"AppList: Unable to register mach server with error %x", err);
}
}
return self;
}
static SBApplicationController *appController(void);
static SBApplication *applicationWithDisplayIdentifier(NSString *displayIdentifier);
static inline NSMutableDictionary *dictionaryOfApplicationsList(id<NSFastEnumeration> applications)
{
NSMutableDictionary *result = [NSMutableDictionary dictionary];
for (SBApplication *app in applications) {
NSString *displayName = [[app displayName] description];
if (displayName) {
NSString *displayIdentifier = [[app displayIdentifier] description];
if (displayIdentifier) {
[result setObject:displayName forKey:displayIdentifier];
}
}
}
return result;
}
- (NSDictionary *)applications
{
return dictionaryOfApplicationsList([appController() allApplications]);
}
- (NSInteger)applicationCount
{
return [[appController() allApplications] count];
}
static NSArray *hiddenDisplayIdentifiers;
- (NSArray *)_hiddenDisplayIdentifiers
{
NSArray *result = hiddenDisplayIdentifiers;
if (!result) {
result = [[NSArray alloc] initWithObjects:
@"com.apple.AdSheet",
@"com.apple.AdSheetPhone",
@"com.apple.AdSheetPad",
@"com.apple.DataActivation",
@"com.apple.DemoApp",
@"com.apple.Diagnostics",
@"com.apple.fieldtest",
@"com.apple.iosdiagnostics",
@"com.apple.iphoneos.iPodOut",
@"com.apple.TrustMe",
@"com.apple.WebSheet",
@"com.apple.springboard",
@"com.apple.purplebuddy",
@"com.apple.datadetectors.DDActionsService",
@"com.apple.FacebookAccountMigrationDialog",
@"com.apple.iad.iAdOptOut",
@"com.apple.ios.StoreKitUIService",
@"com.apple.TextInput.kbd",
@"com.apple.MailCompositionService",
@"com.apple.mobilesms.compose",
@"com.apple.quicklook.quicklookd",
@"com.apple.ShoeboxUIService",
@"com.apple.social.remoteui.SocialUIService",
@"com.apple.WebViewService",
@"com.apple.gamecenter.GameCenterUIService",
@"com.apple.appleaccount.AACredentialRecoveryDialog",
@"com.apple.CompassCalibrationViewService",
@"com.apple.WebContentFilter.remoteUI.WebContentAnalysisUI",
@"com.apple.PassbookUIService",
@"com.apple.uikit.PrintStatus",
@"com.apple.Copilot",
@"com.apple.MusicUIService",
@"com.apple.AccountAuthenticationDialog",
@"com.apple.MobileReplayer",
@"com.apple.SiriViewService",
@"com.apple.TencentWeiboAccountMigrationDialog",
// iOS 8
@"com.apple.AskPermissionUI",
@"com.apple.CoreAuthUI",
@"com.apple.family",
@"com.apple.mobileme.fmip1",
@"com.apple.GameController",
@"com.apple.HealthPrivacyService",
@"com.apple.InCallService",
@"com.apple.mobilesms.notification",
@"com.apple.PhotosViewService",
@"com.apple.PreBoard",
@"com.apple.PrintKit.Print-Center",
@"com.apple.share",
@"com.apple.SharedWebCredentialViewService",
@"com.apple.webapp",
@"com.apple.webapp1",
nil];
hiddenDisplayIdentifiers = result;
}
return result;
}
- (NSDictionary *)applicationsFilteredUsingPredicate:(NSPredicate *)predicate
{
NSArray *apps = [appController() allApplications];
if (predicate)
apps = [apps filteredArrayUsingPredicate:predicate];
return dictionaryOfApplicationsList(apps);
}
- (NSDictionary *)applicationsFilteredUsingPredicate:(NSPredicate *)predicate onlyVisible:(BOOL)onlyVisible titleSortedIdentifiers:(NSArray **)outSortedByTitle
{
NSArray *apps = [appController() allApplications];
if (predicate)
apps = [apps filteredArrayUsingPredicate:predicate];
NSMutableDictionary *result;
if (onlyVisible) {
if (kCFCoreFoundationVersionNumber > 1000) {
Class SBApplicationClass = %c(SBApplication);
if ([SBApplicationClass instancesRespondToSelector:@selector(tags)]) {
result = dictionaryOfApplicationsList([apps filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"not tags contains 'hidden'"]]);
} else if ([SBApplicationClass instancesRespondToSelector:@selector(info)] && [%c(SBApplicationInfo) instancesRespondToSelector:@selector(hasHiddenTag)]) {
result = dictionaryOfApplicationsList([apps filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"info.hasHiddenTag = FALSE"]]);
} else {
result = dictionaryOfApplicationsList(apps);
}
} else {
result = dictionaryOfApplicationsList(apps);
[result removeObjectsForKeys:[self _hiddenDisplayIdentifiers]];
}
} else {
result = dictionaryOfApplicationsList(apps);
}
if (outSortedByTitle) {
// Generate a sorted list of apps
*outSortedByTitle = [[result allKeys] sortedArrayUsingFunction:DictionaryTextComparator context:result];
}
return result;
}
- (id)valueForKeyPath:(NSString *)keyPath forDisplayIdentifier:(NSString *)displayIdentifier
{
return [applicationWithDisplayIdentifier(displayIdentifier) valueForKeyPath:keyPath];
}
- (id)valueForKey:(NSString *)keyPath forDisplayIdentifier:(NSString *)displayIdentifier
{
return [applicationWithDisplayIdentifier(displayIdentifier) valueForKey:keyPath];
}
- (BOOL)applicationWithDisplayIdentifierIsHidden:(NSString *)displayIdentifier
{
SBApplication *app = applicationWithDisplayIdentifier(displayIdentifier);
if ([app respondsToSelector:@selector(tags)]) {
return ![app.tags containsObject:@"hidden"];
}
if ([app respondsToSelector:@selector(info)]) {
return [[app info] hasHiddenTag];
}
return [[self _hiddenDisplayIdentifiers] containsObject:displayIdentifier];
}
static SBIconModel *homescreenIconModel(void)
{
static SBIconModel *iconModel;
if (!iconModel) {
if ([%c(SBIconViewMap) instancesRespondToSelector:@selector(iconModel)]) {
SBIconViewMap *viewMap;
if ([%c(SBIconViewMap) respondsToSelector:@selector(homescreenMap)]) {
viewMap = [%c(SBIconViewMap) homescreenMap];
} else {
viewMap = [(SBIconController *)[%c(SBIconController) sharedInstance] homescreenIconViewMap];
}
iconModel = [viewMap iconModel];
} else {
iconModel = (SBIconModel *)[%c(SBIconModel) sharedInstance];
}
iconModel = [iconModel retain];
}
return iconModel;
}
- (CGImageRef)copyIconOfSize:(ALApplicationIconSize)iconSize forDisplayIdentifier:(NSString *)displayIdentifier
{
if (![NSThread isMainThread]) {
return [super copyIconOfSize:iconSize forDisplayIdentifier:displayIdentifier];
}
SBIcon *icon;
SBIconModel *iconModel = homescreenIconModel();
if ([iconModel respondsToSelector:@selector(applicationIconForDisplayIdentifier:)])
icon = [iconModel applicationIconForDisplayIdentifier:displayIdentifier];
else if ([iconModel respondsToSelector:@selector(applicationIconForBundleIdentifier:)])
icon = [iconModel applicationIconForBundleIdentifier:displayIdentifier];
else if ([iconModel respondsToSelector:@selector(iconForDisplayIdentifier:)])
icon = [iconModel iconForDisplayIdentifier:displayIdentifier];
else
return NULL;
BOOL getIconImage = [icon respondsToSelector:@selector(getIconImage:)];
SBApplication *app = 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
static inline BOOL CloneMethod(Class victim, SEL sourceMethodName, SEL destMethodName)
{
BOOL result = NO;
if (victim) {
unsigned int count = 0;
Method *methods = class_copyMethodList(victim, &count);
Method sourceMethod = NULL;
Method destMethod = NULL;
if (methods) {
for (unsigned int i = 0; i < count; i++) {
SEL methodName = method_getName(methods[i]);
if (methodName == sourceMethodName)
sourceMethod = methods[i];
else if (methodName == destMethodName)
destMethod = methods[i];
}
if (sourceMethod && !destMethod) {
class_addMethod(victim, destMethodName, method_getImplementation(sourceMethod), method_getTypeEncoding(sourceMethod));
result = YES;
}
free(methods);
}
}
return result;
}
static SEL applicationWithDisplayIdentifierSEL;
static SBApplicationController *appController(void)
{
static SBApplicationController *cached;
SBApplicationController *result = cached;
if (!result) {
result = cached = (SBApplicationController *)[%c(SBApplicationController) sharedInstance];
// Load the proper selector to fetch an app by its bundle identifier
if ([result respondsToSelector:@selector(applicationWithBundleIdentifier:)]) {
applicationWithDisplayIdentifierSEL = @selector(applicationWithBundleIdentifier:);
} else {
applicationWithDisplayIdentifierSEL = @selector(applicationWithDisplayIdentifier:);
}
}
return result;
}
static SBApplication *applicationWithDisplayIdentifier(NSString *displayIdentifier)
{
return ((SBApplication *(*)(SBApplicationController *, SEL, NSString *))objc_msgSend)(appController(), applicationWithDisplayIdentifierSEL, displayIdentifier);
}
%group SBApplicationHooks
%hook SBApplication
+ (BOOL)resolveInstanceMethod:(SEL)selector
{
if (selector == @selector(displayIdentifier)) {
if (CloneMethod(%c(SBApplication), @selector(bundleIdentifier), @selector(displayIdentifier))) {
NSLog(@"AppList: Added -[SBApplication displayIdentifier] for compatibility purposes");
return YES;
}
}
return %orig();
}
%end
%end
// Workaround tweaks that mistakenly call applicationWithDisplayIdentifier:
static BOOL cloned;
%group SBApplicationControllerHooks
%hook SBApplicationController
- (BOOL)respondsToSelector:(SEL)selector
{
if (selector == @selector(applicationWithDisplayIdentifier:)) {
BOOL result = %orig();
return cloned ? NO : result;
}
return %orig();
}
+ (BOOL)instancesRespondToSelector:(SEL)selector
{
if (selector == @selector(applicationWithDisplayIdentifier:)) {
BOOL result = %orig();
return cloned ? NO : result;
}
return %orig();
}
+ (BOOL)resolveInstanceMethod:(SEL)selector
{
if (selector == @selector(applicationWithDisplayIdentifier:)) {
if (CloneMethod(self, @selector(applicationWithBundleIdentifier:), @selector(applicationWithDisplayIdentifier:))) {
NSLog(@"AppList: Added -[SBApplicationController applicationWithDisplayIdentifier:] for compatibility purposes");
cloned = YES;
return YES;
}
}
return %orig();
}
%end
%end
%ctor
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (%c(SBIconModel)) {
// Add a displayIdentifier property if one doesn't exist to maintain compatibility with plists that use predicates on displayIdentifier
if (kCFCoreFoundationVersionNumber > 1000) {
%init(SBApplicationHooks);
// Only add applicationWithDisplayIdentifier: on iOS 8
if (kCFCoreFoundationVersionNumber < 1240) {
%init(SBApplicationControllerHooks);
}
}
sharedApplicationList = [[ALApplicationListImpl alloc] init];
}
[pool drain];
}