-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tweak.xm
271 lines (222 loc) · 7.54 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
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
#import <Preferences/PSListController.h>
#import <Preferences/PSViewController.h>
#import <UIKit/UIKit.h>
// The tweak's Settings to enable/disable the indicator //
#define kPreferencesLocationNLC @"/var/mobile/Library/Preferences/lt.pagefau.return0e.networklinkindicator.plist"
#define LOCALISATION_BUNDLE [NSBundle bundleWithPath:@"/Library/PreferenceBundles/networklinkindicator.bundle"]
#define NSLocalizedStringInBundle(key) NSLocalizedStringFromTableInBundle(key, @"NetworkLinkIndicator", LOCALISATION_BUNDLE, nil)
// Apple's Network Link Conditioner plist and //
#define kPreferencesLocation @"/var/mobile/Library/Preferences/com.apple.network.prefPaneSimulate.plist"
@interface UIApplication (Private)
-(void)addStatusBarStyleOverrides:(int)style;
-(void)removeStatusBarStyleOverrides:(int)style;
-(void)setGlowAnimationEnabled:(BOOL)enabled forStyle:(int)style;
-(void)setDoubleHeightStatusText:(NSString*)text forStyle:(int)style;
@end
@interface PSTableCell : UITableViewCell
- (void)setValue:(id)arg1;
@end
// The NLISwitchController class allows the tweak to set the default tweak values in NSUserDefaults.
// Needed for the NLC On/Off Button.
@interface NLISwitchController : NSObject {
NSUserDefaults* userPreferences;
}
@property (nonatomic) BOOL currentEnabledSetting;
@property (nonatomic) BOOL nliEnabled;
+(NLISwitchController*)sharedInstance;
-(void)updateSettings;
-(void)setEnabled:(BOOL)enabled;
@end
@implementation NLISwitchController
+(NLISwitchController*)sharedInstance
{
// A singleton is being created in this class
// Creating a dispatch type
static dispatch_once_t dpatch = 0;
// Creating a strong link to a uninitialised _sharedObject.
__strong static id _sharedObject = nil;
// Define dispatch method to conform to singleton pattern.
// Initialise the sharedObject in a dispatch block using the 'dpatch' pointer.
dispatch_once(&dpatch,^{
// Initialise to self.
_sharedObject = [[self alloc] init];
});
// Next we return the initialised shared object, ready for use in
// different classes and safe access.
return _sharedObject;
}
-(id)init
{
self = [super init];
if(self)
{
NSLog(@"NLISwitchController init");
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:kPreferencesLocationNLC];
// Get the value of the preference key: SKIsEnabled.
id isEnabled = [userPreferences objectForKey:@"NLCIsIndicatorEnabled"];
// Check if the switch is disabled, or else it is enabled.
BOOL switchEnabled = isEnabled ? [isEnabled boolValue] : YES;
// Let us see if NSUserDefaults is fine. Get the user defaults
// in the domain of the Preferences application
userPreferences = [[NSUserDefaults alloc] initWithSuiteName:@"lt.pagefau.return0e.networklinkindicator"];
NSLog(@"User preferences from the announcer identity: %@",userPreferences);
// Register the enabled preferences
[userPreferences registerDefaults:@{
@"enable" : @YES
}];
// Saving the preferences to the setting file.
[userPreferences setBool:switchEnabled forKey:@"NLCIsIndicatorEnabled"];
// Detect if the settings have changed
_currentEnabledSetting = NO;
// Get the settings from the switchEnabled bool and set the
// internal state _nliEnabled to that.
_nliEnabled = switchEnabled;
NSLog(@"_nliEnabled: %d", _nliEnabled);
}
return self;
}
// Code to update the preferences slider.
-(void)updateSettings
{
[self setEnabled:[userPreferences boolForKey:@"NLCIsIndicatorEnabled"]];
_currentEnabledSetting = NO;
}
// Boolean for key for the preferences
-(void)setEnabled:(BOOL)enabled
{
if(enabled == _nliEnabled)
{
return;
}
_nliEnabled = enabled;
if(!_currentEnabledSetting)
{
[userPreferences setBool:_nliEnabled forKey:@"NLCIsIndicatorEnabled"];
}
}
@end
static void loadPreferences();
extern "C" CFNotificationCenterRef CFNotificationCenterGetDistributedCenter(void);
void toggleOn();
void toggleOff();
void switchOn();
void switchOff();
// TODO: Obtain Profile name and preview it as a option.
typedef enum {
NCL_100_PERCENT_LOSS = 0,
NCL_3G,
NCL_DSL,
NCL_EDGE,
NCL_HIGH_LATENCY_DNS,
NCL_LTE,
NCL_VERY_BAD_NETWORK,
NCL_WIFI,
NCL_WIFI_AC
} NLCStatus;
// Apple's settings for the Network Link Conditioner.
static NSString *NLCSelectedProfile = @"";
// Tweak indicator boolean to enable the banner.
static BOOL NLCIsIndicatorEnabled = NO;
static BOOL NLCSwitch = NO;
static BOOL NLCPrefSwitchPane = NO;
// Method to load preferences //
static void loadPreferences() {
NSMutableDictionary * tweak_preferences = [ [NSMutableDictionary alloc] initWithContentsOfFile:kPreferencesLocationNLC];
if(tweak_preferences){
// This is for the tweak's setting.
NLCIsIndicatorEnabled = ([tweak_preferences objectForKey:@"NLCIsIndicatorEnabled"] ? [[tweak_preferences objectForKey:@"NLCIsIndicatorEnabled"] boolValue] : NLCIsIndicatorEnabled);
}
// Check if the tweak is enabled //
if(NLCIsIndicatorEnabled == FALSE){
switchOn();
}
else {
switchOff();
}
}
%ctor{
@autoreleasepool{
loadPreferences();
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
(CFNotificationCallback)loadPreferences,
CFSTR("lt.pagefau.return0e.networklinkindicator/settingschanged"),
NULL,
CFNotificationSuspensionBehaviorCoalesce
);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
(CFNotificationCallback)switchOn,
CFSTR("lt.pagefau.return0e.networklinkindicator/fsSwitchON"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately
);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
(CFNotificationCallback)switchOff,
CFSTR("lt.pagefau.return0e.networklinkindicator/fsSwitchOFF"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately
);
}
}
void toggleOn() {
// NSLog(@"NetworkLinkIndicator: Turn on NLC");
UIApplication * app = [UIApplication sharedApplication];
[app setGlowAnimationEnabled:YES forStyle:202];
[app setDoubleHeightStatusText:@"Network Link Conditioner is Running." forStyle:202];
[app addStatusBarStyleOverrides:4];
}
void toggleOff() {
// NSLog(@"NetworkLinkIndicator: Turn off NLC Indication");
UIApplication * app = [UIApplication sharedApplication];
[app setGlowAnimationEnabled:NO forStyle:202];
[app removeStatusBarStyleOverrides:4];
}
void switchOn() {
// Sets the on switch for the tweak.
[[NLISwitchController sharedInstance] setEnabled:YES];
}
void switchOff() {
// Sets the off switch for the tweak.
[[NLISwitchController sharedInstance] setEnabled:NO];
toggleOff();
}
%hook PSTableCell
- (void)setValue:(id)arg1
{
%orig;
// Check if the NLC is switched on with our tweak also switched on or not.
if(NLCPrefSwitchPane == YES && NLCIsIndicatorEnabled == FALSE) {
if([arg1 isEqual:@"On"]) {
NLCSwitch = YES;
// NSLog(@"NLC is Enabled");
toggleOn();
} else if([arg1 isEqual:@"Off"]) {
NLCSwitch = NO;
// NSLog(@"NLC is Disabled");
toggleOff();
}
}
else {
NLCSwitch = NO;
}
}
%end
%hook PSListController
- (id)specifierForID:(id)arg1
{
NSString * str = [%orig identifier];
NSString * str2 = [%orig name];
// Check if the user is in the NLC settings prefpane and is about to enable/disable the NLC.
// TODO: This hack probably needs to be replaced with something more reliable. (XPC?)
if(([str isEqual:@"NLC"] && [str2 isEqual:@"Status"])) {
NSLog(@"You are in the NLC Prefpane");
NLCPrefSwitchPane = YES;
}
else{
NLCPrefSwitchPane = NO;
}
return %orig;
}
%end