eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

eSpeakActivity.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (C) 2012 Reece H. Dunn
  3. * Copyright (C) 2009 The Android Open Source Project
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.reecedunn.espeak;
  18. import android.app.Activity;
  19. import android.content.Intent;
  20. import android.os.Build;
  21. import android.os.Bundle;
  22. import android.os.Handler;
  23. import android.os.Message;
  24. import android.preference.PreferenceActivity;
  25. import android.speech.tts.TextToSpeech;
  26. import android.util.Log;
  27. import android.util.Pair;
  28. import android.view.Menu;
  29. import android.view.MenuInflater;
  30. import android.view.MenuItem;
  31. import android.view.View;
  32. import android.widget.ListView;
  33. import java.lang.ref.WeakReference;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. import java.util.Locale;
  37. public class eSpeakActivity extends Activity {
  38. private static final String ACTION_TTS_SETTINGS = "com.android.settings.TTS_SETTINGS";
  39. /** Handler code for TTS initialization hand-off. */
  40. private static final int TTS_INITIALIZED = 1;
  41. private static final int REQUEST_CHECK = 1;
  42. private static final int REQUEST_DOWNLOAD = 2;
  43. private static final int REQUEST_DEFAULT = 3;
  44. private static final String TAG = "eSpeakActivity";
  45. private enum State {
  46. LOADING,
  47. DOWNLOAD_FAILED,
  48. ERROR,
  49. SUCCESS
  50. }
  51. private State mState;
  52. private boolean mDownloadedVoiceData;
  53. private ArrayList<String> mVoices;
  54. private TextToSpeech mTts;
  55. private List<Pair<String,String>> mInformation;
  56. private InformationListAdapter mInformationView;
  57. @Override
  58. public void onCreate(Bundle savedInstanceState) {
  59. super.onCreate(savedInstanceState);
  60. setContentView(R.layout.main);
  61. mInformation = new ArrayList<Pair<String,String>>();
  62. mInformationView = new InformationListAdapter(this, mInformation);
  63. ((ListView)findViewById(R.id.properties)).setAdapter(mInformationView);
  64. setState(State.LOADING);
  65. checkVoiceData();
  66. }
  67. @Override
  68. public void onDestroy() {
  69. super.onDestroy();
  70. if (mTts != null) {
  71. mTts.shutdown();
  72. }
  73. }
  74. @Override
  75. public boolean onCreateOptionsMenu(Menu menu)
  76. {
  77. MenuInflater inflater = getMenuInflater();
  78. inflater.inflate(R.menu.options, menu);
  79. if (Build.VERSION.SDK_INT < 14) {
  80. // Hide the eSpeak setting menu item on pre-ICS.
  81. menu.findItem(R.id.espeakSettings).setVisible(false);
  82. }
  83. return true;
  84. }
  85. @Override
  86. public boolean onOptionsItemSelected(MenuItem item)
  87. {
  88. switch (item.getItemId())
  89. {
  90. case R.id.espeakSettings:
  91. startActivityForResult(new Intent(eSpeakActivity.this, TtsSettingsActivity.class), REQUEST_DEFAULT);
  92. return true;
  93. case R.id.ttsSettings:
  94. launchGeneralTtsSettings();
  95. return true;
  96. case R.id.updateVoices:
  97. downloadVoiceData();
  98. return true;
  99. }
  100. return super.onOptionsItemSelected(item);
  101. }
  102. /**
  103. * Sets the UI state.
  104. *
  105. * @param state The current state.
  106. */
  107. private void setState(State state) {
  108. mState = state;
  109. switch (mState)
  110. {
  111. case LOADING:
  112. findViewById(R.id.loading).setVisibility(View.VISIBLE);
  113. findViewById(R.id.success).setVisibility(View.GONE);
  114. break;
  115. default:
  116. findViewById(R.id.loading).setVisibility(View.GONE);
  117. findViewById(R.id.success).setVisibility(View.VISIBLE);
  118. break;
  119. }
  120. }
  121. /**
  122. * Launcher the voice data verifier.
  123. */
  124. private void checkVoiceData() {
  125. final Intent checkIntent = new Intent(this, CheckVoiceData.class);
  126. startActivityForResult(checkIntent, REQUEST_CHECK);
  127. }
  128. /**
  129. * Launches the voice data installer.
  130. */
  131. private void downloadVoiceData() {
  132. final Intent checkIntent = new Intent(this, DownloadVoiceData.class);
  133. startActivityForResult(checkIntent, REQUEST_DOWNLOAD);
  134. }
  135. /**
  136. * Initializes the TTS engine.
  137. */
  138. private void initializeEngine() {
  139. mTts = new TextToSpeech(this, mInitListener);
  140. }
  141. private void populateInformationView() {
  142. mInformation.clear();
  143. if (mTts != null) {
  144. Locale language = mTts.getLanguage();
  145. if (language != null) {
  146. final String currentLocale = getString(R.string.current_tts_locale);
  147. mInformation.add(new Pair<String,String>(currentLocale, mTts.getLanguage().getDisplayName()));
  148. }
  149. }
  150. final String availableVoices = getString(R.string.available_voices);
  151. if (mVoices == null) {
  152. mInformation.add(new Pair<String,String>(availableVoices, "0"));
  153. } else {
  154. mInformation.add(new Pair<String,String>(availableVoices, Integer.toString(mVoices.size())));
  155. }
  156. final String statusText;
  157. switch (mState) {
  158. case ERROR:
  159. statusText = getString(R.string.error_message);
  160. break;
  161. case DOWNLOAD_FAILED:
  162. statusText = getString(R.string.voice_data_failed_message);
  163. break;
  164. default:
  165. if (!getPackageName().equals(mTts.getDefaultEngine())) {
  166. statusText = getString(R.string.set_default_message);
  167. } else {
  168. statusText = null;
  169. }
  170. break;
  171. }
  172. if (statusText != null) {
  173. final String statusLabel = getString(R.string.status);
  174. mInformation.add(new Pair<String,String>(statusLabel, statusText));
  175. }
  176. mInformationView.notifyDataSetChanged();
  177. }
  178. /**
  179. * Handles the result of voice data verification. If verification fails
  180. * following a successful installation, displays an error dialog. Otherwise,
  181. * either launches the installer or attempts to initialize the TTS engine.
  182. *
  183. * @param resultCode The result of voice data verification.
  184. * @param data The intent containing available voices.
  185. */
  186. private void onDataChecked(int resultCode, Intent data) {
  187. if (resultCode != TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
  188. if (mDownloadedVoiceData) {
  189. Log.e(TAG, "Voice data check failed (error code: " + resultCode + ").");
  190. setState(State.ERROR);
  191. } else {
  192. downloadVoiceData();
  193. }
  194. return;
  195. }
  196. mVoices = data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES);
  197. initializeEngine();
  198. populateInformationView();
  199. }
  200. /**
  201. * Handles the result of voice data installation. Either shows a failure
  202. * dialog or launches the voice data verifier.
  203. *
  204. * @param resultCode
  205. */
  206. private void onDataDownloaded(int resultCode) {
  207. if (resultCode != RESULT_OK) {
  208. Log.e(TAG, "Voice data download failed.");
  209. setState(State.DOWNLOAD_FAILED);
  210. return;
  211. }
  212. mDownloadedVoiceData = true;
  213. checkVoiceData();
  214. }
  215. /**
  216. * Handles the result of TTS engine initialization. Either displays an error
  217. * dialog or populates the activity's UI.
  218. *
  219. * @param status The TTS engine initialization status.
  220. */
  221. private void onInitialized(int status) {
  222. if (status != TextToSpeech.SUCCESS) {
  223. Log.e(TAG, "Initialization failed (status: " + status + ").");
  224. setState(State.ERROR);
  225. } else {
  226. setState(State.SUCCESS);
  227. }
  228. populateInformationView();
  229. }
  230. @Override
  231. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  232. switch (requestCode) {
  233. case REQUEST_CHECK:
  234. onDataChecked(resultCode, data);
  235. break;
  236. case REQUEST_DOWNLOAD:
  237. onDataDownloaded(resultCode);
  238. break;
  239. case REQUEST_DEFAULT:
  240. initializeEngine();
  241. break;
  242. }
  243. super.onActivityResult(requestCode, resultCode, data);
  244. }
  245. private final TextToSpeech.OnInitListener mInitListener = new TextToSpeech.OnInitListener() {
  246. @Override
  247. public void onInit(int status) {
  248. mHandler.obtainMessage(TTS_INITIALIZED, status, 0).sendToTarget();
  249. }
  250. };
  251. private static class EspeakHandler extends Handler {
  252. private WeakReference<eSpeakActivity> mActivity;
  253. public EspeakHandler(eSpeakActivity activity)
  254. {
  255. mActivity = new WeakReference<eSpeakActivity>(activity);
  256. }
  257. @Override
  258. public void handleMessage(Message msg) {
  259. switch (msg.what) {
  260. case TTS_INITIALIZED:
  261. mActivity.get().onInitialized(msg.arg1);
  262. break;
  263. }
  264. }
  265. }
  266. private final Handler mHandler = new EspeakHandler(this);
  267. private void launchGeneralTtsSettings()
  268. {
  269. Intent intent;
  270. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  271. {
  272. // The Text-to-Speech settings is a Fragment on 3.x:
  273. intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
  274. intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, "com.android.settings.TextToSpeechSettings");
  275. intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, intent.getExtras());
  276. }
  277. else
  278. {
  279. // The Text-to-Speech settings is an Activity on 2.x and 4.x:
  280. intent = new Intent(ACTION_TTS_SETTINGS);
  281. }
  282. startActivityForResult(intent, REQUEST_DEFAULT);
  283. }
  284. }