forked from shiftcmdk/AutoAlerts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweak.xm
331 lines (242 loc) · 11.9 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
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
#import "AutoAlerts.h"
#import "AAConfigurationViewController.h"
#import "AAAlertManager.h"
extern "C" CFNotificationCenterRef CFNotificationCenterGetDistributedCenter(void);
BOOL autoAlertsEnabled = YES;
%hook UIAlertController
%property (nonatomic, retain) AAAlertInfo *alertInfo;
%property (nonatomic, retain) UIViewController *dummyViewController;
%property (nonatomic, assign) BOOL automated;
-(void)viewDidLoad {
%orig;
if (self.preferredStyle != UIAlertControllerStyleAlert || !autoAlertsEnabled) {
return;
}
NSLog(@"[AutoAlerts] is automated: %i alert info: %@ custom app actions: %@", self.automated, self.alertInfo, self.alertInfo ? self.alertInfo.customAppActions : nil);
self.view.hidden = self.automated;
self._dimmingView.hidden = self.automated;
[self setTextFieldsCanBecomeFirstResponder:!self.automated];
UILongPressGestureRecognizer *longPress = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)] autorelease];
_UIAlertControllerView *theView = (_UIAlertControllerView *)self.view;
[[theView valueForKey:@"_contentViewTopItemsView"] addGestureRecognizer:longPress];
}
%new
-(void)saveAndRunAction:(int)action {
[self aa_runSelectedAction:action];
}
%new
-(void)handleLongPress:(UILongPressGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
self.dummyViewController = [[[UIViewController alloc] init] autorelease];
self.dummyViewController.view.backgroundColor = [UIColor clearColor];
self.dummyViewController.view.userInteractionEnabled = NO;
[self.window addSubview:self.dummyViewController.view];
NSMutableArray *actionTitles = [NSMutableArray array];
for (UIAlertAction *action in self.actions) {
[actionTitles addObject:action.title];
}
NSMutableArray *textFieldValues = [NSMutableArray array];
BOOL secure = NO;
for (UITextField *textField in self.textFields) {
if (textField.text) {
[textFieldValues addObject:textField.text];
}
if (textField.secureTextEntry) {
secure = YES;
}
}
NSDictionary *customAppActions = self.alertInfo ? self.alertInfo.customAppActions : [NSDictionary dictionary];
AAConfigurationViewController *configCtrl = [[[AAConfigurationViewController alloc] initWithActions:actionTitles title:self.title message:self.message textFieldValues:textFieldValues customAppActions:customAppActions secure:secure] autorelease];
configCtrl.view.backgroundColor = [UIColor whiteColor];
configCtrl.delegate = self;
[self.dummyViewController presentViewController:configCtrl animated:YES completion:nil];
}
}
-(void)viewWillAppear:(BOOL)arg1 {
%orig;
if (self.preferredStyle != UIAlertControllerStyleAlert || !autoAlertsEnabled) {
return;
}
self.view.hidden = self.automated;
self._dimmingView.hidden = self.automated;
}
%new
-(void)autoPopulateTextFields {
if (self.automated) {
for (int i = 0; i < self.textFields.count; i++) {
[self.textFields[i] setText:@""];
[self.textFields[i] insertText:self.alertInfo.textFieldValues[i]];
}
}
}
%new
-(void)aa_runSelectedAction:(int)selectedAction {
if (selectedAction > 0) {
if ([self isKindOfClass:[%c(_SBAlertController) class]]) {
if (selectedAction == 1) {
[UIView performWithoutAnimation:^{
SBAlertItem *item = [self valueForKey:@"alertItem"];
[item dismiss];
}];
} else {
[self autoPopulateTextFields];
[self _dismissWithAction:[self._actions objectAtIndex:selectedAction - 2] dismissCompletion:nil];
}
} else {
if (selectedAction == 1) {
[self.presentingViewController dismissViewControllerAnimated:NO completion:nil];
} else {
[self autoPopulateTextFields];
[self _dismissWithAction:[self._actions objectAtIndex:selectedAction - 2] dismissCompletion:nil];
}
}
}
}
-(void)viewDidAppear:(BOOL)arg1 {
%orig;
if (self.preferredStyle != UIAlertControllerStyleAlert || !autoAlertsEnabled) {
return;
}
self.view.hidden = self.automated;
self._dimmingView.hidden = self.automated;
if (self.automated) {
int selectedAction = self.alertInfo.selectedAction;
if ([self.alertInfo.bundleID isEqual:@"com.apple.springboard"] && self.alertInfo.customAppActions.count > 0) {
SpringBoard *sb = (SpringBoard *)[UIApplication sharedApplication];
id currentApp = [sb _accessibilityFrontMostApplication];
if (currentApp) {
NSString *currentAppBundleID = [currentApp valueForKey:@"bundleIdentifier"];
id customAction = self.alertInfo.customAppActions[currentAppBundleID];
if (customAction) {
selectedAction = [customAction intValue];
}
}
}
[self aa_runSelectedAction:selectedAction];
}
}
-(void)dealloc {
self.alertInfo = nil;
if (self.dummyViewController) {
[self.dummyViewController.view removeFromSuperview];
self.dummyViewController = nil;
}
%orig;
}
%end
%hook UIViewController
-(void)presentViewController:(id)arg1 animated:(BOOL)arg2 completion:(id)arg3 {
if (![arg1 isKindOfClass:[UIAlertController class]]) {
%orig;
} else {
UIAlertController *alert = (UIAlertController *)arg1;
if (alert.preferredStyle == UIAlertControllerStyleAlert && autoAlertsEnabled) {
NSMutableArray *actionTitles = [NSMutableArray array];
for (UIAlertAction *action in alert.actions) {
[actionTitles addObject:action.title];
}
NSMutableArray *textFieldValues = [NSMutableArray array];
for (UITextField *textField in alert.textFields) {
if (textField.text) {
[textFieldValues addObject:textField.text];
}
}
NSString *bundleID = [NSBundle mainBundle].bundleIdentifier;
AAAlertInfo *info = [[[AAAlertInfo alloc] initWithActions:actionTitles title:alert.title message:alert.message textFieldValues:textFieldValues selectedAction:0 customAppActions:[NSMutableDictionary dictionary] bundleID:bundleID] autorelease];
alert.alertInfo = [[AAAlertManager sharedManager] alertWithID:info.identifier];
if (alert.alertInfo) {
if ([alert.alertInfo.bundleID isEqual:@"com.apple.springboard"] && alert.alertInfo.customAppActions.count > 0) {
SpringBoard *sb = (SpringBoard *)[UIApplication sharedApplication];
id currentApp = [sb _accessibilityFrontMostApplication];
if (currentApp) {
NSString *currentAppBundleID = [currentApp valueForKey:@"bundleIdentifier"];
alert.automated = alert.alertInfo.customAppActions[currentAppBundleID] != nil && [alert.alertInfo.customAppActions[currentAppBundleID] intValue] > 0;
} else {
alert.automated = NO;
}
} else {
alert.automated = alert.alertInfo.selectedAction > 0;
}
} else {
alert.automated = NO;
}
%orig(arg1, alert.automated ? NO : arg2, arg3);
} else {
%orig;
}
}
}
%end
static void notificationCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
NSString *prefix = @"com.shiftcmdk.autoalerts.save.";
NSString *deletePrefix = @"com.shiftcmdk.autoalerts.delete.";
NSString *deleteWithBundleIDPrefix = @"com.shiftcmdk.autoalerts.deletewithbundleid.";
if ([(NSString *)name hasPrefix:prefix]) {
NSString *stripped = [(NSString *)name substringFromIndex:[prefix length]];
NSString *bundleID;
NSScanner *scanner = [NSScanner scannerWithString:stripped];
[scanner scanUpToString:@" " intoString:&bundleID];
NSString *prefixWithBundleID = [NSString stringWithFormat:@"%@%@ ", prefix, bundleID];
NSString *content = [(NSString *)name substringFromIndex:prefixWithBundleID.length];
NSData *jsonData = [content dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:nil error:&error];
NSLog(@"[AutoAlerts] content: %@", jsonDict);
if (!error) {
NSArray *actions = [jsonDict objectForKey:@"actions"];
NSString *title = [jsonDict objectForKey:@"title"];
NSString *message = [jsonDict objectForKey:@"message"];
int selectedAction = [[jsonDict objectForKey:@"selectedaction"] intValue];
NSArray *textFieldValues = [jsonDict objectForKey:@"textfieldvalues"];
NSMutableDictionary *customAppActions = [jsonDict objectForKey:@"customappactions"];
AAAlertInfo *info = [[[AAAlertInfo alloc] initWithActions:actions title:title message:message textFieldValues:textFieldValues selectedAction:selectedAction customAppActions:customAppActions bundleID:bundleID] autorelease];
[[AAAlertManager sharedManager] saveAlert:info error:nil];
NSLog(@"[AutoAlerts] saved alert: %@", [[AAAlertManager sharedManager] alertWithID:info.identifier]);
}
} else if ([(NSString *)name hasPrefix:deletePrefix]) {
NSString *alertID = [(NSString *)name substringFromIndex:[deletePrefix length]];
NSLog(@"[AutoAlerts] delete: %@", alertID);
[[AAAlertManager sharedManager] deleteAlertWithID:alertID error:nil];
} else if ([(NSString *)name hasPrefix:deleteWithBundleIDPrefix]) {
NSString *bundleID = [(NSString *)name substringFromIndex:[deleteWithBundleIDPrefix length]];
NSLog(@"[AutoAlerts] delete bundle id: %@", bundleID);
[[AAAlertManager sharedManager] deleteAlertsWithBundleID:bundleID error:nil];
} else if ([(NSString *)name isEqual:@"com.shiftcmdk.autoalerts.toggle"]) {
NSUserDefaults *defaults = [[[NSUserDefaults alloc] initWithSuiteName:@"com.shiftcmdk.autoalertspreferences"] autorelease];
autoAlertsEnabled = [defaults objectForKey:@"enabled"] == nil || [defaults boolForKey:@"enabled"];
NSLog(@"[AutoAlerts] toggle: %i", autoAlertsEnabled);
}
}
static void *sbObserver = NULL;
%ctor {
BOOL isSpringBoard = [[NSBundle mainBundle].bundleIdentifier isEqual:@"com.apple.springboard"];
if (isSpringBoard) {
NSUserDefaults *defaults = [[[NSUserDefaults alloc] initWithSuiteName:@"com.shiftcmdk.autoalertspreferences"] autorelease];
autoAlertsEnabled = [defaults objectForKey:@"enabled"] == nil || [defaults boolForKey:@"enabled"];
} else {
NSDictionary *prefsDict = [NSDictionary dictionaryWithContentsOfFile:@"/var/mobile/Library/Preferences/com.shiftcmdk.autoalertspreferences.plist"];
autoAlertsEnabled = !prefsDict || [prefsDict objectForKey:@"enabled"] == nil || [[prefsDict objectForKey:@"enabled"] boolValue];
}
[[AAAlertManager sharedManager] initialize];
if (isSpringBoard) {
CFNotificationCenterAddObserver(
CFNotificationCenterGetDistributedCenter(),
&sbObserver,
notificationCallback,
NULL,
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately
);
}
}
%dtor {
BOOL isSpringBoard = [[NSBundle mainBundle].bundleIdentifier isEqual:@"com.apple.springboard"];
if (isSpringBoard) {
CFNotificationCenterRemoveObserver(
CFNotificationCenterGetDistributedCenter(),
&sbObserver,
NULL,
NULL
);
}
}