From 8f82baa6464a8338be093fad40a3764e70928ef5 Mon Sep 17 00:00:00 2001 From: "Reece H. Dunn" Date: Mon, 14 Oct 2013 15:12:23 +0100 Subject: [PATCH] Fix updating the number of installed voices after installing them within the activity. --- .../com/reecedunn/espeak/SpeechSynthesis.java | 6 +++++ .../src/com/reecedunn/espeak/TtsService.java | 5 ++++ .../com/reecedunn/espeak/eSpeakActivity.java | 25 ++++++++++++------- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/android/src/com/reecedunn/espeak/SpeechSynthesis.java b/android/src/com/reecedunn/espeak/SpeechSynthesis.java index 08067af9..66017df4 100644 --- a/android/src/com/reecedunn/espeak/SpeechSynthesis.java +++ b/android/src/com/reecedunn/espeak/SpeechSynthesis.java @@ -60,6 +60,7 @@ public class SpeechSynthesis { private final String mDatapath; private boolean mInitialized = false; + private static int mVoiceCount = 0; public SpeechSynthesis(Context context, SynthReadyCallback callback) { // First, ensure the data directory exists, otherwise init will crash. @@ -86,6 +87,10 @@ public class SpeechSynthesis { return nativeGetVersion(); } + public static int getVoiceCount() { + return mVoiceCount; + } + public int getSampleRate() { return nativeGetSampleRate(); } @@ -107,6 +112,7 @@ public class SpeechSynthesis { public List getAvailableVoices() { final List voices = new LinkedList(); final String[] results = nativeGetAvailableVoices(); + mVoiceCount = results.length / 4; for (int i = 0; i < results.length; i += 4) { final String name = results[i]; diff --git a/android/src/com/reecedunn/espeak/TtsService.java b/android/src/com/reecedunn/espeak/TtsService.java index 7e3a8083..2967f2bb 100644 --- a/android/src/com/reecedunn/espeak/TtsService.java +++ b/android/src/com/reecedunn/espeak/TtsService.java @@ -51,6 +51,8 @@ import java.util.Locale; */ @SuppressLint("NewApi") public class TtsService extends TextToSpeechService { + public static final String ESPEAK_INITIALIZED = "com.reecedunn.espeak.ESPEAK_INITIALIZED"; + private static final String TAG = TtsService.class.getSimpleName(); private static final boolean DEBUG = false; @@ -95,6 +97,9 @@ public class TtsService extends TextToSpeechService { mEngine = new SpeechSynthesis(this, mSynthCallback); mAvailableVoices = mEngine.getAvailableVoices(); + + final Intent intent = new Intent(ESPEAK_INITIALIZED); + sendBroadcast(intent); } @Override diff --git a/android/src/com/reecedunn/espeak/eSpeakActivity.java b/android/src/com/reecedunn/espeak/eSpeakActivity.java index 8926d6f7..31a88d1d 100644 --- a/android/src/com/reecedunn/espeak/eSpeakActivity.java +++ b/android/src/com/reecedunn/espeak/eSpeakActivity.java @@ -18,7 +18,10 @@ package com.reecedunn.espeak; import android.app.Activity; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.os.Build; import android.os.Bundle; import android.os.Handler; @@ -58,12 +61,18 @@ public class eSpeakActivity extends Activity { } private State mState; - private ArrayList mVoices; private TextToSpeech mTts; private List> mInformation; private InformationListAdapter mInformationView; private EditText mText; + private final BroadcastReceiver mOnEspeakInitialized = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + populateInformationView(); + } + }; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -75,6 +84,9 @@ public class eSpeakActivity extends Activity { ((ListView)findViewById(R.id.properties)).setAdapter(mInformationView); mText = (EditText)findViewById(R.id.editText1); + final IntentFilter filter = new IntentFilter(TtsService.ESPEAK_INITIALIZED); + registerReceiver(mOnEspeakInitialized, filter); + setState(State.LOADING); checkVoiceData(); @@ -102,6 +114,8 @@ public class eSpeakActivity extends Activity { public void onStop() { super.onStop(); + unregisterReceiver(mOnEspeakInitialized); + if (mTts != null) { mTts.shutdown(); } @@ -182,11 +196,7 @@ public class eSpeakActivity extends Activity { } final String availableVoices = getString(R.string.available_voices); - if (mVoices == null) { - mInformation.add(new Pair(availableVoices, "0")); - } else { - mInformation.add(new Pair(availableVoices, Integer.toString(mVoices.size()))); - } + mInformation.add(new Pair(availableVoices, Integer.toString(SpeechSynthesis.getVoiceCount()))); final String version = getString(R.string.tts_version); mInformation.add(new Pair(version, SpeechSynthesis.getVersion())); @@ -227,12 +237,9 @@ public class eSpeakActivity extends Activity { if (resultCode != TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { Log.e(TAG, "Voice data check failed (error code: " + resultCode + ")."); setState(State.ERROR); - } else { - mVoices = data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES); } initializeEngine(); - populateInformationView(); } /**