@@ -121,14 +121,32 @@ public void setup(ReadableMap options) {
121
121
this ._settings = options ;
122
122
123
123
if (isConnectionServiceAvailable ()) {
124
- this .registerPhoneAccount (this .getAppContext ());
125
- voiceBroadcastReceiver = new VoiceBroadcastReceiver ();
126
- registerReceiver ();
127
- VoiceConnectionService .setPhoneAccountHandle (handle );
124
+ this .registerPhoneAccount ();
125
+ this .registerEvents ();
128
126
VoiceConnectionService .setAvailable (true );
129
127
}
130
128
}
131
129
130
+ @ ReactMethod
131
+ public void registerPhoneAccount () {
132
+ if (!isConnectionServiceAvailable ()) {
133
+ return ;
134
+ }
135
+
136
+ this .registerPhoneAccount (this .getAppContext ());
137
+ }
138
+
139
+ @ ReactMethod
140
+ public void registerEvents () {
141
+ if (!isConnectionServiceAvailable ()) {
142
+ return ;
143
+ }
144
+
145
+ voiceBroadcastReceiver = new VoiceBroadcastReceiver ();
146
+ registerReceiver ();
147
+ VoiceConnectionService .setPhoneAccountHandle (handle );
148
+ }
149
+
132
150
@ ReactMethod
133
151
public void displayIncomingCall (String uuid , String number , String callerName ) {
134
152
if (!isConnectionServiceAvailable () || !hasPhoneAccount ()) {
@@ -346,6 +364,10 @@ public void updateDisplay(String uuid, String displayName, String uri) {
346
364
347
365
@ ReactMethod
348
366
public void hasPhoneAccount (Promise promise ) {
367
+ if (telecomManager == null ) {
368
+ this .initializeTelecomManager ();
369
+ }
370
+
349
371
promise .resolve (hasPhoneAccount ());
350
372
}
351
373
@@ -439,15 +461,22 @@ public void backToForeground() {
439
461
}
440
462
}
441
463
464
+ private void initializeTelecomManager () {
465
+ Context context = this .getAppContext ();
466
+ ComponentName cName = new ComponentName (context , VoiceConnectionService .class );
467
+ String appName = this .getApplicationName (context );
468
+
469
+ handle = new PhoneAccountHandle (cName , appName );
470
+ telecomManager = (TelecomManager ) context .getSystemService (Context .TELECOM_SERVICE );
471
+ }
472
+
442
473
private void registerPhoneAccount (Context appContext ) {
443
474
if (!isConnectionServiceAvailable ()) {
444
475
return ;
445
476
}
446
477
447
- ComponentName cName = new ComponentName (this .getAppContext (), VoiceConnectionService .class );
448
- String appName = this .getApplicationName (appContext );
449
-
450
- handle = new PhoneAccountHandle (cName , appName );
478
+ this .initializeTelecomManager ();
479
+ String appName = this .getApplicationName (this .getAppContext ());
451
480
452
481
PhoneAccount .Builder builder = new PhoneAccount .Builder (handle , appName )
453
482
.setCapabilities (PhoneAccount .CAPABILITY_CALL_PROVIDER );
@@ -461,7 +490,6 @@ private void registerPhoneAccount(Context appContext) {
461
490
PhoneAccount account = builder .build ();
462
491
463
492
telephonyManager = (TelephonyManager ) this .getAppContext ().getSystemService (Context .TELEPHONY_SERVICE );
464
- telecomManager = (TelecomManager ) this .getAppContext ().getSystemService (Context .TELECOM_SERVICE );
465
493
466
494
telecomManager .registerPhoneAccount (account );
467
495
}
@@ -492,7 +520,8 @@ private Boolean hasPermissions() {
492
520
}
493
521
494
522
private static boolean hasPhoneAccount () {
495
- return isConnectionServiceAvailable () && telecomManager != null && telecomManager .getPhoneAccount (handle ).isEnabled ();
523
+ return isConnectionServiceAvailable () && telecomManager != null
524
+ && telecomManager .getPhoneAccount (handle ) != null && telecomManager .getPhoneAccount (handle ).isEnabled ();
496
525
}
497
526
498
527
private void registerReceiver () {
0 commit comments