use of net.osmand.plus.activities.SettingsActivity in project Osmand by osmandapp.
the class TTSCommandPlayerImpl method initializeEngine.
private void initializeEngine(final Context ctx, final Activity act) {
if (mTtsContext != ctx) {
internalClear();
}
if (mTts == null) {
mTtsContext = ctx;
ttsVoiceStatus = "";
ttsVoiceUsed = "";
ttsRequests = 0;
final float speechRate = cSpeechRate;
final String[] lsplit = (language + "____.").split("[\\_\\-]");
// constructor supports lang_country_variant
Locale newLocale0 = new Locale(lsplit[0], lsplit[1], lsplit[2]);
// #3344: Try Locale builder instead of constructor (only available from API 21). Also supports script (for now supported as trailing x_x_x_Scrp)
if (android.os.Build.VERSION.SDK_INT >= 21) {
try {
newLocale0 = new Locale.Builder().setLanguage(lsplit[0]).setScript(lsplit[3]).setRegion(lsplit[1]).setVariant(lsplit[2]).build();
} catch (RuntimeException e) {
// Falls back to constructor
}
}
final Locale newLocale = newLocale0;
mTts = new TextToSpeech(ctx, new OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.SUCCESS) {
ttsVoiceStatus = "NO INIT SUCCESS";
internalClear();
} else if (mTts != null) {
speechAllowed = true;
switch(mTts.isLanguageAvailable(newLocale)) {
case TextToSpeech.LANG_MISSING_DATA:
if (isSettingsActivity(act)) {
AlertDialog.Builder builder = createAlertDialog(R.string.tts_missing_language_data_title, R.string.tts_missing_language_data, new IntentStarter(act, TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA), act);
builder.show();
}
ttsVoiceStatus = newLocale.getDisplayName() + ": LANG_MISSING_DATA";
ttsVoiceUsed = getVoiceUsed();
break;
case TextToSpeech.LANG_AVAILABLE:
ttsVoiceStatus = newLocale.getDisplayName() + ": LANG_AVAILABLE";
case TextToSpeech.LANG_COUNTRY_AVAILABLE:
ttsVoiceStatus = "".equals(ttsVoiceStatus) ? newLocale.getDisplayName() + ": LANG_COUNTRY_AVAILABLE" : ttsVoiceStatus;
case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
try {
mTts.setLanguage(newLocale);
} catch (Exception e) {
e.printStackTrace();
mTts.setLanguage(Locale.getDefault());
}
if (speechRate != 1) {
mTts.setSpeechRate(speechRate);
}
ttsVoiceStatus = "".equals(ttsVoiceStatus) ? newLocale.getDisplayName() + ": LANG_COUNTRY_VAR_AVAILABLE" : ttsVoiceStatus;
ttsVoiceUsed = getVoiceUsed();
break;
case TextToSpeech.LANG_NOT_SUPPORTED:
// maybe weird, but I didn't want to introduce parameter in around 5 methods just to do this if condition
if (isSettingsActivity(act)) {
AlertDialog.Builder builder = createAlertDialog(R.string.tts_language_not_supported_title, R.string.tts_language_not_supported, new IntentStarter(act, Intent.ACTION_VIEW, Uri.parse("market://search?q=text to speech engine")), act);
builder.show();
}
ttsVoiceStatus = newLocale.getDisplayName() + ": LANG_NOT_SUPPORTED";
ttsVoiceUsed = getVoiceUsed();
break;
}
}
}
private boolean isSettingsActivity(final Context ctx) {
return ctx instanceof SettingsActivity;
}
private String getVoiceUsed() {
try {
if (android.os.Build.VERSION.SDK_INT >= 21) {
if (mTts.getVoice() != null) {
return mTts.getVoice().toString() + " (API " + android.os.Build.VERSION.SDK_INT + ")";
}
} else {
return mTts.getLanguage() + " (API " + android.os.Build.VERSION.SDK_INT + " only reports language)";
}
} catch (RuntimeException e) {
// mTts.getVoice() might throw NPE
}
return "-";
}
});
mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {
// The call back is on a binder thread.
@Override
public synchronized void onUtteranceCompleted(String utteranceId) {
if (--ttsRequests <= 0)
abandonAudioFocus();
log.debug("ttsRequests=" + ttsRequests);
if (ttsRequests < 0) {
ttsRequests = 0;
}
}
});
}
}
Aggregations