forked from opa334/CCSupport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tweak.xm
1339 lines (1067 loc) · 47.2 KB
/
Tweak.xm
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "CCSupport.h"
#import "Defines.h"
#import <objc/runtime.h>
#import <Preferences/PSListController.h>
#import <Preferences/PSSpecifier.h>
NSArray *fixedModuleIdentifiers; // Identifiers of (normally) fixed modules
NSBundle *CCSupportBundle; // Bundle for icons and localization (only needed / initialized in settings)
NSDictionary *englishLocalizations; // English localizations for fallback
BOOL isSpringBoard; // Are we SpringBoard???
static inline BOOL obj_hasIvar(id object, const char *ivarName)
{
Ivar ivar = class_getInstanceVariable(object_getClass(object), ivarName);
if (ivar) {
return YES;
}
return NO;
}
NSString *localize(NSString *key)
{
if ([key isEqualToString:@"MediaControlsAudioModule"]) {
// Account for different volume module name on iOS 13 and above
key = @"AudioModule";
}
NSString *localizedString = [CCSupportBundle localizedStringForKey:key value:nil table:nil];
if ([localizedString isEqualToString:key]) {
if (!englishLocalizations) {
englishLocalizations = [NSDictionary dictionaryWithContentsOfFile:[CCSupportBundle pathForResource:@"Localizable" ofType:@"strings" inDirectory:@"en.lproj"]];
}
// If no localization was found, fallback to english
NSString *engString = [englishLocalizations objectForKey:key];
if (engString) {
return engString;
}
else {
// If an english localization was not found, just return the key itself
return key;
}
}
return localizedString;
}
UIImage *moduleIconForImage(UIImage *image)
{
long long imageVariant;
CGFloat screenScale = UIScreen.mainScreen.scale;
if (screenScale >= 3.0) {
imageVariant = 34;
}
else if (screenScale >= 2.0) {
imageVariant = 17;
}
else {
imageVariant = 4;
}
CGImageRef liIcon = LICreateIconForImage([image CGImage], imageVariant, 0);
return [[UIImage alloc] initWithCGImage:liIcon scale:screenScale orientation:0];
}
// Get fixed module identifiers from device specific plist (Return value: whether the plist was modified or not)
BOOL loadFixedModuleIdentifiers()
{
static dispatch_once_t onceToken;
dispatch_once (&onceToken,
^{
// This method is called before the hook of it is initialized, that's why we can get the actual fixed identifiers here
NSMutableArray *fixedModuleIdentifiersMutable = ((NSArray *)[%c(CCSModuleSettingsProvider) _defaultFixedModuleIdentifiers]).mutableCopy;
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_15_0) {
[fixedModuleIdentifiersMutable removeObjectsInArray:iOS15_WhitelistedFixedModuleIdentifiers];
}
fixedModuleIdentifiers = fixedModuleIdentifiersMutable.copy;
});
// If this array contains less than 7 objects, something was modified with no doubt
return ([fixedModuleIdentifiers count] < 7);
}
@implementation CCSModuleProviderManager
- (instancetype)init
{
self = [super init];
[self _reloadProviders];
[self _populateProviderToModuleCache];
return self;
}
+ (instancetype)sharedInstance
{
static CCSModuleProviderManager *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^ {
sharedInstance = [[CCSModuleProviderManager alloc] init];
});
return sharedInstance;
}
- (void)_reloadProviders
{
NSMutableDictionary *newModuleIdentifiersByIdentifier = [NSMutableDictionary new];
NSURL *providersURL = [NSURL fileURLWithPath:CCSupportProvidersPath isDirectory:YES];
NSArray<NSURL *> *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:providersURL includingPropertiesForKeys:@[NSURLIsDirectoryKey] options:0 error:nil];
for (NSURL *itemURL in contents) {
NSNumber *isDirectory;
[itemURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil];
if (![itemURL.pathExtension isEqualToString:@"bundle"] || ![isDirectory boolValue]) continue;
NSBundle *bundle = [NSBundle bundleWithURL:itemURL];
NSError *error;
if (![bundle loadAndReturnError:&error]) {
NSLog(@"CCSupport failed to load provider bundle %@: %@", bundle, error);
continue;
}
NSString *providerIdentifier = bundle.bundleIdentifier;
Class providerClass = bundle.principalClass;
if (providerClass) {
NSObject<CCSModuleProvider> *provider = [[providerClass alloc] init];
[newModuleIdentifiersByIdentifier setObject:provider forKey:providerIdentifier];
}
}
self.moduleProvidersByIdentifier = [newModuleIdentifiersByIdentifier copy];
}
- (void)_populateProviderToModuleCache
{
if (!self.moduleProvidersByIdentifier) {
[self _reloadProviders];
}
NSMutableDictionary *newProviderToModuleCache = [NSMutableDictionary new];
for (NSString *key in [self.moduleProvidersByIdentifier allKeys]) {
NSObject<CCSModuleProvider> *provider = [self.moduleProvidersByIdentifier objectForKey:key];
NSMutableArray *moduleIdentifiers = [NSMutableArray new];
NSUInteger moduleNumber = [provider numberOfProvidedModules];
for (NSUInteger i = 0; i < moduleNumber; i++) {
NSString *moduleIdentifier = [provider identifierForModuleAtIndex:i];
if (moduleIdentifier) {
[moduleIdentifiers addObject:moduleIdentifier];
}
}
[newProviderToModuleCache setObject:[moduleIdentifiers copy] forKey:key];
}
self.providerToModuleCache = [newProviderToModuleCache copy];
}
- (NSObject<CCSModuleProvider> *)_moduleProviderForModuleWithIdentifier:(NSString *)moduleIdentifier
{
if (!self.moduleProvidersByIdentifier) {
[self _reloadProviders];
}
if (!self.providerToModuleCache) {
[self _populateProviderToModuleCache];
}
for (NSString *moduleProviderIdentifier in self.providerToModuleCache) {
NSArray *providedModuleIdentifiers = [self.providerToModuleCache objectForKey:moduleProviderIdentifier];
if ([providedModuleIdentifiers containsObject:moduleIdentifier]) {
return [self.moduleProvidersByIdentifier objectForKey:moduleProviderIdentifier];
}
}
return nil;
}
- (CCSModuleMetadata *)_metadataForProvidedModuleWithIdentifier:(NSString *)identifier fromProvider:(NSObject<CCSModuleProvider> *)provider
{
NSSet *supportedDeviceFamilies, *requiredDeviceCapabilities, *requiredDeviceIncapabilities;
NSString *associatedBundleIdentifier, *associatedBundleMinimumVersion;
NSUInteger visibilityPreference = 0;
// This was introduced in iOS 17.1
BOOL requiredDeviceIncapabilitiesSupported = [%c(CCSModuleMetadata) instancesRespondToSelector:@selector(_initWithModuleIdentifier:supportedDeviceFamilies:requiredDeviceCapabilities:requiredDeviceIncapabilities:associatedBundleIdentifier:associatedBundleMinimumVersion:visibilityPreference:moduleBundleURL:)];
if ([provider respondsToSelector:@selector(supportedDeviceFamiliesForModuleWithIdentifier:)]) {
supportedDeviceFamilies = [provider supportedDeviceFamiliesForModuleWithIdentifier:identifier];
}
else {
supportedDeviceFamilies = [NSSet setWithObjects:@1, @2, nil];
}
if ([provider respondsToSelector:@selector(requiredDeviceCapabilitiesForModuleWithIdentifier:)]) {
requiredDeviceCapabilities = [provider requiredDeviceCapabilitiesForModuleWithIdentifier:identifier];
}
else {
requiredDeviceCapabilities = [NSSet setWithObjects:@"arm64", nil];
}
if (requiredDeviceIncapabilitiesSupported && [provider respondsToSelector:@selector(requiredDeviceIncapabilitiesForModuleWithIdentifier:)]) {
requiredDeviceIncapabilities = [provider requiredDeviceIncapabilitiesForModuleWithIdentifier:identifier];
}
if ([provider respondsToSelector:@selector(associatedBundleIdentifierForModuleWithIdentifier:)]) {
associatedBundleIdentifier = [provider associatedBundleIdentifierForModuleWithIdentifier:identifier];
}
if ([provider respondsToSelector:@selector(associatedBundleMinimumVersionForModuleWithIdentifier:)]) {
associatedBundleMinimumVersion = [provider associatedBundleMinimumVersionForModuleWithIdentifier:identifier];
}
if ([provider respondsToSelector:@selector(visibilityPreferenceForModuleWithIdentifier:)]) {
visibilityPreference = [provider visibilityPreferenceForModuleWithIdentifier:identifier];
}
NSBundle *bundle = [NSBundle bundleForClass:[provider class]];
CCSModuleMetadata *metadata;
if (requiredDeviceIncapabilitiesSupported) {
metadata = [[%c(CCSModuleMetadata) alloc] _initWithModuleIdentifier:identifier supportedDeviceFamilies:supportedDeviceFamilies requiredDeviceCapabilities:requiredDeviceCapabilities requiredDeviceIncapabilities:requiredDeviceIncapabilities associatedBundleIdentifier:associatedBundleIdentifier associatedBundleMinimumVersion:associatedBundleMinimumVersion visibilityPreference:visibilityPreference moduleBundleURL:bundle.bundleURL];
}
else {
metadata = [[%c(CCSModuleMetadata) alloc] _initWithModuleIdentifier:identifier supportedDeviceFamilies:supportedDeviceFamilies requiredDeviceCapabilities:requiredDeviceCapabilities associatedBundleIdentifier:associatedBundleIdentifier associatedBundleMinimumVersion:associatedBundleMinimumVersion visibilityPreference:visibilityPreference moduleBundleURL:bundle.bundleURL];
}
return metadata;
}
- (NSMutableSet *)_allProvidedModuleIdentifiers
{
NSMutableSet *allModuleIdentifiers = [NSMutableSet new];
for (NSString *providerIdentifier in self.moduleProvidersByIdentifier.allKeys) {
for (NSString *moduleIdentifier in [self.providerToModuleCache objectForKey:providerIdentifier]) {
[allModuleIdentifiers addObject:moduleIdentifier];
}
}
return allModuleIdentifiers;
}
- (BOOL)doesProvideModule:(NSString *)moduleIdentifier
{
NSObject<CCSModuleProvider> *moduleProvider = [self _moduleProviderForModuleWithIdentifier:moduleIdentifier];
return moduleProvider != nil;
}
- (NSMutableArray *)metadataForAllProvidedModules
{
NSMutableArray *allMetadata = [NSMutableArray new];
for (NSString *moduleProviderIdentifier in self.providerToModuleCache) {
NSObject<CCSModuleProvider> *provider = [self.moduleProvidersByIdentifier objectForKey:moduleProviderIdentifier];
NSArray *providedModuleIdentifiers = [self.providerToModuleCache objectForKey:moduleProviderIdentifier];
for (NSString *identifier in providedModuleIdentifiers) {
CCSModuleMetadata *metadata = [self _metadataForProvidedModuleWithIdentifier:identifier fromProvider:provider];
if (metadata) {
[allMetadata addObject:metadata];
}
}
}
return allMetadata;
}
- (id)moduleInstanceForModuleIdentifier:(NSString *)identifier
{
NSObject<CCSModuleProvider> *provider = [self _moduleProviderForModuleWithIdentifier:identifier];
return [provider moduleInstanceForModuleIdentifier:identifier];
}
- (id)listControllerForModuleIdentifier:(NSString *)identifier
{
NSObject<CCSModuleProvider> *provider = [self _moduleProviderForModuleWithIdentifier:identifier];
if ([provider respondsToSelector:@selector(listControllerForModuleIdentifier:)]) {
return [provider listControllerForModuleIdentifier:identifier];
}
return nil;
}
- (NSString *)displayNameForModuleIdentifier:(NSString *)identifier
{
NSObject<CCSModuleProvider> *provider = [self _moduleProviderForModuleWithIdentifier:identifier];
return [provider displayNameForModuleIdentifier:identifier];
}
- (UIImage *)settingsIconForModuleIdentifier:(NSString *)identifier
{
NSObject<CCSModuleProvider> *provider = [self _moduleProviderForModuleWithIdentifier:identifier];
if ([provider respondsToSelector:@selector(settingsIconForModuleIdentifier:)]) {
return [provider settingsIconForModuleIdentifier:identifier];
}
return nil;
}
- (BOOL)providesListControllerForModuleIdentifier:(NSString *)identifier
{
NSObject<CCSModuleProvider> *provider = [self _moduleProviderForModuleWithIdentifier:identifier];
if ([provider respondsToSelector:@selector(providesListControllerForModuleIdentifier:)]) {
return [provider providesListControllerForModuleIdentifier:identifier];
}
return NO;
}
// Reloads and if any modules have been removed that are still added in CC, they're removed from the plist aswell
// (This is to prevent the plist from having entries that would otherwise never be removed)
- (void)reload
{
if (!isSpringBoard) {
[self _populateProviderToModuleCache];
}
else {
NSMutableSet *moduleIdentifiersBeforeReload = [self _allProvidedModuleIdentifiers];
[self _populateProviderToModuleCache];
NSMutableSet *moduleIdentifiersAfterReload = [self _allProvidedModuleIdentifiers];
[moduleIdentifiersBeforeReload minusSet:moduleIdentifiersAfterReload];
// moduleIdentifiersBeforeReload now contains all module identifiers that have been removed from providers since the last reload
BOOL changed = NO;
CCSModuleSettingsProvider *settingsProvider = [%c(CCSModuleSettingsProvider) sharedProvider];
NSMutableArray *orderedUserEnabledModuleIdentifiers = settingsProvider.orderedUserEnabledModuleIdentifiers.mutableCopy;
for (NSString *removedModuleIdentifier in moduleIdentifiersBeforeReload) {
if ([orderedUserEnabledModuleIdentifiers containsObject:removedModuleIdentifier]) {
changed = YES;
[orderedUserEnabledModuleIdentifiers removeObject:removedModuleIdentifier];
}
}
if (changed) {
[settingsProvider setAndSaveOrderedUserEnabledModuleIdentifiers:orderedUserEnabledModuleIdentifiers];
}
}
}
@end
@implementation CCSProvidedListController // Placeholder
@end
%group ControlCenterServices
%hook CCSModuleRepository
// Add path for third party bundles to directory urls
+ (NSArray<NSURL *> *)_defaultModuleDirectories
{
NSArray<NSURL *> *directories = %orig;
if (directories) {
NSURL *thirdPartyURL = [NSURL fileURLWithPath:CCSupportModulesPath isDirectory:YES];
return [directories arrayByAddingObject:thirdPartyURL];
}
return directories;
}
// Enable non whitelisted modules to be loaded
- (void)_queue_updateAllModuleMetadata // iOS >=12
{
if (obj_hasIvar(self, "_ignoreAllowedList")) { // iOS >=14
MSHookIvar<BOOL>(self, "_ignoreAllowedList") = YES;
}
else if (obj_hasIvar(self, "_ignoreWhitelist")) { // iOS 11-13
MSHookIvar<BOOL>(self, "_ignoreWhitelist") = YES;
}
%orig;
}
- (void)_updateAllModuleMetadata // iOS 11
{
if (![self respondsToSelector:@selector(_queue_updateAllModuleMetadata)]) {
MSHookIvar<BOOL>(self, "_ignoreWhitelist") = YES;
}
%orig;
}
// Module providers
%new
- (NSArray *)ccshook_loadAllModuleMetadataWithOrig:(NSArray *)orig
{
// Add metadata provided by module providers
CCSModuleProviderManager *providerManager = [CCSModuleProviderManager sharedInstance];
NSMutableArray *providedMetadata = [providerManager metadataForAllProvidedModules];
if (!providedMetadata || providedMetadata.count <= 0) {
return orig;
}
NSArray *allModuleMetadata = orig;
NSMutableArray *allModuleMetadataM;
if ([allModuleMetadata respondsToSelector:@selector(addObject:)]) {
allModuleMetadataM = (NSMutableArray *)allModuleMetadata;
}
else {
allModuleMetadataM = [allModuleMetadata mutableCopy];
}
[allModuleMetadataM addObjectsFromArray:providedMetadata];
return allModuleMetadataM;
}
- (NSArray *)_queue_loadAllModuleMetadata // iOS >=12
{
NSArray *orig = %orig;
return [self ccshook_loadAllModuleMetadataWithOrig:orig];
}
- (NSArray *)_loadAllModuleMetadata // iOS 11
{
NSArray *orig = %orig;
return [self ccshook_loadAllModuleMetadataWithOrig:orig];
}
%end
// Hacky fix on iOS 15, see below in _queue_loadSettings
%hook NSMutableArray
NSArray *g_mutableArrayPreventRemoval = nil;
- (void)removeObject:(NSObject *)object
{
if (g_mutableArrayPreventRemoval) {
if ([g_mutableArrayPreventRemoval containsObject:object]) {
return;
}
}
%orig;
}
%end
%hook CCSModuleSettingsProvider
// Return different configuration plist to not mess everything up when the tweak is not enabled
+ (NSURL *)_configurationFileURL
{
NSString *directoryPath = [CCSupportModuleConfigurationPath stringByDeletingLastPathComponent];
if (![[NSFileManager defaultManager] fileExistsAtPath:directoryPath]) {
[[NSFileManager defaultManager] createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil];
}
return [NSURL fileURLWithPath:CCSupportModuleConfigurationPath];
}
// Return empty array for fixed modules
+ (NSMutableArray *)_defaultFixedModuleIdentifiers
{
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_15_0) {
// Fix replaykit modules not showing up on iOS 15
return iOS15_WhitelistedFixedModuleIdentifiers.mutableCopy;
}
else {
return [NSMutableArray array];
}
}
// Return fixed + non fixed modules
+ (NSMutableArray *)_defaultUserEnabledModuleIdentifiers
{
NSMutableArray *defaultUserEnabledModuleIdentifiers = [[fixedModuleIdentifiers arrayByAddingObjectsFromArray:%orig] mutableCopy];
// In iOS 15 the return order of this method is how the modules will be initially ordered on first launch
// Because CCSupport fucks this order up, we need to sort a few things manually so it appears just like it would on stock
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_15_0) {
// 1. Remove com.apple.donotdisturb.DoNotDisturbModule
[defaultUserEnabledModuleIdentifiers enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSString *str, NSUInteger idx, BOOL *stop) {
// For some reason calling removeObject with com.apple.donotdisturb.DoNotDisturbModule does not work
if ([str isEqualToString:@"com.apple.donotdisturb.DoNotDisturbModule"]) {
[defaultUserEnabledModuleIdentifiers removeObjectAtIndex:idx];
*stop = YES;
}
}];
// 2. Put com.apple.mediaremote.controlcenter.airplaymirroring after com.apple.control-center.OrientationLockModule
[defaultUserEnabledModuleIdentifiers removeObject:@"com.apple.mediaremote.controlcenter.airplaymirroring"];
NSInteger orientationLockModuleIndex = [defaultUserEnabledModuleIdentifiers indexOfObject:@"com.apple.control-center.OrientationLockModule"];
if (orientationLockModuleIndex != NSNotFound) {
[defaultUserEnabledModuleIdentifiers insertObject:@"com.apple.mediaremote.controlcenter.airplaymirroring" atIndex:orientationLockModuleIndex+1];
}
// 3. Insert com.apple.FocusUIModule after com.apple.mediaremote.controlcenter.audio
if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_16_0) { // On iOS 16 this is not needed
NSInteger audioIndex = [defaultUserEnabledModuleIdentifiers indexOfObject:@"com.apple.mediaremote.controlcenter.audio"];
if (audioIndex != NSNotFound) {
[defaultUserEnabledModuleIdentifiers insertObject:@"com.apple.FocusUIModule" atIndex:audioIndex+1];
}
}
}
return defaultUserEnabledModuleIdentifiers;
}
// Disable stock home controls
- (NSArray *)orderedUserEnabledFixedModuleIdentifiers
{
return @[];
}
// Problem: On iOS 15, this method removes the Focus module from _orderedUserEnabledModuleIdentifiers
// Additionally this also removes the "Do Not Disturb" and "Do Not Disturb While Driving" modules, these are hidden on stock iOS 15
// but as they exist and work on iOS 15 just fine and are automatically unhidden by CCSupport anyways, I decided to leave them in
- (void)_queue_loadSettings
{
// This is kinda hacky but there is no better way of archiving this due to how the method functions, see NSMutableArray hook above
g_mutableArrayPreventRemoval = @[@"com.apple.FocusUIModule", @"com.apple.donotdisturb.DoNotDisturbModule", @"com.apple.control-center.CarModeModule"];
%orig;
g_mutableArrayPreventRemoval = nil;
}
%end
%end
%group ControlCenterUI
%hook CCUIModuleInstanceManager
%new
- (CCUIModuleInstance *)instanceForModuleIdentifier:(NSString *)moduleIdentifier
{
NSMutableDictionary *moduleInstanceByIdentifier = MSHookIvar<NSMutableDictionary *>(self, "_moduleInstanceByIdentifier");
return [moduleInstanceByIdentifier objectForKey:moduleIdentifier];
}
//Get instances from module providers
- (id)_instantiateModuleWithMetadata:(CCSModuleMetadata *)metadata
{
CCSModuleProviderManager *providerManager = [CCSModuleProviderManager sharedInstance];
if ([providerManager doesProvideModule:metadata.moduleIdentifier]) {
NSObject *module = [providerManager moduleInstanceForModuleIdentifier:metadata.moduleIdentifier];
if ([module respondsToSelector:@selector(setContentModuleContext:)]) {
NSObject<CCUIContentModule> *contentModule = (NSObject<CCUIContentModule> *)module;
CCUIContentModuleContext *contentModuleContext = [[%c(CCUIContentModuleContext) alloc] initWithModuleIdentifier:metadata.moduleIdentifier];
contentModuleContext.delegate = self;
[contentModule setContentModuleContext:contentModuleContext];
}
if (module && [module conformsToProtocol:@protocol(CCUIContentModule)]) {
CCUILayoutSize prototypeModuleSize;
prototypeModuleSize.width = 1;
prototypeModuleSize.height = 1;
CCUIModuleInstance *instance = [[%c(CCUIModuleInstance) alloc] initWithMetadata:metadata module:module prototypeModuleSize:prototypeModuleSize];
return instance;
}
else {
return nil;
}
}
else {
return %orig;
}
}
%end
%hook CCUIModuleSettings
%property(nonatomic, assign) BOOL ccs_usesDynamicSize;
%end
// 14.3 sizes re:
// -[CCUIModuleInstanceManager requestModuleLayoutSizeUpdateForContentModuleContext:]
// runs moduleInstancesLayoutChangedForModuleInstanceManager: on observers
// -[CCUIModuleCollectionViewController moduleInstancesLayoutChangedForModuleInstanceManager:]
// runs _updatePositionProviders -> _refreshPositionProviders -> _sizesForModuleIdentifiers:moduleInstanceByIdentifier:interfaceOrientation:
// in the end -[CCUIModuleCollectionViewController _sizesForModuleIdentifiers:moduleInstanceByIdentifier:interfaceOrientation:] is invoked
// which calls moduleLayoutSizeForOrientation: on the contentViewController of the module if it exists
%hook CCUIModuleCollectionViewController
- (NSArray<NSValue *> *)_sizesForModuleIdentifiers:(NSArray<NSString *> *)moduleIdentifiers moduleInstanceByIdentifier:(NSDictionary *)moduleInstanceByIdentifier interfaceOrientation:(long long)interfaceOrientation
{
NSArray<NSValue *> *sizes = %orig;
NSMutableArray *sizesM = nil;
BOOL shouldReturnCopy = NO;
CCSModuleProviderManager *providerManager = [CCSModuleProviderManager sharedInstance];
CCUIModuleSettingsManager *settingsManager = [self valueForKey:@"_settingsManager"];
int orientationForInterfaceOrientation;
if (interfaceOrientation == 4) {
orientationForInterfaceOrientation = CCOrientationLandscape;
}
else {
orientationForInterfaceOrientation = CCOrientationPortrait;
}
for (NSString *moduleIdentifier in moduleIdentifiers) {
CCUIModuleInstance *moduleInstance = [moduleInstanceByIdentifier objectForKey:moduleIdentifier];
CCSModuleMetadata *metadata = moduleInstance.metadata;
// protect against crashes if moduleBundleURL is nil
if (!metadata.moduleBundleURL) {
continue;
}
NSBundle *moduleBundle = [NSBundle bundleWithURL:metadata.moduleBundleURL];
NSNumber *getSizeAtRuntime = [moduleBundle objectForInfoDictionaryKey:@"CCSGetModuleSizeAtRuntime"];
NSValue *sizeToSet;
BOOL ccs_usesDynamicSizeToSet = NO;
if ([getSizeAtRuntime boolValue] || [providerManager doesProvideModule:moduleIdentifier]) {
NSObject<DynamicSizeModule> *module = (NSObject<DynamicSizeModule> *)moduleInstance.module;
if (module && [module respondsToSelector:@selector(moduleSizeForOrientation:)]) {
CCUILayoutSize layoutSize = [module moduleSizeForOrientation:orientationForInterfaceOrientation];
ccs_usesDynamicSizeToSet = YES;
sizeToSet = [NSValue ccui_valueWithLayoutSize:layoutSize];
}
}
else {
NSDictionary *moduleSizeDict = [moduleBundle objectForInfoDictionaryKey:@"CCSModuleSize"];
NSDictionary *moduleSizeOrientationDict;
if (orientationForInterfaceOrientation == CCOrientationPortrait) {
moduleSizeOrientationDict = [moduleSizeDict objectForKey:@"Portrait"];
}
else if (orientationForInterfaceOrientation == CCOrientationLandscape) {
moduleSizeOrientationDict = [moduleSizeDict objectForKey:@"Landscape"];
}
if (moduleSizeOrientationDict) {
NSNumber *widthNum = [moduleSizeOrientationDict objectForKey:@"Width"];
NSNumber *heightNum = [moduleSizeOrientationDict objectForKey:@"Height"];
if (widthNum && heightNum) {
CCUILayoutSize layoutSize;
layoutSize.width = [widthNum unsignedIntegerValue];
layoutSize.height = [heightNum unsignedIntegerValue];
sizeToSet = [NSValue ccui_valueWithLayoutSize:layoutSize];
}
}
}
// Makes it possible for third party tweaks to check which modules have dynamic sizes
CCUIModuleSettings *moduleSettings = [settingsManager moduleSettingsForModuleIdentifier:moduleIdentifier prototypeSize:moduleInstance.prototypeModuleSize];
moduleSettings.ccs_usesDynamicSize = ccs_usesDynamicSizeToSet;
if (sizeToSet) {
if (!sizesM) {
if ([sizes respondsToSelector:@selector(addObject:)]) {
sizesM = (NSMutableArray *)sizes;
}
else {
// On iOS 12 and below, sizes is not mutable
shouldReturnCopy = YES;
sizesM = [sizes mutableCopy];
}
}
NSInteger index = [moduleIdentifiers indexOfObject:moduleIdentifier];
[sizesM replaceObjectAtIndex:index withObject:sizeToSet];
}
}
if (!sizesM) {
return sizes;
}
if (shouldReturnCopy) {
return [sizesM copy];
}
return sizesM;
}
%end
%end
%group ControlCenterSettings_SortingFix_iOS13Down
%hook CCUISettingsModulesController
// By default there is a bug in iOS 11-13 where this method sorts the identifiers differently than _repoplateModuleData
// We fix this by sorting it in the same way
// _repoplateModuleData sorts with localizedStandardCompare:
// this method normally sorts with compare:
- (NSUInteger)_indexForInsertingItemWithIdentifier:(NSString *)identifier intoArray:(NSArray *)array
{
return [array indexOfObject:identifier inSortedRange:NSMakeRange(0, array.count) options:NSBinarySearchingInsertionIndex usingComparator:^NSComparisonResult(id identifier1, id identifier2) {
CCUISettingsModuleDescription *identifier1Description = [self _descriptionForIdentifier:identifier1];
CCUISettingsModuleDescription *identifier2Description = [self _descriptionForIdentifier:identifier2];
return [identifier1Description.displayName localizedStandardCompare:identifier2Description.displayName];
}];
}
%end
%end
%group ControlCenterSettings_Shared
#define eccSelf ((UIViewController<SettingsControllerSharedAcrossVersions> *)self)
%hook CCUISettingsModuleDescription
CCUISettingsModuleDescription *CCUISettingsModuleDescription_CCSInitHook(NSString *identifier, NSString *displayName, UIImage *iconImage, CCUISettingsModuleDescription *(^origBlock)(NSString *, UIImage *))
{
CCSModuleProviderManager *providerManager = [CCSModuleProviderManager sharedInstance];
if ([providerManager doesProvideModule:identifier]) {
UIImage *providedIconImage = iconImage;
NSString *providedDisplayName = [providerManager displayNameForModuleIdentifier:identifier];
UIImage *moduleIcon = [providerManager settingsIconForModuleIdentifier:identifier];
if (moduleIcon) {
providedIconImage = moduleIconForImage(moduleIcon);
}
return origBlock(providedDisplayName, providedIconImage);
}
return origBlock(displayName, iconImage);
}
// iOS <= 16
- (instancetype)initWithIdentifier:(NSString *)identifier displayName:(NSString *)displayName iconImage:(UIImage *)iconImage
{
return CCUISettingsModuleDescription_CCSInitHook(identifier, displayName, iconImage, ^(NSString *providedDisplayName, UIImage *providedIconImage) {
return %orig(identifier, providedDisplayName, providedIconImage);
});
}
// iOS 17
- (instancetype)initWithIdentifier:(NSString *)identifier displayName:(NSString *)displayName iconImage:(UIImage *)iconImage iconBackgroundColor:(UIColor *)iconBackgroundColor
{
return CCUISettingsModuleDescription_CCSInitHook(identifier, displayName, iconImage, ^(NSString *providedDisplayName, UIImage *providedIconImage) {
return %orig(identifier, providedDisplayName, providedIconImage, iconBackgroundColor);
});
}
%end
%hook SettingsControllerSharedAcrossVersions // iOS >=11
%property (nonatomic, retain) NSDictionary *ccsp_additionalModuleIcons;
%property (nonatomic, retain) NSDictionary *preferenceClassForModuleIdentifiers;
// Load icons for normally fixed modules and determine which modules have preferences
- (void)_repopulateModuleData
{
if (!eccSelf.ccsp_additionalModuleIcons) {
NSMutableDictionary *ccsp_additionalModuleIcons = [NSMutableDictionary new];
for (NSString *moduleIdentifier in fixedModuleIdentifiers) {
NSString *imageIdentifier = moduleIdentifier;
if ([imageIdentifier isEqualToString:@"com.apple.donotdisturb.DoNotDisturbModule"]) {
// Account for different DND module identifier on iOS 12 and above
imageIdentifier = @"com.apple.control-center.DoNotDisturbModule";
}
else if ([imageIdentifier isEqualToString:@"com.apple.mediaremote.controlcenter.audio"]) {
// Account for different audio module identifier on iOS 13 and above
imageIdentifier = @"com.apple.control-center.AudioModule";
}
UIImage *moduleIcon = [UIImage imageNamed:imageIdentifier inBundle:CCSupportBundle compatibleWithTraitCollection:nil];
if (moduleIcon) {
[ccsp_additionalModuleIcons setObject:moduleIcon forKey:moduleIdentifier];
}
}
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_15_0) {
UIImage *moduleIcon = [UIImage imageNamed:@"com.apple.FocusUIModule" inBundle:CCSupportBundle compatibleWithTraitCollection:nil];
[ccsp_additionalModuleIcons setObject:moduleIcon forKey:@"com.apple.FocusUIModule"];
}
eccSelf.ccsp_additionalModuleIcons = [ccsp_additionalModuleIcons copy];
}
%orig;
NSMutableArray *enabledIdentfiers = MSHookIvar<NSMutableArray *>(self, "_enabledIdentifiers");
NSMutableArray *disabledIdentifiers = MSHookIvar<NSMutableArray *>(self, "_disabledIdentifiers");
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_15_0) {
// Remove useless module that's normally hidden because of whitelist on iOS 15
[enabledIdentfiers removeObject:@"com.apple.TelephonyUtilities.SilenceCallsCCWidget"];
[disabledIdentifiers removeObject:@"com.apple.TelephonyUtilities.SilenceCallsCCWidget"];
}
NSArray *moduleIdentifiers = [enabledIdentfiers arrayByAddingObjectsFromArray:disabledIdentifiers];
NSMutableDictionary *preferenceClassForModuleIdentifiersM = [NSMutableDictionary new];
for (NSString *moduleIdentifier in moduleIdentifiers) {
CCSModuleRepository *moduleRepository = MSHookIvar<CCSModuleRepository *>(self, "_moduleRepository");
NSURL *bundleURL = [moduleRepository moduleMetadataForModuleIdentifier:moduleIdentifier].moduleBundleURL;
NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
NSString *rootListControllerClassName = [bundle objectForInfoDictionaryKey:@"CCSPreferencesRootListController"];
CCSModuleProviderManager *providerManager = [CCSModuleProviderManager sharedInstance];
if ([providerManager providesListControllerForModuleIdentifier:moduleIdentifier]) {
rootListControllerClassName = @"CCSProvidedListController";
}
if (rootListControllerClassName) {
[preferenceClassForModuleIdentifiersM setObject:rootListControllerClassName forKey:moduleIdentifier];
}
}
eccSelf.preferenceClassForModuleIdentifiers = [preferenceClassForModuleIdentifiersM copy];
}
// Add localized names & icons
- (CCUISettingsModuleDescription *)_descriptionForIdentifier:(NSString *)identifier
{
CCUISettingsModuleDescription *moduleDescription = %orig;
if ([fixedModuleIdentifiers containsObject:identifier] || [identifier isEqualToString:@"com.apple.FocusUIModule"]) {
MSHookIvar<NSString *>(moduleDescription, "_displayName") = localize(MSHookIvar<NSString *>(moduleDescription, "_displayName"));
}
if ([eccSelf.ccsp_additionalModuleIcons.allKeys containsObject:identifier]) {
MSHookIvar<UIImage *>(moduleDescription, "_iconImage") = moduleIconForImage([eccSelf.ccsp_additionalModuleIcons objectForKey:identifier]);
}
return moduleDescription;
}
%new
- (UITableView *)ccs_getTableView
{
if (obj_hasIvar(self, "_table")) { // iOS >=14
return MSHookIvar<UITableView *>(self, "_table");
}
else if (obj_hasIvar(self, "_tableViewController")) { // iOS 11-13
UITableViewController *tableViewController = MSHookIvar<UITableViewController *>(self, "_tableViewController");
return tableViewController.tableView;
}
return nil;
}
%new
- (void)ccs_unselectSelectedRow
{
UITableView *tableView = [self ccs_getTableView];
NSIndexPath *selectedRow = [tableView indexPathForSelectedRow];
if (selectedRow) {
[tableView deselectRowAtIndexPath:selectedRow animated:YES];
}
}
%new
- (void)ccs_resetButtonPressed
{
UITableView *tableView = [eccSelf ccs_getTableView];
UIAlertController *resetAlert = [UIAlertController alertControllerWithTitle:localize(@"RESET_MODULES") message:localize(@"RESET_MODULES_DESCRIPTION") preferredStyle:UIAlertControllerStyleAlert];
[resetAlert addAction:[UIAlertAction actionWithTitle:localize(@"RESET_DEFAULT_CONFIGURATION") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[[NSFileManager defaultManager] removeItemAtPath:DefaultModuleConfigurationPath error:nil];
[self ccs_unselectSelectedRow];
}]];
[resetAlert addAction:[UIAlertAction actionWithTitle:localize(@"RESET_CCSUPPORT_CONFIGURATION") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[[NSFileManager defaultManager] removeItemAtPath:CCSupportModuleConfigurationPath error:nil];
// Reload CCSupport configuration
[eccSelf _repopulateModuleData];
[tableView reloadData];
[self ccs_unselectSelectedRow];
}]];
[resetAlert addAction:[UIAlertAction actionWithTitle:localize(@"RESET_BOTH_CONFIGURATIONS") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[[NSFileManager defaultManager] removeItemAtPath:DefaultModuleConfigurationPath error:nil];
[[NSFileManager defaultManager] removeItemAtPath:CCSupportModuleConfigurationPath error:nil];
// Reload CCSupport configuration
[eccSelf _repopulateModuleData];
[tableView reloadData];
[self ccs_unselectSelectedRow];
}]];
[resetAlert addAction:[UIAlertAction actionWithTitle:localize(@"CANCEL") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
[self ccs_unselectSelectedRow];
}]];
[eccSelf presentViewController:resetAlert animated:YES completion:nil];
}
%end
%end
%group ControlCenterSettings_ModulesController
%hook CCUISettingsModulesController // iOS 11-13
// Unselect module
- (void)viewDidAppear:(BOOL)animated
{
%orig;
[self ccs_unselectSelectedRow];
}
// Add section for reset button to table view
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
tableView.allowsSelectionDuringEditing = YES;
return %orig + 1;
}
// Set rows of new section to 1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 2) {
return 1;
}
else {
return %orig;
}
}
// Add reset button to new section and add an arrow to modules with preferences
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 2) {
// Create cell for reset button
UITableViewCell *resetCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ResetCell"];
resetCell.textLabel.text = localize(@"RESET_MODULES");
resetCell.textLabel.textColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
return resetCell;
}
else {
UITableViewCell *cell = %orig;
NSString *moduleIdentifier = [self _identifierAtIndexPath:indexPath];
if ([self.preferenceClassForModuleIdentifiers objectForKey:moduleIdentifier]) {
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.editingAccessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
}
// Present alert to reset CC configuration on button click or push preferences controller if the pressed module has preferences
%new
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 2 && indexPath.row == 0) {
[self ccs_resetButtonPressed];
}
else {
NSString *moduleIdentifier = [self _identifierAtIndexPath:indexPath];
NSString *rootListControllerClassName = [self.preferenceClassForModuleIdentifiers objectForKey:moduleIdentifier];
if (rootListControllerClassName) {
if ([rootListControllerClassName isEqualToString:@"CCSProvidedListController"]) {
CCSModuleProviderManager *providerManager = [CCSModuleProviderManager sharedInstance];
PSListController *listController = [providerManager listControllerForModuleIdentifier:moduleIdentifier];
[self.navigationController pushViewController:listController animated:YES];
}
else {
CCSModuleRepository *moduleRepository = MSHookIvar<CCSModuleRepository *>(self, "_moduleRepository");
NSBundle *moduleBundle = [NSBundle bundleWithURL:[moduleRepository moduleMetadataForModuleIdentifier:moduleIdentifier].moduleBundleURL];
Class rootListControllerClass = NSClassFromString(rootListControllerClassName);
if (!rootListControllerClass) {
[moduleBundle load];
rootListControllerClass = NSClassFromString(rootListControllerClassName);
}