@@ -121,14 +121,32 @@ public void setup(ReadableMap options) {
121121 this ._settings = options ;
122122
123123 if (isConnectionServiceAvailable ()) {
124- this .registerPhoneAccount (this .getAppContext ());
125- voiceBroadcastReceiver = new VoiceBroadcastReceiver ();
126- registerReceiver ();
127- VoiceConnectionService .setPhoneAccountHandle (handle );
124+ this .registerPhoneAccount ();
125+ this .registerEvents ();
128126 VoiceConnectionService .setAvailable (true );
129127 }
130128 }
131129
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+
132150 @ ReactMethod
133151 public void displayIncomingCall (String uuid , String number , String callerName ) {
134152 if (!isConnectionServiceAvailable () || !hasPhoneAccount ()) {
@@ -346,6 +364,10 @@ public void updateDisplay(String uuid, String displayName, String uri) {
346364
347365 @ ReactMethod
348366 public void hasPhoneAccount (Promise promise ) {
367+ if (telecomManager == null ) {
368+ this .initializeTelecomManager ();
369+ }
370+
349371 promise .resolve (hasPhoneAccount ());
350372 }
351373
@@ -439,15 +461,22 @@ public void backToForeground() {
439461 }
440462 }
441463
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+
442473 private void registerPhoneAccount (Context appContext ) {
443474 if (!isConnectionServiceAvailable ()) {
444475 return ;
445476 }
446477
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 ());
451480
452481 PhoneAccount .Builder builder = new PhoneAccount .Builder (handle , appName )
453482 .setCapabilities (PhoneAccount .CAPABILITY_CALL_PROVIDER );
@@ -461,7 +490,6 @@ private void registerPhoneAccount(Context appContext) {
461490 PhoneAccount account = builder .build ();
462491
463492 telephonyManager = (TelephonyManager ) this .getAppContext ().getSystemService (Context .TELEPHONY_SERVICE );
464- telecomManager = (TelecomManager ) this .getAppContext ().getSystemService (Context .TELECOM_SERVICE );
465493
466494 telecomManager .registerPhoneAccount (account );
467495 }
@@ -492,7 +520,8 @@ private Boolean hasPermissions() {
492520 }
493521
494522 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 ();
496525 }
497526
498527 private void registerReceiver () {
0 commit comments