@@ -294,7 +294,7 @@ private void startForegroundService() {
294
294
Log .d (TAG , "[VoiceConnectionService] startForegroundService" );
295
295
ReadableMap foregroundSettings = getForegroundSettings (null );
296
296
297
- if (foregroundSettings == null || ! foregroundSettings . hasKey ( "channelId" )) {
297
+ if (! this . isForegroundServiceConfigured ( )) {
298
298
Log .w (TAG , "[VoiceConnectionService] Not creating foregroundService because not configured" );
299
299
return ;
300
300
}
@@ -335,18 +335,39 @@ private void startForegroundService() {
335
335
Log .d (TAG , "[VoiceConnectionService] Starting foreground service" );
336
336
337
337
Notification notification = notificationBuilder .build ();
338
- startForeground (FOREGROUND_SERVICE_TYPE_MICROPHONE , notification );
338
+
339
+ try {
340
+ startForeground (FOREGROUND_SERVICE_TYPE_MICROPHONE , notification );
341
+ } catch (Exception e ) {
342
+ Log .w (TAG , "[VoiceConnectionService] Can't start foreground service : " + e .toString ());
343
+ }
339
344
}
340
345
341
346
private void stopForegroundService () {
342
347
Log .d (TAG , "[VoiceConnectionService] stopForegroundService" );
343
348
ReadableMap foregroundSettings = getForegroundSettings (null );
344
349
345
- if (foregroundSettings == null || ! foregroundSettings . hasKey ( "channelId" )) {
346
- Log .d (TAG , "[VoiceConnectionService] Discarding stop foreground service, no service configured" );
350
+ if (! this . isForegroundServiceConfigured ( )) {
351
+ Log .w (TAG , "[VoiceConnectionService] Not creating foregroundService because not configured" );
347
352
return ;
348
353
}
349
- stopForeground (FOREGROUND_SERVICE_TYPE_MICROPHONE );
354
+
355
+ try {
356
+ stopForeground (FOREGROUND_SERVICE_TYPE_MICROPHONE );
357
+ } catch (Exception e ) {
358
+ Log .w (TAG , "[VoiceConnectionService] can't stop foreground service :" + e .toString ());
359
+ }
360
+ }
361
+
362
+ private boolean isForegroundServiceConfigured () {
363
+ ReadableMap foregroundSettings = getForegroundSettings (null );
364
+ try {
365
+ return foregroundSettings != null && foregroundSettings .hasKey ("channelId" );
366
+ } catch (Exception e ) {
367
+ // Fix ArrayIndexOutOfBoundsException thrown by ReadableNativeMap.hasKey
368
+ Log .w (TAG , "[VoiceConnectionService] Not creating foregroundService due to configuration retrieval error" + e .toString ());
369
+ return false ;
370
+ }
350
371
}
351
372
352
373
private void wakeUpApplication (String uuid , String number , String displayName ) {
0 commit comments