| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
package com.reecedunn.espeak; |
| 26 |
|
| 27 |
import android.app.Activity; |
| 28 |
import android.content.Context; |
| 29 |
import android.content.Intent; |
| 30 |
import android.os.Bundle; |
| 31 |
import android.speech.tts.TextToSpeech.Engine; |
| 32 |
import android.util.Log; |
| 33 |
|
| 34 |
import com.reecedunn.espeak.SpeechSynthesis.SynthReadyCallback; |
| 35 |
|
| 36 |
import java.io.File; |
| 37 |
import java.util.ArrayList; |
| 38 |
import java.util.List; |
| 39 |
import java.util.Locale; |
| 40 |
|
| 41 |
public class CheckVoiceData extends Activity { |
| 42 |
private static final String TAG = "eSpeakTTS"; |
| 43 |
|
| 44 |
|
| 45 |
private static final String[] BASE_RESOURCES = { |
| 46 |
"version", |
| 47 |
"intonations", |
| 48 |
"phondata", |
| 49 |
"phonindex", |
| 50 |
"phontab", |
| 51 |
"en_dict", |
| 52 |
}; |
| 53 |
|
| 54 |
public static File getDataPath(Context context) { |
| 55 |
return new File(context.getDir("voices", MODE_PRIVATE), "espeak-ng-data"); |
| 56 |
} |
| 57 |
|
| 58 |
public static boolean hasBaseResources(Context context) { |
| 59 |
final File dataPath = getDataPath(context); |
| 60 |
|
| 61 |
for (String resource : BASE_RESOURCES) { |
| 62 |
final File resourceFile = new File(dataPath, resource); |
| 63 |
|
| 64 |
if (!resourceFile.exists()) { |
| 65 |
Log.e(TAG, "Missing base resource: " + resourceFile.getPath()); |
| 66 |
return false; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
public static boolean canUpgradeResources(Context context) { |
| 74 |
try { |
| 75 |
final String version = FileUtils.read(context.getResources().openRawResource(R.raw.espeakdata_version)); |
| 76 |
final String installedVersion = FileUtils.read(new File(getDataPath(context), "version")); |
| 77 |
return !version.equals(installedVersion); |
| 78 |
} catch (Exception e) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
protected void onCreate(Bundle savedInstanceState) { |
| 85 |
super.onCreate(savedInstanceState); |
| 86 |
|
| 87 |
Context storageContext = EspeakApp.getStorageContext(); |
| 88 |
ArrayList<String> availableLanguages = new ArrayList<String>(); |
| 89 |
ArrayList<String> unavailableLanguages = new ArrayList<String>(); |
| 90 |
|
| 91 |
boolean haveBaseResources = hasBaseResources(storageContext); |
| 92 |
if (!haveBaseResources || canUpgradeResources(storageContext)) { |
| 93 |
if (!haveBaseResources) { |
| 94 |
unavailableLanguages.add(Locale.ENGLISH.toString()); |
| 95 |
} |
| 96 |
returnResults(Engine.CHECK_VOICE_DATA_FAIL, availableLanguages, unavailableLanguages); |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
final SpeechSynthesis engine = new SpeechSynthesis(storageContext, mSynthReadyCallback); |
| 101 |
final List<Voice> voices = engine.getAvailableVoices(); |
| 102 |
|
| 103 |
for (Voice voice : voices) { |
| 104 |
availableLanguages.add(voice.toString()); |
| 105 |
} |
| 106 |
|
| 107 |
returnResults(Engine.CHECK_VOICE_DATA_PASS, availableLanguages, unavailableLanguages); |
| 108 |
} |
| 109 |
|
| 110 |
private void returnResults(int result, ArrayList<String> availableLanguages, ArrayList<String> unavailableLanguages) { |
| 111 |
final Intent returnData = new Intent(); |
| 112 |
returnData.putStringArrayListExtra(Engine.EXTRA_AVAILABLE_VOICES, availableLanguages); |
| 113 |
returnData.putStringArrayListExtra(Engine.EXTRA_UNAVAILABLE_VOICES, unavailableLanguages); |
| 114 |
setResult(result, returnData); |
| 115 |
finish(); |
| 116 |
} |
| 117 |
|
| 118 |
private final SynthReadyCallback mSynthReadyCallback = new SynthReadyCallback() { |
| 119 |
@Override |
| 120 |
public void onSynthDataReady(byte[] audioData) { |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public void onSynthDataComplete() { |
| 126 |
|
| 127 |
} |
| 128 |
}; |
| 129 |
} |