-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSingleVPN.x
154 lines (114 loc) · 4.67 KB
/
SingleVPN.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
#import <HBLog.h>
#import "Common.h"
#import "UIColor+.h"
#define IsNetworkTypeText(text) ( \
[text isEqualToString:@"G"] || [text isEqualToString:@"3G"] || \
[text isEqualToString:@"4G"] || [text isEqualToString:@"5G"] || \
[text isEqualToString:@"LTE"])
static BOOL _isEnabled = NO;
static BOOL _isVPNEnabled = NO;
static BOOL _isEnabledReversed = NO;
static UIColor *_darkReplacementColor = nil;
static UIColor *_lightReplacementColor = nil;
static UIColor *svpnColorWithHexString(NSString *hexString) {
if (!hexString) {
return nil;
}
return [UIColor svpn_colorWithExternalRepresentation:hexString];
}
static UIColor *svpnColorWithTextColor(UIColor *textColor) {
return [textColor svpn_isDarkColor] ? _lightReplacementColor : _darkReplacementColor;
}
static void ReloadPrefs() {
static NSUserDefaults *prefs = nil;
if (!prefs) {
prefs = [[NSUserDefaults alloc] initWithSuiteName:@"com.82flex.singlevpnprefs"];
}
NSDictionary *settings = [prefs dictionaryRepresentation];
_isEnabled = settings[@"IsEnabled"] ? [settings[@"IsEnabled"] boolValue] : YES;
_isEnabledReversed = settings[@"IsEnabledReversed"] ? [settings[@"IsEnabledReversed"] boolValue] : NO;
_lightReplacementColor = svpnColorWithHexString(settings[@"ForegroundColorLight"]) ?: [UIColor colorWithRed:0.19607843137254902 green:0.7803921568627451 blue:0.34901960784313724 alpha:1];
_darkReplacementColor = svpnColorWithHexString(settings[@"ForegroundColorDark"]) ?: [UIColor colorWithRed:0.17254901960784313 green:0.8156862745098039 blue:0.3411764705882353 alpha:1];
}
%group SingleVPN
%hook _UIStatusBarWifiItem
- (id)applyUpdate:(_UIStatusBarItemUpdate *)update toDisplayItem:(_UIStatusBarDisplayItem *)displayItem {
_isVPNEnabled = update.data.vpnEntry.enabled;
id result = %orig;
UIColor *originalColor = update.styleAttributes.textColor;
UIColor *newColor = nil;
BOOL decision = _isEnabledReversed ? !_isVPNEnabled : _isVPNEnabled;
if (decision) {
newColor = svpnColorWithTextColor(originalColor);
}
if (!newColor) { newColor = update.styleAttributes.imageTintColor ?: originalColor; }
for (_UIStatusBarDisplayItem *item in self.displayItems.allValues) {
%orig(update, item);
if (item.view == self.networkIconView && [item.view isKindOfClass:%c(_UIStatusBarImageView)]) {
_UIStatusBarImageView *imageView = (_UIStatusBarImageView *)item.view;
[imageView setTintColor:newColor];
}
}
return result;
}
- (UIColor *)_fillColorForUpdate:(_UIStatusBarItemUpdate *)update entry:(_UIStatusBarDataWifiEntry *)entry {
BOOL decision = _isEnabledReversed ? !_isVPNEnabled : _isVPNEnabled;
if (decision) {
return svpnColorWithTextColor(update.styleAttributes.textColor);
} else {
return %orig;
}
}
%end
%hook _UIStatusBarCellularItem
- (id)applyUpdate:(_UIStatusBarItemUpdate *)update toDisplayItem:(_UIStatusBarDisplayItem *)displayItem {
_isVPNEnabled = update.data.vpnEntry.enabled;
id result = %orig;
UIColor *originalColor = update.styleAttributes.textColor;
UIColor *newColor = nil;
BOOL decision = _isEnabledReversed ? !_isVPNEnabled : _isVPNEnabled;
if (decision) {
newColor = svpnColorWithTextColor(originalColor);
}
if (!newColor) { newColor = originalColor; }
for (_UIStatusBarDisplayItem *item in self.displayItems.allValues) {
_UIStatusBarStringView *stringView = nil;
if ([item.view isKindOfClass:%c(_UIStatusBarCellularNetworkTypeView)]) {
stringView = ((_UIStatusBarCellularNetworkTypeView *)item.view).stringView;
} else if ([item.view isKindOfClass:%c(_UIStatusBarStringView)]) {
stringView = (_UIStatusBarStringView *)item.view;
}
if (IsNetworkTypeText(stringView.text)) {
[stringView setTextColor:newColor];
} else {
[stringView setTextColor:originalColor];
}
}
return result;
}
%end
%hook _UIStatusBarStringView
- (void)applyStyleAttributes:(_UIStatusBarStyleAttributes *)styleAttrs {
%orig;
BOOL decision = _isEnabledReversed ? !_isVPNEnabled : _isVPNEnabled;
if (decision && IsNetworkTypeText(self.text)) {
[self setTextColor:svpnColorWithTextColor(styleAttrs.textColor)];
}
}
%end
%end // SingleVPN
%ctor {
ReloadPrefs();
if (!_isEnabled) {
return;
}
CFNotificationCenterAddObserver(
CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
(CFNotificationCallback)ReloadPrefs,
CFSTR("com.82flex.singlevpnprefs/saved"),
NULL,
CFNotificationSuspensionBehaviorCoalesce
);
%init(SingleVPN);
}