forked from NoisyFlake/WiFiCarrier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweak.xm
98 lines (75 loc) · 2.51 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
#import "Tweak.h"
static BOOL hasFullyLoaded = NO;
// Original values from SBTelephonyManager
static NSString *originalName = @"";
static id subscriptionContext = nil;
// User settings
static BOOL enabled;
static NSString *customCarrier;
%hook SBWiFiManager
-(void)_updateCurrentNetwork {
%orig;
if (enabled) {
forceUpdate();
}
}
%end
%hook SSVTelephonyController
-(void)operatorNameChanged:(id)arg1 name:(id)arg2 {
subscriptionContext = arg1;
originalName = arg2;
if (!enabled || !hasFullyLoaded) {
%orig;
return;
}
SBWiFiManager *manager = [%c(SBWiFiManager) sharedInstance];
NSString *networkName = [manager currentNetworkName];
if ([networkName length] > 0) {
%orig(arg1, networkName);
} else if ([customCarrier length] > 0) {
%orig(arg1, customCarrier);
} else {
%orig;
}
}
%end
%hook SpringBoard
- (void)applicationDidFinishLaunching:(id)application {
%orig;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
hasFullyLoaded = YES;
forceUpdate();
});
}
%end
static void forceUpdate() {
if (!hasFullyLoaded || subscriptionContext == nil) return;
SSVTelephonyController *manager = [%c(SSVTelephonyController) sharedInstance];
[manager operatorNameChanged:subscriptionContext name:originalName];
}
// ===== PREFERENCE HANDLING ===== //
static void loadPrefs() {
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/mobile/Library/Preferences/com.noisyflake.wificarrier.plist"];
if (prefs) {
enabled = ( [prefs objectForKey:@"enabled"] ? [[prefs objectForKey:@"enabled"] boolValue] : YES );
customCarrier = ( [prefs objectForKey:@"customCarrier"] ? [[prefs objectForKey:@"customCarrier"] stringValue] : nil );
}
}
static void refreshPrefs() {
loadPrefs();
forceUpdate();
}
static void initPrefs() {
// Copy the default preferences file when the actual preference file doesn't exist
NSString *path = @"/User/Library/Preferences/com.noisyflake.wificarrier.plist";
NSString *pathDefault = @"/Library/PreferenceBundles/WiFiCarrier.bundle/defaults.plist";
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:path]) {
[fileManager copyItemAtPath:pathDefault toPath:path error:nil];
}
}
%ctor {
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)refreshPrefs, CFSTR("com.noisyflake.wificarrier/prefsupdated"), NULL, CFNotificationSuspensionBehaviorCoalesce);
initPrefs();
loadPrefs();
}