Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/TtsPlatformImpl.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/TtsPlatformImpl.java b/chrome/android/java/src/org/chromium/chrome/browser/TtsPlatformImpl.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..29a2bed7b96a7bec01d756782b74eb853aef31dd |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/TtsPlatformImpl.java |
| @@ -0,0 +1,165 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser; |
| + |
| +import android.content.Context; |
| +import android.speech.tts.TextToSpeech; |
| +import android.speech.tts.UtteranceProgressListener; |
| +import java.lang.Double; |
| +import java.lang.Integer; |
| +import java.util.ArrayList; |
| +import java.util.HashMap; |
| +import java.util.Locale; |
| +import org.chromium.base.CalledByNative; |
| +import org.chromium.base.ThreadUtils; |
| + |
| +/** |
| + * This class is the Java counterpart to the C++ TtsPlatformImplAndroid class. |
| + * It implements the Android-native text-to-speech code to support the web |
| + * speech synthesis API. |
| + */ |
| +public class TtsPlatformImpl { |
| + private class TtsVoice { |
| + public String name; |
| + public String language; |
| + }; |
| + |
| + private int mNativeTtsPlatformImplAndroid; |
| + private TextToSpeech mTextToSpeech; |
| + private boolean mInitialized; |
| + private ArrayList<TtsVoice> mVoices; |
| + private String mCurrentLanguage; |
| + |
| + private TtsPlatformImpl(int nativeTtsPlatformImplAndroid, Context context) { |
| + mInitialized = false; |
| + mNativeTtsPlatformImplAndroid = nativeTtsPlatformImplAndroid; |
| + mTextToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() { |
| + public void onInit(int status) { |
| + if (status == TextToSpeech.SUCCESS) |
| + initialize(); |
|
David Trainor- moved to gerrit
2013/05/15 20:55:28
move this up on the same line as the if() or put {
dmazzoni
2013/05/16 07:35:13
Done.
|
| + } |
| + }); |
| + mTextToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() { |
| + public void onDone(final String utteranceId) { |
| + ThreadUtils.runOnUiThread(new Runnable() { |
| + @Override |
| + public void run() { |
| + nativeOnEndEvent(mNativeTtsPlatformImplAndroid, |
|
David Trainor- moved to gerrit
2013/05/15 20:55:28
Maybe check mNativeTtsPlatformImplAndroid for 0?
dmazzoni
2013/05/16 07:35:13
Done.
|
| + Integer.parseInt(utteranceId)); |
| + } |
| + }); |
| + } |
| + |
| + public void onError(final String utteranceId) { |
| + ThreadUtils.runOnUiThread(new Runnable() { |
| + @Override |
| + public void run() { |
| + nativeOnErrorEvent(mNativeTtsPlatformImplAndroid, |
| + Integer.parseInt(utteranceId)); |
| + } |
| + }); |
| + } |
| + |
| + public void onStart(final String utteranceId) { |
| + ThreadUtils.runOnUiThread(new Runnable() { |
| + @Override |
| + public void run() { |
| + nativeOnStartEvent(mNativeTtsPlatformImplAndroid, |
| + Integer.parseInt(utteranceId)); |
| + } |
| + }); |
| + } |
| + }); |
| + }; |
| + |
| + @CalledByNative |
| + public static TtsPlatformImpl create(int nativeTtsPlatformImplAndroid, |
|
David Trainor- moved to gerrit
2013/05/15 20:55:28
Put javadocs on the public methods.
dmazzoni
2013/05/16 07:35:13
Done.
|
| + Context context) { |
| + return new TtsPlatformImpl(nativeTtsPlatformImplAndroid, context); |
| + } |
| + |
| + @CalledByNative |
| + public boolean isInitialized() { |
| + return mInitialized; |
| + } |
| + |
| + @CalledByNative |
| + public int getVoiceCount() { |
| + return mVoices.size(); |
|
David Trainor- moved to gerrit
2013/05/15 20:55:28
Would it just be safe to return 0 if mVoices is nu
dmazzoni
2013/05/16 07:35:13
Sure
dmazzoni
2013/05/16 07:35:13
How about I just assert mInitialized == true every
|
| + } |
| + |
| + @CalledByNative |
| + public String getVoiceName(int voiceIndex) { |
| + return mVoices.get(voiceIndex).name; |
| + } |
| + |
| + @CalledByNative |
| + public String getVoiceLanguage(int voiceIndex) { |
| + return mVoices.get(voiceIndex).language; |
| + } |
| + |
| + @CalledByNative |
| + public boolean speak(int utteranceId, String text, String lang, |
| + float rate, float pitch, float volume) { |
| + if (lang != mCurrentLanguage) { |
|
David Trainor- moved to gerrit
2013/05/15 20:55:28
return false if !isInitialized() (and/or assert)?
dmazzoni
2013/05/16 07:35:13
I like asserting better, that doesn't imply there'
|
| + mTextToSpeech.setLanguage(new Locale(lang)); |
| + mCurrentLanguage = lang; |
| + } |
| + |
| + mTextToSpeech.setSpeechRate(rate); |
| + mTextToSpeech.setPitch(pitch); |
| + HashMap<String, String> params = new HashMap<String, String>(); |
| + if (volume != 1.0) |
| + params.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, Double.toString(volume)); |
|
David Trainor- moved to gerrit
2013/05/15 20:55:28
{ } around this even though it's one line.
dmazzoni
2013/05/16 07:35:13
Done.
|
| + params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, Integer.toString(utteranceId)); |
| + int result = mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, params); |
| + return (result == TextToSpeech.SUCCESS); |
| + } |
| + |
| + @CalledByNative |
| + public void stop() { |
| + } |
| + |
| + private void initialize() { |
| + // Note: Android supports multiple speech engines, but querying the |
| + // metadata about all of them is expensive. So we deliberately only |
| + // support the default speech engine, and expose the different |
| + // supported languages for the default engine as different voices. |
| + String defaultEngineName = mTextToSpeech.getDefaultEngine(); |
| + String engineLabel = defaultEngineName; |
| + for (TextToSpeech.EngineInfo info : mTextToSpeech.getEngines()) { |
| + if (info.name.equals(defaultEngineName)) |
| + engineLabel = info.label; |
|
David Trainor- moved to gerrit
2013/05/15 20:55:28
Move up to same line as if.
dmazzoni
2013/05/16 07:35:13
Done.
|
| + } |
| + Locale[] locales = Locale.getAvailableLocales(); |
| + mVoices = new ArrayList<TtsVoice>(); |
| + for (int i = 0; i < locales.length; ++i) { |
| + if (!locales[i].getVariant().isEmpty()) |
| + continue; |
|
David Trainor- moved to gerrit
2013/05/15 20:55:28
Move up to same line as if.
dmazzoni
2013/05/16 07:35:13
Done.
|
| + if (mTextToSpeech.isLanguageAvailable(locales[i]) > 0) { |
| + TtsVoice voice = new TtsVoice(); |
| + voice.name = locales[i].getDisplayLanguage(); |
| + if (!locales[i].getCountry().isEmpty()) |
| + voice.name += " " + locales[i].getDisplayCountry(); |
|
David Trainor- moved to gerrit
2013/05/15 20:55:28
Add { } around this line or move up.
If this is g
dmazzoni
2013/05/16 07:35:13
Done.
|
| + voice.language = locales[i].toString(); |
| + mVoices.add(voice); |
| + } |
| + } |
| + |
| + mInitialized = true; |
| + |
| + ThreadUtils.runOnUiThread(new Runnable() { |
| + @Override |
| + public void run() { |
| + nativeVoicesChanged(mNativeTtsPlatformImplAndroid); |
|
David Trainor- moved to gerrit
2013/05/15 20:55:28
Maybe add if (mNativeTtsPlatformImplAndroid != 0)?
dmazzoni
2013/05/16 07:35:13
Done.
|
| + } |
| + }); |
| + } |
| + |
| + private native void nativeVoicesChanged(int nativeTtsPlatformImplAndroid); |
| + private native void nativeOnEndEvent(int nativeTtsPlatformImplAndroid, int utteranceId); |
| + private native void nativeOnStartEvent(int nativeTtsPlatformImplAndroid, int utteranceId); |
| + private native void nativeOnErrorEvent(int nativeTtsPlatformImplAndroid, int utteranceId); |
| +} |