Skip to content

Commit

Permalink
Merge pull request #669 from react-native-webrtc/fix_foreground_servi…
Browse files Browse the repository at this point in the history
…ce_exception

Catch ArrayIndexOutOfBoundsException thrown by ReadableNativeMap.hasKey
  • Loading branch information
manuquentin authored Mar 1, 2023
2 parents 4d6937e + 0ca9661 commit 4a76c62
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ private void startForegroundService() {
Log.d(TAG, "[VoiceConnectionService] startForegroundService");
ReadableMap foregroundSettings = getForegroundSettings(null);

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

Notification notification = notificationBuilder.build();
startForeground(FOREGROUND_SERVICE_TYPE_MICROPHONE, notification);

try {
startForeground(FOREGROUND_SERVICE_TYPE_MICROPHONE, notification);
} catch (Exception e) {
Log.w(TAG, "[VoiceConnectionService] Can't start foreground service : " + e.toString());
}
}

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

if (foregroundSettings == null || !foregroundSettings.hasKey("channelId")) {
Log.d(TAG, "[VoiceConnectionService] Discarding stop foreground service, no service configured");
if (!this.isForegroundServiceConfigured()) {
Log.w(TAG, "[VoiceConnectionService] Not creating foregroundService because not configured");
return;
}
stopForeground(FOREGROUND_SERVICE_TYPE_MICROPHONE);

try {
stopForeground(FOREGROUND_SERVICE_TYPE_MICROPHONE);
} catch (Exception e) {
Log.w(TAG, "[VoiceConnectionService] can't stop foreground service :" + e.toString());
}
}

private boolean isForegroundServiceConfigured() {
ReadableMap foregroundSettings = getForegroundSettings(null);
try {
return foregroundSettings != null && foregroundSettings.hasKey("channelId");
} catch (Exception e) {
// Fix ArrayIndexOutOfBoundsException thrown by ReadableNativeMap.hasKey
Log.w(TAG, "[VoiceConnectionService] Not creating foregroundService due to configuration retrieval error" + e.toString());
return false;
}
}

private void wakeUpApplication(String uuid, String number, String displayName) {
Expand Down

0 comments on commit 4a76c62

Please sign in to comment.