-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRMLSnoozer.m
185 lines (145 loc) · 4.91 KB
/
RMLSnoozer.m
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
#import "RMLSnoozer.h"
#include "RMLDebug.h"
#import <AVFoundation/AVAudioPlayer.h>
#import <AVFoundation/AVAudioSession.h>
#import <AudioToolbox/AudioToolbox.h>
#define WAKE_TIME 8
#pragma mark Fake interfaces to deal with private APIs
@interface NSURL (private)
-(BOOL)checkResourceIsReachableAndReturnError:(NSError **)error;
@end
#pragma mark Implementation
@implementation RMLSnoozer
@synthesize minutes = _minutes;
@synthesize pendingEvent = _pendingEvent;
@synthesize delegate = _delegate;
@synthesize snoozeTimer = _snoozeTimer;
static AVAudioPlayer *audioPlayer = nil;
#pragma mark Object lifecycle
-(id)initWithTime:(int)minutes delegate:(id <RMLSnoozerDelegate>)delegate {
if ((self = [super init])) {
self.minutes = minutes;
self.delegate = delegate;
}
return self;
}
-(void)dealloc {
[_delegate release];
[_snoozeTimer release];
[super dealloc];
}
#pragma mark Helpers to keep phone awake while snoozing
-(void)setupAudioPlayer {
#if DEBUG_LOG
NSLog(@"RMLSnoozer.setupAudioPlayer: in");
#endif
if (nil != audioPlayer) {
return;
}
#if DEBUG_LOG
NSLog(@"RMLSnoozer.setupAudioPlayer: configuring audioPlayer");
#endif
// Allow audio mixing - seems to be required on iOS 4.1
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&setCategoryError];
#if DEBUG_LOG
if (setCategoryError) {
NSLog(@"RMLSnoozer.setupAudioPlayer: setCategory failed with %@", setCategoryError);
}
#endif
#if TARGET_IPHONE_SIMULATOR
NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"silence" ofType:@"wav"];
#else
NSString *soundFile = @"/Library/RemindMeLater/silence.wav";
#endif
NSURL *fileUrl = [[NSURL alloc] initFileURLWithPath:soundFile];
if ([fileUrl respondsToSelector:@selector(checkResourceIsReachableAndReturnError:)] && [fileUrl checkResourceIsReachableAndReturnError:nil] == NO) {
NSLog(@"RMLSnoozer.setupAudioPlayer: fileUrl %@ is not reachable", fileUrl);
}
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];
#if DEBUG_LOG
NSLog(@"RMLSnoozer.setupAudioPlayer: fileUrl is %@", fileUrl);
#endif
[fileUrl release];
if ([audioPlayer prepareToPlay] == NO) {
#if DEBUG_LOG
NSLog(@"RMLSnoozer.setupAudioPlayer: prepareToPlay failed");
#endif
}
audioPlayer.volume = 0;
#if DEBUG_LOG
NSLog(@"RMLSnoozer.setupAudioPlayer: audioPlayer is %@", audioPlayer);
#endif
}
-(BOOL)audioIsPlaying {
UInt32 propertySize = sizeof(UInt32);
UInt32 audioIsAlreadyPlaying = 0;
AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &audioIsAlreadyPlaying);
#if DEBUG_LOG
NSLog(@"RMLSnoozer.audioIsPlaying: audioIsAlreadyPlaying=%d", audioIsAlreadyPlaying);
#endif
return 0 != audioIsAlreadyPlaying;
}
-(void)keepPhoneAwake {
#if DEBUG_LOG
NSLog(@"RMLSnoozer.keepPhoneAwake: waking up every %d seconds to keep phone from shutting down", WAKE_TIME);
#endif
[self setupAudioPlayer];
[NSTimer scheduledTimerWithTimeInterval:WAKE_TIME target:self selector:@selector(keepPhoneAwakeTimerCallback:) userInfo:nil repeats:false];
}
-(void)keepPhoneAwakeTimerCallback:(NSTimer *)timer {
#if DEBUG_LOG
NSLog(@"RMLSnoozer.keepPhoneAwakeCallback: in, pendingEvents is %d", self.pendingEvent);
#endif
if (!self.pendingEvent) {
return;
}
if ([self audioIsPlaying]) {
#if DEBUG_LOG
NSLog(@"RMLSnoozer.keepPhoneAwakeCallback: Audio is playing right now, not doing anything.");
#endif
} else {
#if DEBUG_LOG
NSLog(@"RMLSnoozer.keepPhoneAwakeCallback: No audio playing right now, sending a blip to keep the phone awake.");
#endif
[audioPlayer play];
}
[self keepPhoneAwake];
}
#pragma mark Core snooze interface
-(void)snoozeTimerCallback:(NSTimer *)timer {
self.pendingEvent = FALSE;
self.snoozeTimer = NULL;
id payload = [timer userInfo];
#if DEBUG_LOG
NSLog(@"RMLSnoozer.snoozeTimerCallback: timer's payload is %@", payload);
#endif
[_delegate snoozeCallback:payload];
}
-(void)snooze:(id)payload {
self.pendingEvent = TRUE;
// First schedule our real action
int sleepSeconds = _minutes * 60;
#if DEBUG_SLEEP_SECONDS || TARGET_IPHONE_SIMULATOR
// User originally picked snooze time in minutes; change units to seconds instead
sleepSeconds /= 60;
#endif
#if DEBUG_LOG
NSLog(@"RMLSnoozer.snooze: Sleeping for %d seconds", sleepSeconds);
#endif
self.snoozeTimer = [NSTimer scheduledTimerWithTimeInterval:sleepSeconds target:self
selector:@selector(snoozeTimerCallback:)
userInfo:payload
repeats:false];
// Now schedule something to keep the phone awake
[self keepPhoneAwake];
}
-(void)invalidate {
#if DEBUG_LOG
NSLog(@"RMLSnoozer.invalidate: Aborting timer");
#endif
[self.snoozeTimer invalidate];
self.pendingEvent = FALSE;
self.snoozeTimer = NULL;
}
@end