| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package com.reecedunn.espeak; |
| 19 |
|
| 20 |
import android.app.Activity; |
| 21 |
import android.content.BroadcastReceiver; |
| 22 |
import android.content.Context; |
| 23 |
import android.content.Intent; |
| 24 |
import android.content.IntentFilter; |
| 25 |
import android.os.Build; |
| 26 |
import android.os.Bundle; |
| 27 |
import android.os.Handler; |
| 28 |
import android.os.Message; |
| 29 |
import android.preference.PreferenceActivity; |
| 30 |
import android.speech.tts.TextToSpeech; |
| 31 |
import android.util.Log; |
| 32 |
import android.util.Pair; |
| 33 |
import android.view.Menu; |
| 34 |
import android.view.MenuInflater; |
| 35 |
import android.view.MenuItem; |
| 36 |
import android.view.View; |
| 37 |
import android.widget.ListView; |
| 38 |
import android.widget.EditText; |
| 39 |
|
| 40 |
import java.lang.ref.WeakReference; |
| 41 |
import java.util.ArrayList; |
| 42 |
import java.util.List; |
| 43 |
import java.util.Locale; |
| 44 |
|
| 45 |
public class eSpeakActivity extends Activity { |
| 46 |
private static final String ACTION_TTS_SETTINGS = "com.android.settings.TTS_SETTINGS"; |
| 47 |
|
| 48 |
|
| 49 |
private static final int TTS_INITIALIZED = 1; |
| 50 |
|
| 51 |
private static final int REQUEST_CHECK = 1; |
| 52 |
private static final int REQUEST_DEFAULT = 3; |
| 53 |
|
| 54 |
private static final String TAG = "eSpeakActivity"; |
| 55 |
|
| 56 |
private enum State { |
| 57 |
LOADING, |
| 58 |
DOWNLOAD_FAILED, |
| 59 |
ERROR, |
| 60 |
SUCCESS |
| 61 |
} |
| 62 |
|
| 63 |
private State mState; |
| 64 |
private TextToSpeech mTts; |
| 65 |
private List<Pair<String,String>> mInformation; |
| 66 |
private InformationListAdapter mInformationView; |
| 67 |
private EditText mText; |
| 68 |
|
| 69 |
private final BroadcastReceiver mOnEspeakInitialized = new BroadcastReceiver() { |
| 70 |
@Override |
| 71 |
public void onReceive(Context context, Intent intent) { |
| 72 |
populateInformationView(); |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
@Override |
| 77 |
public void onCreate(Bundle savedInstanceState) { |
| 78 |
super.onCreate(savedInstanceState); |
| 79 |
|
| 80 |
setContentView(R.layout.main); |
| 81 |
|
| 82 |
mInformation = new ArrayList<Pair<String,String>>(); |
| 83 |
mInformationView = new InformationListAdapter(this, mInformation); |
| 84 |
((ListView)findViewById(R.id.properties)).setAdapter(mInformationView); |
| 85 |
mText = (EditText)findViewById(R.id.editText1); |
| 86 |
|
| 87 |
setState(State.LOADING); |
| 88 |
checkVoiceData(); |
| 89 |
|
| 90 |
findViewById(R.id.speak).setOnClickListener(new View.OnClickListener() { |
| 91 |
@Override |
| 92 |
@SuppressWarnings("deprecation") |
| 93 |
public void onClick(View v) { |
| 94 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 95 |
mTts.speak(mText.getText().toString(), TextToSpeech.QUEUE_ADD, null, null); |
| 96 |
} else { |
| 97 |
mTts.speak(mText.getText().toString(), TextToSpeech.QUEUE_ADD, null); |
| 98 |
} |
| 99 |
} |
| 100 |
}); |
| 101 |
|
| 102 |
findViewById(R.id.ssml).setOnClickListener(new View.OnClickListener() { |
| 103 |
@Override |
| 104 |
public void onClick(View v) { |
| 105 |
String ssml = |
| 106 |
"<?xml version=\"1.0\"?>\n" + |
| 107 |
"<speak xmlns=\"http://www.w3.org/2001/10/synthesis\" version=\"1.0\">\n" + |
| 108 |
"\n" + |
| 109 |
"</speak>"; |
| 110 |
mText.setText(ssml); |
| 111 |
} |
| 112 |
}); |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public void onStart() { |
| 117 |
super.onStart(); |
| 118 |
|
| 119 |
final IntentFilter filter = new IntentFilter(TtsService.ESPEAK_INITIALIZED); |
| 120 |
registerReceiver(mOnEspeakInitialized, filter); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public void onStop() { |
| 125 |
super.onStop(); |
| 126 |
|
| 127 |
unregisterReceiver(mOnEspeakInitialized); |
| 128 |
|
| 129 |
if (mTts != null) { |
| 130 |
mTts.shutdown(); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public boolean onCreateOptionsMenu(Menu menu) |
| 136 |
{ |
| 137 |
MenuInflater inflater = getMenuInflater(); |
| 138 |
inflater.inflate(R.menu.options, menu); |
| 139 |
if (Build.VERSION.SDK_INT < 14) { |
| 140 |
|
| 141 |
menu.findItem(R.id.espeakSettings).setVisible(false); |
| 142 |
} |
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public boolean onOptionsItemSelected(MenuItem item) |
| 148 |
{ |
| 149 |
switch (item.getItemId()) |
| 150 |
{ |
| 151 |
case R.id.espeakSettings: |
| 152 |
startActivityForResult(new Intent(eSpeakActivity.this, TtsSettingsActivity.class), REQUEST_DEFAULT); |
| 153 |
return true; |
| 154 |
case R.id.ttsSettings: |
| 155 |
launchGeneralTtsSettings(); |
| 156 |
return true; |
| 157 |
} |
| 158 |
return super.onOptionsItemSelected(item); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
private void setState(State state) { |
| 167 |
mState = state; |
| 168 |
switch (mState) |
| 169 |
{ |
| 170 |
case LOADING: |
| 171 |
findViewById(R.id.loading).setVisibility(View.VISIBLE); |
| 172 |
findViewById(R.id.success).setVisibility(View.GONE); |
| 173 |
break; |
| 174 |
default: |
| 175 |
findViewById(R.id.loading).setVisibility(View.GONE); |
| 176 |
findViewById(R.id.success).setVisibility(View.VISIBLE); |
| 177 |
break; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
private void checkVoiceData() { |
| 185 |
final Intent checkIntent = new Intent(this, CheckVoiceData.class); |
| 186 |
|
| 187 |
startActivityForResult(checkIntent, REQUEST_CHECK); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
private void initializeEngine() { |
| 194 |
mTts = new TextToSpeech(this, mInitListener); |
| 195 |
} |
| 196 |
|
| 197 |
@SuppressWarnings("deprecation") |
| 198 |
private Locale getTtsLanguage() { |
| 199 |
if (mTts != null) { |
| 200 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 201 |
android.speech.tts.Voice voice = mTts.getVoice(); |
| 202 |
if (voice != null) { |
| 203 |
return voice.getLocale(); |
| 204 |
} |
| 205 |
} else { |
| 206 |
return mTts.getLanguage(); |
| 207 |
} |
| 208 |
} |
| 209 |
return null; |
| 210 |
} |
| 211 |
|
| 212 |
private void populateInformationView() { |
| 213 |
mInformation.clear(); |
| 214 |
|
| 215 |
Locale language = getTtsLanguage(); |
| 216 |
if (language != null) { |
| 217 |
final String currentLocale = getString(R.string.current_tts_locale); |
| 218 |
mInformation.add(new Pair<String, String>(currentLocale, language.getDisplayName())); |
| 219 |
} |
| 220 |
|
| 221 |
final String availableVoices = getString(R.string.available_voices); |
| 222 |
mInformation.add(new Pair<String,String>(availableVoices, Integer.toString(SpeechSynthesis.getVoiceCount()))); |
| 223 |
|
| 224 |
final String version = getString(R.string.tts_version); |
| 225 |
mInformation.add(new Pair<String,String>(version, SpeechSynthesis.getVersion())); |
| 226 |
|
| 227 |
final String statusText; |
| 228 |
switch (mState) { |
| 229 |
case ERROR: |
| 230 |
statusText = getString(R.string.error_message); |
| 231 |
break; |
| 232 |
case DOWNLOAD_FAILED: |
| 233 |
statusText = getString(R.string.voice_data_failed_message); |
| 234 |
break; |
| 235 |
default: |
| 236 |
if (!getPackageName().equals(mTts.getDefaultEngine())) { |
| 237 |
statusText = getString(R.string.set_default_message); |
| 238 |
} else { |
| 239 |
statusText = null; |
| 240 |
} |
| 241 |
break; |
| 242 |
} |
| 243 |
if (statusText != null) { |
| 244 |
final String statusLabel = getString(R.string.status); |
| 245 |
mInformation.add(new Pair<String,String>(statusLabel, statusText)); |
| 246 |
} |
| 247 |
|
| 248 |
mInformationView.notifyDataSetChanged(); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
private void onDataChecked(int resultCode, Intent data) { |
| 260 |
if (resultCode != TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { |
| 261 |
Log.e(TAG, "Voice data check failed (error code: " + resultCode + ")."); |
| 262 |
setState(State.ERROR); |
| 263 |
} |
| 264 |
|
| 265 |
initializeEngine(); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
private void onInitialized(int status) { |
| 275 |
if (status != TextToSpeech.SUCCESS) { |
| 276 |
Log.e(TAG, "Initialization failed (status: " + status + ")."); |
| 277 |
setState(State.ERROR); |
| 278 |
} else { |
| 279 |
setState(State.SUCCESS); |
| 280 |
} |
| 281 |
|
| 282 |
populateInformationView(); |
| 283 |
} |
| 284 |
|
| 285 |
@Override |
| 286 |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 287 |
switch (requestCode) { |
| 288 |
case REQUEST_CHECK: |
| 289 |
onDataChecked(resultCode, data); |
| 290 |
break; |
| 291 |
case REQUEST_DEFAULT: |
| 292 |
initializeEngine(); |
| 293 |
break; |
| 294 |
} |
| 295 |
super.onActivityResult(requestCode, resultCode, data); |
| 296 |
} |
| 297 |
|
| 298 |
private final TextToSpeech.OnInitListener mInitListener = new TextToSpeech.OnInitListener() { |
| 299 |
@Override |
| 300 |
public void onInit(int status) { |
| 301 |
mHandler.obtainMessage(TTS_INITIALIZED, status, 0).sendToTarget(); |
| 302 |
} |
| 303 |
}; |
| 304 |
|
| 305 |
private static class EspeakHandler extends Handler { |
| 306 |
private WeakReference<eSpeakActivity> mActivity; |
| 307 |
|
| 308 |
public EspeakHandler(eSpeakActivity activity) |
| 309 |
{ |
| 310 |
mActivity = new WeakReference<eSpeakActivity>(activity); |
| 311 |
} |
| 312 |
|
| 313 |
@Override |
| 314 |
public void handleMessage(Message msg) { |
| 315 |
switch (msg.what) { |
| 316 |
case TTS_INITIALIZED: |
| 317 |
mActivity.get().onInitialized(msg.arg1); |
| 318 |
break; |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
private final Handler mHandler = new EspeakHandler(this); |
| 323 |
|
| 324 |
private void launchGeneralTtsSettings() |
| 325 |
{ |
| 326 |
Intent intent; |
| 327 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) |
| 328 |
{ |
| 329 |
|
| 330 |
intent = new Intent(android.provider.Settings.ACTION_SETTINGS); |
| 331 |
intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, "com.android.settings.TextToSpeechSettings"); |
| 332 |
intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, intent.getExtras()); |
| 333 |
} |
| 334 |
else |
| 335 |
{ |
| 336 |
|
| 337 |
intent = new Intent(ACTION_TTS_SETTINGS); |
| 338 |
} |
| 339 |
startActivityForResult(intent, REQUEST_DEFAULT); |
| 340 |
} |
| 341 |
} |