Skip to content

Commit

Permalink
fix(android) Error: Comparison method violates its general contract! (#…
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
hpflatorre and henriquelatorre authored Apr 29, 2024
1 parent ab2333a commit ada706e
Showing 1 changed file with 1 addition and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,7 @@ public ArrayList<Voice> getSupportedVoicesOrdered() {
orderedVoices.add(supportedVoice);
}

Collections.sort(
orderedVoices,
new Comparator<Voice>() {
public int compare(Voice v1, Voice v2) {
return v1.hashCode() - v2.hashCode();
}
}
);
Collections.sort(orderedVoices, Comparator.comparing(Voice::hashCode));

return orderedVoices;
}
Expand Down

0 comments on commit ada706e

Please sign in to comment.