PageRenderTime 70ms CodeModel.GetById 38ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 1ms

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