-
Notifications
You must be signed in to change notification settings - Fork 13
/
CCNetworkManager.x
163 lines (133 loc) · 4.9 KB
/
CCNetworkManager.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
#import "CCNetworkManager.h"
NSMutableDictionary *prefs, *defaultPrefs;
int selectedNetwork = 0;
@implementation CCNetworkManager
- (UIImage *)iconGlyph {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
label.textColor = [UIColor blackColor];
label.backgroundColor=[UIColor clearColor];
label.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 10.0f/12.0f;
label.clipsToBounds = YES;
label.textAlignment = NSTextAlignmentCenter;
if (selectedNetwork == 0) {
label.font = [label.font fontWithSize:12];
NSString *customText = getValue(@"customText") ? getValue(@"customText") : @"";
if ([customText length] > 0) {
// String contains space that should be converted to linebreak
if ([customText rangeOfString:@" "].location != NSNotFound) {
label.numberOfLines = 2;
customText = [customText stringByReplacingOccurrencesOfString:@" " withString:@"\n"];
} else {
label.numberOfLines = 1;
}
label.text = customText;
} else {
label.numberOfLines = 2;
label.text = @"Auto\nNetwork";
}
} else {
label.font = [label.font fontWithSize:25];
label.numberOfLines = 1;
if (selectedNetwork == 1) label.text= @"2G";
if (selectedNetwork == 2) label.text= @"3G";
if (selectedNetwork == 3) label.text= @"LTE";
}
UIGraphicsBeginImageContextWithOptions(label.bounds.size, NO, 0.0); // high res
[[label layer] renderInContext: UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (UIColor *)selectedColor {
return [UIColor colorWithRed:1.00 green:0.58 blue:0.00 alpha:1.0];
}
- (BOOL)isSelected {
return selectedNetwork > 0;
}
- (void)setSelected:(BOOL)selected {
if (selectedNetwork == 0) {
if (getBool(@"enable2G")) {
selectedNetwork = 1;
} else if (getBool(@"enable3G")) {
selectedNetwork = 2;
} else if (getBool(@"enableLTE")) {
selectedNetwork = 3;
}
} else if (selectedNetwork == 1) {
if (getBool(@"enable3G")) {
selectedNetwork = 2;
} else if (getBool(@"enableLTE")) {
selectedNetwork = 3;
} else {
selectedNetwork = 0;
}
} else if (selectedNetwork == 2) {
if (getBool(@"enableLTE")) {
selectedNetwork = 3;
} else {
selectedNetwork = 0;
}
} else if (selectedNetwork == 3) {
selectedNetwork = 0;
}
setSelectedNetwork();
writeSelectedNetwork();
[super reconfigureView];
}
@end
static void setSelectedNetwork() {
CTServerConnectionRef cn = _CTServerConnectionCreate(kCFAllocatorDefault, callback, NULL);
if (selectedNetwork == 0) {
_CTServerConnectionSetRATSelection(cn, kAutomatic, 0);
} else if (selectedNetwork == 1) {
if (getBool(@"useCDMA")) {
_CTServerConnectionSetRATSelection(cn, kCDMA, 0);
} else {
_CTServerConnectionSetRATSelection(cn, kGSM, 0);
}
} else if (selectedNetwork == 2) {
if (getBool(@"useCDMA")) {
_CTServerConnectionSetRATSelection(cn, kEVDO, 0);
} else {
_CTServerConnectionSetRATSelection(cn, kUMTS, 0);
}
} else if (selectedNetwork == 3) {
_CTServerConnectionSetRATSelection(cn, kLTE, 0);
}
}
// ----- PREFERENCE HANDLING ----- //
static BOOL getBool(NSString *key) {
id ret = [prefs objectForKey:key];
if(ret == nil) {
ret = [defaultPrefs objectForKey:key];
}
return [ret boolValue];
}
static NSString* getValue(NSString *key) {
return [prefs objectForKey:key] ?: [defaultPrefs objectForKey:key];
}
static void writeSelectedNetwork() {
[prefs setObject:[NSNumber numberWithInt:selectedNetwork] forKey:@"selectedNetwork"];
[prefs writeToFile:@"/User/Library/Preferences/com.noisyflake.networkmanager.plist" atomically:YES];
}
static void loadPrefs() {
prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/mobile/Library/Preferences/com.noisyflake.networkmanager.plist"];
selectedNetwork = [[prefs objectForKey:@"selectedNetwork"] ?: [defaultPrefs objectForKey:@"selectedNetwork"] intValue];
}
static void initPrefs() {
// Copy the default preferences file when the actual preference file doesn't exist
NSString *path = @"/User/Library/Preferences/com.noisyflake.networkmanager.plist";
NSString *pathDefault = @"/Library/PreferenceBundles/NetworkManagerPrefs.bundle/defaults.plist";
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:path]) {
[fileManager copyItemAtPath:pathDefault toPath:path error:nil];
}
defaultPrefs = [[NSMutableDictionary alloc] initWithContentsOfFile:pathDefault];
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)loadPrefs, CFSTR("com.noisyflake.networkmanager/prefsupdated"), NULL, CFNotificationSuspensionBehaviorCoalesce);
}
%ctor {
initPrefs();
loadPrefs();
}