This repository has been archived by the owner on Nov 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTweak.m
482 lines (430 loc) · 19.1 KB
/
Tweak.m
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <Security/Security.h>
#import <MessageUI/MessageUI.h>
#import <CaptainHook.h>
#pragma mark - Interfaces
@interface UIWindow (Private)
- (UIResponder *)firstResponder;
@end
typedef enum PSCellType {
PSGroupCell,
PSLinkCell,
PSLinkListCell,
PSListItemCell,
PSTitleValueCell,
PSSliderCell,
PSSwitchCell,
PSStaticTextCell,
PSEditTextCell,
PSSegmentCell,
PSGiantIconCell,
PSGiantCell,
PSSecureEditTextCell,
PSButtonCell,
PSEditTextViewCell,
} PSCellType;
@interface PSSpecifier : NSObject {
@public
id target;
SEL getter;
SEL setter;
SEL action;
Class detailControllerClass;
PSCellType cellType;
Class editPaneClass;
UIKeyboardType keyboardType;
UITextAutocapitalizationType autoCapsType;
UITextAutocorrectionType autoCorrectionType;
int textFieldType;
@private
NSString *_name;
NSArray *_values;
NSDictionary *_titleDict;
NSDictionary *_shortTitleDict;
id _userInfo;
NSMutableDictionary *_properties;
}
@property (retain) NSMutableDictionary *properties;
@property (retain) NSString *identifier;
@property (retain) NSString *name;
@property (retain) id userInfo;
@property (retain) id titleDictionary;
@property (retain) id shortTitleDictionary;
@property (retain) NSArray *values;
+ (id)preferenceSpecifierNamed:(NSString *)title target:(id)target set:(SEL)set get:(SEL)get detail:(Class)detail cell:(PSCellType)cell edit:(Class)edit;
+ (PSSpecifier *)groupSpecifierWithName:(NSString *)title;
+ (PSSpecifier *)emptyGroupSpecifier;
+ (UITextAutocapitalizationType)autoCapsTypeForString:(PSSpecifier *)string;
+ (UITextAutocorrectionType)keyboardTypeForString:(PSSpecifier *)string;
- (id)propertyForKey:(NSString *)key;
- (void)setProperty:(id)property forKey:(NSString *)key;
- (void)removePropertyForKey:(NSString *)key;
- (void)loadValuesAndTitlesFromDataSource;
- (void)setValues:(NSArray *)values titles:(NSArray *)titles;
- (void)setValues:(NSArray *)values titles:(NSArray *)titles shortTitles:(NSArray *)shortTitles;
- (void)setupIconImageWithPath:(NSString *)path;
- (NSString *)identifier;
- (void)setTarget:(id)target;
- (void)setKeyboardType:(UIKeyboardType)type autoCaps:(UITextAutocapitalizationType)autoCaps autoCorrection:(UITextAutocorrectionType)autoCorrection;
@end
@interface PSViewController : UIViewController {
PSSpecifier *_specifier;
}
@property (retain) PSSpecifier *specifier;
- (id)readPreferenceValue:(PSSpecifier *)specifier;
- (void)setPreferenceValue:(id)value specifier:(PSSpecifier *)specifier;
- (void)pushController:(UIViewController *)controller animate:(BOOL)animate;
@end
@interface PSListController : PSViewController <UITableViewDataSource, UITableViewDelegate> {
NSArray *_specifiers;
}
@property (retain, nonatomic) NSArray *specifiers;
- (NSArray *)loadSpecifiersFromPlistName:(NSString *)plistName target:(id)target;
- (PSSpecifier *)specifierForID:(NSString *)identifier;
- (PSSpecifier *)specifierAtIndex:(NSInteger)index;
- (NSArray *)specifiersForIDs:(NSArray *)identifiers;
- (NSArray *)specifiersInGroup:(NSInteger)group;
- (BOOL)containsSpecifier:(PSSpecifier *)specifier;
- (NSInteger)numberOfGroups;
- (NSInteger)rowsForGroup:(NSInteger)group;
- (NSInteger)indexForRow:(NSInteger)row inGroup:(NSInteger)group;
- (BOOL)getGroup:(NSInteger *)group row:(NSInteger *)row ofSpecifier:(PSSpecifier *)specifier;
- (BOOL)getGroup:(NSInteger *)group row:(NSInteger *)row ofSpecifierID:(NSString *)identifier;
- (BOOL)getGroup:(NSInteger *)group row:(NSInteger *)row ofSpecifierAtIndex:(NSInteger )index;
- (void)addSpecifier:(PSSpecifier *)specifier;
- (void)addSpecifiersFromArray:(NSArray *)array;
- (void)addSpecifier:(PSSpecifier *)specifier animated:(BOOL)animated;
- (void)addSpecifiersFromArray:(NSArray *)array animated:(BOOL)animated;
- (void)insertSpecifier:(PSSpecifier *)specifier afterSpecifier:(PSSpecifier *)afterSpecifier;
- (void)insertSpecifier:(PSSpecifier *)specifier afterSpecifierID:(NSString *)afterSpecifierID;
- (void)insertSpecifier:(PSSpecifier *)specifier atIndex:(NSInteger)index;
- (void)insertSpecifier:(PSSpecifier *)specifier atEndOfGroup:(NSInteger)index;
- (void)insertContiguousSpecifiers:(NSArray *)spcifiers afterSpecifier:(PSSpecifier *)afterSpecifier;
- (void)insertContiguousSpecifiers:(NSArray *)spcifiers afterSpecifierID:(NSString *)afterSpecifierID;
- (void)insertContiguousSpecifiers:(NSArray *)spcifiers atIndex:(NSInteger)index;
- (void)insertContiguousSpecifiers:(NSArray *)spcifiers atEndOfGroup:(NSInteger)index;
- (void)insertSpecifier:(PSSpecifier *)specifier afterSpecifier:(PSSpecifier *)afterSpecifier animated:(BOOL)animated;
- (void)insertSpecifier:(PSSpecifier *)specifier afterSpecifierID:(NSString *)afterSpecifierID animated:(BOOL)animated;
- (void)insertSpecifier:(PSSpecifier *)specifier atIndex:(NSInteger)index animated:(BOOL)animated;
- (void)insertSpecifier:(PSSpecifier *)specifier atEndOfGroup:(NSInteger)index animated:(BOOL)animated;
- (void)insertContiguousSpecifiers:(NSArray *)spcifiers afterSpecifier:(PSSpecifier *)afterSpecifier animated:(BOOL)animated;
- (void)insertContiguousSpecifiers:(NSArray *)spcifiers afterSpecifierID:(NSString *)afterSpecifierID animated:(BOOL)animated;
- (void)insertContiguousSpecifiers:(NSArray *)spcifiers atIndex:(NSInteger)index animated:(BOOL)animated;
- (void)insertContiguousSpecifiers:(NSArray *)spcifiers atEndOfGroup:(NSInteger)index animated:(BOOL)animated;
- (void)replaceContiguousSpecifiers:(NSArray *)oldSpecifiers withSpecifiers:(NSArray *)newSpecifiers;
- (void)replaceContiguousSpecifiers:(NSArray *)oldSpecifiers withSpecifiers:(NSArray *)newSpecifiers animated:(BOOL)animated;
- (void)removeSpecifier:(PSSpecifier *)specifier;
- (void)removeSpecifierID:(NSString *)identifier;
- (void)removeSpecifierAtIndex:(NSInteger)index;
- (void)removeLastSpecifier;
- (void)removeContiguousSpecifiers:(NSArray *)specifiers;
- (void)removeSpecifier:(PSSpecifier *)specifier animated:(BOOL)animated;
- (void)removeSpecifierID:(NSString *)identifier animated:(BOOL)animated;
- (void)removeSpecifierAtIndex:(NSInteger)index animated:(BOOL)animated;
- (void)removeLastSpecifierAnimated:(BOOL)animated;
- (void)removeContiguousSpecifiers:(NSArray *)specifiers animated:(BOOL)animated;
- (void)reloadSpecifier:(PSSpecifier *)specifier;
- (void)reloadSpecifierID:(NSString *)identifier;
- (void)reloadSpecifierAtIndex:(NSInteger)index;
- (void)reloadSpecifier:(PSSpecifier *)specifier animated:(BOOL)animated;
- (void)reloadSpecifierID:(NSString *)identifier animated:(BOOL)animated;
- (void)reloadSpecifierAtIndex:(NSInteger)index animated:(BOOL)animated;
- (void)reloadSpecifiers;
- (void)updateSpecifiers:(NSArray *)oldSpecifiers withSpecifiers:(NSArray *)newSpecifiers;
- (void)updateSpecifiersInRange:(NSRange)range withSpecifiers:(NSArray *)newSpecifiers;
- (void)lazyLoadBundle:(PSSpecifier *)specifier;
@end
@interface PSEditableListController : PSListController
@end
@interface APNetworksController : PSListController
@end
@interface APKnownNetworksController : PSEditableListController
- (void)removeNetwork:(PSSpecifier *)specifier;
@end
@interface NetworkListViewController : UITableViewController
@end
@interface NetworkListViewCell : UITableViewCell
@end
#pragma mark - Globals
static NSString * const NetworkListIdentifier = @"me.qusic.NetworkList";
static NSString * const SSIDKey = @"SSID";
static NSString * const PasswordKey = @"Password";
static PSSpecifier *Specifier;
static NetworkListViewController *Controller;
CHDeclareClass(PSUIPrefsListController)
CHDeclareClass(PrefsListController)
CHDeclareClass(APNetworksController)
CHDeclareClass(APKnownNetworksController)
#pragma mark - Functions
static NSMutableArray *getNetworks() {
static NSString * (^ const getPassword)(NSString *) = ^(NSString *ssid) {
NSMutableDictionary *query = @{(__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
(__bridge id)kSecAttrService: @"AirPort",
(__bridge id)kSecAttrAccount: ssid,
(__bridge id)kSecMatchLimit: (__bridge id)kSecMatchLimitOne,
(__bridge id)kSecReturnData: (__bridge id)kCFBooleanTrue}.mutableCopy;
if (NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_7_0) query[(__bridge id)kSecAttrSynchronizable] = (__bridge id)kSecAttrSynchronizableAny;
CFTypeRef result = NULL;
SecItemCopyMatching((__bridge CFDictionaryRef)query, &result);
NSString *password = [[NSString alloc]initWithData:(__bridge_transfer NSData *)result encoding:NSUTF8StringEncoding];
return password;
};
NSMutableArray *networks = [NSMutableArray array];
for (PSSpecifier *specifier in [CHAlloc(APKnownNetworksController)init].specifiers) {
if (specifier->cellType == PSTitleValueCell) {
NSString *ssid = specifier.identifier;
NSString *password = getPassword(ssid);
[networks addObject:@{SSIDKey: ssid, PasswordKey: password ? : @""}];
}
}
return networks;
}
static void deleteNetwork(NSString *ssid) {
PSSpecifier *specifier = [PSSpecifier preferenceSpecifierNamed:ssid target:nil set:NULL get:NULL detail:Nil cell:PSTitleValueCell edit:Nil];
specifier.identifier = ssid;
[[CHAlloc(APKnownNetworksController)init] removeNetwork:specifier];
}
#pragma mark - Implementations
@interface NetworkListViewController () <MFMailComposeViewControllerDelegate>
@property(retain) NSMutableArray *networks;
@property(retain) UIMenuController *menuController;
@property(retain) id menuControllerObserver;
@end
@implementation NetworkListViewController
- (id)init {
self = [super initWithStyle:UITableViewStyleGrouped];
return self;
}
- (void)loadNetworks {
_networks = getNetworks();
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.refreshControl endRefreshing];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
_menuController = [UIMenuController sharedMenuController];
_menuController.menuItems = @[[[UIMenuItem alloc]initWithTitle:@"Copy SSID" action:@selector(copySSID:)], [[UIMenuItem alloc]initWithTitle:@"Copy Password" action:@selector(copyPassword:)]];
[_menuController update];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc]init];
[refreshControl addTarget:self action:@selector(loadNetworks) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
_menuControllerObserver = [[NSNotificationCenter defaultCenter]addObserverForName:UIMenuControllerDidHideMenuNotification object:_menuController queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
if (!_menuController.isMenuVisible) {
[[UIApplication sharedApplication].keyWindow.firstResponder resignFirstResponder];
}
}];
[self.refreshControl beginRefreshing];
[self.refreshControl sendActionsForControlEvents:UIControlEventValueChanged];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self setEditing:NO animated:YES];
[[UIApplication sharedApplication].keyWindow.firstResponder resignFirstResponder];
[[NSNotificationCenter defaultCenter]removeObserver:_menuControllerObserver];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case 0:
return _networks.count;
case 1:
return 1;
default:
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
switch (indexPath.section) {
case 0: {
static NSString *const CellIdentifier = @"NetworkListItemCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[NetworkListViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSDictionary *network = _networks[indexPath.row];
cell.textLabel.text = network[SSIDKey];
cell.detailTextLabel.text = network[PasswordKey];
break;
}
case 1: {
static NSString *const CellIdentifier = @"NetworkListButtonCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
}
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Export This List";
break;
default:
break;
}
break;
}
default: {
break;
}
}
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
switch (section) {
case 0:
return nil;
case 1:
return @"\nNetworkList © Qusic";
default:
return nil;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0: {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell becomeFirstResponder];
[_menuController setTargetRect:cell.frame inView:tableView];
[_menuController setMenuVisible:YES animated:YES];
break;
}
case 1: {
switch (indexPath.row) {
case 0:
[self exportAction];
break;
default:
break;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
break;
}
default: {
break;
}
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0:
return YES;
case 1:
return NO;
default:
return NO;
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0: {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSDictionary *network = _networks[indexPath.row];
deleteNetwork(network[SSIDKey]);
[_networks removeObject:network];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
break;
}
case 1: {
break;
}
default: {
break;
}
}
}
- (void)exportAction {
NSMutableString *body = [NSMutableString string];
for (NSDictionary *network in _networks) {
[body appendFormat:@"%@: %@\n", network[SSIDKey], [network[PasswordKey]length] > 0 ? network[PasswordKey] : @"(no password)"];
}
MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc]init];
composeViewController.mailComposeDelegate = self;
[composeViewController setSubject:[NSString stringWithFormat:@"Known Networks - %@", [UIDevice currentDevice].name]];
[composeViewController setMessageBody:body isHTML:NO];
[self presentViewController:composeViewController animated:YES completion:NULL];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[controller dismissViewControllerAnimated:YES completion:NULL];
}
@end
@implementation NetworkListViewCell
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
return (action == @selector(copySSID:) || (action == @selector(copyPassword:) && self.detailTextLabel.text.length > 0));
}
- (void)copySSID:(id)sender {
[UIPasteboard generalPasteboard].string = self.textLabel.text;
}
- (void)copyPassword:(id)sender {
[UIPasteboard generalPasteboard].string = self.detailTextLabel.text;
}
@end
#pragma mark - Hooks
CHMethod(0, NSMutableArray *, APNetworksController, specifiers) {
NSMutableArray *specifiers = CHSuper(0, APNetworksController, specifiers);
NSMutableArray *askToJoinGroup = CHIvar(self, _askToJoinGroup, NSMutableArray * const);
if (![askToJoinGroup containsObject:Specifier]) {
Specifier.target = self;
Specifier->action = @selector(openNetworkList);
[askToJoinGroup addObject:Specifier];
NSUInteger count = specifiers.count;
for (NSUInteger i = 0; i < count; i++) {
if ([[specifiers[i]identifier]isEqualToString:@"ASK_TO_JOIN"]) {
[specifiers insertObject:Specifier atIndex:i + 1];
break;
}
}
}
return specifiers;
}
CHMethod(0, void, APNetworksController, openNetworkList) {
[self pushController:Controller animate:YES];
}
static void lazyConstructor() {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *title = [[NSBundle bundleWithPath:@"/System/Library/PreferenceBundles/AirPortSettings.bundle"]localizedStringForKey:@"KNOWN_NETWORKS" value:@"Known Networks" table:@"Networks"];
Specifier = [PSSpecifier preferenceSpecifierNamed:title target:nil set:NULL get:NULL detail:Nil cell:PSLinkCell edit:Nil];
Specifier.identifier = NetworkListIdentifier;
Controller = [NetworkListViewController new];
Controller.title = title;
CHLoadLateClass(APNetworksController);
CHLoadLateClass(APKnownNetworksController);
CHHook(0, APNetworksController, specifiers);
CHHook(0, APNetworksController, openNetworkList);
});
}
CHOptimizedMethod(1, self, void, PSUIPrefsListController, lazyLoadBundle, PSSpecifier *, specifier) {
CHSuper(1, PSUIPrefsListController, lazyLoadBundle, specifier);
if ([specifier.identifier isEqualToString:@"WIFI"]) {
lazyConstructor();
}
}
CHOptimizedMethod(1, self, void, PrefsListController, lazyLoadBundle, PSSpecifier *, specifier) {
CHSuper(1, PrefsListController, lazyLoadBundle, specifier);
if ([specifier.identifier isEqualToString:@"WIFI"]) {
lazyConstructor();
}
}
#pragma mark - Constructor
CHConstructor {
@autoreleasepool {
if ([[NSBundle mainBundle].bundleIdentifier isEqualToString:@"com.apple.Preferences"]) {
CHLoadLateClass(PSUIPrefsListController);
CHLoadLateClass(PrefsListController);
CHHook(1, PSUIPrefsListController, lazyLoadBundle);
CHHook(1, PrefsListController, lazyLoadBundle);
}
}
}