FPRTraceBackgroundActivityTracker.m 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FirebasePerformance/Sources/AppActivity/FPRTraceBackgroundActivityTracker.h"
  15. #import <Foundation/Foundation.h>
  16. #import <UIKit/UIKit.h>
  17. #import "FirebasePerformance/Sources/AppActivity/FPRAppActivityTracker.h"
  18. @interface FPRTraceBackgroundActivityTracker ()
  19. @property(nonatomic, readwrite) FPRTraceState traceBackgroundState;
  20. - (void)registerNotificationObservers;
  21. @end
  22. @implementation FPRTraceBackgroundActivityTracker
  23. - (instancetype)init {
  24. self = [super init];
  25. if (self) {
  26. if ([FPRAppActivityTracker sharedInstance].applicationState == FPRApplicationStateBackground) {
  27. _traceBackgroundState = FPRTraceStateBackgroundOnly;
  28. } else {
  29. _traceBackgroundState = FPRTraceStateForegroundOnly;
  30. }
  31. __weak typeof(self) weakSelf = self;
  32. dispatch_async(dispatch_get_main_queue(), ^{
  33. __strong typeof(weakSelf) strongSelf = weakSelf;
  34. if (strongSelf) {
  35. [strongSelf registerNotificationObservers];
  36. }
  37. });
  38. }
  39. return self;
  40. }
  41. - (void)registerNotificationObservers {
  42. [[NSNotificationCenter defaultCenter] addObserver:self
  43. selector:@selector(applicationDidBecomeActive:)
  44. name:UIApplicationDidBecomeActiveNotification
  45. object:[UIApplication sharedApplication]];
  46. [[NSNotificationCenter defaultCenter] addObserver:self
  47. selector:@selector(applicationDidEnterBackground:)
  48. name:UIApplicationDidEnterBackgroundNotification
  49. object:[UIApplication sharedApplication]];
  50. }
  51. - (void)dealloc {
  52. // Remove all the notification observers registered.
  53. [[NSNotificationCenter defaultCenter] removeObserver:self];
  54. }
  55. #pragma mark - UIApplicationDelegate events
  56. /**
  57. * This gets called whenever the app becomes active.
  58. *
  59. * @param notification Notification received during app launch.
  60. */
  61. - (void)applicationDidBecomeActive:(NSNotification *)notification {
  62. if (_traceBackgroundState == FPRTraceStateBackgroundOnly) {
  63. _traceBackgroundState = FPRTraceStateBackgroundAndForeground;
  64. }
  65. }
  66. /**
  67. * This gets called whenever the app enters background.
  68. *
  69. * @param notification Notification received when the app enters background.
  70. */
  71. - (void)applicationDidEnterBackground:(NSNotification *)notification {
  72. if (_traceBackgroundState == FPRTraceStateForegroundOnly) {
  73. _traceBackgroundState = FPRTraceStateBackgroundAndForeground;
  74. }
  75. }
  76. @end