1 |
efrain |
1 |
/*
|
|
|
2 |
* Copyright 2019 Google
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORLifecycle.h"
|
|
|
18 |
|
|
|
19 |
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORConsoleLogger.h"
|
|
|
20 |
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREvent.h"
|
|
|
21 |
|
|
|
22 |
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORRegistrar_Private.h"
|
|
|
23 |
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORTransformer_Private.h"
|
|
|
24 |
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORUploadCoordinator.h"
|
|
|
25 |
|
|
|
26 |
@implementation GDTCORLifecycle
|
|
|
27 |
|
|
|
28 |
+ (void)load {
|
|
|
29 |
[self sharedInstance];
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
/** Creates/returns the singleton instance of this class.
|
|
|
33 |
*
|
|
|
34 |
* @return The singleton instance of this class.
|
|
|
35 |
*/
|
|
|
36 |
+ (instancetype)sharedInstance {
|
|
|
37 |
static GDTCORLifecycle *sharedInstance;
|
|
|
38 |
static dispatch_once_t onceToken;
|
|
|
39 |
dispatch_once(&onceToken, ^{
|
|
|
40 |
sharedInstance = [[GDTCORLifecycle alloc] init];
|
|
|
41 |
});
|
|
|
42 |
return sharedInstance;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
- (instancetype)init {
|
|
|
46 |
self = [super init];
|
|
|
47 |
if (self) {
|
|
|
48 |
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
|
|
|
49 |
[notificationCenter addObserver:self
|
|
|
50 |
selector:@selector(applicationDidEnterBackground:)
|
|
|
51 |
name:kGDTCORApplicationDidEnterBackgroundNotification
|
|
|
52 |
object:nil];
|
|
|
53 |
[notificationCenter addObserver:self
|
|
|
54 |
selector:@selector(applicationWillEnterForeground:)
|
|
|
55 |
name:kGDTCORApplicationWillEnterForegroundNotification
|
|
|
56 |
object:nil];
|
|
|
57 |
|
|
|
58 |
NSString *name = kGDTCORApplicationWillTerminateNotification;
|
|
|
59 |
[notificationCenter addObserver:self
|
|
|
60 |
selector:@selector(applicationWillTerminate:)
|
|
|
61 |
name:name
|
|
|
62 |
object:nil];
|
|
|
63 |
}
|
|
|
64 |
return self;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
- (void)dealloc {
|
|
|
68 |
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
- (void)applicationDidEnterBackground:(NSNotification *)notification {
|
|
|
72 |
GDTCORApplication *application = [GDTCORApplication sharedApplication];
|
|
|
73 |
if ([[GDTCORTransformer sharedInstance] respondsToSelector:@selector(appWillBackground:)]) {
|
|
|
74 |
GDTCORLogDebug(@"%@", @"Signaling GDTCORTransformer that the app is backgrounding.");
|
|
|
75 |
[[GDTCORTransformer sharedInstance] appWillBackground:application];
|
|
|
76 |
}
|
|
|
77 |
if ([[GDTCORUploadCoordinator sharedInstance] respondsToSelector:@selector(appWillBackground:)]) {
|
|
|
78 |
GDTCORLogDebug(@"%@", @"Signaling GDTCORUploadCoordinator that the app is backgrounding.");
|
|
|
79 |
[[GDTCORUploadCoordinator sharedInstance] appWillBackground:application];
|
|
|
80 |
}
|
|
|
81 |
if ([[GDTCORRegistrar sharedInstance] respondsToSelector:@selector(appWillBackground:)]) {
|
|
|
82 |
GDTCORLogDebug(@"%@", @"Signaling GDTCORRegistrar that the app is backgrounding.");
|
|
|
83 |
[[GDTCORRegistrar sharedInstance] appWillBackground:application];
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
- (void)applicationWillEnterForeground:(NSNotification *)notification {
|
|
|
88 |
GDTCORApplication *application = [GDTCORApplication sharedApplication];
|
|
|
89 |
if ([[GDTCORTransformer sharedInstance] respondsToSelector:@selector(appWillForeground:)]) {
|
|
|
90 |
GDTCORLogDebug(@"%@", @"Signaling GDTCORTransformer that the app is foregrounding.");
|
|
|
91 |
[[GDTCORTransformer sharedInstance] appWillForeground:application];
|
|
|
92 |
}
|
|
|
93 |
if ([[GDTCORUploadCoordinator sharedInstance] respondsToSelector:@selector(appWillForeground:)]) {
|
|
|
94 |
GDTCORLogDebug(@"%@", @"Signaling GDTCORUploadCoordinator that the app is foregrounding.");
|
|
|
95 |
[[GDTCORUploadCoordinator sharedInstance] appWillForeground:application];
|
|
|
96 |
}
|
|
|
97 |
if ([[GDTCORRegistrar sharedInstance] respondsToSelector:@selector(appWillForeground:)]) {
|
|
|
98 |
GDTCORLogDebug(@"%@", @"Signaling GDTCORRegistrar that the app is foregrounding.");
|
|
|
99 |
[[GDTCORRegistrar sharedInstance] appWillForeground:application];
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
- (void)applicationWillTerminate:(NSNotification *)notification {
|
|
|
104 |
GDTCORApplication *application = [GDTCORApplication sharedApplication];
|
|
|
105 |
if ([[GDTCORTransformer sharedInstance] respondsToSelector:@selector(appWillTerminate:)]) {
|
|
|
106 |
GDTCORLogDebug(@"%@", @"Signaling GDTCORTransformer that the app is terminating.");
|
|
|
107 |
[[GDTCORTransformer sharedInstance] appWillTerminate:application];
|
|
|
108 |
}
|
|
|
109 |
if ([[GDTCORUploadCoordinator sharedInstance] respondsToSelector:@selector(appWillTerminate:)]) {
|
|
|
110 |
GDTCORLogDebug(@"%@", @"Signaling GDTCORUploadCoordinator that the app is terminating.");
|
|
|
111 |
[[GDTCORUploadCoordinator sharedInstance] appWillTerminate:application];
|
|
|
112 |
}
|
|
|
113 |
if ([[GDTCORRegistrar sharedInstance] respondsToSelector:@selector(appWillTerminate:)]) {
|
|
|
114 |
GDTCORLogDebug(@"%@", @"Signaling GDTCORRegistrar that the app is terminating.");
|
|
|
115 |
[[GDTCORRegistrar sharedInstance] appWillTerminate:application];
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
@end
|