-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDelegate.mm
246 lines (203 loc) · 7.65 KB
/
AppDelegate.mm
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
// -*- Mode: ObjC -*-
//
// Copyright (C) 2011, Brad Howes. All rights reserved.
//
#import "AppDelegate.h"
#import "BitDetector.h"
#import "BitFrameDecoder.h"
#import "BitStreamFrameDetector.h"
#import "DetectionsViewController.h"
#import "Dropbox.keys"
#import "DropboxUploader.h"
#import "FrequencyDetector.h"
#import "IASKSpecifier.h"
#import "IndicatorButton.h"
#import "RecordingInfo.h"
#import "RecordingsViewController.h"
#import "SampleRecorder.h"
#import "PeakDetector.h"
#import "PulseWidthDetector.h"
#import "SampleViewController.h"
#import "SettingsViewController.h"
#import "UserSettings.h"
#import "VertexBufferManager.h"
#import "WaveCycleDetector.h"
@interface AppDelegate (Private)
- (void)makeSignalDetector;
@end
@implementation AppDelegate
@synthesize window, tabBarController, sampleViewController, settingsViewController, recordingsViewController;
@synthesize detectionsViewController, dropboxSession;
@synthesize dataCapture, signalDetector, switchDetector, vertexBufferManager;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
LOG(@"AppDelegate.application:didFinishLaunchingWithOptions:");
NSUserDefaults* settings = [NSUserDefaults standardUserDefaults];
//
// Dropbox SDK initialization
//
NSString* consumerKey = DROPBOX_KEY;
NSString* consumerSecret = DROPBOX_SECRET;
dropboxSession = [[[DBSession alloc] initWithAppKey:consumerKey appSecret:consumerSecret root:kDBRootDropbox] autorelease];
dropboxSession.delegate = self;
[DBSession setSharedSession:dropboxSession];
[application setStatusBarStyle:UIStatusBarStyleBlackOpaque];
self.dataCapture = [DataCapture create];
[self makeSignalDetector];
self.switchDetector = [MicSwitchDetector createWithSampleRate:dataCapture.sampleRate];
self.vertexBufferManager = [VertexBufferManager createForDuration:1.0 sampleRate:dataCapture.sampleRate bufferSize:512 * 4];
dataCapture.invertSignal = [settings boolForKey:kSettingsSignalProcessingInvertSignalKey];
dataCapture.switchDetector = switchDetector;
dataCapture.vertexBufferManager = vertexBufferManager;
application.idleTimerDisabled = YES;
self.window.rootViewController = tabBarController;
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(start) userInfo:nil repeats:NO];
LOG(@"AppDelegate.application:didFinishLaunchingWithOptions: - END");
return YES;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
if ([[DBSession sharedSession] handleOpenURL:url]) {
if ([[DBSession sharedSession] isLinked]) {
LOG(@"App linked successfully!");
// At this point you can start making API calls
if (settingsViewController != nil) {
[settingsViewController updateDropboxCell];
}
}
return YES;
}
// Add whatever other url handling code your app requires here
return NO;
}
- (void)makeSignalDetector
{
NSString* detectorClass = [[NSUserDefaults standardUserDefaults]
stringForKey:kSettingsSignalProcessingActiveDetectorKey];
NSBundle* bundle = [NSBundle mainBundle];
Class cls = [bundle classNamed:detectorClass];
self.signalDetector = [[[cls alloc] init] autorelease];
self.detectionsViewController.detector = self.signalDetector;
self.dataCapture.sampleProcessor = [self.signalDetector sampleProcessor];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
LOG(@"applicationWillResignActive");
[self stop];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
LOG(@"applicationDidBecomeActive");
vertexBufferManager.sampleView = sampleViewController.sampleView;
[dataCapture start];
[self start];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
LOG(@"applicationWillTerminate");
[dataCapture stop];
[self stop];
}
- (void)start
{
LOG(@"AppDelegate.start");
[signalDetector reset];
[sampleViewController start];
}
- (void)stop
{
LOG(@"AppDelegate.stop");
[self stopRecording];
[recordingsViewController saveContext];
}
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
LOG(@"applicationDidReceiveMemoryWarning");
}
- (void)dealloc {
[window release];
self.dataCapture = nil;
self.signalDetector = nil;
self.switchDetector = nil;
self.vertexBufferManager = nil;
[super dealloc];
}
#pragma mark -
#pragma mark DBSession Delegate Protocol
- (void)sessionDidReceiveAuthorizationFailure:(DBSession *)session userId:(NSString *)userId;
{
LOG(@"failed to receive authorization");
[[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Dropbox Authorization",
@"Dropbox authorization failure alert title.")
message:NSLocalizedString(@"Failed to access configured Dropbox account.",
@"Dropbox authorization failure alert text.")
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease] show];
}
#pragma mark -
#pragma mark UITabBarControllerDelegate protocol
- (BOOL)tabBarController:(UITabBarController*)sender shouldSelectViewController:(UIViewController*)viewController
{
LOG(@"AppDelegate.tabBarController:shouldSelectViewController: %@", viewController);
UIViewController* current = [sender selectedViewController];
if (current == viewController) {
if (sender.selectedIndex == 0) {
[sampleViewController toggleInfoOverlay];
}
}
else {
if (sender.selectedIndex == 3) {
[settingsViewController dismiss:self];
}
}
return YES;
}
#pragma mark -
#pragma mark Recording Management
- (void)startRecording
{
RecordingInfo* recording = [recordingsViewController startRecording];
dataCapture.sampleRecorder = [SampleRecorder createRecording:recording withFormat:dataCapture.streamFormat];
UITabBarItem* item = [tabBarController.tabBar.items objectAtIndex:0];
item.badgeValue = @"REC";
}
- (void)stopRecording
{
sampleViewController.recordIndicator.on = NO;
dataCapture.sampleRecorder = nil;
[recordingsViewController stopRecording];
UITabBarItem* item = [tabBarController.tabBar.items objectAtIndex:0];
item.badgeValue = nil;
}
- (BOOL)isRecording
{
return dataCapture != nil && dataCapture.sampleRecorder != nil;
}
- (BOOL)isRecordingInto:(RecordingInfo*)recording
{
return [self isRecording] && dataCapture.sampleRecorder.recording == recording;
}
- (void)recordingDeleted:(RecordingInfo*)recording
{
if ([self isRecordingInto:recording]) [self stopRecording];
}
- (void)updateFromSettings
{
NSUserDefaults* settings = [NSUserDefaults standardUserDefaults];
NSString* detectorClassName = [settings stringForKey:kSettingsSignalProcessingActiveDetectorKey];
NSString* currentClassName = NSStringFromClass([signalDetector class]);
if ([detectorClassName isEqualToString:currentClassName] == NO) {
[self makeSignalDetector];
}
dataCapture.invertSignal = [settings boolForKey:kSettingsSignalProcessingInvertSignalKey];
[sampleViewController updateFromSettings];
[detectionsViewController updateFromSettings];
[recordingsViewController updateFromSettings];
[signalDetector updateFromSettings];
[switchDetector updateFromSettings];
}
@end