diff options
Diffstat (limited to 'packages/TtsService/src/android/tts/TtsService.java')
-rwxr-xr-x | packages/TtsService/src/android/tts/TtsService.java | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/packages/TtsService/src/android/tts/TtsService.java b/packages/TtsService/src/android/tts/TtsService.java index 217d3bb..8d8ef8e 100755 --- a/packages/TtsService/src/android/tts/TtsService.java +++ b/packages/TtsService/src/android/tts/TtsService.java @@ -20,8 +20,10 @@ import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; +import android.content.pm.ResolveInfo; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; @@ -39,6 +41,7 @@ import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Locale; import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.TimeUnit; @@ -146,6 +149,8 @@ public class TtsService extends Service implements OnCompletionListener { private final ReentrantLock synthesizerLock = new ReentrantLock(); private static SynthProxy sNativeSynth = null; + private String currentSpeechEngineSOFile = ""; + @Override public void onCreate() { super.onCreate(); @@ -153,6 +158,9 @@ public class TtsService extends Service implements OnCompletionListener { mResolver = getContentResolver(); + currentSpeechEngineSOFile = ""; + setEngine(getDefaultEngine()); + String soLibPath = "/system/lib/libttspico.so"; if (sNativeSynth == null) { sNativeSynth = new SynthProxy(soLibPath); @@ -194,6 +202,54 @@ public class TtsService extends Service implements OnCompletionListener { } + private int setEngine(String enginePackageName) { + String soFilename = ""; + // The SVOX TTS is an exception to how the TTS packaging scheme works + // because it is part of the system and not a 3rd party add-on; thus + // its binary is actually located under /system/lib/ + if (enginePackageName.equals("com.svox.pico")) { + soFilename = "/system/lib/libttspico.so"; + } else { + // Find the package + Intent intent = new Intent("android.intent.action.START_TTS_ENGINE"); + intent.setPackage(enginePackageName); + ResolveInfo[] enginesArray = new ResolveInfo[0]; + PackageManager pm = getPackageManager(); + List <ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0); + if ((resolveInfos == null) || resolveInfos.isEmpty()) { + Log.e(SERVICE_TAG, "Invalid TTS Engine Package: " + enginePackageName); + return TextToSpeech.ERROR; + } + enginesArray = resolveInfos.toArray(enginesArray); + // Generate the TTS .so filename from the package + ActivityInfo aInfo = enginesArray[0].activityInfo; + soFilename = aInfo.name.replace(aInfo.packageName + ".", "") + ".so"; + soFilename = soFilename.toLowerCase(); + soFilename = "/data/data/" + aInfo.packageName + "/lib/libtts" + soFilename; + } + + if (currentSpeechEngineSOFile.equals(soFilename)) { + return TextToSpeech.SUCCESS; + } + + File f = new File(soFilename); + if (!f.exists()) { + Log.e(SERVICE_TAG, "Invalid TTS Binary: " + soFilename); + return TextToSpeech.ERROR; + } + + if (sNativeSynth != null) { + sNativeSynth.stopSync(); + sNativeSynth.shutdown(); + sNativeSynth = null; + } + sNativeSynth = new SynthProxy(soFilename); + currentSpeechEngineSOFile = soFilename; + return TextToSpeech.SUCCESS; + } + + + private void setDefaultSettings() { setLanguage("", this.getDefaultLanguage(), getDefaultCountry(), getDefaultLocVariant()); @@ -209,6 +265,15 @@ public class TtsService extends Service implements OnCompletionListener { == 1 ); } + private String getDefaultEngine() { + String defaultEngine = android.provider.Settings.Secure.getString(mResolver, + android.provider.Settings.Secure.TTS_DEFAULT_SYNTH); + if (defaultEngine == null) { + return TextToSpeech.Engine.DEFAULT_SYNTH; + } else { + return defaultEngine; + } + } private int getDefaultRate() { return android.provider.Settings.Secure.getInt(mResolver, @@ -1261,6 +1326,17 @@ public class TtsService extends Service implements OnCompletionListener { return mSelf.synthesizeToFile(callingApp, text, speakingParams, filename); } + /** + * Sets the speech synthesis engine for the TTS by specifying its packagename + * + * @param packageName the packageName of the speech synthesis engine (ie, "com.svox.pico") + * + * @return SUCCESS or ERROR as defined in android.speech.tts.TextToSpeech. + */ + public int setEngineByPackageName(String packageName) {
+ return mSelf.setEngine(packageName);
+ } + }; } |