Commit 3a18f195 authored by dmazzoni's avatar dmazzoni Committed by Commit bot

Update TtsPlatformImpl to support Lollipop SDK.

BUG=405174

Review URL: https://codereview.chromium.org/923173003

Cr-Commit-Position: refs/heads/master@{#318075}
parent 96dd77c5
// Copyright 2015 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.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
/**
* Subclass of TtsPlatformImpl for Lollipop to make use of newer APIs.
*/
class LollipopTtsPlatformImpl extends TtsPlatformImpl {
protected LollipopTtsPlatformImpl(long nativeTtsPlatformImplAndroid, Context context) {
super(nativeTtsPlatformImplAndroid, context);
}
/**
* Overrides TtsPlatformImpl because the API changed in Lollipop.
*/
@Override
protected void addOnUtteranceProgressListener() {
mTextToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onDone(final String utteranceId) {
sendEndEventOnUiThread(utteranceId);
}
@Override
public void onError(final String utteranceId, int errorCode) {
sendErrorEventOnUiThread(utteranceId);
}
@Override
@Deprecated
public void onError(final String utteranceId) {
}
@Override
public void onStart(final String utteranceId) {
sendStartEventOnUiThread(utteranceId);
}
});
}
/**
* Overrides TtsPlatformImpl because the API changed in Lollipop.
*/
@Override
protected int callSpeak(String text, float volume, int utteranceId) {
Bundle params = new Bundle();
if (volume != 1.0) {
params.putFloat(TextToSpeech.Engine.KEY_PARAM_VOLUME, volume);
}
return mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, params,
Integer.toString(utteranceId));
}
}
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package org.chromium.chrome.browser; package org.chromium.chrome.browser;
import android.content.Context; import android.content.Context;
import android.os.Build;
import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener; import android.speech.tts.UtteranceProgressListener;
...@@ -61,13 +62,13 @@ class TtsPlatformImpl { ...@@ -61,13 +62,13 @@ class TtsPlatformImpl {
} }
private long mNativeTtsPlatformImplAndroid; private long mNativeTtsPlatformImplAndroid;
private final TextToSpeech mTextToSpeech; protected final TextToSpeech mTextToSpeech;
private boolean mInitialized; private boolean mInitialized;
private ArrayList<TtsVoice> mVoices; private ArrayList<TtsVoice> mVoices;
private String mCurrentLanguage; private String mCurrentLanguage;
private PendingUtterance mPendingUtterance; private PendingUtterance mPendingUtterance;
private TtsPlatformImpl(long nativeTtsPlatformImplAndroid, Context context) { protected TtsPlatformImpl(long nativeTtsPlatformImplAndroid, Context context) {
mInitialized = false; mInitialized = false;
mNativeTtsPlatformImplAndroid = nativeTtsPlatformImplAndroid; mNativeTtsPlatformImplAndroid = nativeTtsPlatformImplAndroid;
mTextToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() { mTextToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
...@@ -83,46 +84,7 @@ class TtsPlatformImpl { ...@@ -83,46 +84,7 @@ class TtsPlatformImpl {
} }
} }
}); });
mTextToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() { addOnUtteranceProgressListener();
@Override
public void onDone(final String utteranceId) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mNativeTtsPlatformImplAndroid != 0) {
nativeOnEndEvent(mNativeTtsPlatformImplAndroid,
Integer.parseInt(utteranceId));
}
}
});
}
@Override
public void onError(final String utteranceId) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mNativeTtsPlatformImplAndroid != 0) {
nativeOnErrorEvent(mNativeTtsPlatformImplAndroid,
Integer.parseInt(utteranceId));
}
}
});
}
@Override
public void onStart(final String utteranceId) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mNativeTtsPlatformImplAndroid != 0) {
nativeOnStartEvent(mNativeTtsPlatformImplAndroid,
Integer.parseInt(utteranceId));
}
}
});
}
});
} }
/** /**
...@@ -135,7 +97,11 @@ class TtsPlatformImpl { ...@@ -135,7 +97,11 @@ class TtsPlatformImpl {
@CalledByNative @CalledByNative
private static TtsPlatformImpl create(long nativeTtsPlatformImplAndroid, private static TtsPlatformImpl create(long nativeTtsPlatformImplAndroid,
Context context) { Context context) {
return new TtsPlatformImpl(nativeTtsPlatformImplAndroid, context); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return new LollipopTtsPlatformImpl(nativeTtsPlatformImplAndroid, context);
} else {
return new TtsPlatformImpl(nativeTtsPlatformImplAndroid, context);
}
} }
/** /**
...@@ -213,12 +179,8 @@ class TtsPlatformImpl { ...@@ -213,12 +179,8 @@ class TtsPlatformImpl {
mTextToSpeech.setSpeechRate(rate); mTextToSpeech.setSpeechRate(rate);
mTextToSpeech.setPitch(pitch); mTextToSpeech.setPitch(pitch);
HashMap<String, String> params = new HashMap<String, String>();
if (volume != 1.0) { int result = callSpeak(text, volume, utteranceId);
params.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, Double.toString(volume));
}
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, Integer.toString(utteranceId));
int result = mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, params);
return (result == TextToSpeech.SUCCESS); return (result == TextToSpeech.SUCCESS);
} }
...@@ -231,6 +193,89 @@ class TtsPlatformImpl { ...@@ -231,6 +193,89 @@ class TtsPlatformImpl {
if (mPendingUtterance != null) mPendingUtterance = null; if (mPendingUtterance != null) mPendingUtterance = null;
} }
/**
* Post a task to the UI thread to send the TTS "end" event.
*/
protected void sendEndEventOnUiThread(final String utteranceId) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mNativeTtsPlatformImplAndroid != 0) {
nativeOnEndEvent(mNativeTtsPlatformImplAndroid, Integer.parseInt(utteranceId));
}
}
});
}
/**
* Post a task to the UI thread to send the TTS "error" event.
*/
protected void sendErrorEventOnUiThread(final String utteranceId) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mNativeTtsPlatformImplAndroid != 0) {
nativeOnErrorEvent(mNativeTtsPlatformImplAndroid,
Integer.parseInt(utteranceId));
}
}
});
}
/**
* Post a task to the UI thread to send the TTS "start" event.
*/
protected void sendStartEventOnUiThread(final String utteranceId) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mNativeTtsPlatformImplAndroid != 0) {
nativeOnStartEvent(mNativeTtsPlatformImplAndroid,
Integer.parseInt(utteranceId));
}
}
});
}
/**
* This is overridden by LollipopTtsPlatformImpl because the API changed.
*/
@SuppressWarnings("deprecation")
protected void addOnUtteranceProgressListener() {
mTextToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onDone(final String utteranceId) {
sendEndEventOnUiThread(utteranceId);
}
// This is deprecated in Lollipop and higher but we still need to catch it
// on pre-Lollipop builds.
@Override
@SuppressWarnings("deprecation")
public void onError(final String utteranceId) {
sendErrorEventOnUiThread(utteranceId);
}
@Override
public void onStart(final String utteranceId) {
sendStartEventOnUiThread(utteranceId);
}
});
}
/**
* This is overridden by LollipopTtsPlatformImpl because the API changed.
*/
@SuppressWarnings("deprecation")
protected int callSpeak(String text, float volume, int utteranceId) {
HashMap<String, String> params = new HashMap<String, String>();
if (volume != 1.0) {
params.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, Double.toString(volume));
}
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, Integer.toString(utteranceId));
return mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, params);
}
/** /**
* Note: we enforce that this method is called on the UI thread, so * Note: we enforce that this method is called on the UI thread, so
* we can call nativeVoicesChanged directly. * we can call nativeVoicesChanged directly.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment