@@ -121,12 +121,13 @@ public class RNCallKeepModule extends ReactContextBaseJavaModule {
121121 public static PhoneAccountHandle handle ;
122122 private boolean isReceiverRegistered = false ;
123123 private VoiceBroadcastReceiver voiceBroadcastReceiver ;
124- private WritableMap _settings ;
124+ private static WritableMap _settings ;
125125 private WritableNativeArray delayedEvents ;
126126 private boolean hasListeners = false ;
127127
128128 public static RNCallKeepModule getInstance (ReactApplicationContext reactContext , boolean realContext ) {
129129 if (instance == null ) {
130+ Log .d (TAG , "[RNCallKeepModule] getInstance : " + (reactContext == null ? "null" : "ok" ));
130131 instance = new RNCallKeepModule (reactContext );
131132 }
132133 if (realContext ) {
@@ -135,8 +136,12 @@ public static RNCallKeepModule getInstance(ReactApplicationContext reactContext,
135136 return instance ;
136137 }
137138
138- public static WritableMap getInstanceSettings () {
139- return getInstance (null , false ).getSettings ();
139+ public static WritableMap getSettings () {
140+ if (_settings == null ) {
141+ fetchStoredSettings ();
142+ }
143+
144+ return _settings ;
140145 }
141146
142147 private RNCallKeepModule (ReactApplicationContext reactContext ) {
@@ -198,6 +203,10 @@ public void startObserving() {
198203
199204 public void initializeTelecomManager () {
200205 Context context = this .getAppContext ();
206+ if (context == null ) {
207+ Log .w (TAG , "[RNCallKeepModule][initializeTelecomManager] no react context found." );
208+ return ;
209+ }
201210 ComponentName cName = new ComponentName (context , VoiceConnectionService .class );
202211 String appName = this .getApplicationName (context );
203212
@@ -213,15 +222,7 @@ public void setSettings(ReadableMap options) {
213222 Log .d (TAG , "[RNCallKeepModule] setSettings: " + options );
214223 storeSettings (options );
215224
216- this ._settings = getSettings ();
217- }
218-
219- public WritableMap getSettings () {
220- if (_settings == null ) {
221- fetchStoredSettings ();
222- }
223-
224- return _settings ;
225+ _settings = getSettings ();
225226 }
226227
227228 @ ReactMethod
@@ -275,8 +276,13 @@ public void registerPhoneAccount(ReadableMap options) {
275276 }
276277
277278 Log .d (TAG , "[RNCallKeepModule] registerPhoneAccount" );
279+ Context context = this .getAppContext ();
280+ if (context == null ) {
281+ Log .w (TAG , "[RNCallKeepModule][registerPhoneAccount] no react context found." );
282+ return ;
283+ }
278284
279- this .registerPhoneAccount (this . getAppContext () );
285+ this .registerPhoneAccount (context );
280286 }
281287
282288 @ ReactMethod
@@ -666,6 +672,11 @@ public void setAudioRoute(String uuid, String audioRoute, Promise promise){
666672 public void getAudioRoutes (Promise promise ){
667673 try {
668674 Context context = this .getAppContext ();
675+ if (context == null ) {
676+ Log .w (TAG , "[RNCallKeepModule][getAudioRoutes] no react context found." );
677+ promise .reject ("No react context found to list audio routes" );
678+ return ;
679+ }
669680 AudioManager audioManager = (AudioManager ) context .getSystemService (context .AUDIO_SERVICE );
670681 WritableArray devices = Arguments .createArray ();
671682 ArrayList <String > typeChecker = new ArrayList <>();
@@ -804,7 +815,13 @@ public void openPhoneAccounts() {
804815 intent .setComponent (new ComponentName ("com.android.server.telecom" ,
805816 "com.android.server.telecom.settings.EnableAccountPreferenceActivity" ));
806817
807- this .getAppContext ().startActivity (intent );
818+ Context context = this .getAppContext ();
819+ if (context == null ) {
820+ Log .w (TAG , "[RNCallKeepModule][openPhoneAccounts] no react context found." );
821+ return ;
822+ }
823+
824+ context .startActivity (intent );
808825 return ;
809826 }
810827
@@ -821,7 +838,12 @@ public void openPhoneAccountSettings() {
821838
822839 Intent intent = new Intent (TelecomManager .ACTION_CHANGE_PHONE_ACCOUNTS );
823840 intent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK | Intent .FLAG_ACTIVITY_MULTIPLE_TASK );
824- this .getAppContext ().startActivity (intent );
841+ Context context = this .getAppContext ();
842+ if (context == null ) {
843+ Log .w (TAG , "[RNCallKeepModule][openPhoneAccountSettings] no react context found." );
844+ return ;
845+ }
846+ context .startActivity (intent );
825847 }
826848
827849 public static Boolean isConnectionServiceAvailable () {
@@ -842,6 +864,10 @@ public void checkPhoneAccountEnabled(Promise promise) {
842864 @ ReactMethod
843865 public void backToForeground () {
844866 Context context = getAppContext ();
867+ if (context == null ) {
868+ Log .w (TAG , "[RNCallKeepModule][backToForeground] no react context found." );
869+ return ;
870+ }
845871 String packageName = context .getApplicationContext ().getPackageName ();
846872 Intent focusIntent = context .getPackageManager ().getLaunchIntentForPackage (packageName ).cloneFilter ();
847873 Activity activity = getCurrentActivity ();
@@ -881,7 +907,12 @@ private void registerPhoneAccount(Context appContext) {
881907 }
882908
883909 this .initializeTelecomManager ();
884- String appName = this .getApplicationName (this .getAppContext ());
910+ Context context = this .getAppContext ();
911+ if (context == null ) {
912+ Log .w (TAG , "[RNCallKeepModule][registerPhoneAccount] no react context found." );
913+ return ;
914+ }
915+ String appName = this .getApplicationName (context );
885916
886917 PhoneAccount .Builder builder = new PhoneAccount .Builder (handle , appName );
887918 if (isSelfManaged ()) {
@@ -899,7 +930,7 @@ private void registerPhoneAccount(Context appContext) {
899930
900931 PhoneAccount account = builder .build ();
901932
902- telephonyManager = (TelephonyManager ) this . getAppContext () .getSystemService (Context .TELEPHONY_SERVICE );
933+ telephonyManager = (TelephonyManager ) context .getSystemService (Context .TELEPHONY_SERVICE );
903934
904935 telecomManager .registerPhoneAccount (account );
905936 }
@@ -971,19 +1002,27 @@ private void registerReceiver() {
9711002 intentFilter .addAction (ACTION_ON_CREATE_CONNECTION_FAILED );
9721003 intentFilter .addAction (ACTION_DID_CHANGE_AUDIO_ROUTE );
9731004
974- LocalBroadcastManager .getInstance (this .reactContext ).registerReceiver (voiceBroadcastReceiver , intentFilter );
975- isReceiverRegistered = true ;
1005+ Log .w (TAG , "[RNCallKeepModule][registerReceiver] reactContext:" + (this .reactContext == null ? "NOPE" : "OK" ));
1006+
1007+
1008+ if (this .reactContext != null ) {
1009+ LocalBroadcastManager .getInstance (this .reactContext ).registerReceiver (voiceBroadcastReceiver , intentFilter );
1010+ isReceiverRegistered = true ;
1011+
1012+ VoiceConnectionService .startObserving ();
1013+ }
9761014 }
9771015 }
9781016
9791017 private Context getAppContext () {
980- return this .reactContext . getApplicationContext ();
1018+ return this .reactContext != null ? this . reactContext . getApplicationContext () : null ;
9811019 }
9821020
9831021 // Store all callkeep settings in JSON
9841022 private void storeSettings (ReadableMap options ) {
9851023 Context context = getInstance (null , false ).getAppContext ();
9861024 if (context == null ) {
1025+ Log .w (TAG , "[RNCallKeepModule][storeSettings] no react context found." );
9871026 return ;
9881027 }
9891028
@@ -996,10 +1035,14 @@ private void storeSettings(ReadableMap options) {
9961035 }
9971036 }
9981037
999- private void fetchStoredSettings () {
1000- Context context = getInstance (null , false ).getAppContext ();
1038+ private static void fetchStoredSettings () {
1039+ if (instance == null ) {
1040+ return ;
1041+ }
1042+ Context context = instance .getAppContext ();
10011043 _settings = new WritableNativeMap ();
10021044 if (context == null ) {
1045+ Log .w (TAG , "[RNCallKeepModule][fetchStoredSettings] no react context found." );
10031046 return ;
10041047 }
10051048
@@ -1021,6 +1064,8 @@ public void onReceive(Context context, Intent intent) {
10211064 WritableMap args = Arguments .createMap ();
10221065 HashMap <String , String > attributeMap = (HashMap <String , String >)intent .getSerializableExtra ("attributeMap" );
10231066
1067+ Log .d (TAG , "[RNCallKeepModule][onReceive] :" + intent .getAction ());
1068+
10241069 switch (intent .getAction ()) {
10251070 case ACTION_END_CALL :
10261071 args .putString ("callUUID" , attributeMap .get (EXTRA_CALL_UUID ));
0 commit comments