-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTweak.xm
131 lines (121 loc) · 4.92 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
// Interfaces
@interface CSTimerView : UIView
@end
@interface SBFLockScreenDateSubtitleDateView : UIView
@end
static NSTimer *whatsthedateTimer;
/* Preferences */
static BOOL kEnabled;
static float kTimeBeforeSwitch;
// Create loadPrefs method
// We call it from the constructor (end of file)
static void loadPrefs() {
// Initialise NSMutableDictionary from the preferences plist file
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/mobile/Library/Preferences/com.icraze.whatsthedateprefs.plist"];
// Assign the value of kEnabled to a BOOL, and set the default value to YES
kEnabled = [prefs objectForKey:@"kEnabled"] ? [[prefs objectForKey:@"kEnabled"] boolValue] : YES;
// Assign the value of kTimeBeforeSwitch to a float, and set the default value to 3.5
kTimeBeforeSwitch = [prefs objectForKey:@"kTimeBeforeSwitch"] ? [[prefs objectForKey:@"kTimeBeforeSwitch"] floatValue] : 3.5;
}
/* Main tweak code */
%hook SBBacklightController
// This method is called whenever the screen turns on or off
-(void)setBacklightFactorPending:(float)arg1 {
// Run the original code first
%orig;
// Turn the argument (arg1) into a string
NSString *checkForAutoLockString = [NSString stringWithFormat: @"%f", arg1];
// When the screen turns on, the string contains "1.00000"
// When the screen turns of, the string contains "0.00000"
// We want to check if the string doesn't contain "1", so we can detect when the screen turned off
if (![checkForAutoLockString containsString:@"1"]) {
// Reset the timer
[whatsthedateTimer invalidate];
}
}
%end
%hook CSTimerView
// This method is called when the view is displayed
-(void)movedToWindow:(id)arg1 {
// Run the original code first
%orig;
// Check if the tweak is enabled
if (kEnabled) {
// Initialise NSMutableDictionary from the preferences plist file
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/mobile/Library/Preferences/com.icraze.whatsthedateprefs.plist"];
// Get the value of kTimeBeforeSwitch
// For some reason, no notification is sent to the constructor to call loadPrefs when the slider is changed
// So we have to read the value from here
kTimeBeforeSwitch = [prefs objectForKey:@"kTimeBeforeSwitch"] ? [[prefs objectForKey:@"kTimeBeforeSwitch"] floatValue] : 3.5;
// Start timer for (kTimeBeforeSwitch) after CSTimerView is displayed
whatsthedateTimer = [NSTimer scheduledTimerWithTimeInterval:kTimeBeforeSwitch target:self selector:@selector(afterTimerViewTimer) userInfo:nil repeats:NO];
}
}
// Create a new method in the class
%new
// When the timer ends
-(void)afterTimerViewTimer {
// Check if the tweak is enabled
if (kEnabled) {
// Start animating the view to disappear
// I have chosen to make the animation take 0.5 seconds,
// as it seems very similar to the timing Apple uses for the Barrtery Percentage,
// when you unlock the device whilst charging
[UIView animateWithDuration:0.5f animations:^{
// Set the alpha of the view to 0
self.alpha = 0.0f;
// Once the animation is complete
} completion:^(BOOL finished){
// Erase the timer from memory
whatsthedateTimer = nil;
// Send NSNotification to SBFLockScreenDateSubtitleDateView
NSNotification *alrmaShowDateNotification = [NSNotification notificationWithName:@"alrmaShowDateNotification" object:self userInfo:nil];
[[NSNotificationCenter defaultCenter] postNotification:alrmaShowDateNotification];
}
];
}
}
%end
%hook SBFLockScreenDateSubtitleDateView
// This method is called when the view is displayed
-(void)didMoveToWindow {
// Run the original code first
%orig;
// Add an NSNotification observer
// This detects the notification from CSTimerView
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(whatsthedateShowDateText) name:@"alrmaShowDateNotification" object:nil];
}
// Create a new method in the class
%new
// When the NSNotification is received
-(void)whatsthedateShowDateText {
// Check if the tweak is enabled
if (kEnabled) {
// Start animating the view to disappear
// I have chosen to make the animation take 0.5 seconds,
// as it seems very similar to the timing Apple uses for the Barrtery Percentage,
// when you unlock the device whilst charging
[UIView animateWithDuration:0.5f animations:^{
// Set the alpha of the view to 1
self.alpha = 1.0f;
} completion:^(BOOL finished){}
];
}
}
%end
/* Constructor */
// This code runs when the tweak is injected into a process
// In this case, we are just injecting into SpringBoard
%ctor {
// Call the loadPrefs method
loadPrefs();
// Add a CFNotification obsever
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)loadPrefs, CFSTR("com.icraze.whatsthedateprefs.settingschanged"), NULL, CFNotificationSuspensionBehaviorCoalesce);
// Check if the tweak is not enabled
if (!kEnabled) {
// Don't run anymore code past here
return;
}
// Run the tweak
%init;
}