Fixed that emergency alert text-to-speech does not work
For ETWS alert, we used to only support text-to-speech
in Japanese language. Now added support for more languages.
Test: Manual
Bug: 117590557
Merged-In: I3447b59162897f7d5d9cc1c9f7c9a07991c8334d
Change-Id: I3447b59162897f7d5d9cc1c9f7c9a07991c8334d
(cherry picked from commit b224caa8857135ba4074868302dad97300304fcc)
diff --git a/res/raw-zh-rTW b/res/raw-zh-rTW
new file mode 120000
index 0000000..cef6542
--- /dev/null
+++ b/res/raw-zh-rTW
@@ -0,0 +1 @@
+raw-zh
\ No newline at end of file
diff --git a/src/com/android/cellbroadcastreceiver/CellBroadcastAlertAudio.java b/src/com/android/cellbroadcastreceiver/CellBroadcastAlertAudio.java
index 4bf3c79..abb3235 100644
--- a/src/com/android/cellbroadcastreceiver/CellBroadcastAlertAudio.java
+++ b/src/com/android/cellbroadcastreceiver/CellBroadcastAlertAudio.java
@@ -17,7 +17,6 @@
package com.android.cellbroadcastreceiver;
import static com.android.cellbroadcastreceiver.CellBroadcastReceiver.DBG;
-import static com.android.cellbroadcastreceiver.CellBroadcastReceiver.VDBG;
import android.app.Service;
import android.content.Context;
@@ -31,7 +30,6 @@
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
-import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
@@ -42,12 +40,12 @@
import android.speech.tts.TextToSpeech;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
+import android.text.TextUtils;
import android.util.Log;
import com.android.cellbroadcastreceiver.CellBroadcastAlertService.AlertType;
import java.util.Locale;
-import java.util.MissingResourceException;
/**
* Manages alert audio and vibration and text-to-speech. Runs as a service so that
@@ -65,13 +63,8 @@
"com.android.cellbroadcastreceiver.ALERT_AUDIO_MESSAGE_BODY";
/** Extra for text-to-speech preferred language (if speech enabled in settings). */
- public static final String ALERT_AUDIO_MESSAGE_PREFERRED_LANGUAGE =
- "com.android.cellbroadcastreceiver.ALERT_AUDIO_MESSAGE_PREFERRED_LANGUAGE";
-
- /** Extra for text-to-speech default language when preferred language is
- not available (if speech enabled in settings). */
- public static final String ALERT_AUDIO_MESSAGE_DEFAULT_LANGUAGE =
- "com.android.cellbroadcastreceiver.ALERT_AUDIO_MESSAGE_DEFAULT_LANGUAGE";
+ public static final String ALERT_AUDIO_MESSAGE_LANGUAGE =
+ "com.android.cellbroadcastreceiver.ALERT_AUDIO_MESSAGE_LANGUAGE";
/** Extra for alert tone type */
public static final String ALERT_AUDIO_TONE_TYPE =
@@ -97,8 +90,7 @@
private boolean mTtsEngineReady;
private String mMessageBody;
- private String mMessagePreferredLanguage;
- private String mMessageDefaultLanguage;
+ private String mMessageLanguage;
private boolean mTtsLanguageSupported;
private boolean mEnableVibrate;
private boolean mEnableAudio;
@@ -145,16 +137,8 @@
if (mMessageBody != null && mTtsEngineReady && mTtsLanguageSupported) {
if (DBG) log("Speaking broadcast text: " + mMessageBody);
- Bundle params = new Bundle();
- // Play TTS in the alarm stream, which we use for playing alert tones as
- // well.
- params.putInt(TextToSpeech.Engine.KEY_PARAM_STREAM,
- AudioManager.STREAM_ALARM);
- // Use the non-public parameter 2 --> TextToSpeech.QUEUE_DESTROY for TTS.
- // The entire playback queue is purged. This is different from QUEUE_FLUSH
- // in that all entries are purged, not just entries from a given caller.
- // This is for emergency so we want to kill all other TTS sessions.
- res = mTts.speak(mMessageBody, 2, params, TTS_UTTERANCE_ID);
+ mTts.setAudioAttributes(getAlertAudioAttributes());
+ res = mTts.speak(mMessageBody, 2, null, TTS_UTTERANCE_ID);
mState = STATE_SPEAKING;
}
if (res != TextToSpeech.SUCCESS) {
@@ -187,7 +171,7 @@
*/
@Override
public void onInit(int status) {
- if (VDBG) log("onInit() TTS engine status: " + status);
+ if (DBG) log("onInit() TTS engine status: " + status);
if (status == TextToSpeech.SUCCESS) {
mTtsEngineReady = true;
mTts.setOnUtteranceCompletedListener(this);
@@ -205,31 +189,20 @@
* it to the default language. mTtsLanguageSupported will be updated based on the response.
*/
private void setTtsLanguage() {
-
- String language = mMessagePreferredLanguage;
- if (language == null || language.isEmpty() ||
- TextToSpeech.LANG_AVAILABLE != mTts.isLanguageAvailable(new Locale(language))) {
- language = mMessageDefaultLanguage;
- if (language == null || language.isEmpty() ||
- TextToSpeech.LANG_AVAILABLE != mTts.isLanguageAvailable(new Locale(language))) {
- mTtsLanguageSupported = false;
- return;
- }
- if (DBG) log("Language '" + mMessagePreferredLanguage + "' is not available, using" +
- "the default language '" + mMessageDefaultLanguage + "'");
+ Locale locale;
+ if (!TextUtils.isEmpty(mMessageLanguage)) {
+ locale = new Locale(mMessageLanguage);
+ } else {
+ // If the cell broadcast message does not specify the language, use device's default
+ // language.
+ locale = Locale.getDefault();
}
- if (DBG) log("Setting TTS language to '" + language + '\'');
+ if (DBG) log("Setting TTS language to '" + locale + '\'');
- try {
- int result = mTts.setLanguage(new Locale(language));
- if (DBG) log("TTS setLanguage() returned: " + result);
- mTtsLanguageSupported = (result == TextToSpeech.LANG_AVAILABLE);
- }
- catch (MissingResourceException e) {
- mTtsLanguageSupported = false;
- loge("Language '" + language + "' is not available.");
- }
+ int result = mTts.setLanguage(locale);
+ if (DBG) log("TTS setLanguage() returned: " + result);
+ mTtsLanguageSupported = (result >= TextToSpeech.LANG_AVAILABLE);
}
/**
@@ -298,8 +271,7 @@
// Get text to speak (if enabled by user)
mMessageBody = intent.getStringExtra(ALERT_AUDIO_MESSAGE_BODY);
- mMessagePreferredLanguage = intent.getStringExtra(ALERT_AUDIO_MESSAGE_PREFERRED_LANGUAGE);
- mMessageDefaultLanguage = intent.getStringExtra(ALERT_AUDIO_MESSAGE_DEFAULT_LANGUAGE);
+ mMessageLanguage = intent.getStringExtra(ALERT_AUDIO_MESSAGE_LANGUAGE);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
@@ -464,9 +436,7 @@
// Request audio focus (though we're going to play even if we don't get it)
mAudioManager.requestAudioFocus(null, AudioManager.STREAM_ALARM,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
- mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
-
- setAlertAudioAttributes();
+ mMediaPlayer.setAudioAttributes(getAlertAudioAttributes());
setAlertVolume();
// If we are using the custom alert duration, set looping to true so we can repeat
@@ -542,10 +512,9 @@
}
/**
- * Set AudioAttributes for mMediaPlayer. Replacement of deprecated
- * mMediaPlayer.setAudioStreamType.
+ * Get audio attribute for the alarm.
*/
- private void setAlertAudioAttributes() {
+ private AudioAttributes getAlertAudioAttributes() {
AudioAttributes.Builder builder = new AudioAttributes.Builder();
builder.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION);
@@ -557,7 +526,7 @@
| AudioAttributes.FLAG_BYPASS_MUTE);
}
- mMediaPlayer.setAudioAttributes(builder.build());
+ return builder.build();
}
/**
diff --git a/src/com/android/cellbroadcastreceiver/CellBroadcastAlertService.java b/src/com/android/cellbroadcastreceiver/CellBroadcastAlertService.java
index a6e3e96..c779043 100644
--- a/src/com/android/cellbroadcastreceiver/CellBroadcastAlertService.java
+++ b/src/com/android/cellbroadcastreceiver/CellBroadcastAlertService.java
@@ -588,34 +588,11 @@
if (prefs.getBoolean(CellBroadcastSettings.KEY_ENABLE_ALERT_SPEECH, true)) {
audioIntent.putExtra(CellBroadcastAlertAudio.ALERT_AUDIO_MESSAGE_BODY, messageBody);
- String preferredLanguage = message.getLanguageCode();
- String defaultLanguage = null;
- if (message.isEtwsMessage()) {
- // Only do TTS for ETWS secondary message.
- // There is no text in ETWS primary message. When we construct the ETWS primary
- // message, we hardcode "ETWS" as the body hence we don't want to speak that out
- // here.
+ String language = message.getLanguageCode();
- // Also in many cases we see the secondary message comes few milliseconds after
- // the primary one. If we play TTS for the primary one, It will be overwritten by
- // the secondary one immediately anyway.
- if (!message.getEtwsWarningInfo().isPrimary()) {
- // Since only Japanese carriers are using ETWS, if there is no language
- // specified in the ETWS message, we'll use Japanese as the default language.
- defaultLanguage = "ja";
- }
- } else {
- // If there is no language specified in the CMAS message, use device's
- // default language.
- defaultLanguage = Locale.getDefault().getLanguage();
- }
-
- Log.d(TAG, "Preferred language = " + preferredLanguage +
- ", Default language = " + defaultLanguage);
- audioIntent.putExtra(CellBroadcastAlertAudio.ALERT_AUDIO_MESSAGE_PREFERRED_LANGUAGE,
- preferredLanguage);
- audioIntent.putExtra(CellBroadcastAlertAudio.ALERT_AUDIO_MESSAGE_DEFAULT_LANGUAGE,
- defaultLanguage);
+ Log.d(TAG, "Message language = " + language);
+ audioIntent.putExtra(CellBroadcastAlertAudio.ALERT_AUDIO_MESSAGE_LANGUAGE,
+ language);
}
startService(audioIntent);