FPRTraceBackgroundActivityTracker.m 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. @end
  21. @implementation FPRTraceBackgroundActivityTracker
  22. - (instancetype)init {
  23. self = [super init];
  24. if (self) {
  25. if ([FPRAppActivityTracker sharedInstance].applicationState == FPRApplicationStateBackground) {
  26. _traceBackgroundState = FPRTraceStateBackgroundOnly;
  27. } else {
  28. _traceBackgroundState = FPRTraceStateForegroundOnly;
  29. }
  30. dispatch_async(dispatch_get_main_queue(), ^{
  31. [[NSNotificationCenter defaultCenter] addObserver:self
  32. selector:@selector(applicationDidBecomeActive:)
  33. name:UIApplicationDidBecomeActiveNotification
  34. object:[UIApplication sharedApplication]];
  35. [[NSNotificationCenter defaultCenter] addObserver:self
  36. selector:@selector(applicationDidEnterBackground:)
  37. name:UIApplicationDidEnterBackgroundNotification
  38. object:[UIApplication sharedApplication]];
  39. });
  40. }
  41. return self;
  42. }
  43. - (void)dealloc {
  44. // Remove all the notification observers registered.
  45. [[NSNotificationCenter defaultCenter] removeObserver:self];
  46. }
  47. #pragma mark - UIApplicationDelegate events
  48. /**
  49. * This gets called whenever the app becomes active.
  50. *
  51. * @param notification Notification received during app launch.
  52. */
  53. - (void)applicationDidBecomeActive:(NSNotification *)notification {
  54. if (_traceBackgroundState == FPRTraceStateBackgroundOnly) {
  55. _traceBackgroundState = FPRTraceStateBackgroundAndForeground;
  56. }
  57. }
  58. /**
  59. * This gets called whenever the app enters background.
  60. *
  61. * @param notification Notification received when the app enters background.
  62. */
  63. - (void)applicationDidEnterBackground:(NSNotification *)notification {
  64. if (_traceBackgroundState == FPRTraceStateForegroundOnly) {
  65. _traceBackgroundState = FPRTraceStateBackgroundAndForeground;
  66. }
  67. }
  68. @end