FIRCLSNotificationManager.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2021 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 "Crashlytics/Crashlytics/Controllers/FIRCLSNotificationManager.h"
  15. #import "Crashlytics/Crashlytics/Components/FIRCLSApplication.h"
  16. #import "Crashlytics/Crashlytics/Components/FIRCLSGlobals.h"
  17. #import "Crashlytics/Crashlytics/Components/FIRCLSUserLogging.h"
  18. #if TARGET_OS_IPHONE
  19. #import <UIKit/UIKit.h>
  20. #else
  21. #import <AppKit/AppKit.h>
  22. #endif
  23. @implementation FIRCLSNotificationManager
  24. - (void)registerNotificationListener {
  25. [self captureInitialNotificationStates];
  26. #if TARGET_OS_IOS
  27. [[NSNotificationCenter defaultCenter] addObserver:self
  28. selector:@selector(willBecomeActive:)
  29. name:UIApplicationWillEnterForegroundNotification
  30. object:nil];
  31. [[NSNotificationCenter defaultCenter] addObserver:self
  32. selector:@selector(didBecomeInactive:)
  33. name:UIApplicationDidEnterBackgroundNotification
  34. object:nil];
  35. [[NSNotificationCenter defaultCenter] addObserver:self
  36. selector:@selector(didChangeOrientation:)
  37. name:UIDeviceOrientationDidChangeNotification
  38. object:nil];
  39. #pragma clang diagnostic push
  40. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  41. [[NSNotificationCenter defaultCenter]
  42. addObserver:self
  43. selector:@selector(didChangeUIOrientation:)
  44. name:UIApplicationDidChangeStatusBarOrientationNotification
  45. object:nil];
  46. #pragma clang diagnostic pop
  47. #elif CLS_TARGET_OS_OSX
  48. [[NSNotificationCenter defaultCenter] addObserver:self
  49. selector:@selector(willBecomeActive:)
  50. name:@"NSApplicationWillBecomeActiveNotification"
  51. object:nil];
  52. [[NSNotificationCenter defaultCenter] addObserver:self
  53. selector:@selector(didBecomeInactive:)
  54. name:@"NSApplicationDidResignActiveNotification"
  55. object:nil];
  56. #endif
  57. }
  58. - (void)captureInitialNotificationStates {
  59. #if TARGET_OS_IOS
  60. UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
  61. UIInterfaceOrientation statusBarOrientation =
  62. [FIRCLSApplicationSharedInstance() statusBarOrientation];
  63. #endif
  64. // It's nice to do this async, so we don't hold up the main thread while doing three
  65. // consecutive IOs here.
  66. dispatch_async(FIRCLSGetLoggingQueue(), ^{
  67. FIRCLSUserLoggingWriteInternalKeyValue(FIRCLSInBackgroundKey, @"0");
  68. #if TARGET_OS_IOS
  69. FIRCLSUserLoggingWriteInternalKeyValue(FIRCLSDeviceOrientationKey,
  70. [@(orientation) description]);
  71. FIRCLSUserLoggingWriteInternalKeyValue(FIRCLSUIOrientationKey,
  72. [@(statusBarOrientation) description]);
  73. #endif
  74. });
  75. }
  76. - (void)willBecomeActive:(NSNotification *)notification {
  77. FIRCLSUserLoggingRecordInternalKeyValue(FIRCLSInBackgroundKey, @NO);
  78. }
  79. - (void)didBecomeInactive:(NSNotification *)notification {
  80. FIRCLSUserLoggingRecordInternalKeyValue(FIRCLSInBackgroundKey, @YES);
  81. }
  82. #if TARGET_OS_IOS
  83. - (void)didChangeOrientation:(NSNotification *)notification {
  84. UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
  85. FIRCLSUserLoggingRecordInternalKeyValue(FIRCLSDeviceOrientationKey, @(orientation));
  86. }
  87. - (void)didChangeUIOrientation:(NSNotification *)notification {
  88. UIInterfaceOrientation statusBarOrientation =
  89. [FIRCLSApplicationSharedInstance() statusBarOrientation];
  90. FIRCLSUserLoggingRecordInternalKeyValue(FIRCLSUIOrientationKey, @(statusBarOrientation));
  91. }
  92. #endif
  93. @end