Skip to content

Commit 4a76c62

Browse files
authored
Merge pull request #669 from react-native-webrtc/fix_foreground_service_exception
Catch ArrayIndexOutOfBoundsException thrown by ReadableNativeMap.hasKey
2 parents 4d6937e + 0ca9661 commit 4a76c62

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java

+26-5
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ private void startForegroundService() {
294294
Log.d(TAG, "[VoiceConnectionService] startForegroundService");
295295
ReadableMap foregroundSettings = getForegroundSettings(null);
296296

297-
if (foregroundSettings == null || !foregroundSettings.hasKey("channelId")) {
297+
if (!this.isForegroundServiceConfigured()) {
298298
Log.w(TAG, "[VoiceConnectionService] Not creating foregroundService because not configured");
299299
return;
300300
}
@@ -335,18 +335,39 @@ private void startForegroundService() {
335335
Log.d(TAG, "[VoiceConnectionService] Starting foreground service");
336336

337337
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+
}
339344
}
340345

341346
private void stopForegroundService() {
342347
Log.d(TAG, "[VoiceConnectionService] stopForegroundService");
343348
ReadableMap foregroundSettings = getForegroundSettings(null);
344349

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");
347352
return;
348353
}
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+
}
350371
}
351372

352373
private void wakeUpApplication(String uuid, String number, String displayName) {

0 commit comments

Comments
 (0)