/documentation/TextToSpeech_Plugin_Engine_Examples/PicoUnbundled/src/com/svox/pico/unbundled/CheckVoiceData.java

http://eyes-free.googlecode.com/ · Java · 123 lines · 77 code · 19 blank · 27 comment · 13 complexity · 6b54d9f2e045e809a71e9200df127d76 MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.svox.pico.unbundled;
  17. import android.app.Activity;
  18. import android.content.Intent;
  19. import android.os.Bundle;
  20. import android.os.Environment;
  21. import android.speech.tts.TextToSpeech;
  22. import java.io.File;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. /*
  26. * Checks if the voice data for the SVOX Pico Engine is present on the
  27. * sd card.
  28. */
  29. public class CheckVoiceData extends Activity {
  30. // The following constants are the same path constants as the ones defined
  31. // in external/svox/pico/tts/com_svox_picottsengine.cpp
  32. private final static String PICO_LINGWARE_PATH =
  33. Environment.getExternalStorageDirectory() + "/svox/";
  34. //private final static String PICO_SYSTEM_LINGWARE_PATH = "/system/tts/lang_pico/";
  35. private final static String[] dataFiles = {
  36. "de-DE_gl0_sg.bin", "de-DE_ta.bin", "en-GB_kh0_sg.bin", "en-GB_ta.bin",
  37. "en-US_lh0_sg.bin", "en-US_ta.bin", "es-ES_ta.bin", "es-ES_zl0_sg.bin",
  38. "fr-FR_nk0_sg.bin", "fr-FR_ta.bin", "it-IT_cm0_sg.bin", "it-IT_ta.bin"
  39. };
  40. private final static String[] dataFilesInfo = {
  41. "deu-DEU", "deu-DEU", "eng-GBR", "eng-GBR", "eng-USA", "eng-USA",
  42. "spa-ESP", "spa-ESP", "fra-FRA", "fra-FRA", "ita-ITA", "ita-ITA"
  43. };
  44. private final static String[] supportedLanguages = {
  45. "deu-DEU", "eng-GBR", "eng-USA", "spa-ESP", "fra-FRA", "ita-ITA"
  46. };
  47. @Override
  48. protected void onCreate(Bundle savedInstanceState) {
  49. super.onCreate(savedInstanceState);
  50. int result = TextToSpeech.Engine.CHECK_VOICE_DATA_PASS;
  51. boolean foundMatch = false;
  52. ArrayList<String> available = new ArrayList<String>();
  53. ArrayList<String> unavailable = new ArrayList<String>();
  54. HashMap<String, Boolean> languageCountry = new HashMap<String, Boolean>();
  55. Bundle bundle = getIntent().getExtras();
  56. if (bundle != null){
  57. // TODO (clchen): Add this intent to TextToSpeech.Engine
  58. ArrayList<String> langCountryVars = bundle.getStringArrayList(
  59. "TextToSpeech.Engine.EXTRA_CHECK_VOICE_DATA_FOR");
  60. if (langCountryVars != null){
  61. for (int i = 0; i < langCountryVars.size(); i++){
  62. if (langCountryVars.get(i).length() > 0){
  63. languageCountry.put(langCountryVars.get(i), true);
  64. }
  65. }
  66. }
  67. }
  68. // Check for files
  69. for (int i = 0; i < supportedLanguages.length; i++){
  70. if ((languageCountry.size() < 1) ||
  71. (languageCountry.containsKey(supportedLanguages[i]))){
  72. if (!fileExists(dataFiles[2 * i]) ||
  73. !fileExists(dataFiles[(2 * i) + 1])){
  74. result = TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA;
  75. unavailable.add(supportedLanguages[i]);
  76. } else {
  77. available.add(supportedLanguages[i]);
  78. foundMatch = true;
  79. }
  80. }
  81. }
  82. if ((languageCountry.size() > 0) && !foundMatch){
  83. result = TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL;
  84. }
  85. // Put the root directory for the sd card data + the data filenames
  86. Intent returnData = new Intent();
  87. returnData.putExtra(TextToSpeech.Engine.EXTRA_VOICE_DATA_ROOT_DIRECTORY, PICO_LINGWARE_PATH);
  88. returnData.putExtra(TextToSpeech.Engine.EXTRA_VOICE_DATA_FILES, dataFiles);
  89. returnData.putExtra(TextToSpeech.Engine.EXTRA_VOICE_DATA_FILES_INFO, dataFilesInfo);
  90. // TODO (clchen): Add these intents to TextToSpeech.Engine
  91. returnData.putStringArrayListExtra("availableVoices", available);
  92. returnData.putStringArrayListExtra("unavailableVoices", unavailable);
  93. setResult(result, returnData);
  94. finish();
  95. }
  96. private boolean fileExists(String filename){
  97. File tempFile = new File(PICO_LINGWARE_PATH + filename);
  98. //File tempFileSys = new File(PICO_SYSTEM_LINGWARE_PATH + filename);
  99. if ((!tempFile.exists()) ){ //&& (!tempFileSys.exists())) {
  100. return false;
  101. }
  102. return true;
  103. }
  104. }