-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tweak.xm
359 lines (275 loc) · 9.94 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
#import <Celestial/AVSystemController.h>
#define settingsPath [NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Preferences/com.exormeter.alarmfade.plist"]
#define LOWEST_POSSIBLE_VOLUME 0.0625
#define DEFAULT_VOLUME 0.5
#define DEFAULT_FADE 120
#define TIMER_INTERVAL 1.0
////////////ALARM
@interface MTAlarmScheduler
@property (nonatomic) float originalRingerVolume;
@property (nonatomic) float currentVolume;
@property (nonatomic) float volumeIncrement;
@property (nonatomic) float maxVolume;
@property (nonatomic) BOOL isEnabled;
@property (nonatomic) BOOL fadeIsEnabled;
@property (nonatomic) int fadeSeconds;
@property (nonatomic, assign) AVSystemController* avSystemController;
@property (nonatomic, assign) NSTimer* timer;
-(void)restoreRingerVolume;
-(void)onTick:(NSTimer *)timer;
-(void)stopTimer;
-(void)updatePreferences;
@end
@interface MTAlarmStorage
@property (assign,nonatomic,weak) MTAlarmScheduler* scheduler;
@end
%hook MTAlarmScheduler
%property (assign) float currentVolume;
%property (assign) float originalRingerVolume;
%property (assign) float maxVolume;
%property (assign) float volumeIncrement;
%property (assign) BOOL isEnabled;
%property (assign) BOOL fadeIsEnabled;
%property (assign) int fadeSeconds;
%property (nonatomic, assign) AVSystemController* avSystemController;
%property (nonatomic, assign) NSTimer* timer;
-(id)initWithStorage:(id)arg1 notificationCenter:(id)arg2 scheduler:(id)arg3{
NSLog(@"Alarmfade Initialized");
self.avSystemController = [%c(AVSystemController) sharedAVSystemController];
self.timer = nil;
self.isEnabled = true;
self.fadeIsEnabled = true;
self.fadeSeconds = DEFAULT_FADE;
self.maxVolume = DEFAULT_VOLUME;
return %orig;
}
-(void)_fireScheduledAlarm:(id)arg1 firedDate:(id)arg2 completionBlock:(/*^block*/id)arg3 {
//Check if preferences have changed since last alarm fire
[self updatePreferences];
//If tweal is disabled, just fire the origianl _fireScheduledAlarm
if(!self.isEnabled){
return %orig;
}
//Calculate the incements to reach the desired volume in the given time
self.volumeIncrement = self.maxVolume / self.fadeSeconds;
self.currentVolume = LOWEST_POSSIBLE_VOLUME;
//Read the original RingerVolume and save it to be restored after the alarm was dismissed
float *originalVolume = (float*) malloc(sizeof(float));
if(originalVolume){
[self.avSystemController getVolume: originalVolume forCategory:@"Ringtone"];
self.originalRingerVolume = *originalVolume;
free(originalVolume);
}
//Set the volume to the lowest possible if fadeIn is enabled, to the desited volume otherwise
if(self.fadeIsEnabled){
if(self.fadeSeconds <= 0){
return %orig;
}
[self.avSystemController setVolumeTo: LOWEST_POSSIBLE_VOLUME forCategory:@"Ringtone"];
}
else{
[self.avSystemController setVolumeTo: self.maxVolume forCategory:@"Ringtone"];
}
//If no timer is running, start a new time which increments the volume every second
if(self.timer == nil && self.fadeIsEnabled){
dispatch_async(dispatch_get_main_queue(), ^{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: TIMER_INTERVAL
target: self
selector:@selector(onTick:)
userInfo: nil
repeats:YES];
self.timer = timer;
});
}
NSLog(@"Fired Alarm");
%orig;
}
%new
-(void)updatePreferences{
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:settingsPath];
if([prefs objectForKey:@"isEnabled"] != nil){
self.isEnabled = [[prefs objectForKey:@"isEnabled"] boolValue];
}
if ([prefs objectForKey:@"fadeIsEnabled"] != nil){
self.fadeIsEnabled = [[prefs objectForKey:@"fadeIsEnabled"] boolValue];
}
if([prefs objectForKey:@"fadein"] != nil){
self.fadeSeconds = [[prefs objectForKey:@"fadein"] intValue];
}
if([prefs objectForKey:@"maxVolume"] != nil){
self.maxVolume = [[prefs objectForKey:@"maxVolume"] floatValue];
}
[prefs release];
}
%new
-(void)onTick:(NSTimer *)timer {
NSLog(@"Timer active");
//Adds the incement to the volume if it wasn't reached yet. If volume was reached, terminate the timer
if(self.currentVolume <= self.maxVolume){
[self.avSystemController setActiveCategoryVolumeTo: self.currentVolume];
self.currentVolume += self.volumeIncrement;
}
else{
[self.timer invalidate];
self.timer = nil;
}
}
//This method is called when the alarm was dissmised
%new
-(void)stopTimer{
NSLog(@"Timer stopped");
if(self.timer != nil && [self.timer isValid]){
[self.timer invalidate];
self.timer = nil;
}
}
//After the alarm was dissmissed, restore the old ringtone value
%new
-(void)restoreRingerVolume{
if(self.isEnabled){
NSLog(@"Reset Volume");
[self.avSystemController setVolumeTo: self.originalRingerVolume forCategory:@"Ringtone"];
}
}
%end
%hook MTAlarmStorage
-(void)dismissAlarmWithIdentifier:(id)arg1 dismissDate:(id)arg2 dismissAction:(unsigned long long)arg3 withCompletion:(/*^block*/id)arg4 source:(id)arg5{
[self.scheduler stopTimer];
[self.scheduler restoreRingerVolume];
NSLog(@"Dissmissed Alarm");
return %orig;
}
-(void)snoozeAlarmWithIdentifier:(id)arg1 snoozeAction:(unsigned long long)arg2 withCompletion:(/*^block*/id)arg3 source:(id)arg4{
[self.scheduler stopTimer];
[self.scheduler restoreRingerVolume];
NSLog(@"Snoozed Alarm");
return %orig;
}
%end
////TIMER
@interface MTTimerStorage
@property (nonatomic) float originalRingerVolume;
@property (nonatomic) float currentVolume;
@property (nonatomic) float volumeIncrement;
@property (nonatomic) float maxVolume;
@property (nonatomic) BOOL isEnabled;
@property (nonatomic) BOOL fadeIsEnabled;
@property (nonatomic) int fadeSeconds;
@property (nonatomic, assign) AVSystemController* avSystemController;
@property (nonatomic, assign) NSTimer* timer;
-(void)restoreRingerVolume;
-(void)onTick:(NSTimer *)timer;
-(void)stopTimer;
-(void)updatePreferences;
@end
%hook MTTimerStorage
%property (assign) float originalRingerVolume;
%property (assign) float currentVolume;
%property (assign) float maxVolume;
%property (assign) float volumeIncrement;
%property (assign) BOOL isEnabled;
%property (assign) BOOL fadeIsEnabled;
%property (assign) int fadeSeconds;
%property (nonatomic, assign) AVSystemController* avSystemController;
%property (nonatomic, assign) NSTimer* timer;
-(id)init{
NSLog(@"Timer Initialized");
self.avSystemController = [%c(AVSystemController) sharedAVSystemController];
self.timer = nil;
self.isEnabled = true;
self.fadeIsEnabled = true;
self.fadeSeconds = DEFAULT_FADE;
self.maxVolume = DEFAULT_VOLUME;
return %orig;
}
-(void)_queue_notifyObserversForTimerFire:(id)arg1 source:(id)arg2{
[self updatePreferences];
if(!self.isEnabled){
return %orig;
}
self.volumeIncrement = self.maxVolume / self.fadeSeconds;
self.currentVolume = LOWEST_POSSIBLE_VOLUME;
float *originalVolume = (float*) malloc(sizeof(float));
if(originalVolume){
[self.avSystemController getVolume: originalVolume forCategory:@"Ringtone"];
self.originalRingerVolume = *originalVolume;
free(originalVolume);
}
if(self.fadeIsEnabled){
if(self.fadeSeconds <= 0){
return %orig;
}
[self.avSystemController setVolumeTo: LOWEST_POSSIBLE_VOLUME forCategory:@"Ringtone"];
}
else{
[self.avSystemController setVolumeTo: self.maxVolume forCategory:@"Ringtone"];
}
if(self.timer == nil && self.fadeIsEnabled){
dispatch_async(dispatch_get_main_queue(), ^{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: TIMER_INTERVAL
target: self
selector:@selector(onTick:)
userInfo: nil
repeats:YES];
self.timer = timer;
});
}
NSLog(@"Fired Timer");
}
-(void)dismissTimerWithIdentifier:(id)arg1 withCompletion:(/*^block*/id)arg2 source:(id)arg3{
[self stopTimer];
[self restoreRingerVolume];
NSLog(@"Dissmissed Timer");
return %orig;
}
-(void)repeatTimerWithIdentifier:(id)arg1 withCompletion:(/*^block*/id)arg2 source:(id)arg3{
[self stopTimer];
[self restoreRingerVolume];
NSLog(@"Repeated Timer");
return %orig;
}
%new
-(void)updatePreferences{
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:settingsPath];
if([prefs objectForKey:@"isEnabledForTimer"] != nil){
self.isEnabled = [[prefs objectForKey:@"isEnabledForTimer"] boolValue];
}
if ([prefs objectForKey:@"fadeIsEnabledForTimer"] != nil){
self.fadeIsEnabled = [[prefs objectForKey:@"fadeIsEnabledForTimer"] boolValue];
}
if([prefs objectForKey:@"fadeInSecondsTimer"] != nil){
self.fadeSeconds = [[prefs objectForKey:@"fadeInSecondsTimer"] intValue];
}
if([prefs objectForKey:@"maxVolumeTimer"] != nil){
self.maxVolume = [[prefs objectForKey:@"maxVolumeTimer"] floatValue];
}
[prefs release];
}
%new
-(void)onTick:(NSTimer *)timer {
NSLog(@"Timer active");
if(self.currentVolume <= self.maxVolume){
[self.avSystemController setActiveCategoryVolumeTo: self.currentVolume];
self.currentVolume += self.volumeIncrement;
}
else{
[self.timer invalidate];
self.timer = nil;
}
}
%new
-(void)stopTimer{
NSLog(@"Timer stopped");
if(self.timer != nil && [self.timer isValid]){
[self.timer invalidate];
self.timer = nil;
}
}
%new
-(void)restoreRingerVolume{
if(self.isEnabled){
NSLog(@"Reset Volume");
[self.avSystemController setVolumeTo: self.originalRingerVolume forCategory:@"Ringtone"];
}
}
%end