/tts/src/com/google/tts/OldPrefsActivity.java

http://eyes-free.googlecode.com/ · Java · 200 lines · 175 code · 20 blank · 5 comment · 10 complexity · f63351479db1c4b1839b0105e5b91659 MD5 · raw file

  1. package com.google.tts;
  2. import java.util.HashMap;
  3. import android.content.ComponentName;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.content.pm.PackageManager;
  7. import android.content.pm.ResolveInfo;
  8. import android.media.AudioManager;
  9. import android.net.Uri;
  10. import android.os.Bundle;
  11. import android.preference.ListPreference;
  12. import android.preference.Preference;
  13. import android.preference.PreferenceActivity;
  14. import android.preference.PreferenceManager;
  15. import android.preference.Preference.OnPreferenceChangeListener;
  16. import android.preference.Preference.OnPreferenceClickListener;
  17. import android.view.Menu;
  18. import android.view.MenuItem;
  19. public class OldPrefsActivity extends PreferenceActivity {
  20. private static final int TTS_VOICE_DATA_CHECK_CODE = 42;
  21. private static final int TTS_VOICE_DATA_INSTALL_CODE = 43;
  22. private TTS myTts;
  23. private HashMap<String, Integer> hellos;
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setVolumeControlStream(AudioManager.STREAM_MUSIC);
  28. myTts = new TTS(this, ttsInitListener, true);
  29. }
  30. private TTS.InitListener ttsInitListener = new TTS.InitListener() {
  31. public void onInit(int version) {
  32. addPreferencesFromResource(R.xml.oldprefs);
  33. loadEngines();
  34. loadHellos();
  35. Preference previewPref = findPreference("preview");
  36. previewPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
  37. public boolean onPreferenceClick(Preference preference) {
  38. sayHello();
  39. return true;
  40. }
  41. });
  42. }
  43. };
  44. private void loadEngines() {
  45. ListPreference enginesPref = (ListPreference) findPreference("engine_pref");
  46. Intent intent = new Intent("android.intent.action.START_TTS_ENGINE");
  47. ResolveInfo[] enginesArray = new ResolveInfo[0];
  48. PackageManager pm = getPackageManager();
  49. enginesArray = pm.queryIntentActivities(intent, 0).toArray(enginesArray);
  50. CharSequence entries[] = new CharSequence[enginesArray.length];
  51. CharSequence values[] = new CharSequence[enginesArray.length];
  52. for (int i = 0; i < enginesArray.length; i++) {
  53. entries[i] = enginesArray[i].loadLabel(pm);
  54. values[i] = enginesArray[i].activityInfo.packageName;
  55. }
  56. enginesPref.setEntries(entries);
  57. enginesPref.setEntryValues(values);
  58. // This is somewhat hacky because the eSpeak engine isn't fully ported
  59. // over yet. In addition, the framework has an Install Data option, so the
  60. // workflow is different. Therefore, do NOT take this into the framework!
  61. enginesPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  62. @Override
  63. public boolean onPreferenceChange(Preference preference, Object newValue) {
  64. String chosenEngine = newValue.toString();
  65. if (chosenEngine.equals("com.svox.pico")) {
  66. Intent intent = new Intent();
  67. intent.setAction(TextToSpeechBeta.Engine.ACTION_CHECK_TTS_DATA);
  68. intent.setClassName("com.svox.pico", "com.svox.pico.CheckVoiceData");
  69. startActivityForResult(intent, TTS_VOICE_DATA_CHECK_CODE);
  70. }
  71. return true;
  72. }
  73. });
  74. }
  75. private void loadHellos() {
  76. hellos = new HashMap<String, Integer>();
  77. hellos.put("afr", R.string.af);
  78. hellos.put("bos", R.string.bs);
  79. hellos.put("yue", R.string.zhrHK);
  80. hellos.put("cmn", R.string.zh);
  81. hellos.put("hrv", R.string.hr);
  82. hellos.put("ces", R.string.cz);
  83. hellos.put("nld", R.string.nl);
  84. hellos.put("eng-USA", R.string.enrUS);
  85. hellos.put("eng-GBR", R.string.enrGB);
  86. hellos.put("epo", R.string.eo);
  87. hellos.put("fin", R.string.fi);
  88. hellos.put("fra", R.string.fr);
  89. hellos.put("deu", R.string.de);
  90. hellos.put("ell", R.string.el);
  91. hellos.put("hin", R.string.hi);
  92. hellos.put("hun", R.string.hu);
  93. hellos.put("isl", R.string.is);
  94. hellos.put("ind", R.string.id);
  95. hellos.put("ita", R.string.it);
  96. hellos.put("kur", R.string.ku);
  97. hellos.put("lat", R.string.la);
  98. hellos.put("mkd", R.string.mk);
  99. hellos.put("nor", R.string.no);
  100. hellos.put("pol", R.string.pl);
  101. hellos.put("por", R.string.pt);
  102. hellos.put("ron", R.string.ro);
  103. hellos.put("rus", R.string.ru);
  104. hellos.put("srp", R.string.sr);
  105. hellos.put("slk", R.string.sk);
  106. hellos.put("spa", R.string.es);
  107. hellos.put("spa-MEX", R.string.esrMX);
  108. hellos.put("swe", R.string.sw);
  109. hellos.put("swe", R.string.sv);
  110. hellos.put("tam", R.string.ta);
  111. hellos.put("tur", R.string.tr);
  112. hellos.put("vie", R.string.vi);
  113. hellos.put("cym", R.string.cy);
  114. }
  115. private void sayHello() {
  116. SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  117. // Change this to the system/lib path when in the framework
  118. String DEFAULT_TTS_BINARY = "com.svox.pico";
  119. String engine = prefs.getString("engine_pref", DEFAULT_TTS_BINARY);
  120. myTts.setEngine(engine);
  121. String languageCode = prefs.getString("lang_pref", "eng-USA");
  122. int rate = Integer.parseInt(prefs.getString("rate_pref", "140"));
  123. myTts.setLanguage(languageCode);
  124. myTts.setSpeechRate(rate);
  125. if (!hellos.containsKey(languageCode)) {
  126. languageCode = "eng-USA";
  127. }
  128. String hello = getString(hellos.get(languageCode));
  129. myTts.speak(hello, 0, null);
  130. }
  131. @Override
  132. public boolean onCreateOptionsMenu(Menu menu) {
  133. menu.add(0, R.string.tts_apps, 0, R.string.tts_apps).setIcon(android.R.drawable.ic_menu_search);
  134. menu.add(0, R.string.homepage, 0, R.string.homepage).setIcon(
  135. android.R.drawable.ic_menu_info_details);
  136. return super.onCreateOptionsMenu(menu);
  137. }
  138. @Override
  139. public boolean onOptionsItemSelected(MenuItem item) {
  140. Intent i = new Intent();
  141. ComponentName comp =
  142. new ComponentName("com.android.browser", "com.android.browser.BrowserActivity");
  143. i.setComponent(comp);
  144. i.setAction("android.intent.action.VIEW");
  145. i.addCategory("android.intent.category.BROWSABLE");
  146. Uri uri;
  147. switch (item.getItemId()) {
  148. case R.string.tts_apps:
  149. uri = Uri.parse("http://eyes-free.googlecode.com/svn/trunk/documentation/tts_apps.html");
  150. i.setData(uri);
  151. startActivity(i);
  152. break;
  153. case R.string.homepage:
  154. uri = Uri.parse("http://eyes-free.googlecode.com/");
  155. i.setData(uri);
  156. startActivity(i);
  157. break;
  158. }
  159. return super.onOptionsItemSelected(item);
  160. }
  161. // TODO: This should be generic, not hardcoded to the SVOX installer.
  162. @Override
  163. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  164. if (requestCode == TTS_VOICE_DATA_CHECK_CODE) {
  165. if (resultCode != TextToSpeechBeta.Engine.CHECK_VOICE_DATA_PASS) {
  166. Intent intent = new Intent();
  167. intent.setAction(TextToSpeechBeta.Engine.ACTION_INSTALL_TTS_DATA);
  168. intent.setClassName("com.svox.pico", "com.svox.pico.DownloadVoiceData");
  169. startActivityForResult(intent, TTS_VOICE_DATA_INSTALL_CODE);
  170. }
  171. }
  172. }
  173. @Override
  174. protected void onDestroy() {
  175. if (myTts != null) {
  176. myTts.shutdown();
  177. }
  178. super.onDestroy();
  179. }
  180. }