From ada706ed26ea99e8d3a6bc66d509b0528a6203c3 Mon Sep 17 00:00:00 2001 From: Henrique Date: Mon, 29 Apr 2024 16:16:06 -0300 Subject: [PATCH] fix(android) Error: Comparison method violates its general contract! (#114) * fix(android) Error: Comparison method violates its general contract! Fixes a runtime crash on getSupportedVoices observed on Android after changing the default TTS from Samsung to Google from system settings. Stack: @capacitor-community/text-to-speech: 4.0.0, @capacitor/android : 6.0.0 @ionic/angular:7.8.6 Traces: Sending plugin error: {"save":false,"callbackId":"72380935","pluginId":"TextToSpeech","methodName":"getSupportedVoices","success":false,"error":{"message":"Comparison method violates its general contract!"}} * fix(android) Error: Comparison method violates its general contract! revisited This change to the solution keeps the behavior 100% consistent with original code that orders based on hash instead of name Fixes a runtime crash on getSupportedVoices observed on Android after changing the default TTS from Samsung to Google from system settings. Stack: @capacitor-community/text-to-speech: 4.0.0, @capacitor/android : 6.0.0 @ionic/angular:7.8.6 Traces: Sending plugin error: {"save":false,"callbackId":"72380935","pluginId":"TextToSpeech","methodName":"getSupportedVoices","success":false,"error":{"message":"Comparison method violates its general contract!"}} * fix(android) Error: Comparison method violates its general contract! Fixes #112 Changes compare logic to avoid the issue described without changing the ordering behavior. --------- Co-authored-by: Henrique Latorre --- .../com/getcapacitor/community/tts/TextToSpeech.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/android/src/main/java/com/getcapacitor/community/tts/TextToSpeech.java b/android/src/main/java/com/getcapacitor/community/tts/TextToSpeech.java index a900f29..2df17bf 100644 --- a/android/src/main/java/com/getcapacitor/community/tts/TextToSpeech.java +++ b/android/src/main/java/com/getcapacitor/community/tts/TextToSpeech.java @@ -123,14 +123,7 @@ public ArrayList getSupportedVoicesOrdered() { orderedVoices.add(supportedVoice); } - Collections.sort( - orderedVoices, - new Comparator() { - public int compare(Voice v1, Voice v2) { - return v1.hashCode() - v2.hashCode(); - } - } - ); + Collections.sort(orderedVoices, Comparator.comparing(Voice::hashCode)); return orderedVoices; }