33
33
static NSString *const RNCallKeepDidToggleHoldAction = @" RNCallKeepDidToggleHoldAction" ;
34
34
static NSString *const RNCallKeepProviderReset = @" RNCallKeepProviderReset" ;
35
35
static NSString *const RNCallKeepCheckReachability = @" RNCallKeepCheckReachability" ;
36
+ static NSString *const RNCallKeepDidLoadWithEvents = @" RNCallKeepDidLoadWithEvents" ;
36
37
37
38
@implementation RNCallKeep
38
39
{
39
40
NSOperatingSystemVersion _version;
40
41
BOOL _isStartCallActionEventListenerAdded;
42
+ bool _hasListeners;
43
+ NSMutableArray *_delayedEvents;
41
44
}
42
45
43
46
static CXProvider * sharedProvider;
@@ -52,6 +55,7 @@ - (instancetype)init
52
55
#endif
53
56
if (self = [super init ]) {
54
57
_isStartCallActionEventListenerAdded = NO ;
58
+ _delayedEvents = [NSMutableArray array ];
55
59
}
56
60
return self;
57
61
}
@@ -92,10 +96,36 @@ - (void)dealloc
92
96
RNCallKeepPerformPlayDTMFCallAction,
93
97
RNCallKeepDidToggleHoldAction,
94
98
RNCallKeepProviderReset,
95
- RNCallKeepCheckReachability
99
+ RNCallKeepCheckReachability,
100
+ RNCallKeepDidLoadWithEvents
96
101
];
97
102
}
98
103
104
+ - (void )startObserving
105
+ {
106
+ _hasListeners = YES ;
107
+ if ([_delayedEvents count ] > 0 ) {
108
+ [self sendEventWithName: RNCallKeepDidLoadWithEvents body: _delayedEvents];
109
+ }
110
+ }
111
+
112
+ - (void )stopObserving
113
+ {
114
+ _hasListeners = FALSE ;
115
+ }
116
+
117
+ - (void )sendEventWithNameWrapper : (NSString *)name body : (id )body {
118
+ if (_hasListeners) {
119
+ [self sendEventWithName: name body: body];
120
+ } else {
121
+ NSDictionary *dictionary = @{
122
+ @" name" : name,
123
+ @" data" : body
124
+ };
125
+ [_delayedEvents addObject: dictionary];
126
+ }
127
+ }
128
+
99
129
+ (void )initCallKitProvider {
100
130
if (sharedProvider == nil ) {
101
131
NSDictionary *settings = [[NSUserDefaults standardUserDefaults ] dictionaryForKey: @" RNCallKeepSettings" ];
@@ -392,7 +422,7 @@ + (void)reportNewIncomingCall:(NSString *)uuidString
392
422
[RNCallKeep initCallKitProvider ];
393
423
[sharedProvider reportNewIncomingCallWithUUID: uuid update: callUpdate completion: ^(NSError * _Nullable error) {
394
424
RNCallKeep *callKeep = [RNCallKeep allocWithZone: nil ];
395
- [callKeep sendEventWithName : RNCallKeepDidDisplayIncomingCall body: @{
425
+ [callKeep sendEventWithNameWrapper : RNCallKeepDidDisplayIncomingCall body: @{
396
426
@" error" : error && error.localizedDescription ? error.localizedDescription : @" " ,
397
427
@" callUUID" : uuidString,
398
428
@" handle" : handle,
@@ -588,7 +618,7 @@ + (BOOL)application:(UIApplication *)application
588
618
};
589
619
590
620
RNCallKeep *callKeep = [RNCallKeep allocWithZone: nil ];
591
- [callKeep handleStartCallNotification: userInfo];
621
+ [callKeep sendEventWithNameWrapper: RNCallKeepDidReceiveStartCallAction body: userInfo];
592
622
return YES ;
593
623
}
594
624
return NO ;
@@ -599,24 +629,6 @@ + (BOOL)requiresMainQueueSetup
599
629
return YES ;
600
630
}
601
631
602
- - (void )handleStartCallNotification:(NSDictionary *)userInfo
603
- {
604
- #ifdef DEBUG
605
- NSLog (@" [RNCallKeep][handleStartCallNotification] userInfo = %@ " , userInfo);
606
- #endif
607
- int delayInSeconds;
608
- if (!_isStartCallActionEventListenerAdded) {
609
- // Workaround for when app is just launched and JS side hasn't registered to the event properly
610
- delayInSeconds = OUTGOING_CALL_WAKEUP_DELAY;
611
- } else {
612
- delayInSeconds = 0 ;
613
- }
614
- dispatch_time_t popTime = dispatch_time (DISPATCH_TIME_NOW, (int64_t )(delayInSeconds * NSEC_PER_SEC));
615
- dispatch_after (popTime, dispatch_get_main_queue (), ^{
616
- [self sendEventWithName: RNCallKeepDidReceiveStartCallAction body: userInfo];
617
- });
618
- }
619
-
620
632
#pragma mark - CXProviderDelegate
621
633
622
634
- (void )providerDidReset:(CXProvider *)provider{
@@ -625,7 +637,7 @@ - (void)providerDidReset:(CXProvider *)provider{
625
637
#endif
626
638
// this means something big changed, so tell the JS. The JS should
627
639
// probably respond by hanging up all calls.
628
- [self sendEventWithName : RNCallKeepProviderReset body: nil ];
640
+ [self sendEventWithNameWrapper : RNCallKeepProviderReset body: nil ];
629
641
}
630
642
631
643
// Starting outgoing call
@@ -637,7 +649,7 @@ - (void)provider:(CXProvider *)provider performStartCallAction:(CXStartCallActio
637
649
// do this first, audio sessions are flakey
638
650
[self configureAudioSession ];
639
651
// tell the JS to actually make the call
640
- [self sendEventWithName : RNCallKeepDidReceiveStartCallAction body: @{ @" callUUID" : [action.callUUID.UUIDString lowercaseString ], @" handle" : action.handle .value }];
652
+ [self sendEventWithNameWrapper : RNCallKeepDidReceiveStartCallAction body: @{ @" callUUID" : [action.callUUID.UUIDString lowercaseString ], @" handle" : action.handle .value }];
641
653
[action fulfill ];
642
654
}
643
655
@@ -662,7 +674,7 @@ - (void)provider:(CXProvider *)provider performAnswerCallAction:(CXAnswerCallAct
662
674
NSLog (@" [RNCallKeep][CXProviderDelegate][provider:performAnswerCallAction]" );
663
675
#endif
664
676
[self configureAudioSession ];
665
- [self sendEventWithName : RNCallKeepPerformAnswerCallAction body: @{ @" callUUID" : [action.callUUID.UUIDString lowercaseString ] }];
677
+ [self sendEventWithNameWrapper : RNCallKeepPerformAnswerCallAction body: @{ @" callUUID" : [action.callUUID.UUIDString lowercaseString ] }];
666
678
[action fulfill ];
667
679
}
668
680
@@ -672,7 +684,7 @@ - (void)provider:(CXProvider *)provider performEndCallAction:(CXEndCallAction *)
672
684
#ifdef DEBUG
673
685
NSLog (@" [RNCallKeep][CXProviderDelegate][provider:performEndCallAction]" );
674
686
#endif
675
- [self sendEventWithName : RNCallKeepPerformEndCallAction body: @{ @" callUUID" : [action.callUUID.UUIDString lowercaseString ] }];
687
+ [self sendEventWithNameWrapper : RNCallKeepPerformEndCallAction body: @{ @" callUUID" : [action.callUUID.UUIDString lowercaseString ] }];
676
688
[action fulfill ];
677
689
}
678
690
@@ -682,15 +694,15 @@ -(void)provider:(CXProvider *)provider performSetHeldCallAction:(CXSetHeldCallAc
682
694
NSLog (@" [RNCallKeep][CXProviderDelegate][provider:performSetHeldCallAction]" );
683
695
#endif
684
696
685
- [self sendEventWithName : RNCallKeepDidToggleHoldAction body: @{ @" hold" : @(action.onHold ), @" callUUID" : [action.callUUID.UUIDString lowercaseString ] }];
697
+ [self sendEventWithNameWrapper : RNCallKeepDidToggleHoldAction body: @{ @" hold" : @(action.onHold ), @" callUUID" : [action.callUUID.UUIDString lowercaseString ] }];
686
698
[action fulfill ];
687
699
}
688
700
689
701
- (void )provider:(CXProvider *)provider performPlayDTMFCallAction:(CXPlayDTMFCallAction *)action {
690
702
#ifdef DEBUG
691
703
NSLog (@" [RNCallKeep][CXProviderDelegate][provider:performPlayDTMFCallAction]" );
692
704
#endif
693
- [self sendEventWithName : RNCallKeepPerformPlayDTMFCallAction body: @{ @" digits" : action.digits , @" callUUID" : [action.callUUID.UUIDString lowercaseString ] }];
705
+ [self sendEventWithNameWrapper : RNCallKeepPerformPlayDTMFCallAction body: @{ @" digits" : action.digits , @" callUUID" : [action.callUUID.UUIDString lowercaseString ] }];
694
706
[action fulfill ];
695
707
}
696
708
@@ -700,7 +712,7 @@ -(void)provider:(CXProvider *)provider performSetMutedCallAction:(CXSetMutedCall
700
712
NSLog (@" [RNCallKeep][CXProviderDelegate][provider:performSetMutedCallAction]" );
701
713
#endif
702
714
703
- [self sendEventWithName : RNCallKeepDidPerformSetMutedCallAction body: @{ @" muted" : @(action.muted ), @" callUUID" : [action.callUUID.UUIDString lowercaseString ] }];
715
+ [self sendEventWithNameWrapper : RNCallKeepDidPerformSetMutedCallAction body: @{ @" muted" : @(action.muted ), @" callUUID" : [action.callUUID.UUIDString lowercaseString ] }];
704
716
[action fulfill ];
705
717
}
706
718
@@ -724,15 +736,15 @@ - (void)provider:(CXProvider *)provider didActivateAudioSession:(AVAudioSession
724
736
[[NSNotificationCenter defaultCenter ] postNotificationName: AVAudioSessionInterruptionNotification object: nil userInfo: userInfo];
725
737
726
738
[self configureAudioSession ];
727
- [self sendEventWithName : RNCallKeepDidActivateAudioSession body: nil ];
739
+ [self sendEventWithNameWrapper : RNCallKeepDidActivateAudioSession body: nil ];
728
740
}
729
741
730
742
- (void )provider:(CXProvider *)provider didDeactivateAudioSession:(AVAudioSession *)audioSession
731
743
{
732
744
#ifdef DEBUG
733
745
NSLog (@" [RNCallKeep][CXProviderDelegate][provider:didDeactivateAudioSession]" );
734
746
#endif
735
- [self sendEventWithName : RNCallKeepDidDeactivateAudioSession body: nil ];
747
+ [self sendEventWithNameWrapper : RNCallKeepDidDeactivateAudioSession body: nil ];
736
748
}
737
749
738
750
@end
0 commit comments