1 | //
|
---|
2 | // PSTimer.m
|
---|
3 | // Pester
|
---|
4 | //
|
---|
5 | // Created by Nicholas Riley on Sun Jan 05 2003.
|
---|
6 | // Copyright (c) 2003 Nicholas Riley. All rights reserved.
|
---|
7 | //
|
---|
8 |
|
---|
9 | #import "PSTimer.h"
|
---|
10 | #import "PSAlarm.h"
|
---|
11 | #import "PSPowerManager.h"
|
---|
12 |
|
---|
13 | NSTimer *PSTimerCurrent = nil;
|
---|
14 | PSTimer *PSTimerOnWake = nil;
|
---|
15 | NSMutableArray *PSTimerAllTimers = nil;
|
---|
16 |
|
---|
17 | @interface PSTimer (Private)
|
---|
18 | + (void)_schedule;
|
---|
19 | @end
|
---|
20 |
|
---|
21 | @implementation PSTimer
|
---|
22 |
|
---|
23 | + (void)setUp;
|
---|
24 | {
|
---|
25 | static PSPowerManager *powerManager;
|
---|
26 |
|
---|
27 | if (powerManager == nil) {
|
---|
28 | powerManager = [[PSPowerManager alloc] initWithDelegate: self];
|
---|
29 | PSTimerAllTimers = [[NSMutableArray alloc] init];
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | + (void)_schedule;
|
---|
34 | {
|
---|
35 | NSDate *aboutNow = [NSDate dateWithTimeIntervalSinceNow: 0.1];
|
---|
36 | [PSTimerCurrent invalidate]; [PSTimerCurrent release]; PSTimerCurrent = nil;
|
---|
37 | PSTimerOnWake = nil;
|
---|
38 | if ([PSTimerAllTimers count] > 0) {
|
---|
39 | PSTimer *timer = nil;
|
---|
40 | NSEnumerator *e;
|
---|
41 | [PSTimerAllTimers sortUsingSelector: @selector(compare:)];
|
---|
42 | // NSLog(@"_schedule: timers %@", [PSTimerAllTimers description]);
|
---|
43 | e = [PSTimerAllTimers objectEnumerator];
|
---|
44 | while ( (timer = [e nextObject]) != nil) {
|
---|
45 | if ([timer isWakeUp]) {
|
---|
46 | PSTimerOnWake = timer;
|
---|
47 | // NSLog(@"scheduling wake timer %@", timer);
|
---|
48 | break;
|
---|
49 | }
|
---|
50 | }
|
---|
51 | e = [PSTimerAllTimers objectEnumerator];
|
---|
52 | while ( (timer = [e nextObject]) != nil) {
|
---|
53 | if ([[timer fireDate] compare: aboutNow] != NSOrderedDescending) {
|
---|
54 | [timer performSelector: @selector(_timerExpired) withObject: nil afterDelay: 0];
|
---|
55 | return;
|
---|
56 | } else {
|
---|
57 | NSTimeInterval ti = [[timer fireDate] timeIntervalSinceNow];
|
---|
58 | if (ti > 0.1) {
|
---|
59 | PSTimerCurrent = [[NSTimer scheduledTimerWithTimeInterval: ti target: timer selector: @selector(_timerExpired) userInfo: nil repeats: NO] retain];
|
---|
60 | // NSLog(@"setting timer: %@", PSTimerCurrent);
|
---|
61 | } else {
|
---|
62 | // NSLog(@"timer would have been too fast, setting: %@", timer);
|
---|
63 | [timer performSelector: @selector(_timerExpired) withObject: nil afterDelay: 0];
|
---|
64 | }
|
---|
65 | return;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | NSAssert(NO, @"shouldn't get here");
|
---|
69 | } else {
|
---|
70 | // NSLog(@"_schedule: no timers");
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | + (void)_timerAdded:(PSTimer *)timer;
|
---|
75 | {
|
---|
76 | NSAssert1([PSTimerAllTimers indexOfObject: timer] == NSNotFound, @"PSTimerAllTimers already contains %@", timer);
|
---|
77 | [PSTimerAllTimers addObject: timer];
|
---|
78 | [self _schedule];
|
---|
79 | }
|
---|
80 |
|
---|
81 | + (void)_timerDeleted:(PSTimer *)timer;
|
---|
82 | {
|
---|
83 | NSAssert1([PSTimerAllTimers indexOfObject: timer] != NSNotFound, @"PSTimerAllTimers does not contain %@", timer);
|
---|
84 | [PSTimerAllTimers removeObject: timer];
|
---|
85 | [self _schedule];
|
---|
86 | }
|
---|
87 |
|
---|
88 | #pragma mark private
|
---|
89 |
|
---|
90 | - (void)_setFireDate:(NSDate *)date;
|
---|
91 | {
|
---|
92 | if (fireDate != date) {
|
---|
93 | [fireDate release];
|
---|
94 | fireDate = [date retain];
|
---|
95 | if (fireDate != nil) {
|
---|
96 | isValid = YES;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | - (void)_setFireDateFromInterval;
|
---|
102 | {
|
---|
103 | [self _setFireDate: [NSDate dateWithTimeIntervalSinceNow: timeInterval]];
|
---|
104 | }
|
---|
105 |
|
---|
106 | - (void)_invalidate;
|
---|
107 | {
|
---|
108 | if (isValid) {
|
---|
109 | isValid = NO;
|
---|
110 | [[self class] _timerDeleted: self];
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | - (void)_timerExpired;
|
---|
115 | {
|
---|
116 | if (!isValid) return; // in case the timer went off after we were invalidated
|
---|
117 | [self retain]; // make sure we’re still accessible during the invocation
|
---|
118 | // NSLog(@"timer expired: %@", self);
|
---|
119 | if (repeats) {
|
---|
120 | [invocation invoke];
|
---|
121 | if (isValid) {
|
---|
122 | [self _setFireDateFromInterval];
|
---|
123 | [[self class] _schedule];
|
---|
124 | // NSLog(@"timer repeats: %@", self);
|
---|
125 | }
|
---|
126 | } else {
|
---|
127 | [self _invalidate];
|
---|
128 | [invocation invoke];
|
---|
129 | }
|
---|
130 | [self release];
|
---|
131 | }
|
---|
132 |
|
---|
133 | #pragma mark initialize-release
|
---|
134 |
|
---|
135 | - (id)initWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)anObject repeats:(BOOL)yesOrNo;
|
---|
136 | {
|
---|
137 | if ( (self = [self init]) != nil) {
|
---|
138 | invocation = [[NSInvocation invocationWithMethodSignature:
|
---|
139 | [aTarget methodSignatureForSelector: aSelector]] retain];
|
---|
140 | [invocation setSelector: aSelector];
|
---|
141 | [invocation setTarget: aTarget];
|
---|
142 | [invocation setArgument: &self atIndex: 2];
|
---|
143 | userInfo = [anObject retain];
|
---|
144 | repeats = yesOrNo;
|
---|
145 | timeInterval = ti;
|
---|
146 | [invocation retainArguments]; // mimics retain behavior
|
---|
147 | [self _setFireDateFromInterval];
|
---|
148 | [[self class] _timerAdded: self]; // mimics runloop retention behavior
|
---|
149 | }
|
---|
150 | return self;
|
---|
151 | }
|
---|
152 |
|
---|
153 | + (PSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)anObject repeats:(BOOL)yesOrNo;
|
---|
154 | {
|
---|
155 | PSTimer *timer = [[self alloc] initWithTimeInterval: ti target: aTarget selector: aSelector userInfo: anObject repeats: yesOrNo];
|
---|
156 | [timer release];
|
---|
157 | return timer;
|
---|
158 | }
|
---|
159 |
|
---|
160 | - (void)dealloc;
|
---|
161 | {
|
---|
162 | // NSLog(@"DEALLOC %@", self);
|
---|
163 | isValid = NO;
|
---|
164 | [fireDate release]; fireDate = nil;
|
---|
165 | [invocation release]; invocation = nil;
|
---|
166 | [userInfo release]; userInfo = nil;
|
---|
167 | [super dealloc];
|
---|
168 | }
|
---|
169 |
|
---|
170 | #pragma mark accessing
|
---|
171 |
|
---|
172 | - (NSDate *)fireDate;
|
---|
173 | {
|
---|
174 | return fireDate;
|
---|
175 | }
|
---|
176 |
|
---|
177 | - (void)invalidate;
|
---|
178 | {
|
---|
179 | repeats = NO;
|
---|
180 | [self _invalidate];
|
---|
181 | }
|
---|
182 |
|
---|
183 | - (BOOL)isValid;
|
---|
184 | {
|
---|
185 | return isValid;
|
---|
186 | }
|
---|
187 |
|
---|
188 | - (id)userInfo;
|
---|
189 | {
|
---|
190 | return userInfo;
|
---|
191 | }
|
---|
192 |
|
---|
193 | - (BOOL)isWakeUp;
|
---|
194 | {
|
---|
195 | return isWakeUp;
|
---|
196 | }
|
---|
197 |
|
---|
198 | - (void)setWakeUp:(BOOL)doWake;
|
---|
199 | {
|
---|
200 | isWakeUp = doWake;
|
---|
201 | }
|
---|
202 |
|
---|
203 | - (NSComparisonResult)compare:(PSTimer *)other;
|
---|
204 | {
|
---|
205 | return [fireDate compare: [other fireDate]];
|
---|
206 | }
|
---|
207 |
|
---|
208 | - (NSString *)description;
|
---|
209 | {
|
---|
210 | return [NSString stringWithFormat: @"%@: at %@ do %@ on %@", [super description], [self fireDate], NSStringFromSelector([invocation selector]), [[invocation target] class]];
|
---|
211 | }
|
---|
212 |
|
---|
213 | @end
|
---|
214 |
|
---|
215 | @implementation PSTimer (PSPowerManagerDelegate)
|
---|
216 |
|
---|
217 | + (BOOL)powerManagerShouldIdleSleep:(PSPowerManager *)powerManager;
|
---|
218 | {
|
---|
219 | [PSTimerCurrent invalidate];
|
---|
220 | if (PSTimerOnWake != nil) {
|
---|
221 | NSDate *date = [PSTimerOnWake fireDate];
|
---|
222 | // NSLog(@"%lf sec remain until alarm", [date timeIntervalSinceNow]);
|
---|
223 | if ([date timeIntervalSinceNow] > 30) {
|
---|
224 | // NSLog(@"going to sleep, setting timer %@", PSTimerOnWake);
|
---|
225 | [PSPowerManager setWakeTime: [[PSTimerOnWake fireDate] addTimeInterval: -15]];
|
---|
226 | return YES;
|
---|
227 | } else {
|
---|
228 | // NSLog(@"not setting timer, will attempt to prevent idle sleep");
|
---|
229 | return NO;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | return YES;
|
---|
233 | }
|
---|
234 |
|
---|
235 | + (void)powerManagerWillDemandSleep:(PSPowerManager *)powerManager;
|
---|
236 | {
|
---|
237 | [self powerManagerShouldIdleSleep: powerManager];
|
---|
238 | return;
|
---|
239 | }
|
---|
240 |
|
---|
241 | + (void)powerManagerDidWake:(PSPowerManager *)powerManager;
|
---|
242 | {
|
---|
243 | if (PSTimerCurrent != nil) {
|
---|
244 | [self _schedule];
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | @end |
---|