-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tweak.xm
481 lines (417 loc) · 19.3 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
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
/*
*
* ____ _ __ _____ __ __ ____
* / __ \__ __(_)____/ /__/ ___// /_ ____ ____ / /_/ __ \_________
* / / / / / / / / ___/ //_/\__ \/ __ \/ __ \/ __ \/ __/ /_/ / ___/ __ \
* / /_/ / /_/ / / /__/ ,< ___/ / / / / /_/ / /_/ / /_/ ____/ / / /_/ /
* \___\_\__,_/_/\___/_/|_|/____/_/ /_/\____/\____/\__/_/ /_/ \____/
*
* Tweak.xm
* © 2013 Aditya KD
*/
#import <UIKit/UIKit.h>
#import <CoreFoundation/CoreFoundation.h>
#import <CoreFoundation/CFUserNotification.h>
// #import <8_1/SpringBoard/SpringBoard.h>
#import <SpringBoard/SBIconController.h>
#import <SpringBoard/SBIconModel.h>
#import <SpringBoard/SBIconView.h>
#import <SpringBoard/SBIconViewMap.h>
#import <SpringBoard/SBIconImageView.h>
#import <SpringBoard/SBIcon.h>
#import <SpringBoard/SBApplicationIcon.h>
#import <SpringBoard/SBScreenFlash.h>
#import <UIKit/UIGestureRecognizerTarget.h>
#import "LibstatusBar.h"
#import "QSCameraController.h"
#import "QSIconOverlayView.h"
#import "QSActivatorListener.h"
#import "QSConstants.h"
#import "QSAntiPiracy.h"
#import <objc/runtime.h>
#pragma mark - Variables
static BOOL _enabled = NO;
static BOOL _shownWelcomeAlert = NO;
static BOOL _isCapturingImage = NO;
static BOOL _isCapturingVideo = NO;
static BOOL _hasInitialized = NO;
static BOOL _flashScreen = NO;
static BOOL _showRecordingIcon = NO; // This one is not strictly necessary, as nothing in this file shows the status bar icon. Kept for the future!
static NSMutableArray *_enabledAppIDs = nil;
static NSString *_currentlyOverlayedAppID = nil;
static char *doubleTapGRKey;
static char *tripleTapGRKey;
#pragma mark - Function Declarations
static void QSUpdatePrefs(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo);
static BOOL QSAppIsEnabled(NSString *identifier);
static void QSAddGestureRecognizersToView(SBIconView *view);
static void QSUserNotificationCallBack(CFUserNotificationRef userNotification, CFOptionFlags responseFlags);
#pragma mark - Application Icon Hook
%hook SBIconView
- (void)setIcon:(SBIcon *)icon
{
// this is an awesome hook, makes it work in the switcher too.
%orig;
_hasInitialized = YES;
if (!(QSAppIsEnabled([icon leafIdentifier]))) {
UITapGestureRecognizer *dtr = objc_getAssociatedObject(self, &doubleTapGRKey);
UITapGestureRecognizer *ttr = objc_getAssociatedObject(self, &tripleTapGRKey);
if (dtr) {
[self removeGestureRecognizer:dtr];
objc_setAssociatedObject(self, &doubleTapGRKey, nil, OBJC_ASSOCIATION_ASSIGN);
}
if (ttr) {
[self removeGestureRecognizer:ttr];
objc_setAssociatedObject(self, &tripleTapGRKey, nil, OBJC_ASSOCIATION_ASSIGN);
}
// remove the associated objects. Coz YOLO
return;
}
CLog(@"Adding recognizer to icon: %@", [icon leafIdentifier]);
QSAddGestureRecognizersToView(self);
}
- (void)dealloc
{
objc_setAssociatedObject(self, &doubleTapGRKey, nil, OBJC_ASSOCIATION_ASSIGN);
objc_setAssociatedObject(self, &tripleTapGRKey, nil, OBJC_ASSOCIATION_ASSIGN);
%orig;
}
%new(v@:@)
- (void)qs_gestureRecognizerFired:(UITapGestureRecognizer *)gr
{
if (!_enabled || _isCapturingImage || [QSCameraController sharedInstance].isCapturingImage || IS_PIRATED) {
// this check is necessary, because the user might be using other quickshoot methods too.
return;
}
_currentlyOverlayedAppID = [[self icon] leafIdentifier];
// image capture
if (gr.state == UIGestureRecognizerStateEnded && gr.numberOfTapsRequired == 2 && !_isCapturingVideo) {
_isCapturingImage = YES;
SBIconImageView *imageView = [self _iconImageView];
QSIconOverlayView *overlayView = [[[QSIconOverlayView alloc] initWithFrame:imageView.frame captureMode:QSCaptureModePhoto] autorelease];
[self setUserInteractionEnabled:NO];
EXECUTE_BLOCK_AFTER_DELAY(10, ^{
// create a failsafe, which runs after 10 seconds
[self setUserInteractionEnabled:YES];
});
overlayView.animationCompletionHandler = ^{
[overlayView removeFromSuperview];
[self setUserInteractionEnabled:YES];
overlayView.animationCompletionHandler = nil;
};
[imageView addSubview:overlayView];
[overlayView captureBegan];
[[QSCameraController sharedInstance] takePhotoWithCompletionHandler:^(BOOL success){
_isCapturingImage = NO;
[overlayView captureCompletedWithResult:success];
[self setUserInteractionEnabled:YES]; // let the user tap the icon now.
}];
}
// video capture
if (gr.state == UIGestureRecognizerStateEnded && gr.numberOfTapsRequired == 3) {
static QSIconOverlayView *overlayView;
static BOOL wasInterrupted;
if (!_isCapturingVideo) {
if ([QSCameraController sharedInstance].isCapturingVideo) {
// this check is necessary, because the user might be recording a video some other way, too.
return;
}
_isCapturingVideo = YES;
SBIconImageView *imageView = [self _iconImageView];
overlayView = [[QSIconOverlayView alloc] initWithFrame:imageView.frame captureMode:QSCaptureModeVideo];
overlayView.animationCompletionHandler = ^{
[overlayView removeFromSuperview];
if (!wasInterrupted) {
[overlayView release];
}
wasInterrupted = YES;
overlayView.animationCompletionHandler = nil;
};
[imageView addSubview:overlayView];
[overlayView captureBegan];
[[QSCameraController sharedInstance] startVideoCaptureWithHandler:^(BOOL success) {
if (!success) {
[overlayView captureCompletedWithResult:NO];
_isCapturingVideo = NO;
}
} interruptionHandler:^(BOOL success) {
wasInterrupted = YES;
_isCapturingVideo = NO;
[overlayView captureCompletedWithResult:success];
}];
}
else {
[overlayView captureIsStopping];
[[QSCameraController sharedInstance] stopVideoCaptureWithHandler:^(BOOL success) {
[overlayView captureCompletedWithResult:success];
_isCapturingVideo = NO;
}];
}
}
}
%end
#pragma mark - App Launch Hook
%hook SBIcon
- (void)launchFromLocation:(SBIconLocation)location
{
if (_isCapturingVideo && ([[(SBApplicationIcon *)self leafIdentifier] isEqualToString:_currentlyOverlayedAppID])) {
// the app should not launch if it has an overlay.
// this won't be called when capturing an image, because user interaction is disabled! :P
return;
}
%orig;
}
%end
%hook SBUIController
- (void)activateApplicationFromSwitcher:(SBApplication *)app
{
if (_isCapturingVideo && ([[(SBApplicationIcon *)self leafIdentifier] isEqualToString:_currentlyOverlayedAppID])) {
// same as above method
return;
}
%orig;
}
%end
#pragma mark - Camera Grabber Hooks
%hook SBLockScreenView
- (void)setCameraGrabberHidden:(BOOL)hidden forRequester:(id)requester
{
%orig(hidden, requester);
if (hidden || IS_PIRATED) {
return;
}
UIView *grabberView = MSHookIvar<UIView *>(self, "_cameraGrabberView");
UITapGestureRecognizer *tapRecognizer = objc_getAssociatedObject(self, @selector(qsTapRecognizer));
if (grabberView && !tapRecognizer) {
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(qs_handleDoubleTap:)];
tapRecognizer.numberOfTapsRequired = 2;
objc_setAssociatedObject(grabberView, @selector(qsTapRecognizer), tapRecognizer, OBJC_ASSOCIATION_ASSIGN);
for (UIGestureRecognizer *recognizer in grabberView.gestureRecognizers) {
// Ensure all other gesture recognisers wait for this one to fail
[recognizer requireGestureRecognizerToFail:tapRecognizer];
}
[grabberView addGestureRecognizer:tapRecognizer];
}
}
%new
- (void)qs_handleDoubleTap:(UITapGestureRecognizer *)sender
{
if (!_enabled || _isCapturingImage || _isCapturingVideo) {
return;
}
_isCapturingImage = YES;
[[QSCameraController sharedInstance] takePhotoWithCompletionHandler:^(BOOL success) {
_isCapturingImage = NO;
if (success) {
if (_flashScreen) {
[[%c(SBScreenFlash) mainScreenFlasher] flashWhiteWithCompletion:nil];
}
}
}];
}
%end
#pragma mark - SpringBoard Hook
%hook SpringBoard
/*
* Hook to ensure UIDevice begins generating rotation events
*/
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
%orig;
[(SpringBoard *)[UIApplication sharedApplication] setWantsOrientationEvents:YES];
[(SpringBoard *)[UIApplication sharedApplication] updateOrientationDetectionSettings];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[QSCameraController sharedInstance]; // make sure the object is created, hence setting it up to receive orientation notifs.
QSUpdatePrefs(NULL, NULL, kPrefsChangedNotificationName, NULL, NULL);
QSUpdatePrefs(NULL, NULL, kAppPrefsChangedNotificationName, NULL, NULL);
}
- (void)_reportAppLaunchFinished
{
%orig;
// called on unlock.
if (!_shownWelcomeAlert) {
NSDictionary *fields = @{(id)kCFUserNotificationAlertHeaderKey: @"Welcome to QuickShoot",
(id)kCFUserNotificationAlertMessageKey: @"Thank you for your purchase. Open settings for more options and help, or get started right away. Try double tapping the camera icon.\n",
(id)kCFUserNotificationDefaultButtonTitleKey: @"Dismiss",
(id)kCFUserNotificationAlternateButtonTitleKey: @"Settings"};
SInt32 error = 0;
CFUserNotificationRef notificationRef = CFUserNotificationCreate(kCFAllocatorDefault, 0, kCFUserNotificationNoteAlertLevel, &error, (CFDictionaryRef)fields);
// Get and add a run loop source to the current run loop to get notified when the alert is dismissed
CFRunLoopSourceRef runLoopSource = CFUserNotificationCreateRunLoopSource(kCFAllocatorDefault, notificationRef, QSUserNotificationCallBack, 0);
CFRunLoopAddSource(CFRunLoopGetMain(), runLoopSource, kCFRunLoopCommonModes);
CFRelease(runLoopSource);
if (error == 0) {
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:kPrefPath];
if (!prefs) {
prefs = [[NSMutableDictionary alloc] init];
}
prefs[QSUserHasSeenAlertKey] = @(YES);
[prefs writeToFile:kPrefPath atomically:YES];
[prefs release];
}
}
}
%end
#pragma mark - User Notification Callback
static void QSUserNotificationCallBack(CFUserNotificationRef userNotification, CFOptionFlags responseFlags)
{
if ((responseFlags & 0x3) == kCFUserNotificationAlternateResponse) {
// Open settings to custom bundle
[(SpringBoard *)[UIApplication sharedApplication] applicationOpenURL:[NSURL URLWithString:@"prefs:root=QuickShoot%20Pro"] publicURLsOnly:NO];
}
CFRelease(userNotification);
}
#pragma mark - App Enabled Check
static BOOL QSAppIsEnabled(NSString *identifier)
{
if (!identifier) {
return NO;
}
if ([identifier isEqualToString:@"com.apple.camera"]) {
return YES;
}
for (NSString *appID in _enabledAppIDs) {
if ([appID isEqualToString:identifier]) {
return YES;
}
}
return NO;
}
#pragma mark - Gesture Recognizer Handling
static void QSAddGestureRecognizersToView(SBIconView *view)
{
// Remove the current recognizers
UITapGestureRecognizer *doubleTapGR = objc_getAssociatedObject(view, &doubleTapGRKey);
UITapGestureRecognizer *tripleTapGR = objc_getAssociatedObject(view, &tripleTapGRKey);
[doubleTapGR.view removeGestureRecognizer:doubleTapGR];
[tripleTapGR.view removeGestureRecognizer:tripleTapGR];
objc_setAssociatedObject(view, &doubleTapGRKey, nil, OBJC_ASSOCIATION_ASSIGN);
objc_setAssociatedObject(view, &tripleTapGRKey, nil, OBJC_ASSOCIATION_ASSIGN);
doubleTapGR = [[UITapGestureRecognizer alloc] initWithTarget:view action:@selector(qs_gestureRecognizerFired:)];
doubleTapGR.numberOfTapsRequired = 2;
tripleTapGR = [[UITapGestureRecognizer alloc] initWithTarget:view action:@selector(qs_gestureRecognizerFired:)];
tripleTapGR.numberOfTapsRequired = 3;
[doubleTapGR requireGestureRecognizerToFail:tripleTapGR];
[(SBIconView *)view addGestureRecognizer:doubleTapGR];
[(SBIconView *)view addGestureRecognizer:tripleTapGR];
[doubleTapGR release];
[tripleTapGR release];
// ASSIGN association is used, because the gesture recognizers are autoreleased gesture recognizers of that object, so no issues there.
objc_setAssociatedObject(view, &doubleTapGRKey, doubleTapGR, OBJC_ASSOCIATION_ASSIGN);
objc_setAssociatedObject(view, &tripleTapGRKey, tripleTapGR, OBJC_ASSOCIATION_ASSIGN);
[(SBIconView *)view setUserInteractionEnabled:YES];
}
/*
* This method is most probably useless, because the icon views don't exist when the user is an another application
* But it makes up for when and if SpringBoard feels like keeping the icon views around.
* It adds and remove the gesture recognizer based on the new/removed apps using associated object magic
*/
#pragma mark - Gesture Recognizer Updates
static void QSUpdateAppIconRecognizersRemovingApps(NSArray *disabledApps)
{
[disabledApps retain];
for (NSString *appID in disabledApps) {
@autoreleasepool {
if ([appID isEqualToString:@"com.apple.camera"]) {
continue;
}
SBIconModel *iconModel = (SBIconModel *)[(SBIconController *)[%c(SBIconController) sharedInstance] model];
SBIcon *icon = (SBIcon *)[(SBIconModel *)iconModel leafIconForIdentifier:appID];
SBIconView *iconView = [[%c(SBIconViewMap) homescreenMap] iconViewForIcon:icon];
UITapGestureRecognizer *dtr = objc_getAssociatedObject(iconView, &doubleTapGRKey);
UITapGestureRecognizer *ttr = objc_getAssociatedObject(iconView, &tripleTapGRKey);
if (dtr || ttr) {
[iconView removeGestureRecognizer:dtr];
[iconView removeGestureRecognizer:ttr];
objc_setAssociatedObject(iconView, &doubleTapGRKey, nil, OBJC_ASSOCIATION_ASSIGN);
objc_setAssociatedObject(iconView, &tripleTapGRKey, nil, OBJC_ASSOCIATION_ASSIGN);
}
}
}
for (NSString *appID in _enabledAppIDs) {
@autoreleasepool {
SBIconModel *iconModel = (SBIconModel *)[(SBIconController *)[%c(SBIconController) sharedInstance] model];
SBIcon *icon = (SBIcon *)[(SBIconModel *)iconModel leafIconForIdentifier:appID];
SBIconView *iconView = [[%c(SBIconViewMap) homescreenMap] iconViewForIcon:icon];
QSAddGestureRecognizersToView(iconView);
}
}
[disabledApps release];
}
static void QSUpdatePrefs(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
if ([(NSString *)name isEqualToString:(NSString *)kPrefsChangedNotificationName]) {
NSDictionary *prefs = [[NSUserDefaults standardUserDefaults] persistentDomainForName:kPrefsDomainName];
if (!prefs) {
_enabled = YES;
return;
}
if (!(prefs[QSEnabledKey])) {
_enabled = YES;
}
else {
_enabled = [prefs[QSEnabledKey] boolValue];
}
_shownWelcomeAlert = [prefs[QSUserHasSeenAlertKey] boolValue];
QSCameraController *controller = [QSCameraController sharedInstance];
controller.cameraDevice = QSCameraDeviceFromString(prefs[QSCameraDeviceKey]);
controller.flashMode = QSFlashModeFromString(prefs[QSFlashModeKey]);
controller.enableHDR = [prefs[QSHDRModeKey] boolValue];
controller.waitForFocusCompletion = [prefs[QSWaitForFocusKey] boolValue];
controller.videoCaptureQuality = prefs[QSVideoQualityKey];
controller.videoFlashMode = QSFlashModeFromString(prefs[QSFlashModeKey]);
_flashScreen = ((prefs[QSScreenFlashKey] != nil) ? [prefs[QSScreenFlashKey] boolValue] : YES);
_showRecordingIcon = ((prefs[QSRecordingIconKey] != nil) ? [prefs[QSRecordingIconKey] boolValue] : YES);
[QSActivatorListener sharedInstance].shouldFlashScreen = _flashScreen;
[QSActivatorListener sharedInstance].shouldShowRecordingIcon = _showRecordingIcon;
[[NSNotificationCenter defaultCenter] postNotificationName:QSPrefsChangedNotificationName object:nil];
}
else if ([(NSString *)name isEqualToString:(NSString *)kAppPrefsChangedNotificationName]) {
NSDictionary *appPrefs = [[NSDictionary alloc] initWithContentsOfFile:kAppPrefPath];
if (!appPrefs) {
[@{} writeToFile:kAppPrefPath atomically:YES];
}
NSMutableArray *disabledApps = [[NSMutableArray new] autorelease];
[_enabledAppIDs release];
_enabledAppIDs = nil;
_enabledAppIDs = [NSMutableArray new];
for (NSString *key in [appPrefs allKeys]) {
if ([key hasPrefix:@"QSApp-"]) {
if (([appPrefs[key] boolValue] == YES)) {
CLog(@"Adding %@", key);
[_enabledAppIDs addObject:[key stringByReplacingOccurrencesOfString:@"QSApp-" withString:@""]];
}
else {
[disabledApps addObject:[key stringByReplacingOccurrencesOfString:@"QSApp-" withString:@""]];
}
}
}
if (_hasInitialized) {
// running this before SpringBoard has loaded completely == BAD IDEA.
QSUpdateAppIconRecognizersRemovingApps(disabledApps);
}
[appPrefs release];
}
}
#pragma mark - Constructor
%ctor
{
@autoreleasepool {
NSLog(@"QS: Initialising, registering listeners and preference callbacks");
%init;
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
(CFNotificationCallback)&QSUpdatePrefs,
kPrefsChangedNotificationName,
NULL,
CFNotificationSuspensionBehaviorHold);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
(CFNotificationCallback)&QSUpdatePrefs,
kAppPrefsChangedNotificationName,
NULL,
CFNotificationSuspensionBehaviorHold);
[[LAActivator sharedInstance] registerListener:[QSActivatorListener sharedInstance] forName:QSImageCaptureListenerName];
[[LAActivator sharedInstance] registerListener:[QSActivatorListener sharedInstance] forName:QSVideoCaptureListenerName];
}
}