Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / speech / tts_android.cc
blobbcc501c302ec72ddf3e312b0340a35101d9bbae3
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 Java_TtsPlatformImpl_destroy(env, java_ref_.obj());
36 bool TtsPlatformImplAndroid::PlatformImplAvailable() {
37 return true;
40 bool TtsPlatformImplAndroid::Speak(
41 int utterance_id,
42 const std::string& utterance,
43 const std::string& lang,
44 const VoiceData& voice,
45 const UtteranceContinuousParameters& params) {
46 JNIEnv* env = AttachCurrentThread();
47 jboolean success = Java_TtsPlatformImpl_speak(
48 env, java_ref_.obj(), utterance_id,
49 base::android::ConvertUTF8ToJavaString(env, utterance).obj(),
50 base::android::ConvertUTF8ToJavaString(env, lang).obj(), params.rate,
51 params.pitch, params.volume);
52 if (!success)
53 return false;
55 utterance_ = utterance;
56 utterance_id_ = utterance_id;
57 return true;
60 bool TtsPlatformImplAndroid::StopSpeaking() {
61 JNIEnv* env = AttachCurrentThread();
62 Java_TtsPlatformImpl_stop(env, java_ref_.obj());
63 utterance_id_ = 0;
64 utterance_.clear();
65 return true;
68 void TtsPlatformImplAndroid::Pause() {
69 StopSpeaking();
72 void TtsPlatformImplAndroid::Resume() {
75 bool TtsPlatformImplAndroid::IsSpeaking() {
76 return (utterance_id_ != 0);
79 void TtsPlatformImplAndroid::GetVoices(
80 std::vector<VoiceData>* out_voices) {
81 JNIEnv* env = AttachCurrentThread();
82 if (!Java_TtsPlatformImpl_isInitialized(env, java_ref_.obj()))
83 return;
85 int count = Java_TtsPlatformImpl_getVoiceCount(env, java_ref_.obj());
86 for (int i = 0; i < count; ++i) {
87 out_voices->push_back(VoiceData());
88 VoiceData& data = out_voices->back();
89 data.native = true;
90 data.name = base::android::ConvertJavaStringToUTF8(
91 Java_TtsPlatformImpl_getVoiceName(env, java_ref_.obj(), i));
92 data.lang = base::android::ConvertJavaStringToUTF8(
93 Java_TtsPlatformImpl_getVoiceLanguage(env, java_ref_.obj(), i));
94 data.gender = TTS_GENDER_NONE;
95 data.events.insert(TTS_EVENT_START);
96 data.events.insert(TTS_EVENT_END);
97 data.events.insert(TTS_EVENT_ERROR);
101 void TtsPlatformImplAndroid::VoicesChanged(JNIEnv* env, jobject obj) {
102 TtsController::GetInstance()->VoicesChanged();
105 void TtsPlatformImplAndroid::OnEndEvent(
106 JNIEnv* env, jobject obj, jint utterance_id) {
107 SendFinalTtsEvent(utterance_id, TTS_EVENT_END,
108 static_cast<int>(utterance_.size()));
111 void TtsPlatformImplAndroid::OnErrorEvent(
112 JNIEnv* env, jobject obj, jint utterance_id) {
113 SendFinalTtsEvent(utterance_id, TTS_EVENT_ERROR, 0);
116 void TtsPlatformImplAndroid::OnStartEvent(
117 JNIEnv* env, jobject obj, jint utterance_id) {
118 if (utterance_id != utterance_id_)
119 return;
121 TtsController::GetInstance()->OnTtsEvent(
122 utterance_id_, TTS_EVENT_START, 0, std::string());
125 void TtsPlatformImplAndroid::SendFinalTtsEvent(
126 int utterance_id, TtsEventType event_type, int char_index) {
127 if (utterance_id != utterance_id_)
128 return;
130 TtsController::GetInstance()->OnTtsEvent(
131 utterance_id_, event_type, char_index, std::string());
132 utterance_id_ = 0;
133 utterance_.clear();
136 // static
137 TtsPlatformImplAndroid* TtsPlatformImplAndroid::GetInstance() {
138 return base::Singleton<
139 TtsPlatformImplAndroid,
140 base::LeakySingletonTraits<TtsPlatformImplAndroid>>::get();
143 // static
144 bool TtsPlatformImplAndroid::Register(JNIEnv* env) {
145 return RegisterNativesImpl(env);