Skip to content

Commit

Permalink
fix(android): getSupportedVoices() is not available for api levels …
Browse files Browse the repository at this point in the history
…< 24 (#124)

* Fix support issue on API levels < 24

Comparator.comparing does not work below API level 24

* Sort using getName() rather than hashCode(), remove crash below API level 24

getName() is guaranteed to be unique, hashCode is not, so this may fix potential issues with the use of hashCode. Also switch from using Comparator.comparing to allow for support on API levels 21-24
  • Loading branch information
ecc521 authored Jun 22, 2024
1 parent 3e6fc1d commit 0ccace8
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.getcapacitor.JSObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Locale;
import java.util.Set;
Expand Down Expand Up @@ -116,14 +115,18 @@ public JSArray getSupportedLanguages() {
return result;
}

/**
* @return Ordered list of voices. The order is guaranteed to remain the same as long as the voices in tts.getVoices() do not change.
*/
public ArrayList<Voice> getSupportedVoicesOrdered() {
Set<Voice> supportedVoices = tts.getVoices();
ArrayList<Voice> orderedVoices = new ArrayList<Voice>();
for (Voice supportedVoice : supportedVoices) {
orderedVoices.add(supportedVoice);
}

Collections.sort(orderedVoices, Comparator.comparing(Voice::hashCode));
//voice.getName() is guaranteed to be unique, so will be used for sorting.
Collections.sort(orderedVoices, (v1, v2) -> v1.getName().compareTo(v2.getName()));

return orderedVoices;
}
Expand Down

0 comments on commit 0ccace8

Please sign in to comment.