Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / speech / tts_android.cc
bloba9e7ee8884f71e5f4e2de01216148958881a7ae7
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/speech/tts_android.h"
7 #include <string>
9 #include "base/android/jni_string.h"
10 #include "base/memory/singleton.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/speech/tts_controller.h"
13 #include "jni/TtsPlatformImpl_jni.h"
15 using base::android::AttachCurrentThread;
17 // static
18 TtsPlatformImpl* TtsPlatformImpl::GetInstance() {
19 return TtsPlatformImplAndroid::GetInstance();
22 TtsPlatformImplAndroid::TtsPlatformImplAndroid()
23 : utterance_id_(0) {
24 JNIEnv* env = AttachCurrentThread();
25 java_ref_.Reset(
26 Java_TtsPlatformImpl_create(env,
27 reinterpret_cast<intptr_t>(this),
28 base::android::GetApplicationContext()));
31 TtsPlatformImplAndroid::~TtsPlatformImplAndroid() {
32 JNIEnv* env = AttachCurrentThread();
33 if (java_ref_.obj())
34 Java_TtsPlatformImpl_destroy(env, java_ref_.obj());
37 bool TtsPlatformImplAndroid::PlatformImplAvailable() {
38 return true;
41 bool TtsPlatformImplAndroid::Speak(
42 int utterance_id,
43 const std::string& utterance,
44 const std::string& lang,
45 const VoiceData& voice,
46 const UtteranceContinuousParameters& params) {
47 if (!java_ref_.obj())
48 return false;
50 JNIEnv* env = AttachCurrentThread();
51 jboolean success = Java_TtsPlatformImpl_speak(
52 env, java_ref_.obj(),
53 utterance_id,
54 base::android::ConvertUTF8ToJavaString(env, utterance).Release(),
55 base::android::ConvertUTF8ToJavaString(env, lang).Release(),
56 params.rate, params.pitch, params.volume);
57 if (!success)
58 return false;
60 utterance_ = utterance;
61 utterance_id_ = utterance_id;
62 return true;
65 bool TtsPlatformImplAndroid::StopSpeaking() {
66 if (!java_ref_.obj())
67 return false;
69 JNIEnv* env = AttachCurrentThread();
70 Java_TtsPlatformImpl_stop(env, java_ref_.obj());
71 utterance_id_ = 0;
72 utterance_.clear();
73 return true;
76 void TtsPlatformImplAndroid::Pause() {
77 StopSpeaking();
80 void TtsPlatformImplAndroid::Resume() {
83 bool TtsPlatformImplAndroid::IsSpeaking() {
84 return (utterance_id_ != 0);
87 void TtsPlatformImplAndroid::GetVoices(
88 std::vector<VoiceData>* out_voices) {
89 if (!java_ref_.obj())
90 return;
92 JNIEnv* env = AttachCurrentThread();
93 if (!Java_TtsPlatformImpl_isInitialized(env, java_ref_.obj()))
94 return;
96 int count = Java_TtsPlatformImpl_getVoiceCount(env, java_ref_.obj());
97 for (int i = 0; i < count; ++i) {
98 out_voices->push_back(VoiceData());
99 VoiceData& data = out_voices->back();
100 data.native = true;
101 data.name = base::android::ConvertJavaStringToUTF8(
102 Java_TtsPlatformImpl_getVoiceName(env, java_ref_.obj(), i));
103 data.lang = base::android::ConvertJavaStringToUTF8(
104 Java_TtsPlatformImpl_getVoiceLanguage(env, java_ref_.obj(), i));
105 data.gender = TTS_GENDER_NONE;
106 data.events.insert(TTS_EVENT_START);
107 data.events.insert(TTS_EVENT_END);
108 data.events.insert(TTS_EVENT_ERROR);
112 void TtsPlatformImplAndroid::VoicesChanged(JNIEnv* env, jobject obj) {
113 TtsController::GetInstance()->VoicesChanged();
116 void TtsPlatformImplAndroid::OnEndEvent(
117 JNIEnv* env, jobject obj, jint utterance_id) {
118 SendFinalTtsEvent(utterance_id, TTS_EVENT_END,
119 static_cast<int>(utterance_.size()));
122 void TtsPlatformImplAndroid::OnErrorEvent(
123 JNIEnv* env, jobject obj, jint utterance_id) {
124 SendFinalTtsEvent(utterance_id, TTS_EVENT_ERROR, 0);
127 void TtsPlatformImplAndroid::OnStartEvent(
128 JNIEnv* env, jobject obj, jint utterance_id) {
129 if (utterance_id != utterance_id_)
130 return;
132 TtsController::GetInstance()->OnTtsEvent(
133 utterance_id_, TTS_EVENT_START, 0, std::string());
136 void TtsPlatformImplAndroid::SendFinalTtsEvent(
137 int utterance_id, TtsEventType event_type, int char_index) {
138 if (utterance_id != utterance_id_)
139 return;
141 TtsController::GetInstance()->OnTtsEvent(
142 utterance_id_, event_type, char_index, std::string());
143 utterance_id_ = 0;
144 utterance_.clear();
147 // static
148 TtsPlatformImplAndroid* TtsPlatformImplAndroid::GetInstance() {
149 return Singleton<TtsPlatformImplAndroid,
150 LeakySingletonTraits<TtsPlatformImplAndroid> >::get();
153 // static
154 bool TtsPlatformImplAndroid::Register(JNIEnv* env) {
155 return RegisterNativesImpl(env);