Alert for using network-only-tts with no network.
If TTS requires network for selected language, we will show dialog informing why we can't synthesize an example if user have no internet connectivity. Bug: 7398316 Change-Id: I01b033a9a8b8538b322ed3639b671f90f849ae29
This commit is contained in:
committed by
Android (Google) Code Review
parent
6e16e83326
commit
03b9f86722
@@ -3438,6 +3438,9 @@
|
|||||||
all the text that will be spoken, including personal data like passwords and credit
|
all the text that will be spoken, including personal data like passwords and credit
|
||||||
card numbers. It comes from the <xliff:g id="tts_plugin_engine_name">%s</xliff:g> engine.
|
card numbers. It comes from the <xliff:g id="tts_plugin_engine_name">%s</xliff:g> engine.
|
||||||
Enable the use of this speech synthesis engine?</string>
|
Enable the use of this speech synthesis engine?</string>
|
||||||
|
<!-- Warning message about required internet conectivity for TTS synthesis, displayed as a dialog
|
||||||
|
message when the user selects to play an example for network only locale and there's no internet connectivity. -->
|
||||||
|
<string name="tts_engine_network_required">This language requires a working network connection for text-to-speech output.</string>
|
||||||
<!-- On main TTS Settings screen, text for divider under which all TTS engines are listed -->
|
<!-- On main TTS Settings screen, text for divider under which all TTS engines are listed -->
|
||||||
<string name="tts_engines_section">Engines</string>
|
<string name="tts_engines_section">Engines</string>
|
||||||
<!-- On main TTS Settings screen, text preceded by the TTS engine name, clicking this button will launch the engine settings -->
|
<!-- On main TTS Settings screen, text preceded by the TTS engine name, clicking this button will launch the engine settings -->
|
||||||
|
@@ -36,14 +36,17 @@ import android.preference.PreferenceCategory;
|
|||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.provider.Settings.SettingNotFoundException;
|
import android.provider.Settings.SettingNotFoundException;
|
||||||
import android.speech.tts.TextToSpeech;
|
import android.speech.tts.TextToSpeech;
|
||||||
|
import android.speech.tts.UtteranceProgressListener;
|
||||||
import android.speech.tts.TextToSpeech.EngineInfo;
|
import android.speech.tts.TextToSpeech.EngineInfo;
|
||||||
import android.speech.tts.TtsEngines;
|
import android.speech.tts.TtsEngines;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.Checkable;
|
import android.widget.Checkable;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class TextToSpeechSettings extends SettingsPreferenceFragment implements
|
public class TextToSpeechSettings extends SettingsPreferenceFragment implements
|
||||||
Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener,
|
Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener,
|
||||||
@@ -137,9 +140,28 @@ public class TextToSpeechSettings extends SettingsPreferenceFragment implements
|
|||||||
mTts = new TextToSpeech(getActivity().getApplicationContext(), mInitListener);
|
mTts = new TextToSpeech(getActivity().getApplicationContext(), mInitListener);
|
||||||
mEnginesHelper = new TtsEngines(getActivity().getApplicationContext());
|
mEnginesHelper = new TtsEngines(getActivity().getApplicationContext());
|
||||||
|
|
||||||
|
setTtsUtteranceProgressListener();
|
||||||
initSettings();
|
initSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setTtsUtteranceProgressListener() {
|
||||||
|
if (mTts == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mTts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
|
||||||
|
@Override
|
||||||
|
public void onStart(String utteranceId) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDone(String utteranceId) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(String utteranceId) {
|
||||||
|
Log.e(TAG, "Error while trying to synthesize sample text");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
@@ -258,6 +280,12 @@ public class TextToSpeechSettings extends SettingsPreferenceFragment implements
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isNetworkRequiredForSynthesis() {
|
||||||
|
Set<String> features = mTts.getFeatures(mTts.getLanguage());
|
||||||
|
return features.contains(TextToSpeech.Engine.KEY_FEATURE_NETWORK_SYNTHESIS) &&
|
||||||
|
!features.contains(TextToSpeech.Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS);
|
||||||
|
}
|
||||||
|
|
||||||
private void onSampleTextReceived(int resultCode, Intent data) {
|
private void onSampleTextReceived(int resultCode, Intent data) {
|
||||||
String sample = getDefaultSampleString();
|
String sample = getDefaultSampleString();
|
||||||
|
|
||||||
@@ -273,7 +301,18 @@ public class TextToSpeechSettings extends SettingsPreferenceFragment implements
|
|||||||
if (sample != null && mTts != null) {
|
if (sample != null && mTts != null) {
|
||||||
// The engine is guaranteed to have been initialized here
|
// The engine is guaranteed to have been initialized here
|
||||||
// because this preference is not enabled otherwise.
|
// because this preference is not enabled otherwise.
|
||||||
mTts.speak(sample, TextToSpeech.QUEUE_FLUSH, null);
|
|
||||||
|
final boolean networkRequired = isNetworkRequiredForSynthesis();
|
||||||
|
if (!networkRequired || networkRequired &&
|
||||||
|
(mTts.isLanguageAvailable(mTts.getLanguage()) >= TextToSpeech.LANG_AVAILABLE)) {
|
||||||
|
HashMap<String, String> params = new HashMap<String, String>();
|
||||||
|
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "Sample");
|
||||||
|
|
||||||
|
mTts.speak(sample, TextToSpeech.QUEUE_FLUSH, params);
|
||||||
|
} else {
|
||||||
|
Log.w(TAG, "Network required for sample synthesis for requested language");
|
||||||
|
displayNetworkAlert();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// TODO: Display an error here to the user.
|
// TODO: Display an error here to the user.
|
||||||
Log.e(TAG, "Did not have a sample string for the requested language");
|
Log.e(TAG, "Did not have a sample string for the requested language");
|
||||||
@@ -340,6 +379,18 @@ public class TextToSpeechSettings extends SettingsPreferenceFragment implements
|
|||||||
dialog.show();
|
dialog.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void displayNetworkAlert() {
|
||||||
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||||
|
builder.setTitle(android.R.string.dialog_alert_title);
|
||||||
|
builder.setIconAttribute(android.R.attr.alertDialogIcon);
|
||||||
|
builder.setMessage(getActivity().getString(R.string.tts_engine_network_required));
|
||||||
|
builder.setCancelable(false);
|
||||||
|
builder.setPositiveButton(android.R.string.ok, null);
|
||||||
|
|
||||||
|
AlertDialog dialog = builder.create();
|
||||||
|
dialog.show();
|
||||||
|
}
|
||||||
|
|
||||||
private void updateDefaultEngine(String engine) {
|
private void updateDefaultEngine(String engine) {
|
||||||
if (DBG) Log.d(TAG, "Updating default synth to : " + engine);
|
if (DBG) Log.d(TAG, "Updating default synth to : " + engine);
|
||||||
|
|
||||||
@@ -369,6 +420,7 @@ public class TextToSpeechSettings extends SettingsPreferenceFragment implements
|
|||||||
// the app binds successfully to the engine.
|
// the app binds successfully to the engine.
|
||||||
if (DBG) Log.d(TAG, "Updating engine : Attempting to connect to engine: " + engine);
|
if (DBG) Log.d(TAG, "Updating engine : Attempting to connect to engine: " + engine);
|
||||||
mTts = new TextToSpeech(getActivity().getApplicationContext(), mUpdateListener, engine);
|
mTts = new TextToSpeech(getActivity().getApplicationContext(), mUpdateListener, engine);
|
||||||
|
setTtsUtteranceProgressListener();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -390,6 +442,7 @@ public class TextToSpeechSettings extends SettingsPreferenceFragment implements
|
|||||||
// null if the previous bind to this engine failed.
|
// null if the previous bind to this engine failed.
|
||||||
mTts = new TextToSpeech(getActivity().getApplicationContext(), mInitListener,
|
mTts = new TextToSpeech(getActivity().getApplicationContext(), mInitListener,
|
||||||
mPreviousEngine);
|
mPreviousEngine);
|
||||||
|
setTtsUtteranceProgressListener();
|
||||||
}
|
}
|
||||||
mPreviousEngine = null;
|
mPreviousEngine = null;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user