-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClockViewController.m
564 lines (474 loc) · 18.5 KB
/
ClockViewController.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//
// ClockViewController.m
// Sleep Blaster touch
//
// Created by Eamon Ford on 11/24/09.
// Copyright 2009 The Byte Factory. All rights reserved.
//
#import "ClockViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import "Constants.h"
#import <dlfcn.h>
#import <MediaPlayer/MediaPlayer.h>
#import "SCListener.h"
#import "AlarmRingingViewController.h"
#import "AlarmController.h"
#import "Sleep_Blaster_touchAppDelegate.h"
#import "NSLocale+Misc.h"
#import "AlarmSettingsViewController.h"
#define degreesToRadian(x) (M_PI * x / 180.0)
@implementation ClockViewController
@synthesize timer;
@synthesize alarmPopoverController;
@synthesize sleepTimerPopoverController;
@synthesize rightSettingsButton;
BOOL alreadyFadedInClock = NO;
BOOL alarmPopoverWasVisibleBeforeRotation = NO;
BOOL sleepTimerPopoverWasVisibleBeforeRotation = NO;
UIInterfaceOrientation oldOrientation;
NSUInteger loadFonts()
{
NSUInteger newFontCount = 0;
NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:@"com.apple.GraphicsServices"];
const char *frameworkPath = [[frameworkBundle executablePath] UTF8String];
if (frameworkPath) {
void *graphicsServices = dlopen(frameworkPath, RTLD_NOLOAD | RTLD_LAZY);
if (graphicsServices) {
BOOL (*GSFontAddFromFile)(const char *) = dlsym(graphicsServices, "GSFontAddFromFile");
if (GSFontAddFromFile)
for (NSString *fontFile in [[NSBundle mainBundle] pathsForResourcesOfType:@"ttf" inDirectory:nil])
newFontCount += GSFontAddFromFile([fontFile UTF8String]);
}
}
return newFontCount;
}
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"clock view did load");
CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
self.view.frame = rect;
loadFonts();
[self setFontsForLabels];
[self setCurrentDateAndTimeLabels];
[self positionSettingsButtons];
// CGRect rect = rightSettingsButton.frame;
// rect.origin.x = [UIScreen mainScreen].bounds.size.width - rect.size.width - 10.0;
// rect.origin.y = [UIScreen mainScreen].bounds.size.height - rect.size.height - 10.0;
// rightSettingsButton.frame = rect;
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self
selector:@selector(updateClock:)
userInfo:nil repeats:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(userDefaultsChanged:)
name:NSUserDefaultsDidChangeNotification
object:[NSUserDefaults standardUserDefaults]];
[[AlarmController sharedAlarmController] setupAlarm:self];
}
- (void)userDefaultsChanged:(NSNotification *)notification
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
if ([[[NSUserDefaults standardUserDefaults] objectForKey:kAlarmOn] boolValue])
{
alarmBell.alpha = 1.0;
} else {
portraitAlarmBell.alpha = 0.0;
alarmBell.alpha = 0.0;
}
[UIView commitAnimations];
}
/*- (void)didEnterBackground:(NSNotification *)notification
{
if (self.timer)
{
[self.timer invalidate];
}
NSLog(@"did enter background!");
}
- (void)didBecomeActive:(NSNotification *)notification
{
NSLog(@"did become active!");
if (![timer isValid])
{
NSLog(@"revalidating the timer!");
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self
selector:@selector(updateClock:)
userInfo:nil repeats:YES];
} else {
NSLog(@"it is valid already...");
}
}
*/
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if ([[[NSUserDefaults standardUserDefaults] objectForKey:kAlarmOn] boolValue])
{
portraitAlarmBell.alpha = 1.0;
alarmBell.alpha = 1.0;
} else {
portraitAlarmBell.alpha = 0.0;
alarmBell.alpha = 0.0;
}
/*if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
NSLog(@"it's portrait!");
} else if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
NSLog(@"it's landscape!");
} else if (!UIDeviceOrientationIsValidInterfaceOrientation([UIDevice currentDevice].orientation))
{
NSLog(@"it's not a valid orientation!");
}*/
}
- (void)viewDidAppear:(BOOL)animated
{
if (!alreadyFadedInClock)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
self.view.alpha = 1.0;
[UIView commitAnimations];
alreadyFadedInClock = YES;
}
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:@"iPhone"])
{
if ([[[NSUserDefaults standardUserDefaults] objectForKey:kAlarmOn] boolValue])
{
if (![[[NSUserDefaults standardUserDefaults] objectForKey:kHasShownBrightnessMessage] boolValue])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Dimming the screen at night" message:@"You can dim the screen by double-tapping on it."
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:kHasShownBrightnessMessage];
}
}
}
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
oldOrientation = self.interfaceOrientation;
if (alarmPopoverController.popoverVisible)
{
if (alarmPopoverController.contentViewController.modalViewController)
{
// Sleep_Blaster_touchAppDelegate *mainDelegate = (Sleep_Blaster_touchAppDelegate *)[[UIApplication sharedApplication] delegate];
[((MPMediaPickerController *)alarmPopoverController.contentViewController.modalViewController).delegate mediaPickerDidCancel:alarmPopoverController.contentViewController.modalViewController];
}
[alarmPopoverController dismissPopoverAnimated:YES];
alarmPopoverWasVisibleBeforeRotation = YES;
}
[self.view setNeedsLayout];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if (alarmPopoverWasVisibleBeforeRotation)
{
[alarmPopoverController presentPopoverFromRect:rightSettingsButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
alarmPopoverWasVisibleBeforeRotation = NO;
}
}
- (void)positionSettingsButtons
{
CGRect rightRect = rightSettingsButton.frame;
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation) ||
!UIDeviceOrientationIsValidInterfaceOrientation([UIDevice currentDevice].orientation))
{
rightRect.origin.x = [UIScreen mainScreen].bounds.size.width - rightRect.size.width - 10.0;
rightRect.origin.y = [UIScreen mainScreen].bounds.size.height - rightRect.size.height - 10.0;
} else {
rightRect.origin.y = [UIScreen mainScreen].bounds.size.width - rightRect.size.width - 10.0;
rightRect.origin.x = [UIScreen mainScreen].bounds.size.height - rightRect.size.height - 10.0;
}
rightSettingsButton.frame = rightRect;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
[self positionSettingsButtons];
}
- (void)setFontsForLabels
{
UIFont *bigFont;
UIFont *mediumFont;
UIFont *smallFont;
UIFont *smallerFont;
UIColor *shadowColor = [UIColor colorWithRed:0.4 green:0.83 blue:0.9 alpha:0.7];
hourLabel1.shadowColor = shadowColor;
hourLabel2.shadowColor = shadowColor;
minuteLabel1.shadowColor = shadowColor;
minuteLabel2.shadowColor = shadowColor;
colonLabel.shadowColor = shadowColor;
secondLabel1.shadowColor = shadowColor;
secondLabel2.shadowColor = shadowColor;
[shadowColor release];
shadowColor = [UIColor colorWithRed:0.29 green:0.75 blue:0.14 alpha:0.5];
sunLabel.shadowColor = shadowColor;
monLabel.shadowColor = shadowColor;
tueLabel.shadowColor = shadowColor;
wedLabel.shadowColor = shadowColor;
thuLabel.shadowColor = shadowColor;
friLabel.shadowColor = shadowColor;
satLabel.shadowColor = shadowColor;
amLabel.shadowColor = shadowColor;
pmLabel.shadowColor = shadowColor;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// The device is an iPad running iPhone 3.2 or later.
bigFont = [UIFont fontWithName:@"Digital-7" size:216.0];
mediumFont = [UIFont fontWithName:@"Digital-7" size:108.0];
smallFont = [UIFont fontWithName:@"Digital-7" size:42.0];
smallerFont = [UIFont fontWithName:@"Digital-7" size:30.0];
hourLabel1.shadowBlur = 10.0;
hourLabel2.shadowBlur = 10.0;
minuteLabel1.shadowBlur = 10.0;
minuteLabel2.shadowBlur = 10.0;
colonLabel.shadowBlur = 10.0;
secondLabel1.shadowBlur = 10.0;
secondLabel2.shadowBlur = 10.0;
} else {
// The device is an iPhone or iPod touch.
bigFont = [UIFont fontWithName:@"Digital-7" size:93.0];
mediumFont = [UIFont fontWithName:@"Digital-7" size:47.0];
smallFont = [UIFont fontWithName:@"Digital-7" size:18.3];
smallerFont = [UIFont fontWithName:@"Digital-7" size:13.0];
}
/* portraitHourLabel1.font = bigFont;
portraitHourLabel2.font = bigFont;
portraitMinuteLabel1.font = bigFont;
portraitMinuteLabel2.font = bigFont;
portraitColonLabel.font = mediumFont;
portraitSecondLabel1.font = mediumFont;
portraitSecondLabel2.font = mediumFont;
portraitSunLabel.font = smallerFont;
portraitMonLabel.font = smallerFont;
portraitTueLabel.font = smallerFont;
portraitWedLabel.font = smallerFont;
portraitThuLabel.font = smallerFont;
portraitFriLabel.font = smallerFont;
portraitSatLabel.font = smallerFont;
portraitAmLabel.font = smallFont;
portraitPmLabel.font = smallFont;
*/
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// The device is an iPad running iPhone 3.2 or later.
bigFont = [UIFont fontWithName:@"Digital-7" size:288.0];
mediumFont = [UIFont fontWithName:@"Digital-7" size:144.0];
smallFont = [UIFont fontWithName:@"Digital-7" size:56];
smallerFont = [UIFont fontWithName:@"Digital-7" size:40.0];
}
else
#endif
{
// The device is an iPhone or iPod touch.
bigFont = [UIFont fontWithName:@"Digital-7" size:144.0];
mediumFont = [UIFont fontWithName:@"Digital-7" size:72.0];
smallFont = [UIFont fontWithName:@"Digital-7" size:28.0];
smallerFont = [UIFont fontWithName:@"Digital-7" size:20.0];
}
hourLabel1.font = bigFont;
hourLabel2.font = bigFont;
minuteLabel1.font = bigFont;
minuteLabel2.font = bigFont;
colonLabel.font = mediumFont;
secondLabel1.font = mediumFont;
secondLabel2.font = mediumFont;
sunLabel.font = smallerFont;
monLabel.font = smallerFont;
tueLabel.font = smallerFont;
wedLabel.font = smallerFont;
thuLabel.font = smallerFont;
friLabel.font = smallerFont;
satLabel.font = smallerFont;
amLabel.font = smallFont;
pmLabel.font = smallFont;
}
- (void)updateClock:(NSTimer *)theTimer
{
// NSLog(@"update clock!");
[self setCurrentDateAndTimeLabels];
}
- (void)setCurrentDateAndTimeLabels
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
if ([[[NSCalendar currentCalendar] locale] timeIs24HourFormat])
{
[dateFormatter setDateFormat:@"HH:mm:ss"];
} else {
[dateFormatter setDateFormat:@"h:mm:ss"];
}
NSString *currentTime = [dateFormatter stringFromDate:[NSDate date]];
// We'll go through this string backwards, and then check if the hour should be 1 or 2 digits.
[portraitSecondLabel2 setText:[NSString stringWithFormat:@"%C", [currentTime characterAtIndex:[currentTime length]-1]]];
[portraitSecondLabel1 setText:[NSString stringWithFormat:@"%C", [currentTime characterAtIndex:[currentTime length]-2]]];
[portraitMinuteLabel2 setText:[NSString stringWithFormat:@"%C", [currentTime characterAtIndex:[currentTime length]-4]]];
[portraitMinuteLabel1 setText:[NSString stringWithFormat:@"%C", [currentTime characterAtIndex:[currentTime length]-5]]];
[portraitHourLabel2 setText:[NSString stringWithFormat:@"%C:", [currentTime characterAtIndex:[currentTime length]-7]]];
[secondLabel2 setText:[NSString stringWithFormat:@"%C", [currentTime characterAtIndex:[currentTime length]-1]]];
[secondLabel1 setText:[NSString stringWithFormat:@"%C", [currentTime characterAtIndex:[currentTime length]-2]]];
[minuteLabel2 setText:[NSString stringWithFormat:@"%C", [currentTime characterAtIndex:[currentTime length]-4]]];
[minuteLabel1 setText:[NSString stringWithFormat:@"%C", [currentTime characterAtIndex:[currentTime length]-5]]];
[hourLabel2 setText:[NSString stringWithFormat:@"%C:", [currentTime characterAtIndex:[currentTime length]-7]]];
if ([currentTime length] == 8) {
[portraitHourLabel1 setText:[NSString stringWithFormat:@"%C", [currentTime characterAtIndex:0]]];
[hourLabel1 setText:[NSString stringWithFormat:@"%C", [currentTime characterAtIndex:0]]];
} else {
[portraitHourLabel1 setText:@""];
[hourLabel1 setText:@""];
}
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocale];
[usLocale release];
[dateFormatter setDateFormat:@"e"];
NSString *weekday = [dateFormatter stringFromDate:[NSDate date]];
// portraitSunLabel.hidden = YES;
// portraitMonLabel.hidden = YES;
// portraitTueLabel.hidden = YES;
// portraitWedLabel.hidden = YES;
// portraitThuLabel.hidden = YES;
// portraitFriLabel.hidden = YES;
// portraitSatLabel.hidden = YES;
sunLabel.hidden = YES;
monLabel.hidden = YES;
tueLabel.hidden = YES;
wedLabel.hidden = YES;
thuLabel.hidden = YES;
friLabel.hidden = YES;
satLabel.hidden = YES;
if ([weekday isEqualToString:@"1"]) {
// portraitSunLabel.hidden = NO;
sunLabel.hidden = NO;
} else if ([weekday isEqualToString:@"2"]) {
// portraitMonLabel.hidden = NO;
monLabel.hidden = NO;
} else if ([weekday isEqualToString:@"3"]) {
// portraitTueLabel.hidden = NO;
tueLabel.hidden = NO;
} else if ([weekday isEqualToString:@"4"]) {
// portraitWedLabel.hidden = NO;
wedLabel.hidden = NO;
} else if ([weekday isEqualToString:@"5"]) {
// portraitThuLabel.hidden = NO;
thuLabel.hidden = NO;
} else if ([weekday isEqualToString:@"6"]) {
// portraitFriLabel.hidden = NO;
friLabel.hidden = NO;
} else if ([weekday isEqualToString:@"7"]) {
// portraitSatLabel.hidden = NO;
satLabel.hidden = NO;
}
[dateFormatter setDateFormat:@"a"];
NSString *ampm = [dateFormatter stringFromDate:[NSDate date]];
// portraitAmLabel.hidden = YES;
amLabel.hidden = YES;
// portraitPmLabel.hidden = YES;
pmLabel.hidden = YES;
if (![[[NSCalendar currentCalendar] locale] timeIs24HourFormat])
{
if ([ampm isEqualToString:@"AM"]) {
// portraitAmLabel.hidden = NO;
amLabel.hidden = NO;
} else if ([ampm isEqualToString:@"PM"]) {
// portraitPmLabel.hidden = NO;
pmLabel.hidden = NO;
}
}
}
/*
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([viewController class] == [UINavigationController class])
{ NSLog(@"navigation controller!");
} else {
NSLog(@"not a navigation controller");
}
NSLog(@"did show a view controller, resizing to %f, %f", viewController.view.frame.size.width, viewController.view.frame.size.height);
[popoverController setPopoverContentSize:viewController.view.frame.size animated:NO];
}
*/
- (IBAction)infoButtonTapped:(id)sender
{
Sleep_Blaster_touchAppDelegate *mainDelegate = (Sleep_Blaster_touchAppDelegate *)[[UIApplication sharedApplication] delegate];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// The device is an iPad running iPhone 3.2 or later.
if (!alarmPopoverController)
{
// alarmPopoverController = [[UIPopoverController alloc] initWithContentViewController:mainDelegate.alarmSettingsNavigationController];
alarmPopoverController = [[UIPopoverController alloc] initWithContentViewController:mainDelegate.tabBarController];
CGSize size = ((AlarmSettingsViewController *)[mainDelegate.alarmSettingsNavigationController.viewControllers objectAtIndex:0]).view.frame.size;
size.height += 37.0; // For some reason, only here, we have to add 37 pixels to the height. It has something to do with the navigation controller.
size.width = 320.0;
[alarmPopoverController setPopoverContentSize:size];
}
[alarmPopoverController presentPopoverFromRect:[sender frame] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
// The device is an iPhone or iPod touch.
[mainDelegate flipToSettings];
}
}
- (IBAction)sleepTimerButtonTapped:(id)sender
{
Sleep_Blaster_touchAppDelegate *mainDelegate = (Sleep_Blaster_touchAppDelegate *)[[UIApplication sharedApplication] delegate];
// The device is an iPad running iPhone 3.2 or later.
if (!sleepTimerPopoverController)
{
sleepTimerPopoverController = [[UIPopoverController alloc] initWithContentViewController:mainDelegate.sleepTimerSettingsNavigationController];
CGSize size = ((SleepTimerSettingsViewController *)[mainDelegate.sleepTimerSettingsNavigationController.viewControllers objectAtIndex:0]).view.frame.size;
size.height += 37; // For some reason, only here, we have to add 37 pixels to the height. It has something to do with the navigation controller.
[sleepTimerPopoverController setPopoverContentSize:size];
}
[sleepTimerPopoverController presentPopoverFromRect:[sender frame] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSUInteger tapCount = [touch tapCount];
switch (tapCount) {
case 2:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
if (self.view.alpha == 1.0)
{
self.view.alpha = 0.25;
} else {
self.view.alpha = 1.0;
}
[UIView commitAnimations];
break;
default:
break;
}
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[alarmPopoverController release];
[sleepTimerPopoverController release];
[super dealloc];
}
@end