forked from ArtikusHG/StopChillin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTweak.xm
More file actions
executable file
·169 lines (129 loc) · 6.81 KB
/
Tweak.xm
File metadata and controls
executable file
·169 lines (129 loc) · 6.81 KB
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
#define PLIST_PATH_Settings "/var/mobile/Library/Preferences/com.artikus.stopchillinprefs.plist"
@interface JBBulletinManager : NSObject
+ (id)sharedInstance;
- (id)showBulletinWithTitle:(NSString *)title message:(NSString *)message bundleID:(NSString *)bundleID;
- (id)showBulletinWithTitle:(NSString *)title message:(NSString *)message overrideBundleImage:(UIImage *)overrideBundleImage;
- (id)showBulletinWithTitle:(NSString *)title message:(NSString *)message overrideBundleImage:(UIImage *)overrideBundleImage soundPath:(NSString *)soundPath;
@end
@interface SpringBoard : UIApplication
- (void)cancelUsageNotificationCountdown;
- (void)startUsageNotificationCountdown;
- (void)showUsageNotification:(BOOL)isTest;
@end
NSDictionary *dict;
NSString *usageTitle;
NSString *usageMessage;
NSString *customHourString;
NSString *customHoursString;
NSString *customMinuteString;
NSString *customMinutesString;
NSString *customAndString;
BOOL enabled;
double countdown;
BOOL isCountdownRunning;
%group StopChillinMethods
%hook SpringBoard
%new
- (void)startUsageNotificationCountdown {
if (enabled && countdown > 0 && !isCountdownRunning) {
isCountdownRunning = YES;
//[[[UIAlertView alloc] initWithTitle:@"A" message:@"A" delegate:nil cancelButtonTitle:@"a" otherButtonTitles:nil] show];
[self performSelector:@selector(showUsageNotification:) withObject:self afterDelay:countdown];
}
}
%new
- (void)cancelUsageNotificationCountdown {
isCountdownRunning = NO;
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showUsageNotification:) object:self];
}
%new
- (void)showUsageNotification:(BOOL)isTest {
if (!enabled && !isTest) return;
int minutes = round(countdown) / 60;
int hours = round(minutes / 60);
NSString *minutesString = customMinutesString;
NSString *hoursString = customHoursString;
if (minutes == 1) minutesString = customMinuteString;
if (hours == 1) hoursString = customHourString;
NSMutableString *usageString = [[NSMutableString alloc] init];
if (hours != 0) {
[usageString appendString:[NSString stringWithFormat:@"%d %@", hours, hoursString]];
minutes = minutes - (hours * 60); // in case we have 1 hour 30 minutes so we dont get 1 hour 90 minutes; i minus the amount of minutes in the hour from the amount of actual minutes, so from for example 1 hour 90 minutes it turns into 1 hour 30 minutes
if(minutes == 1) minutesString = customMinuteString;
if(minutes != 0) {
[usageString appendString:[NSString stringWithFormat:@" %@ %d %@", customAndString, minutes, minutesString]];
}
} else if (minutes != 0) {
[usageString appendString:[NSString stringWithFormat:@"%d %@", minutes, minutesString]];
}
[[objc_getClass("JBBulletinManager") sharedInstance] showBulletinWithTitle:usageTitle message:[NSString stringWithFormat:usageMessage, usageString] overrideBundleImage:nil];
if(!isTest) {
isCountdownRunning = NO;
[self performSelector:@selector(showUsageNotification:) withObject:self afterDelay:countdown];
isCountdownRunning = YES;
}
}
%end
%end
%group StopChillinCountdown
%hook SpringBoard
- (void)applicationDidFinishLaunching:(id)application {
%orig;
[self startUsageNotificationCountdown];
}
%end
%hook SBLockScreenManager
- (BOOL)_lockUI {
[(SpringBoard *)[objc_getClass("SpringBoard") sharedApplication] cancelUsageNotificationCountdown];
return %orig;
}
- (void)lockUIFromSource:(int)source withOptions:(id)options {
%orig;
[(SpringBoard *)[objc_getClass("SpringBoard") sharedApplication] cancelUsageNotificationCountdown];
}
%end
%hook SBBacklightController
- (void)turnOnScreenFullyWithBacklightSource:(long long)source {
//[[[UIAlertView alloc] initWithTitle:@"A" message:[NSString stringWithFormat:@"%lld",source] delegate:nil cancelButtonTitle:@"a" otherButtonTitles:nil] show];
%orig;
// 26 - source of screenshots on newer ios version (afaik); hope this shitty hotfix i made right before release works... please
if (source != 26) [(SpringBoard *)[objc_getClass("SpringBoard") sharedApplication] startUsageNotificationCountdown];
}
%end
%end
static void LoadPreferences() {
dict = nil;
dict = [[NSDictionary alloc] initWithContentsOfFile:@PLIST_PATH_Settings]?:[NSMutableDictionary dictionary];
if([(NSString *)dict[@"kCustomTitle"] length] > 0) usageTitle = dict[@"kCustomTitle"];
else usageTitle = @"Put your phone down";
if([(NSString *)dict[@"kCustomMessage"] length] > 0) usageMessage = dict[@"kCustomMessage"];
else usageMessage = @"You've been using your phone for %@ continuously. Go watch the world, don't waste your time.";
if([(NSString *)dict[@"kCustomHour"] length] > 0) customHourString = dict[@"kCustomHour"];
else customHourString = @"hour";
if([(NSString *)dict[@"kCustomHours"] length] > 0) customHoursString = dict[@"kCustomHours"];
else customHoursString = @"hours";
if([(NSString *)dict[@"kCustomMinute"] length] > 0) customMinuteString = dict[@"kCustomMinute"];
else customMinuteString = @"minute";
if([(NSString *)dict[@"kCustomMinutes"] length] > 0) customMinutesString = dict[@"kCustomMinutes"];
else customMinutesString = @"minutes";
if([(NSString *)dict[@"kCustomAnd"] length] > 0) customAndString = dict[@"kCustomAnd"];
else customAndString = @"and";
enabled = [dict[@"kEnabled"] boolValue]?:NO;
countdown = [dict[@"kNotificationInterval"] doubleValue]?:0;
[(SpringBoard *)[objc_getClass("SpringBoard") sharedApplication] cancelUsageNotificationCountdown];
if (enabled) [(SpringBoard *)[objc_getClass("SpringBoard") sharedApplication] startUsageNotificationCountdown];
}
static void PreferencesChangedCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
[[objc_getClass("JBBulletinManager") sharedInstance] showBulletinWithTitle:@"Value saved" message:@"If you're reading this, your custom time has been saved. Long story short, I couldn't understand why sometimes the preferences don't save, so I made this notification... Also, lock you device and unlock it again to start the countdown with the new time." bundleID:@"com.apple.Preferences"];
LoadPreferences();
}
static void ShowTestNotification(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
[(SpringBoard *)[objc_getClass("SpringBoard") sharedApplication] showUsageNotification:YES];
}
%ctor {
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, PreferencesChangedCallback, CFSTR("com.artikus.stopchillin.changed"), NULL, 0);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, ShowTestNotification, CFSTR("com.artikus.stopchillin.testnotif"), NULL, 0);
LoadPreferences();
%init(StopChillinMethods); // we init springboard methods anyway cause test notif and then init countdown if enabled
if (enabled) %init(StopChillinCountdown);
}