2014 snapchat source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.3 KiB

  1. //
  2. // SCCaptureFaceDetectorTrigger.m
  3. // Snapchat
  4. //
  5. // Created by Jiyang Zhu on 3/22/18.
  6. // Copyright © 2018 Snapchat, Inc. All rights reserved.
  7. //
  8. #import "SCCaptureFaceDetectorTrigger.h"
  9. #import "SCCaptureFaceDetector.h"
  10. #import <SCFoundation/SCAppLifecycle.h>
  11. #import <SCFoundation/SCIdleMonitor.h>
  12. #import <SCFoundation/SCQueuePerformer.h>
  13. #import <SCFoundation/SCTaskManager.h>
  14. #import <SCFoundation/SCTraceODPCompatible.h>
  15. @interface SCCaptureFaceDetectorTrigger () {
  16. id<SCCaptureFaceDetector> __weak _detector;
  17. }
  18. @end
  19. @implementation SCCaptureFaceDetectorTrigger
  20. - (instancetype)initWithDetector:(id<SCCaptureFaceDetector>)detector
  21. {
  22. self = [super init];
  23. if (self) {
  24. _detector = detector;
  25. [[NSNotificationCenter defaultCenter] addObserver:self
  26. selector:@selector(_applicationDidBecomeActive)
  27. name:kSCPostponedUIApplicationDidBecomeActiveNotification
  28. object:nil];
  29. [[NSNotificationCenter defaultCenter] addObserver:self
  30. selector:@selector(_applicationWillResignActive)
  31. name:UIApplicationWillResignActiveNotification
  32. object:nil];
  33. }
  34. return self;
  35. }
  36. #pragma mark - Internal Methods
  37. - (void)_applicationWillResignActive
  38. {
  39. SCTraceODPCompatibleStart(2);
  40. [self _stopDetection];
  41. }
  42. - (void)_applicationDidBecomeActive
  43. {
  44. SCTraceODPCompatibleStart(2);
  45. [self _waitUntilAppStartCompleteToStartDetection];
  46. }
  47. - (void)_waitUntilAppStartCompleteToStartDetection
  48. {
  49. SCTraceODPCompatibleStart(2);
  50. @weakify(self);
  51. if (SCExperimentWithWaitUntilIdleReplacement()) {
  52. [[SCTaskManager sharedManager] addTaskToRunWhenAppIdle:"SCCaptureFaceDetectorTrigger.startDetection"
  53. performer:[_detector detectionPerformer]
  54. block:^{
  55. @strongify(self);
  56. SC_GUARD_ELSE_RETURN(self);
  57. [self _startDetection];
  58. }];
  59. } else {
  60. [[SCIdleMonitor sharedInstance] waitUntilIdleForTag:"SCCaptureFaceDetectorTrigger.startDetection"
  61. callbackQueue:[_detector detectionPerformer].queue
  62. block:^{
  63. @strongify(self);
  64. SC_GUARD_ELSE_RETURN(self);
  65. [self _startDetection];
  66. }];
  67. }
  68. }
  69. - (void)_startDetection
  70. {
  71. SCTraceODPCompatibleStart(2);
  72. [[_detector detectionPerformer] performImmediatelyIfCurrentPerformer:^{
  73. [_detector startDetection];
  74. }];
  75. }
  76. - (void)_stopDetection
  77. {
  78. SCTraceODPCompatibleStart(2);
  79. [[_detector detectionPerformer] performImmediatelyIfCurrentPerformer:^{
  80. [_detector stopDetection];
  81. }];
  82. }
  83. @end