1#import "AppDelegate.h"
2#import "ViewController.h"
3#import "Common/System/System.h"
4#import "Common/System/NativeApp.h"
5#import "Core/System.h"
6#import "Core/Config.h"
7#import "Common/Log.h"
8
9#import <AVFoundation/AVFoundation.h>
10
11@implementation AppDelegate
12
13// This will be called when the user receives and dismisses a phone call
14// or other interruption to the audio session
15// Registered in application:didFinishLaunchingWithOptions:
16// for AVAudioSessionInterruptionNotification
17-(void) handleAudioSessionInterruption:(NSNotification *)notification {
18	NSNumber *interruptionType = notification.userInfo[AVAudioSessionInterruptionTypeKey];
19
20	// Sanity check in case it's somehow not an NSNumber
21	if (![interruptionType respondsToSelector:@selector(unsignedIntegerValue)]) {
22		return;  // Lets not crash
23	}
24
25	switch ([interruptionType unsignedIntegerValue]) {
26		case AVAudioSessionInterruptionTypeBegan:
27			INFO_LOG(SYSTEM, "ios audio session interruption beginning");
28			if (g_Config.bEnableSound) {
29				Audio_Shutdown();
30			}
31			break;
32
33		case AVAudioSessionInterruptionTypeEnded:
34			INFO_LOG(SYSTEM, "ios audio session interruption ending");
35			if (g_Config.bEnableSound) {
36				/*
37				 * Only try to reinit audio if in the foreground, otherwise
38				 * it may fail. Instead, trust that applicationDidBecomeActive
39				 * will do it later.
40				 */
41				if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
42					Audio_Init();
43				}
44			}
45			break;
46
47		default:
48			break;
49	};
50}
51
52// This will be called when the iOS's shared media process was reset
53// Registered in application:didFinishLaunchingWithOptions:
54// for AVAudioSessionMediaServicesWereResetNotification
55-(void) handleMediaServicesWereReset:(NSNotification *)notification {
56	INFO_LOG(SYSTEM, "ios media services were reset - reinitializing audio");
57
58	/*
59	 When media services were reset, Apple recommends:
60	 1) Dispose of orphaned audio objects (such as players, recorders,
61	    converters, or audio queues) and create new ones
62	 2) Reset any internal audio states being tracked, including all
63	    properties of AVAudioSession
64	 3) When appropriate, reactivate the AVAudioSession instance using the
65	    setActive:error: method
66	 We accomplish this by shutting down and reinitializing audio
67	 */
68
69	if (g_Config.bEnableSound) {
70		Audio_Shutdown();
71		Audio_Init();
72	}
73}
74
75-(BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
76
77	int argc = 1;
78	char *argv[3] = { 0 };
79	NSURL* nsUrl = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
80
81	if (nsUrl != nullptr && nsUrl.isFileURL) {
82		NSString *nsString = nsUrl.path;
83		const char *string = nsString.UTF8String;
84		argv[argc++] = (char*)string;
85	}
86
87	NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
88	NSString *bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/assets/"];
89	NativeInit(argc, (const char**)argv, documentsPath.UTF8String, bundlePath.UTF8String, NULL);
90
91
92	self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
93	self.viewController = [[ViewController alloc] init];
94
95	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAudioSessionInterruption:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];
96
97	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMediaServicesWereReset:) name:AVAudioSessionMediaServicesWereResetNotification object:nil];
98
99	self.window.rootViewController = self.viewController;
100	[self.window makeKeyAndVisible];
101
102	return YES;
103}
104
105-(void) dealloc {
106	[[NSNotificationCenter defaultCenter] removeObserver:self];
107}
108
109-(void) applicationWillResignActive:(UIApplication *)application {
110	if (g_Config.bEnableSound) {
111		Audio_Shutdown();
112	}
113
114	NativeMessageReceived("lost_focus", "");
115}
116
117-(void) applicationDidBecomeActive:(UIApplication *)application {
118	if (g_Config.bEnableSound) {
119		Audio_Init();
120	}
121
122	NativeMessageReceived("got_focus", "");
123}
124
125- (void)applicationWillTerminate:(UIApplication *)application {
126	exit(0);
127}
128
129@end
130