From 3d2308e2859d4beef57cb14d015d987a8fa50adf Mon Sep 17 00:00:00 2001 From: "Reece H. Dunn" Date: Tue, 28 Oct 2014 18:30:16 +0000 Subject: [PATCH] Use API 21. --- android/build.gradle | 4 +- .../espeak/test/TextToSpeechTest.java | 44 +++++++++---------- .../espeak/test/TextToSpeechTestCase.java | 16 +++++++ .../com/reecedunn/espeak/GetSampleText.java | 2 + .../com/reecedunn/espeak/eSpeakActivity.java | 32 +++++++++++--- 5 files changed, 67 insertions(+), 31 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 4b74f1e2..e9c1e639 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -19,11 +19,11 @@ dependencies { android { buildToolsVersion '19.1' - compileSdkVersion 19 + compileSdkVersion 21 defaultConfig { minSdkVersion 14 - targetSdkVersion 19 + targetSdkVersion 21 } sourceSets { diff --git a/android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTest.java b/android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTest.java index e349b76d..fd8629a2 100644 --- a/android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTest.java +++ b/android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTest.java @@ -30,11 +30,11 @@ public class TextToSpeechTest extends TextToSpeechTestCase { assertThat(getEngine(), is(notNullValue())); - Locale initialLocale = getEngine().getLanguage(); + Locale initialLocale = getLanguage(getEngine()); assertThat(getEngine().isLanguageAvailable(new Locale("cel")), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED)); - assertThat(getEngine().getLanguage().getLanguage(), is(initialLocale.getLanguage())); - assertThat(getEngine().getLanguage().getCountry(), is(initialLocale.getCountry())); - assertThat(getEngine().getLanguage().getVariant(), is(initialLocale.getVariant())); + assertThat(getLanguage(getEngine()).getLanguage(), is(initialLocale.getLanguage())); + assertThat(getLanguage(getEngine()).getCountry(), is(initialLocale.getCountry())); + assertThat(getLanguage(getEngine()).getVariant(), is(initialLocale.getVariant())); } public void testLanguages() @@ -60,44 +60,44 @@ public class TextToSpeechTest extends TextToSpeechTestCase context = "iana1"; assertThat(getEngine().isLanguageAvailable(iana1), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE)); assertThat(getEngine().setLanguage(iana1), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE)); - assertThat(getEngine().getLanguage().getLanguage(), is(data.javaLanguage)); - assertThat(getEngine().getLanguage().getCountry(), is(data.javaCountry)); - assertThat(getEngine().getLanguage().getVariant(), is(data.variant)); + assertThat(getLanguage(getEngine()).getLanguage(), is(data.javaLanguage)); + assertThat(getLanguage(getEngine()).getCountry(), is(data.javaCountry)); + assertThat(getLanguage(getEngine()).getVariant(), is(data.variant)); context = "iana2"; assertThat(getEngine().isLanguageAvailable(iana2), isTtsLangCode(TextToSpeech.LANG_COUNTRY_AVAILABLE)); assertThat(getEngine().setLanguage(iana2), isTtsLangCode(TextToSpeech.LANG_COUNTRY_AVAILABLE)); - assertThat(getEngine().getLanguage().getLanguage(), is(data.javaLanguage)); - assertThat(getEngine().getLanguage().getCountry(), is(data.javaCountry)); - assertThat(getEngine().getLanguage().getVariant(), is("")); + assertThat(getLanguage(getEngine()).getLanguage(), is(data.javaLanguage)); + assertThat(getLanguage(getEngine()).getCountry(), is(data.javaCountry)); + assertThat(getLanguage(getEngine()).getVariant(), is("")); context = "iana3"; assertThat(getEngine().isLanguageAvailable(iana3), isTtsLangCode(TextToSpeech.LANG_AVAILABLE)); assertThat(getEngine().setLanguage(iana3), isTtsLangCode(TextToSpeech.LANG_AVAILABLE)); - assertThat(getEngine().getLanguage().getLanguage(), is(data.javaLanguage)); - assertThat(getEngine().getLanguage().getCountry(), is("")); - assertThat(getEngine().getLanguage().getVariant(), is("")); + assertThat(getLanguage(getEngine()).getLanguage(), is(data.javaLanguage)); + assertThat(getLanguage(getEngine()).getCountry(), is("")); + assertThat(getLanguage(getEngine()).getVariant(), is("")); context = "java1"; assertThat(getEngine().isLanguageAvailable(java1), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE)); assertThat(getEngine().setLanguage(java1), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE)); - assertThat(getEngine().getLanguage().getLanguage(), is(data.javaLanguage)); - assertThat(getEngine().getLanguage().getCountry(), is(data.javaCountry)); - assertThat(getEngine().getLanguage().getVariant(), is(data.variant)); + assertThat(getLanguage(getEngine()).getLanguage(), is(data.javaLanguage)); + assertThat(getLanguage(getEngine()).getCountry(), is(data.javaCountry)); + assertThat(getLanguage(getEngine()).getVariant(), is(data.variant)); context = "java2"; assertThat(getEngine().isLanguageAvailable(java2), isTtsLangCode(TextToSpeech.LANG_COUNTRY_AVAILABLE)); assertThat(getEngine().setLanguage(java2), isTtsLangCode(TextToSpeech.LANG_COUNTRY_AVAILABLE)); - assertThat(getEngine().getLanguage().getLanguage(), is(data.javaLanguage)); - assertThat(getEngine().getLanguage().getCountry(), is(data.javaCountry)); - assertThat(getEngine().getLanguage().getVariant(), is("")); + assertThat(getLanguage(getEngine()).getLanguage(), is(data.javaLanguage)); + assertThat(getLanguage(getEngine()).getCountry(), is(data.javaCountry)); + assertThat(getLanguage(getEngine()).getVariant(), is("")); context = "java3"; assertThat(getEngine().isLanguageAvailable(java3), isTtsLangCode(TextToSpeech.LANG_AVAILABLE)); assertThat(getEngine().setLanguage(java3), isTtsLangCode(TextToSpeech.LANG_AVAILABLE)); - assertThat(getEngine().getLanguage().getLanguage(), is(data.javaLanguage)); - assertThat(getEngine().getLanguage().getCountry(), is("")); - assertThat(getEngine().getLanguage().getVariant(), is("")); + assertThat(getLanguage(getEngine()).getLanguage(), is(data.javaLanguage)); + assertThat(getLanguage(getEngine()).getCountry(), is("")); + assertThat(getLanguage(getEngine()).getVariant(), is("")); } catch (AssertionError e) { diff --git a/android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTestCase.java b/android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTestCase.java index d7ff73e4..615d38c1 100644 --- a/android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTestCase.java +++ b/android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechTestCase.java @@ -16,6 +16,7 @@ package com.reecedunn.espeak.test; +import android.os.Build; import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech.OnInitListener; import android.test.AndroidTestCase; @@ -93,4 +94,19 @@ public class TextToSpeechTestCase extends AndroidTestCase { return mEngine; } + + @SuppressWarnings("deprecation") + public static Locale getLanguage(TextToSpeech engine) { + if (engine != null) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + android.speech.tts.Voice voice = engine.getVoice(); + if (voice != null) { + return voice.getLocale(); + } + } else { + return engine.getLanguage(); + } + } + return null; + } } diff --git a/android/src/com/reecedunn/espeak/GetSampleText.java b/android/src/com/reecedunn/espeak/GetSampleText.java index 1df384b7..d400d842 100755 --- a/android/src/com/reecedunn/espeak/GetSampleText.java +++ b/android/src/com/reecedunn/espeak/GetSampleText.java @@ -17,6 +17,7 @@ package com.reecedunn.espeak; +import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.os.Bundle; @@ -27,6 +28,7 @@ import java.util.Locale; /* * Returns the sample text string for the language requested */ +@SuppressLint("NewApi") public class GetSampleText extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { diff --git a/android/src/com/reecedunn/espeak/eSpeakActivity.java b/android/src/com/reecedunn/espeak/eSpeakActivity.java index 6ebca75f..66a2a3ab 100644 --- a/android/src/com/reecedunn/espeak/eSpeakActivity.java +++ b/android/src/com/reecedunn/espeak/eSpeakActivity.java @@ -89,8 +89,13 @@ public class eSpeakActivity extends Activity { findViewById(R.id.speak).setOnClickListener(new View.OnClickListener() { @Override + @SuppressWarnings("deprecation") public void onClick(View v) { - mTts.speak(mText.getText().toString(), TextToSpeech.QUEUE_ADD, null); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + mTts.speak(mText.getText().toString(), TextToSpeech.QUEUE_ADD, null, null); + } else { + mTts.speak(mText.getText().toString(), TextToSpeech.QUEUE_ADD, null); + } } }); @@ -189,15 +194,28 @@ public class eSpeakActivity extends Activity { mTts = new TextToSpeech(this, mInitListener); } + @SuppressWarnings("deprecation") + private Locale getTtsLanguage() { + if (mTts != null) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + android.speech.tts.Voice voice = mTts.getVoice(); + if (voice != null) { + return voice.getLocale(); + } + } else { + return mTts.getLanguage(); + } + } + return null; + } + private void populateInformationView() { mInformation.clear(); - if (mTts != null) { - Locale language = mTts.getLanguage(); - if (language != null) { - final String currentLocale = getString(R.string.current_tts_locale); - mInformation.add(new Pair(currentLocale, mTts.getLanguage().getDisplayName())); - } + Locale language = getTtsLanguage(); + if (language != null) { + final String currentLocale = getString(R.string.current_tts_locale); + mInformation.add(new Pair(currentLocale, language.getDisplayName())); } final String availableVoices = getString(R.string.available_voices);