-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSignalManager.m
More file actions
262 lines (212 loc) · 7.72 KB
/
SignalManager.m
File metadata and controls
262 lines (212 loc) · 7.72 KB
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
#import "SignalManager.h"
#import "SBTelephonyManager.h"
#import "SBWiFiManager.h"
#import "SBAirplaneModeController.h"
@implementation SignalManager
+ (BOOL)enabled {
NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:PREFERENCES_PATH];
if ([dict objectForKey:@"enabled"]) {
return [[dict objectForKey:@"enabled"] boolValue];
}
return YES;
}
- (id)init {
if (self = [super init]) {
telephonyManager = [objc_getClass("SBTelephonyManager") sharedTelephonyManager];
wifiManager = [objc_getClass("SBWiFiManager") sharedInstance];
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_11_0) {
airplaneModeController = [objc_getClass("SBAirplaneModeController") sharedInstance];
}
[self loadPreferences];
if ([[preferences objectForKey:@"fakeCellularConnection"] boolValue]) {
[self setCellularConnectionEnabled:YES];
}
}
return self;
}
- (void)loadPreferences {
preferences = [NSMutableDictionary dictionaryWithContentsOfFile:PREFERENCES_PATH];
if (!preferences) {
preferences = [NSMutableDictionary new];
}
// Determine whether Signal is enabled or not
if (![preferences objectForKey:@"enabled"]) {
[preferences setValue:[NSNumber numberWithBool:YES] forKey:@"enabled"];
}
// Fake the cellular connection
if (![preferences objectForKey:@"fakeCellularConnection"]) {
[preferences setValue:[NSNumber numberWithBool:YES] forKey:@"fakeCellularConnection"];
}
// Show WiFi connection strength on signal dots
if (![preferences objectForKey:@"showWiFiOnSignalBars"]) {
[preferences setValue:[NSNumber numberWithBool:NO] forKey:@"showWiFiOnSignalBars"];
}
// Override the data connection type when actually connected to a network (WiFi or Cellular)
if (![preferences objectForKey:@"overrideConnectionTypeWhenOnline"]) {
[preferences setValue:[NSNumber numberWithBool:NO] forKey:@"overrideConnectionTypeWhenOnline"];
}
// Override the data connection type if the user is not connected to any network
if (![preferences objectForKey:@"overrideConnectionTypeWhenOffline"]) {
[preferences setValue:[NSNumber numberWithBool:YES] forKey:@"overrideConnectionTypeWhenOffline"];
}
// Set the data connection type if the user decides to override it
if (![preferences objectForKey:@"dataConnectionType"]) {
[preferences setValue:[NSNumber numberWithInt:7] forKey:@"dataConnectionType"];
}
// Override the Operator Name
if (![preferences objectForKey:@"operatorName"]) {
[preferences setValue:@"Carrier" forKey:@"operatorName"];
}
// Always show the custom operator name
if (![preferences objectForKey:@"operatorNameShowAlways"]) {
[preferences setValue:[NSNumber numberWithBool:NO] forKey:@"operatorNameShowAlways"];
}
// Enables signal bar/dot count override
if (![preferences objectForKey:@"overrideSignalStrength"]) {
[preferences setValue:[NSNumber numberWithBool:YES] forKey:@"overrideSignalStrength"];
}
// Set the amount of signal bars (or dots on pre-iOS 11) shown to the user
if (![preferences objectForKey:@"signalStrengthBars"]) {
[preferences setValue:[NSNumber numberWithInt:5] forKey:@"signalStrengthBars"];
}
[preferences writeToFile:PREFERENCES_PATH atomically:YES];
}
- (void)preferencesChanged {
[self loadPreferences];
[self setCellularConnectionEnabled:[[preferences objectForKey:@"fakeCellularConnection"] boolValue]];
if (airplaneModeController) {
[airplaneModeController airplaneModeChanged];
} else {
[telephonyManager airplaneModeChanged];
}
}
- (BOOL)cellularRadioCapabilityIsActive {
return [[preferences objectForKey:@"fakeCellularConnection"] boolValue] && connectionEnabled;
}
- (BOOL)cellularConnectionInitialized {
if ([[preferences objectForKey:@"showWiFiOnSignalBars"] boolValue]) {
return [wifiManager isAssociated] && connectionInitialized;
} else {
return connectionInitialized;
}
}
- (BOOL)forceCustomCarrier {
return [[preferences objectForKey:@"operatorNameShowAlways"] boolValue];
}
- (void)airplaneModeChanged {
if (airplaneModeController ? [airplaneModeController isInAirplaneMode] : [telephonyManager isInAirplaneMode]) {
connectionInitialized = NO;
} else {
if (connectionEnabled) {
[self setCellularConnectionEnabled:YES];
}
}
}
- (void)_updateState {
if (connectionInitialized) {
[self _enableCellularConnection];
}
if ([[preferences objectForKey:@"showWiFiOnSignalBars"] boolValue]) {
if (![wifiManager isAssociated]) {
[self _disableCellularConnection];
} else if ([wifiManager isAssociated] &&
connectionEnabled &&
!connectionInitialized &&
(airplaneModeController
? ![airplaneModeController isInAirplaneMode]
: ![telephonyManager isInAirplaneMode])) {
[self _enableCellularConnection];
}
}
}
- (int)signalStrengthBars {
if ([[preferences objectForKey:@"showWiFiOnSignalBars"] boolValue]) {
switch ([wifiManager signalStrengthBars]) {
case 0:
return 0;
case 1:
return 1;
case 2:
return 3;
case 3:
return 5;
}
}
if ([[preferences objectForKey:@"overrideSignalStrength"] boolValue]) {
return [[preferences objectForKey:@"signalStrengthBars"] intValue];
}
return 0;
}
- (id)operatorName {
if ([[preferences objectForKey:@"operatorNameShowAlways"] boolValue]) {
return [preferences objectForKey:@"operatorName"];
}
if ([[preferences objectForKey:@"fakeCellularConnection"] boolValue]) {
if ([[preferences objectForKey:@"showWiFiOnSignalBars"] boolValue]) {
if ([wifiManager isAssociated]) {
return [wifiManager currentNetworkName];
}
}
if ([[preferences objectForKey:@"operatorName"] length] > 0) {
return [preferences objectForKey:@"operatorName"];
}
}
return nil;
}
- (int)dataConnectionType:(int)currentConnectionType {
if (airplaneModeController ? [airplaneModeController isInAirplaneMode] : [telephonyManager isInAirplaneMode]) {
return currentConnectionType;
}
if ([[preferences objectForKey:@"fakeCellularConnection"] boolValue] &&
[[preferences objectForKey:@"showWiFiOnSignalBars"] boolValue]) {
if (currentConnectionType != 0 &&
[[preferences objectForKey:@"overrideConnectionTypeWhenOnline"] boolValue]) {
if (connectionEnabled && !connectionInitialized) {
return 0;
}
return [[preferences objectForKey:@"dataConnectionType"] intValue];
}
} else {
if (currentConnectionType != 0 &&
[[preferences objectForKey:@"overrideConnectionTypeWhenOnline"] boolValue]) {
return [[preferences objectForKey:@"dataConnectionType"] intValue];
}
if (currentConnectionType == 0 &&
[[preferences objectForKey:@"overrideConnectionTypeWhenOffline"] boolValue]) {
return [[preferences objectForKey:@"dataConnectionType"] intValue];
}
}
return currentConnectionType;
}
- (void)setCellularConnectionEnabled:(BOOL)enabled {
if (connectionEnabled == enabled) {
return;
}
connectionEnabled = enabled;
connectionInitialized = NO;
[self _disableCellularConnection];
[telephonyManager _updateState];
if (enabled && (airplaneModeController ? ![airplaneModeController isInAirplaneMode] : ![telephonyManager isInAirplaneMode])) {
[self _enableCellularConnection];
}
}
- (void)_enableCellularConnection {
if (connectionInitTimer) {
[connectionInitTimer invalidate];
connectionInitTimer = nil;
}
connectionInitTimer = [NSTimer scheduledTimerWithTimeInterval:3 repeats:NO block:^(NSTimer* timer) {
if (([[preferences objectForKey:@"showWiFiOnSignalBars"] boolValue] && [wifiManager isAssociated]) ||
![[preferences objectForKey:@"showWiFiOnSignalBars"] boolValue]) {
connectionInitialized = YES;
[telephonyManager _setRegistrationStatus:2];
[telephonyManager _setCellRegistrationStatus:2];
}
}];
}
- (void)_disableCellularConnection {
connectionInitialized = NO;
[telephonyManager _setRegistrationStatus:3];
[telephonyManager _setCellRegistrationStatus:3];
}
@end