From 30fdd06f0c391c3156e7e355d3c3e6200da569ff Mon Sep 17 00:00:00 2001 From: Marcoo09 Date: Sun, 22 May 2022 14:56:46 -0300 Subject: [PATCH] Update CallDetectionManager.m add background task --- .../RCTCallDetection/CallDetectionManager.m | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/iOS/RCTCallDetection/RCTCallDetection/CallDetectionManager.m b/iOS/RCTCallDetection/RCTCallDetection/CallDetectionManager.m index 7deb00ec..fe9130f7 100644 --- a/iOS/RCTCallDetection/RCTCallDetection/CallDetectionManager.m +++ b/iOS/RCTCallDetection/RCTCallDetection/CallDetectionManager.m @@ -15,8 +15,10 @@ @interface CallDetectionManager() @property(strong, nonatomic) RCTResponseSenderBlock block; @property(strong, nonatomic) CXCallObserver* callObserver; +@property(nonatomic) long* _backgroundUpdateTask; @end + @implementation CallDetectionManager - (NSArray *)supportedEvents { @@ -38,6 +40,7 @@ + (BOOL)requiresMainQueueSetup { } RCT_EXPORT_METHOD(startListener) { + [self beginBackgroundUpdateTask]; // Setup call tracking self.callObserver = [[CXCallObserver alloc] init]; __typeof(self) weakSelf = self; @@ -47,18 +50,35 @@ + (BOOL)requiresMainQueueSetup { RCT_EXPORT_METHOD(stopListener) { // Setup call tracking self.callObserver = nil; + [self endBackgroundUpdateTask]; +} + +- (void) beginBackgroundUpdateTask +{ + self._backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ + [self endBackgroundUpdateTask]; + }]; +} + +- (void) endBackgroundUpdateTask +{ + [[UIApplication sharedApplication] endBackgroundTask: self._backgroundUpdateTask]; + self._backgroundUpdateTask = UIBackgroundTaskInvalid; } - (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call { if (call.hasEnded == true) { [self sendEventWithName:@"PhoneCallStateUpdate" body:@"Disconnected"]; - } else if (call.hasConnected == true) { - [self sendEventWithName:@"PhoneCallStateUpdate" body:@"Connected"]; - } else if (call.isOutgoing == true) { + } + if (call.isOutgoing == true && call.hasConnected == false && call.hasEnded == false) { [self sendEventWithName:@"PhoneCallStateUpdate" body:@"Dialing"]; - } else { + } + if (call.isOutgoing == false && call.hasConnected == false) { [self sendEventWithName:@"PhoneCallStateUpdate" body:@"Incoming"]; } + if (call.hasEnded == false && call.hasConnected == true) { + [self sendEventWithName:@"PhoneCallStateUpdate" body:@"Connected"]; + } } @end