FPRTraceBackgroundActivityTracker.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. [[NSNotificationCenter defaultCenter] addObserver:self
  31. selector:@selector(applicationDidBecomeActive:)
  32. name:UIApplicationDidBecomeActiveNotification
  33. object:[UIApplication sharedApplication]];
  34. [[NSNotificationCenter defaultCenter] addObserver:self
  35. selector:@selector(applicationDidEnterBackground:)
  36. name:UIApplicationDidEnterBackgroundNotification
  37. object:[UIApplication sharedApplication]];
  38. }
  39. return self;
  40. }
  41. - (void)dealloc {
  42. // Remove all the notification observers registered.
  43. [[NSNotificationCenter defaultCenter] removeObserver:self];
  44. }
  45. #pragma mark - UIApplicationDelegate events
  46. /**
  47. * This gets called whenever the app becomes active.
  48. *
  49. * @param notification Notification received during app launch.
  50. */
  51. - (void)applicationDidBecomeActive:(NSNotification *)notification {
  52. if (_traceBackgroundState == FPRTraceStateBackgroundOnly) {
  53. _traceBackgroundState = FPRTraceStateBackgroundAndForeground;
  54. }
  55. }
  56. /**
  57. * This gets called whenever the app enters background.
  58. *
  59. * @param notification Notification received when the app enters background.
  60. */
  61. - (void)applicationDidEnterBackground:(NSNotification *)notification {
  62. if (_traceBackgroundState == FPRTraceStateForegroundOnly) {
  63. _traceBackgroundState = FPRTraceStateBackgroundAndForeground;
  64. }
  65. }
  66. @end