This repository was archived by the owner on Apr 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathHUDRootViewController.mm
1293 lines (1113 loc) · 48 KB
/
HUDRootViewController.mm
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
//
// HUDRootViewController.mm
// TrollSpeed
//
// Created by Lessica on 2024/1/24.
//
#import <notify.h>
#import <net/if.h>
#import <ifaddrs.h>
#import <objc/runtime.h>
#import <mach/vm_param.h>
#import <Foundation/Foundation.h>
#import "HUDPresetPosition.h"
#import "HUDRootViewController.h"
#import "HUDBackdropLabel.h"
#import "TrollSpeed-Swift.h"
#pragma mark -
#import "FBSOrientationUpdate.h"
#import "FBSOrientationObserver.h"
#import "UIApplication+Private.h"
#import "LSApplicationProxy.h"
#import "LSApplicationWorkspace.h"
#import "SpringBoardServices.h"
#define NOTIFY_UI_LOCKSTATE "com.apple.springboard.lockstate"
#define NOTIFY_LS_APP_CHANGED "com.apple.LaunchServices.ApplicationsChanged"
static void LaunchServicesApplicationStateChanged
(CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo)
{
/* Application installed or uninstalled */
BOOL isAppInstalled = NO;
for (LSApplicationProxy *app in [[objc_getClass("LSApplicationWorkspace") defaultWorkspace] allApplications])
{
if ([app.applicationIdentifier isEqualToString:@"ch.xxtou.hudapp"])
{
isAppInstalled = YES;
break;
}
}
if (!isAppInstalled)
{
UIApplication *app = [UIApplication sharedApplication];
[app terminateWithSuccess];
}
}
static void SpringBoardLockStatusChanged
(CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo)
{
HUDRootViewController *rootViewController = (__bridge HUDRootViewController *)observer;
NSString *lockState = (__bridge NSString *)name;
if ([lockState isEqualToString:@NOTIFY_UI_LOCKSTATE])
{
mach_port_t sbsPort = SBSSpringBoardServerPort();
if (sbsPort == MACH_PORT_NULL)
return;
BOOL isLocked;
BOOL isPasscodeSet;
SBGetScreenLockStatus(sbsPort, &isLocked, &isPasscodeSet);
if (!isLocked)
{
[rootViewController.view setHidden:NO];
[rootViewController resetLoopTimer];
}
else
{
[rootViewController stopLoopTimer];
[rootViewController.view setHidden:YES];
}
}
}
#pragma mark - NetworkSpeed13
#define KILOBITS 1000
#define MEGABITS 1000000
#define GIGABITS 1000000000
#define KILOBYTES (1 << 10)
#define MEGABYTES (1 << 20)
#define GIGABYTES (1 << 30)
#define UPDATE_INTERVAL 1.0
#define SHOW_ALWAYS 1
#define INLINE_SEPARATOR "\t"
#define IDLE_INTERVAL 3.0
static const double HUD_MIN_FONT_SIZE = 9.0;
static const double HUD_MAX_FONT_SIZE = 10.0;
static const double HUD_MIN_CORNER_RADIUS = 4.5;
static const double HUD_MAX_CORNER_RADIUS = 5.0;
static double HUD_FONT_SIZE = 8.0;
static UIFontWeight HUD_FONT_WEIGHT = UIFontWeightRegular;
static CGFloat HUD_INACTIVE_OPACITY = 0.667;
static uint8_t HUD_DATA_UNIT = 0;
static uint8_t HUD_SHOW_UPLOAD_SPEED = 1;
static uint8_t HUD_SHOW_DOWNLOAD_SPEED = 1;
static uint8_t HUD_SHOW_DOWNLOAD_SPEED_FIRST = 1;
static uint8_t HUD_SHOW_SECOND_SPEED_IN_NEW_LINE = 0;
static const char *HUD_UPLOAD_PREFIX = "▲";
static const char *HUD_DOWNLOAD_PREFIX = "▼";
typedef struct {
uint64_t inputBytes;
uint64_t outputBytes;
} UpDownBytes;
static NSString *formattedSpeed(uint64_t bytes, BOOL isFocused)
{
if (isFocused)
{
if (0 == HUD_DATA_UNIT)
{
if (bytes < KILOBYTES) {
static NSString *_string = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_string = NSLocalizedString(@"0 KB", @"formattedSpeed");
});
return _string;
}
else if (bytes < MEGABYTES) {
static NSString *_string = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_string = NSLocalizedString(@"%.0f KB", @"formattedSpeed");
});
return [NSString stringWithFormat:_string, (double)bytes / KILOBYTES];
}
else if (bytes < GIGABYTES) {
static NSString *_string = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_string = NSLocalizedString(@"%.2f MB", @"formattedSpeed");
});
return [NSString stringWithFormat:_string, (double)bytes / MEGABYTES];
}
else {
static NSString *_string = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_string = NSLocalizedString(@"%.2f GB", @"formattedSpeed");
});
return [NSString stringWithFormat:_string, (double)bytes / GIGABYTES];
}
}
else
{
if (bytes < KILOBITS) {
static NSString *_string = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_string = NSLocalizedString(@"0 Kb", @"formattedSpeed");
});
return _string;
}
else if (bytes < MEGABITS) {
static NSString *_string = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_string = NSLocalizedString(@"%.0f Kb", @"formattedSpeed");
});
return [NSString stringWithFormat:_string, (double)bytes / KILOBITS];
}
else if (bytes < GIGABITS) {
static NSString *_string = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_string = NSLocalizedString(@"%.2f Mb", @"formattedSpeed");
});
return [NSString stringWithFormat:_string, (double)bytes / MEGABITS];
}
else {
static NSString *_string = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_string = NSLocalizedString(@"%.2f Gb", @"formattedSpeed");
});
return [NSString stringWithFormat:_string, (double)bytes / GIGABITS];
}
}
}
else {
if (0 == HUD_DATA_UNIT)
{
if (bytes < KILOBYTES) {
static NSString *_string = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_string = NSLocalizedString(@"0 KB/s", @"formattedSpeed");
});
return _string;
}
else if (bytes < MEGABYTES) {
static NSString *_string = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_string = NSLocalizedString(@"%.0f KB/s", @"formattedSpeed");
});
return [NSString stringWithFormat:_string, (double)bytes / KILOBYTES];
}
else if (bytes < GIGABYTES) {
static NSString *_string = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_string = NSLocalizedString(@"%.2f MB/s", @"formattedSpeed");
});
return [NSString stringWithFormat:_string, (double)bytes / MEGABYTES];
}
else {
static NSString *_string = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_string = NSLocalizedString(@"%.2f GB/s", @"formattedSpeed");
});
return [NSString stringWithFormat:_string, (double)bytes / GIGABYTES];
}
}
else
{
if (bytes < KILOBITS) {
static NSString *_string = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_string = NSLocalizedString(@"0 Kb/s", @"formattedSpeed");
});
return _string;
}
else if (bytes < MEGABITS) {
static NSString *_string = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_string = NSLocalizedString(@"%.0f Kb/s", @"formattedSpeed");
});
return [NSString stringWithFormat:_string, (double)bytes / KILOBITS];
}
else if (bytes < GIGABITS) {
static NSString *_string = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_string = NSLocalizedString(@"%.2f Mb/s", @"formattedSpeed");
});
return [NSString stringWithFormat:_string, (double)bytes / MEGABITS];
}
else {
static NSString *_string = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_string = NSLocalizedString(@"%.2f Gb/s", @"formattedSpeed");
});
return [NSString stringWithFormat:_string, (double)bytes / GIGABITS];
}
}
}
}
static UpDownBytes getUpDownBytes()
{
struct ifaddrs *ifa_list = 0, *ifa;
UpDownBytes upDownBytes;
upDownBytes.inputBytes = 0;
upDownBytes.outputBytes = 0;
if (getifaddrs(&ifa_list) == -1) return upDownBytes;
for (ifa = ifa_list; ifa; ifa = ifa->ifa_next)
{
/* Skip invalid interfaces */
if (ifa->ifa_name == NULL || ifa->ifa_addr == NULL || ifa->ifa_data == NULL)
continue;
/* Skip interfaces that are not link level interfaces */
if (AF_LINK != ifa->ifa_addr->sa_family)
continue;
/* Skip interfaces that are not up or running */
if (!(ifa->ifa_flags & IFF_UP) && !(ifa->ifa_flags & IFF_RUNNING))
continue;
/* Skip interfaces that are not ethernet or cellular */
if (strncmp(ifa->ifa_name, "en", 2) && strncmp(ifa->ifa_name, "pdp_ip", 6))
continue;
struct if_data *if_data = (struct if_data *)ifa->ifa_data;
upDownBytes.inputBytes += if_data->ifi_ibytes;
upDownBytes.outputBytes += if_data->ifi_obytes;
}
freeifaddrs(ifa_list);
return upDownBytes;
}
static BOOL shouldUpdateSpeedLabel;
static uint64_t prevOutputBytes = 0, prevInputBytes = 0;
static NSAttributedString *attributedUploadPrefix = nil;
static NSAttributedString *attributedDownloadPrefix = nil;
static NSAttributedString *attributedInlineSeparator = nil;
static NSAttributedString *attributedLineSeparator = nil;
static NSAttributedString *formattedAttributedString(BOOL isFocused)
{
@autoreleasepool
{
if (!attributedUploadPrefix)
attributedUploadPrefix = [[NSAttributedString alloc] initWithString:[[NSString stringWithUTF8String:HUD_UPLOAD_PREFIX] stringByAppendingString:@" "] attributes:@{ NSFontAttributeName: [UIFont boldSystemFontOfSize:HUD_FONT_SIZE] }];
if (!attributedDownloadPrefix)
attributedDownloadPrefix = [[NSAttributedString alloc] initWithString:[[NSString stringWithUTF8String:HUD_DOWNLOAD_PREFIX] stringByAppendingString:@" "] attributes:@{ NSFontAttributeName: [UIFont boldSystemFontOfSize:HUD_FONT_SIZE] }];
if (!attributedInlineSeparator)
attributedInlineSeparator = [[NSAttributedString alloc] initWithString:[NSString stringWithUTF8String:INLINE_SEPARATOR] attributes:@{ NSFontAttributeName: [UIFont boldSystemFontOfSize:HUD_FONT_SIZE] }];
if (!attributedLineSeparator)
attributedLineSeparator = [[NSAttributedString alloc] initWithString:@"\n" attributes:@{ NSFontAttributeName: [UIFont boldSystemFontOfSize:HUD_FONT_SIZE] }];
NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] init];
UpDownBytes upDownBytes = getUpDownBytes();
uint64_t upDiff;
uint64_t downDiff;
if (isFocused)
{
upDiff = upDownBytes.outputBytes;
downDiff = upDownBytes.inputBytes;
}
else
{
if (upDownBytes.outputBytes > prevOutputBytes)
upDiff = upDownBytes.outputBytes - prevOutputBytes;
else
upDiff = 0;
if (upDownBytes.inputBytes > prevInputBytes)
downDiff = upDownBytes.inputBytes - prevInputBytes;
else
downDiff = 0;
}
prevOutputBytes = upDownBytes.outputBytes;
prevInputBytes = upDownBytes.inputBytes;
if (!SHOW_ALWAYS && (upDiff < 2 * KILOBYTES && downDiff < 2 * KILOBYTES))
{
shouldUpdateSpeedLabel = NO;
return nil;
}
else shouldUpdateSpeedLabel = YES;
if (HUD_DATA_UNIT == 1)
{
upDiff *= BYTE_SIZE;
downDiff *= BYTE_SIZE;
}
if (HUD_SHOW_DOWNLOAD_SPEED_FIRST)
{
if (HUD_SHOW_DOWNLOAD_SPEED)
{
[mutableString appendAttributedString:attributedDownloadPrefix];
[mutableString appendAttributedString:[[NSAttributedString alloc] initWithString:formattedSpeed(downDiff, isFocused) attributes:@{ NSFontAttributeName: [UIFont monospacedDigitSystemFontOfSize:HUD_FONT_SIZE weight:HUD_FONT_WEIGHT] }]];
}
if (HUD_SHOW_UPLOAD_SPEED)
{
if ([mutableString length] > 0)
{
if (HUD_SHOW_SECOND_SPEED_IN_NEW_LINE) [mutableString appendAttributedString:attributedLineSeparator];
else [mutableString appendAttributedString:attributedInlineSeparator];
}
[mutableString appendAttributedString:attributedUploadPrefix];
[mutableString appendAttributedString:[[NSAttributedString alloc] initWithString:formattedSpeed(upDiff, isFocused) attributes:@{ NSFontAttributeName: [UIFont monospacedDigitSystemFontOfSize:HUD_FONT_SIZE weight:HUD_FONT_WEIGHT] }]];
}
}
else
{
if (HUD_SHOW_UPLOAD_SPEED)
{
[mutableString appendAttributedString:attributedUploadPrefix];
[mutableString appendAttributedString:[[NSAttributedString alloc] initWithString:formattedSpeed(upDiff, isFocused) attributes:@{ NSFontAttributeName: [UIFont monospacedDigitSystemFontOfSize:HUD_FONT_SIZE weight:HUD_FONT_WEIGHT] }]];
}
if (HUD_SHOW_DOWNLOAD_SPEED)
{
if ([mutableString length] > 0)
{
if (HUD_SHOW_SECOND_SPEED_IN_NEW_LINE) [mutableString appendAttributedString:attributedLineSeparator];
else [mutableString appendAttributedString:attributedInlineSeparator];
}
[mutableString appendAttributedString:attributedDownloadPrefix];
[mutableString appendAttributedString:[[NSAttributedString alloc] initWithString:formattedSpeed(downDiff, isFocused) attributes:@{ NSFontAttributeName: [UIFont monospacedDigitSystemFontOfSize:HUD_FONT_SIZE weight:HUD_FONT_WEIGHT] }]];
}
}
return [mutableString copy];
}
}
#pragma mark - HUDRootViewController
@interface HUDRootViewController (Troll)
- (void)updateOrientation:(UIInterfaceOrientation)orientation animateWithDuration:(NSTimeInterval)duration;
@end
static const CACornerMask kCornerMaskBottom = kCALayerMinXMaxYCorner | kCALayerMaxXMaxYCorner;
static const CACornerMask kCornerMaskAll = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner | kCALayerMinXMaxYCorner | kCALayerMaxXMaxYCorner;
@implementation HUDRootViewController {
NSMutableDictionary *_userDefaults;
NSMutableArray <NSLayoutConstraint *> *_constraints;
UIBlurEffect *_blurEffect;
UIVisualEffectView *_blurView;
ScreenshotInvisibleContainer *_containerView;
UIView *_contentView;
HUDBackdropLabel *_speedLabel;
UIImageView *_lockedView;
NSTimer *_timer;
UITapGestureRecognizer *_tapGestureRecognizer;
UIPanGestureRecognizer *_panGestureRecognizer;
UIImpactFeedbackGenerator *_impactFeedbackGenerator;
UINotificationFeedbackGenerator *_notificationFeedbackGenerator;
BOOL _isFocused;
NSLayoutConstraint *_topConstraint;
NSLayoutConstraint *_centerXConstraint;
NSLayoutConstraint *_leadingConstraint;
NSLayoutConstraint *_trailingConstraint;
UIInterfaceOrientation _orientation;
FBSOrientationObserver *_orientationObserver;
}
- (void)registerNotifications
{
int token;
notify_register_dispatch(NOTIFY_RELOAD_HUD, &token, dispatch_get_main_queue(), ^(int token) {
[self reloadUserDefaults];
});
CFNotificationCenterRef darwinCenter = CFNotificationCenterGetDarwinNotifyCenter();
CFNotificationCenterAddObserver(
darwinCenter,
(__bridge const void *)self,
LaunchServicesApplicationStateChanged,
CFSTR(NOTIFY_LS_APP_CHANGED),
NULL,
CFNotificationSuspensionBehaviorCoalesce
);
CFNotificationCenterAddObserver(
darwinCenter,
(__bridge const void *)self,
SpringBoardLockStatusChanged,
CFSTR(NOTIFY_UI_LOCKSTATE),
NULL,
CFNotificationSuspensionBehaviorCoalesce
);
NSUserDefaults *userDefaults = GetStandardUserDefaults();
[userDefaults addObserver:self forKeyPath:HUDUserDefaultsKeyUsesCustomFontSize options:NSKeyValueObservingOptionNew context:nil];
[userDefaults addObserver:self forKeyPath:HUDUserDefaultsKeyRealCustomFontSize options:NSKeyValueObservingOptionNew context:nil];
[userDefaults addObserver:self forKeyPath:HUDUserDefaultsKeyUsesCustomOffset options:NSKeyValueObservingOptionNew context:nil];
[userDefaults addObserver:self forKeyPath:HUDUserDefaultsKeyRealCustomOffsetX options:NSKeyValueObservingOptionNew context:nil];
[userDefaults addObserver:self forKeyPath:HUDUserDefaultsKeyRealCustomOffsetY options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:HUDUserDefaultsKeyUsesCustomFontSize] ||
[keyPath isEqualToString:HUDUserDefaultsKeyRealCustomFontSize] ||
[keyPath isEqualToString:HUDUserDefaultsKeyUsesCustomOffset] ||
[keyPath isEqualToString:HUDUserDefaultsKeyRealCustomOffsetX] ||
[keyPath isEqualToString:HUDUserDefaultsKeyRealCustomOffsetY])
{
[self reloadUserDefaults];
}
}
- (void)loadUserDefaults:(BOOL)forceReload
{
if (forceReload || !_userDefaults)
_userDefaults = [[NSDictionary dictionaryWithContentsOfFile:(JBROOT_PATH_NSSTRING(USER_DEFAULTS_PATH))] mutableCopy] ?: [NSMutableDictionary dictionary];
}
- (void)saveUserDefaults
{
BOOL wroteSucceed = [_userDefaults writeToFile:(JBROOT_PATH_NSSTRING(USER_DEFAULTS_PATH)) atomically:YES];
if (wroteSucceed) {
[[NSFileManager defaultManager] setAttributes:@{
NSFileOwnerAccountID: @501,
NSFileGroupOwnerAccountID: @501,
} ofItemAtPath:(JBROOT_PATH_NSSTRING(USER_DEFAULTS_PATH)) error:nil];
notify_post(NOTIFY_RELOAD_APP);
}
}
- (void)reloadUserDefaults
{
[self loadUserDefaults:YES];
BOOL singleLineMode = [self singleLineMode];
HUD_SHOW_UPLOAD_SPEED = !singleLineMode;
BOOL usesBitrate = [self usesBitrate];
HUD_DATA_UNIT = usesBitrate;
BOOL usesArrowPrefixes = [self usesArrowPrefixes];
HUD_UPLOAD_PREFIX = (usesArrowPrefixes ? "↑" : "▲");
HUD_DOWNLOAD_PREFIX = (usesArrowPrefixes ? "↓" : "▼");
BOOL usesCustomFontSize = [self usesCustomFontSize];
if (!usesCustomFontSize) {
BOOL usesLargeFont = [self usesLargeFont];
HUD_FONT_SIZE = (usesLargeFont ? HUD_MAX_FONT_SIZE : HUD_MIN_FONT_SIZE);
[_blurView.layer setCornerRadius:(usesLargeFont ? HUD_MAX_CORNER_RADIUS : HUD_MIN_CORNER_RADIUS)];
} else {
CGFloat realCustomFontSize = MIN(MAX([self realCustomFontSize], 8), 12);
HUD_FONT_SIZE = realCustomFontSize;
[_blurView.layer setCornerRadius:realCustomFontSize / 2.0];
}
BOOL usesInvertedColor = [self usesInvertedColor];
HUD_FONT_WEIGHT = (usesInvertedColor ? UIFontWeightMedium : UIFontWeightRegular);
HUD_INACTIVE_OPACITY = (usesInvertedColor ? 1.0 : 0.667);
[_blurView setEffect:(usesInvertedColor ? nil : _blurEffect)];
[_speedLabel setColorInvertEnabled:usesInvertedColor];
[_lockedView setHidden:usesInvertedColor];
BOOL hideAtSnapshot = [self hideAtSnapshot];
if (hideAtSnapshot) {
[_containerView setupContainerAsHideContentInScreenshots];
} else {
[_containerView setupContainerAsDisplayContentInScreenshots];
}
prevInputBytes = 0;
prevOutputBytes = 0;
attributedUploadPrefix = nil;
attributedDownloadPrefix = nil;
[self removeAllAnimations];
[self resetGestureRecognizers];
[self updateViewConstraints];
if (!_isFocused) {
[self onFocus:_contentView];
} else {
[self keepFocus:_contentView];
}
[self performSelector:@selector(onBlur:) withObject:_contentView afterDelay:IDLE_INTERVAL];
}
+ (BOOL)passthroughMode
{
return [[[NSDictionary dictionaryWithContentsOfFile:(JBROOT_PATH_NSSTRING(USER_DEFAULTS_PATH))] objectForKey:HUDUserDefaultsKeyPassthroughMode] boolValue];
}
- (BOOL)isLandscapeOrientation
{
BOOL isLandscape;
if (_orientation == UIInterfaceOrientationUnknown) {
isLandscape = CGRectGetWidth(self.view.bounds) > CGRectGetHeight(self.view.bounds);
} else {
isLandscape = UIInterfaceOrientationIsLandscape(_orientation);
}
return isLandscape;
}
- (HUDUserDefaultsKey)selectedModeKeyForCurrentOrientation
{
return [self isLandscapeOrientation] ? HUDUserDefaultsKeySelectedModeLandscape : HUDUserDefaultsKeySelectedMode;
}
- (HUDPresetPosition)selectedModeForCurrentOrientation
{
[self loadUserDefaults:NO];
NSNumber *mode = [_userDefaults objectForKey:[self selectedModeKeyForCurrentOrientation]];
return mode != nil ? (HUDPresetPosition)[mode integerValue] : HUDPresetPositionTopCenter;
}
- (BOOL)singleLineMode
{
[self loadUserDefaults:NO];
NSNumber *mode = [_userDefaults objectForKey:HUDUserDefaultsKeySingleLineMode];
return mode != nil ? [mode boolValue] : NO;
}
- (BOOL)usesBitrate
{
[self loadUserDefaults:NO];
NSNumber *mode = [_userDefaults objectForKey:HUDUserDefaultsKeyUsesBitrate];
return mode != nil ? [mode boolValue] : NO;
}
- (BOOL)usesArrowPrefixes
{
[self loadUserDefaults:NO];
NSNumber *mode = [_userDefaults objectForKey:HUDUserDefaultsKeyUsesArrowPrefixes];
return mode != nil ? [mode boolValue] : NO;
}
- (BOOL)usesLargeFont
{
[self loadUserDefaults:NO];
NSNumber *mode = [_userDefaults objectForKey:HUDUserDefaultsKeyUsesLargeFont];
return mode != nil ? [mode boolValue] : NO;
}
- (BOOL)usesRotation
{
[self loadUserDefaults:NO];
NSNumber *mode = [_userDefaults objectForKey:HUDUserDefaultsKeyUsesRotation];
return mode != nil ? [mode boolValue] : NO;
}
- (BOOL)usesInvertedColor
{
[self loadUserDefaults:NO];
NSNumber *mode = [_userDefaults objectForKey:HUDUserDefaultsKeyUsesInvertedColor];
return mode != nil ? [mode boolValue] : NO;
}
- (BOOL)keepInPlace
{
[self loadUserDefaults:NO];
NSNumber *mode = [_userDefaults objectForKey:HUDUserDefaultsKeyKeepInPlace];
return mode != nil ? [mode boolValue] : NO;
}
- (BOOL)hideAtSnapshot
{
[self loadUserDefaults:NO];
NSNumber *mode = [_userDefaults objectForKey:HUDUserDefaultsKeyHideAtSnapshot];
return mode != nil ? [mode boolValue] : NO;
}
- (CGFloat)currentPositionY
{
[self loadUserDefaults:NO];
NSNumber *positionY = [_userDefaults objectForKey:HUDUserDefaultsKeyCurrentPositionY];
return positionY != nil ? [positionY doubleValue] : CGFLOAT_MAX;
}
- (void)setCurrentPositionY:(CGFloat)positionY
{
[self loadUserDefaults:NO];
[_userDefaults setObject:[NSNumber numberWithDouble:positionY] forKey:HUDUserDefaultsKeyCurrentPositionY];
[self saveUserDefaults];
}
- (CGFloat)currentLandscapePositionY
{
[self loadUserDefaults:NO];
NSNumber *positionY = [_userDefaults objectForKey:HUDUserDefaultsKeyCurrentLandscapePositionY];
return positionY != nil ? [positionY doubleValue] : CGFLOAT_MAX;
}
- (void)setCurrentLandscapePositionY:(CGFloat)positionY
{
[self loadUserDefaults:NO];
[_userDefaults setObject:[NSNumber numberWithDouble:positionY] forKey:HUDUserDefaultsKeyCurrentLandscapePositionY];
[self saveUserDefaults];
}
#define PREFS_PATH "/var/mobile/Library/Preferences/ch.xxtou.hudapp.prefs.plist"
- (NSDictionary *)extraUserDefaultsDictionary {
static BOOL isJailbroken = NO;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
isJailbroken = [[NSFileManager defaultManager]
fileExistsAtPath:JBROOT_PATH_NSSTRING(@"/Library/PreferenceBundles/TrollSpeedPrefs.bundle")];
});
if (!isJailbroken) {
return nil;
}
return [NSDictionary dictionaryWithContentsOfFile:JBROOT_PATH_NSSTRING(@PREFS_PATH)];
}
- (BOOL)usesCustomFontSize {
NSDictionary *extraUserDefaults = [self extraUserDefaultsDictionary];
if (extraUserDefaults) {
return [extraUserDefaults[HUDUserDefaultsKeyUsesCustomFontSize] boolValue];
}
return [GetStandardUserDefaults() boolForKey:HUDUserDefaultsKeyUsesCustomFontSize];
}
- (CGFloat)realCustomFontSize {
NSDictionary *extraUserDefaults = [self extraUserDefaultsDictionary];
if (extraUserDefaults) {
return [extraUserDefaults[HUDUserDefaultsKeyRealCustomFontSize] doubleValue];
}
return [GetStandardUserDefaults() doubleForKey:HUDUserDefaultsKeyRealCustomFontSize];
}
- (BOOL)usesCustomOffset {
NSDictionary *extraUserDefaults = [self extraUserDefaultsDictionary];
if (extraUserDefaults) {
return [extraUserDefaults[HUDUserDefaultsKeyUsesCustomOffset] boolValue];
}
return [GetStandardUserDefaults() boolForKey:HUDUserDefaultsKeyUsesCustomOffset];
}
- (CGFloat)realCustomOffsetX {
NSDictionary *extraUserDefaults = [self extraUserDefaultsDictionary];
if (extraUserDefaults) {
return [extraUserDefaults[HUDUserDefaultsKeyRealCustomOffsetX] doubleValue];
}
return [GetStandardUserDefaults() doubleForKey:HUDUserDefaultsKeyRealCustomOffsetX];
}
- (CGFloat)realCustomOffsetY {
NSDictionary *extraUserDefaults = [self extraUserDefaultsDictionary];
if (extraUserDefaults) {
return [extraUserDefaults[HUDUserDefaultsKeyRealCustomOffsetY] doubleValue];
}
return [GetStandardUserDefaults() doubleForKey:HUDUserDefaultsKeyRealCustomOffsetY];
}
- (instancetype)init
{
self = [super init];
if (self) {
_constraints = [NSMutableArray array];
[self registerNotifications];
_orientationObserver = [[objc_getClass("FBSOrientationObserver") alloc] init];
__weak HUDRootViewController *weakSelf = self;
[_orientationObserver setHandler:^(FBSOrientationUpdate *orientationUpdate) {
HUDRootViewController *strongSelf = weakSelf;
dispatch_async(dispatch_get_main_queue(), ^{
[strongSelf updateOrientation:(UIInterfaceOrientation)orientationUpdate.orientation animateWithDuration:orientationUpdate.duration];
});
}];
}
return self;
}
- (void)dealloc
{
[_orientationObserver invalidate];
}
- (void)updateSpeedLabel
{
log_debug(OS_LOG_DEFAULT, "updateSpeedLabel");
NSAttributedString *attributedText = formattedAttributedString(_isFocused);
if (attributedText) {
[_speedLabel setAttributedText:attributedText];
}
[_speedLabel sizeToFit];
}
- (void)viewDidLoad
{
[super viewDidLoad];
/* Just put your HUD view here */
_contentView = [[UIView alloc] init];
_contentView.backgroundColor = [UIColor clearColor];
_contentView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:_contentView];
_blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
_blurView = [[UIVisualEffectView alloc] initWithEffect:_blurEffect];
_blurView.layer.cornerRadius = HUD_MIN_CORNER_RADIUS;
_blurView.layer.masksToBounds = YES;
_blurView.translatesAutoresizingMaskIntoConstraints = NO;
_containerView = [[ScreenshotInvisibleContainer alloc] initWithContent:_blurView];
_containerView.hiddenContainer.translatesAutoresizingMaskIntoConstraints = NO;
[_contentView addSubview:_containerView.hiddenContainer];
_speedLabel = [[HUDBackdropLabel alloc] initWithFrame:CGRectZero];
_speedLabel.numberOfLines = 0;
_speedLabel.textAlignment = NSTextAlignmentCenter;
_speedLabel.textColor = [UIColor whiteColor];
_speedLabel.font = [UIFont systemFontOfSize:HUD_FONT_SIZE];
_speedLabel.translatesAutoresizingMaskIntoConstraints = NO;
[_speedLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];
[_blurView.contentView addSubview:_speedLabel];
_lockedView = [[UIImageView alloc] initWithImage:[UIImage systemImageNamed:@"lock.fill"]];
_lockedView.tintColor = [UIColor whiteColor];
_lockedView.translatesAutoresizingMaskIntoConstraints = NO;
_lockedView.contentMode = UIViewContentModeScaleAspectFit;
_lockedView.alpha = 0.0;
[_lockedView setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisVertical];
[_lockedView setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisVertical];
[_blurView.contentView addSubview:_lockedView];
_tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognized:)];
_tapGestureRecognizer.numberOfTapsRequired = 1;
_tapGestureRecognizer.numberOfTouchesRequired = 1;
[_contentView addGestureRecognizer:_tapGestureRecognizer];
_panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognized:)];
_panGestureRecognizer.minimumNumberOfTouches = 1;
_panGestureRecognizer.maximumNumberOfTouches = 1;
[_contentView addGestureRecognizer:_panGestureRecognizer];
[_contentView setUserInteractionEnabled:YES];
[self reloadUserDefaults];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
notify_post(NOTIFY_LAUNCHED_HUD);
}
- (void)resetLoopTimer
{
[_timer invalidate];
_timer = [NSTimer scheduledTimerWithTimeInterval:UPDATE_INTERVAL target:self selector:@selector(updateSpeedLabel) userInfo:nil repeats:YES];
}
- (void)stopLoopTimer
{
[_timer invalidate];
_timer = nil;
}
- (void)viewSafeAreaInsetsDidChange
{
[super viewSafeAreaInsetsDidChange];
[self removeAllAnimations];
[self resetGestureRecognizers];
[self updateViewConstraints];
}
- (void)updateViewConstraints
{
[NSLayoutConstraint deactivateConstraints:_constraints];
[_constraints removeAllObjects];
BOOL isLandscape;
if (_orientation == UIInterfaceOrientationUnknown) {
isLandscape = CGRectGetWidth(self.view.bounds) > CGRectGetHeight(self.view.bounds);
} else {
isLandscape = UIInterfaceOrientationIsLandscape(_orientation);
}
HUDPresetPosition selectedMode = [self selectedModeForCurrentOrientation];
BOOL isCentered = (selectedMode == HUDPresetPositionTopCenter || selectedMode == HUDPresetPositionTopCenterMost);
BOOL isCenteredMost = (selectedMode == HUDPresetPositionTopCenterMost);
BOOL isPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
HUD_SHOW_DOWNLOAD_SPEED_FIRST = isCentered;
HUD_SHOW_SECOND_SPEED_IN_NEW_LINE = !isCentered;
[_speedLabel setTextAlignment:(isCentered ? NSTextAlignmentCenter : NSTextAlignmentLeft)];
[_lockedView setImage:[UIImage systemImageNamed:(isCentered ? @"hand.raised.slash.fill" : @"lock.fill")]];
[_blurView.layer setMaskedCorners:((isCenteredMost && !isLandscape) ? kCornerMaskBottom : kCornerMaskAll)];
BOOL usesCustomOffset = [self usesCustomOffset];
CGFloat realCustomOffsetX = 0;
CGFloat realCustomOffsetY = 0;
if (usesCustomOffset)
{
realCustomOffsetX = [self realCustomOffsetX] * (-1);
realCustomOffsetY = [self realCustomOffsetY];
}
UILayoutGuide *layoutGuide = self.view.safeAreaLayoutGuide;
if (isLandscape)
{
CGFloat notchHeight;
CGFloat paddingNearNotch;
CGFloat paddingFarFromNotch;
notchHeight = CGRectGetMinY(layoutGuide.layoutFrame);
paddingNearNotch = (notchHeight > 30) ? notchHeight - 16 : 4;
paddingFarFromNotch = (notchHeight > 30) ? -24 : -4;
paddingNearNotch += realCustomOffsetX;
paddingFarFromNotch += realCustomOffsetX;
[_constraints addObjectsFromArray:@[
[_contentView.leadingAnchor constraintEqualToAnchor:layoutGuide.leadingAnchor constant:(_orientation == UIInterfaceOrientationLandscapeLeft ? -paddingFarFromNotch : paddingNearNotch)],
[_contentView.trailingAnchor constraintEqualToAnchor:layoutGuide.trailingAnchor constant:(_orientation == UIInterfaceOrientationLandscapeLeft ? -paddingNearNotch : paddingFarFromNotch)],
]];
CGFloat minimumLandscapeTopConstant = 0;
CGFloat minimumLandscapeBottomConstant = 0;
minimumLandscapeTopConstant = (isPad ? 30 : 10);
minimumLandscapeBottomConstant = (isPad ? -34 : -14);
minimumLandscapeTopConstant += realCustomOffsetY;
minimumLandscapeBottomConstant += realCustomOffsetY;
/* Fixed Constraints */
[_constraints addObjectsFromArray:@[
[_contentView.topAnchor constraintGreaterThanOrEqualToAnchor:self.view.topAnchor constant:minimumLandscapeTopConstant],
[_contentView.bottomAnchor constraintLessThanOrEqualToAnchor:self.view.bottomAnchor constant:minimumLandscapeBottomConstant],
]];
/* Flexible Constraint */
_topConstraint = [_contentView.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:minimumLandscapeTopConstant];
if (!isCentered) {
CGFloat currentPositionY = [self currentLandscapePositionY];
if (currentPositionY < CGFLOAT_MAX) {
_topConstraint.constant = currentPositionY;
}
}
_topConstraint.priority = UILayoutPriorityDefaultLow;
[_constraints addObject:_topConstraint];
}
else
{
[_constraints addObjectsFromArray:@[
[_contentView.leadingAnchor constraintEqualToAnchor:layoutGuide.leadingAnchor constant:realCustomOffsetX],
[_contentView.trailingAnchor constraintEqualToAnchor:layoutGuide.trailingAnchor constant:realCustomOffsetX],
]];
if (isCenteredMost && !isPad) {
[_constraints addObject:[_contentView.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:0]];
}
else
{
CGFloat minimumTopConstraintConstant = 0;
CGFloat minimumBottomConstraintConstant = 0;
if (CGRectGetMinY(layoutGuide.layoutFrame) >= 51) {
minimumTopConstraintConstant = -8;
minimumBottomConstraintConstant = -4;
}
else if (CGRectGetMinY(layoutGuide.layoutFrame) > 30) {
minimumTopConstraintConstant = -12;
minimumBottomConstraintConstant = -4;
} else {
minimumTopConstraintConstant = (isPad ? 30 : 20);
minimumBottomConstraintConstant = -20;
}
minimumTopConstraintConstant += realCustomOffsetY;
minimumBottomConstraintConstant += realCustomOffsetY;
/* Fixed Constraints */
[_constraints addObjectsFromArray:@[
[_contentView.topAnchor constraintGreaterThanOrEqualToAnchor:layoutGuide.topAnchor constant:minimumTopConstraintConstant],
[_contentView.bottomAnchor constraintLessThanOrEqualToAnchor:layoutGuide.bottomAnchor constant:minimumBottomConstraintConstant],
]];
/* Flexible Constraint */
_topConstraint = [_contentView.topAnchor constraintEqualToAnchor:layoutGuide.topAnchor constant:minimumTopConstraintConstant];
if (!isCentered) {
CGFloat currentPositionY = [self currentPositionY];
if (currentPositionY < CGFLOAT_MAX) {
_topConstraint.constant = currentPositionY;
}
}
_topConstraint.priority = UILayoutPriorityDefaultLow;
[_constraints addObject:_topConstraint];
}
}
[_constraints addObjectsFromArray:@[
[_speedLabel.topAnchor constraintEqualToAnchor:_contentView.topAnchor],
[_speedLabel.bottomAnchor constraintEqualToAnchor:_contentView.bottomAnchor],
]];
_centerXConstraint = [_speedLabel.centerXAnchor constraintEqualToAnchor:layoutGuide.centerXAnchor];
if (isCentered) {
[_constraints addObject:_centerXConstraint];
}
_leadingConstraint = [_speedLabel.leadingAnchor constraintEqualToAnchor:_contentView.leadingAnchor constant:10];
if (selectedMode == HUDPresetPositionTopLeft) {
[_constraints addObject:_leadingConstraint];
}
_trailingConstraint = [_speedLabel.trailingAnchor constraintEqualToAnchor:_contentView.trailingAnchor constant:-10];
if (selectedMode == HUDPresetPositionTopRight) {
[_constraints addObject:_trailingConstraint];
}
[_constraints addObjectsFromArray:@[
[_blurView.topAnchor constraintEqualToAnchor:_speedLabel.topAnchor constant:-2],
[_blurView.leadingAnchor constraintEqualToAnchor:_speedLabel.leadingAnchor constant:-4],
[_blurView.trailingAnchor constraintEqualToAnchor:_speedLabel.trailingAnchor constant:4],
[_blurView.bottomAnchor constraintEqualToAnchor:_speedLabel.bottomAnchor constant:2],