GDTCORLifecycle.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #import "GDTCORLibrary/Public/GDTCORLifecycle.h"
  17. #import <GoogleDataTransport/GDTCORConsoleLogger.h>
  18. #import <GoogleDataTransport/GDTCOREvent.h>
  19. #import "GDTCORLibrary/Private/GDTCORRegistrar_Private.h"
  20. #import "GDTCORLibrary/Private/GDTCORStorage_Private.h"
  21. #import "GDTCORLibrary/Private/GDTCORTransformer_Private.h"
  22. #import "GDTCORLibrary/Private/GDTCORUploadCoordinator.h"
  23. @implementation GDTCORLifecycle
  24. + (void)load {
  25. [self sharedInstance];
  26. }
  27. /** Creates/returns the singleton instance of this class.
  28. *
  29. * @return The singleton instance of this class.
  30. */
  31. + (instancetype)sharedInstance {
  32. static GDTCORLifecycle *sharedInstance;
  33. static dispatch_once_t onceToken;
  34. dispatch_once(&onceToken, ^{
  35. sharedInstance = [[GDTCORLifecycle alloc] init];
  36. });
  37. return sharedInstance;
  38. }
  39. - (instancetype)init {
  40. self = [super init];
  41. if (self) {
  42. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  43. [notificationCenter addObserver:self
  44. selector:@selector(applicationDidEnterBackground:)
  45. name:kGDTCORApplicationDidEnterBackgroundNotification
  46. object:nil];
  47. [notificationCenter addObserver:self
  48. selector:@selector(applicationWillEnterForeground:)
  49. name:kGDTCORApplicationWillEnterForegroundNotification
  50. object:nil];
  51. NSString *name = kGDTCORApplicationWillTerminateNotification;
  52. [notificationCenter addObserver:self
  53. selector:@selector(applicationWillTerminate:)
  54. name:name
  55. object:nil];
  56. }
  57. return self;
  58. }
  59. - (void)dealloc {
  60. [[NSNotificationCenter defaultCenter] removeObserver:self];
  61. }
  62. - (void)applicationDidEnterBackground:(NSNotification *)notification {
  63. GDTCORApplication *application = [GDTCORApplication sharedApplication];
  64. if ([[GDTCORTransformer sharedInstance] respondsToSelector:@selector(appWillBackground:)]) {
  65. GDTCORLogDebug("%@", @"Signaling GDTCORTransformer that the app is backgrounding.");
  66. [[GDTCORTransformer sharedInstance] appWillBackground:application];
  67. }
  68. if ([[GDTCORStorage sharedInstance] respondsToSelector:@selector(appWillBackground:)]) {
  69. GDTCORLogDebug("%@", @"Signaling GDTCORStorage that the app is backgrounding.");
  70. [[GDTCORStorage sharedInstance] appWillBackground:application];
  71. }
  72. if ([[GDTCORUploadCoordinator sharedInstance] respondsToSelector:@selector(appWillBackground:)]) {
  73. GDTCORLogDebug("%@", @"Signaling GDTCORUploadCoordinator that the app is backgrounding.");
  74. [[GDTCORUploadCoordinator sharedInstance] appWillBackground:application];
  75. }
  76. if ([[GDTCORRegistrar sharedInstance] respondsToSelector:@selector(appWillBackground:)]) {
  77. GDTCORLogDebug("%@", @"Signaling GDTCORRegistrar that the app is backgrounding.");
  78. [[GDTCORRegistrar sharedInstance] appWillBackground:application];
  79. }
  80. }
  81. - (void)applicationWillEnterForeground:(NSNotification *)notification {
  82. GDTCORApplication *application = [GDTCORApplication sharedApplication];
  83. if ([[GDTCORTransformer sharedInstance] respondsToSelector:@selector(appWillForeground:)]) {
  84. GDTCORLogDebug("%@", @"Signaling GDTCORTransformer that the app is foregrounding.");
  85. [[GDTCORTransformer sharedInstance] appWillForeground:application];
  86. }
  87. if ([[GDTCORStorage sharedInstance] respondsToSelector:@selector(appWillForeground:)]) {
  88. GDTCORLogDebug("%@", @"Signaling GDTCORStorage that the app is foregrounding.");
  89. [[GDTCORStorage sharedInstance] appWillForeground:application];
  90. }
  91. if ([[GDTCORUploadCoordinator sharedInstance] respondsToSelector:@selector(appWillForeground:)]) {
  92. GDTCORLogDebug("%@", @"Signaling GDTCORUploadCoordinator that the app is foregrounding.");
  93. [[GDTCORUploadCoordinator sharedInstance] appWillForeground:application];
  94. }
  95. if ([[GDTCORRegistrar sharedInstance] respondsToSelector:@selector(appWillForeground:)]) {
  96. GDTCORLogDebug("%@", @"Signaling GDTCORRegistrar that the app is foregrounding.");
  97. [[GDTCORRegistrar sharedInstance] appWillForeground:application];
  98. }
  99. }
  100. - (void)applicationWillTerminate:(NSNotification *)notification {
  101. GDTCORApplication *application = [GDTCORApplication sharedApplication];
  102. if ([[GDTCORTransformer sharedInstance] respondsToSelector:@selector(appWillTerminate:)]) {
  103. GDTCORLogDebug("%@", @"Signaling GDTCORTransformer that the app is terminating.");
  104. [[GDTCORTransformer sharedInstance] appWillTerminate:application];
  105. }
  106. if ([[GDTCORStorage sharedInstance] respondsToSelector:@selector(appWillTerminate:)]) {
  107. GDTCORLogDebug("%@", @"Signaling GDTCORStorage that the app is terminating.");
  108. [[GDTCORStorage sharedInstance] appWillTerminate:application];
  109. }
  110. if ([[GDTCORUploadCoordinator sharedInstance] respondsToSelector:@selector(appWillTerminate:)]) {
  111. GDTCORLogDebug("%@", @"Signaling GDTCORUploadCoordinator that the app is terminating.");
  112. [[GDTCORUploadCoordinator sharedInstance] appWillTerminate:application];
  113. }
  114. if ([[GDTCORRegistrar sharedInstance] respondsToSelector:@selector(appWillTerminate:)]) {
  115. GDTCORLogDebug("%@", @"Signaling GDTCORRegistrar that the app is terminating.");
  116. [[GDTCORRegistrar sharedInstance] appWillTerminate:application];
  117. }
  118. }
  119. @end