Browse Source

Factor out the onIsLanguageAvailable logic into a findVoice helper method.

master
Reece H. Dunn 9 years ago
parent
commit
d6786a4df4
1 changed files with 14 additions and 11 deletions
  1. 14
    11
      android/src/com/reecedunn/espeak/TtsService.java

+ 14
- 11
android/src/com/reecedunn/espeak/TtsService.java View File

import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeechService; import android.speech.tts.TextToSpeechService;
import android.util.Log; import android.util.Log;
import android.util.Pair;


import com.reecedunn.espeak.SpeechSynthesis.SynthReadyCallback; import com.reecedunn.espeak.SpeechSynthesis.SynthReadyCallback;


}; };
} }


@Override
protected int onIsLanguageAvailable(String language, String country, String variant) {
private Pair<Voice, Integer> findVoice(String language, String country, String variant) {
if (!CheckVoiceData.hasBaseResources(this) || CheckVoiceData.canUpgradeResources(this)) { if (!CheckVoiceData.hasBaseResources(this) || CheckVoiceData.canUpgradeResources(this)) {
if (mOnLanguagesDownloaded == null) { if (mOnLanguagesDownloaded == null) {
mOnLanguagesDownloaded = new BroadcastReceiver() { mOnLanguagesDownloaded = new BroadcastReceiver() {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent); startActivity(intent);


return TextToSpeech.LANG_MISSING_DATA;
return new Pair<>(null, TextToSpeech.LANG_MISSING_DATA);
} }


final Locale query = new Locale(language, country, variant); final Locale query = new Locale(language, country, variant);
for (Voice voice : mAvailableVoices.values()) { for (Voice voice : mAvailableVoices.values()) {
switch (voice.match(query)) { switch (voice.match(query)) {
case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE: case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
mMatchingVoice = voice;
return TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE;
return new Pair<>(voice, TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE);
case TextToSpeech.LANG_COUNTRY_AVAILABLE: case TextToSpeech.LANG_COUNTRY_AVAILABLE:
countryVoice = voice; countryVoice = voice;
case TextToSpeech.LANG_AVAILABLE: case TextToSpeech.LANG_AVAILABLE:
} }


if (languageVoice == null) { if (languageVoice == null) {
mMatchingVoice = null;
return TextToSpeech.LANG_NOT_SUPPORTED;
return new Pair<>(null, TextToSpeech.LANG_NOT_SUPPORTED);
} else if (countryVoice == null) { } else if (countryVoice == null) {
mMatchingVoice = languageVoice;
return TextToSpeech.LANG_AVAILABLE;
return new Pair<>(languageVoice, TextToSpeech.LANG_AVAILABLE);
} else { } else {
mMatchingVoice = countryVoice;
return TextToSpeech.LANG_COUNTRY_AVAILABLE;
return new Pair<>(countryVoice, TextToSpeech.LANG_COUNTRY_AVAILABLE);
} }
} }


@Override
protected int onIsLanguageAvailable(String language, String country, String variant) {
final Pair<Voice, Integer> match = findVoice(language, country, variant);
mMatchingVoice = match.first;
return match.second;
}

@Override @Override
protected int onLoadLanguage(String language, String country, String variant) { protected int onLoadLanguage(String language, String country, String variant) {
final int result = onIsLanguageAvailable(language, country, variant); final int result = onIsLanguageAvailable(language, country, variant);

Loading…
Cancel
Save