/tts/src/com/google/tts/ConfigurationManager.java

http://eyes-free.googlecode.com/ · Java · 206 lines · 163 code · 18 blank · 25 comment · 18 complexity · 092ee2d7b6a300408c1676a13e66daaf MD5 · raw file

  1. /*
  2. * Copyright (C) 2008 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.tts;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.net.URL;
  22. import java.net.URLConnection;
  23. import java.util.Enumeration;
  24. import java.util.zip.ZipEntry;
  25. import java.util.zip.ZipFile;
  26. import android.app.Activity;
  27. import android.content.Intent;
  28. import android.os.Build;
  29. import android.os.Bundle;
  30. /**
  31. * Enables the user to configure TTS settings. This activity has not been
  32. * implemented yet.
  33. *
  34. * @author clchen@google.com (Charles L. Chen)
  35. */
  36. public class ConfigurationManager extends Activity {
  37. @Override
  38. public void onCreate(Bundle icicle) {
  39. super.onCreate(icicle);
  40. if (!allFilesExist()) {
  41. setContentView(R.layout.downloading);
  42. (new Thread(new dataDownloader())).start();
  43. } else {
  44. int version = Integer.parseInt(Build.VERSION.SDK);
  45. Intent intent = new Intent(this, TextToSpeechSettings.class);
  46. if (version < 4) {
  47. intent = new Intent(this, OldPrefsActivity.class);
  48. }
  49. startActivityForResult(intent, 42);
  50. finish();
  51. }
  52. }
  53. public class dataDownloader implements Runnable {
  54. public void run() {
  55. downloadEspeakData();
  56. finish();
  57. }
  58. }
  59. public static boolean allFilesExist() {
  60. String espeakDataDirStr = "/sdcard/espeak-data/";
  61. String[] datafiles = {
  62. "af_dict", "config", "cs_dict", "cy_dict", "de_dict", "el_dict", "en_dict",
  63. "eo_dict", "es_dict", "fi_dict", "fr_dict", "grc_dict", "hbs_dict", "hi_dict",
  64. "hu_dict", "id_dict", "is_dict", "it_dict", "jbo_dict", "ku_dict", "la_dict",
  65. "mk_dict", "nl_dict", "no_dict", "phondata", "phonindex", "phontab", "pl_dict",
  66. "pt_dict", "ro_dict", "ru_dict", "sk_dict", "sv_dict", "sw_dict", "ta_dict",
  67. "tr_dict", "vi_dict", "zh_dict", "zhy_dict", "mbrola/dummyfile",
  68. "mbrola_ph/af1_phtrans", "mbrola_ph/ca1_phtrans", "mbrola_ph/cr1_phtrans",
  69. "mbrola_ph/cs_phtrans", "mbrola_ph/de2_phtrans", "mbrola_ph/de4_phtrans",
  70. "mbrola_ph/de6_phtrans", "mbrola_ph/en1_phtrans", "mbrola_ph/es_phtrans",
  71. "mbrola_ph/es4_phtrans", "mbrola_ph/fr1_phtrans", "mbrola_ph/gr2_phtrans",
  72. "mbrola_ph/grc-de6_phtrans", "mbrola_ph/hu1_phtrans", "mbrola_ph/id1_phtrans",
  73. "mbrola_ph/in1_phtrans", "mbrola_ph/it3_phtrans", "mbrola_ph/la1_phtrans",
  74. "mbrola_ph/nl_phtrans", "mbrola_ph/pl1_phtrans", "mbrola_ph/pt_phtrans",
  75. "mbrola_ph/ptbr_phtrans", "mbrola_ph/ptbr4_phtrans", "mbrola_ph/ro1_phtrans",
  76. "mbrola_ph/sv_phtrans", "mbrola_ph/sv2_phtrans", "mbrola_ph/us_phtrans",
  77. "mbrola_ph/us3_phtrans", "soundicons/dummyfile", "voices/af", "voices/bs",
  78. "voices/cs", "voices/cy", "voices/de", "voices/default", "voices/el", "voices/eo",
  79. "voices/es", "voices/es-la", "voices/fi", "voices/fr", "voices/fr-be",
  80. "voices/grc", "voices/hi", "voices/hr", "voices/hu", "voices/id", "voices/is",
  81. "voices/it", "voices/jbo", "voices/ku", "voices/la", "voices/mk", "voices/nl",
  82. "voices/no", "voices/pl", "voices/pt", "voices/pt-pt", "voices/ro", "voices/ru",
  83. "voices/sk", "voices/sr", "voices/sv", "voices/sw", "voices/ta", "voices/tr",
  84. "voices/vi", "voices/zh", "voices/zhy", "voices/!v/croak", "voices/!v/f1",
  85. "voices/!v/f2", "voices/!v/f3", "voices/!v/f4", "voices/!v/m1", "voices/!v/m2",
  86. "voices/!v/m3", "voices/!v/m4", "voices/!v/m5", "voices/!v/m6",
  87. "voices/!v/whisper", "voices/en/en", "voices/en/en-n", "voices/en/en-r",
  88. "voices/en/en-rp", "voices/en/en-sc", "voices/en/en-wi", "voices/en/en-wm",
  89. "voices/mb/mb-af1", "voices/mb/mb-af1-en", "voices/mb/mb-br1", "voices/mb/mb-br3",
  90. "voices/mb/mb-br4", "voices/mb/mb-cr1", "voices/mb/mb-cz2", "voices/mb/mb-de2",
  91. "voices/mb/mb-de4", "voices/mb/mb-de4-en", "voices/mb/mb-de5",
  92. "voices/mb/mb-de5-en", "voices/mb/mb-de6", "voices/mb/mb-de6-grc",
  93. "voices/mb/mb-de7", "voices/mb/mb-en1", "voices/mb/mb-es1", "voices/mb/mb-es2",
  94. "voices/mb/mb-fr1", "voices/mb/mb-fr1-en", "voices/mb/mb-fr4",
  95. "voices/mb/mb-fr4-en", "voices/mb/mb-gr2", "voices/mb/mb-gr2-en",
  96. "voices/mb/mb-hu1", "voices/mb/mb-hu1-en", "voices/mb/mb-id1", "voices/mb/mb-it3",
  97. "voices/mb/mb-it4", "voices/mb/mb-la1", "voices/mb/mb-nl2", "voices/mb/mb-nl2-en",
  98. "voices/mb/mb-pl1", "voices/mb/mb-pl1-en", "voices/mb/mb-ro1",
  99. "voices/mb/mb-ro1-en", "voices/mb/mb-sw1", "voices/mb/mb-sw1-en",
  100. "voices/mb/mb-sw2", "voices/mb/mb-sw2-en", "voices/mb/mb-us1", "voices/mb/mb-us2",
  101. "voices/mb/mb-us3"
  102. };
  103. File espeakDataDir = new File(espeakDataDirStr);
  104. boolean directoryExists = espeakDataDir.isDirectory();
  105. if (!directoryExists) {
  106. return false;
  107. } else {
  108. for (int i = 0; i < datafiles.length; i++) {
  109. File tempFile = new File(espeakDataDirStr + datafiles[i]);
  110. if (!tempFile.exists()) {
  111. for (int j = 0; j < datafiles.length; j++) {
  112. File delFile = new File(espeakDataDirStr + datafiles[j]);
  113. delFile.delete();
  114. }
  115. return false;
  116. }
  117. }
  118. }
  119. return true;
  120. }
  121. public static void downloadEspeakData() {
  122. try {
  123. // Make sure the SD card is writable
  124. if (!new File("/sdcard/").canWrite()) {
  125. return;
  126. }
  127. // Create the espeak-data directory
  128. File espeakDataDir = new File("/sdcard/espeak-data/");
  129. espeakDataDir.mkdir();
  130. // Download the espeak-data zip file
  131. String fileUrl = "http://eyes-free.googlecode.com/svn/trunk/thirdparty/espeak-data.zip";
  132. fileUrl = (new URL(new URL(fileUrl), fileUrl)).toString();
  133. URL url = new URL(fileUrl);
  134. URLConnection cn = url.openConnection();
  135. cn.connect();
  136. InputStream stream = cn.getInputStream();
  137. File dlFile = new File("/sdcard/espeak-data/data.zip");
  138. dlFile.createNewFile();
  139. FileOutputStream out = new FileOutputStream(dlFile);
  140. byte buf[] = new byte[16384];
  141. do {
  142. int numread = stream.read(buf);
  143. if (numread <= 0) {
  144. break;
  145. } else {
  146. out.write(buf, 0, numread);
  147. }
  148. } while (true);
  149. stream.close();
  150. out.close();
  151. // Unzip into the espeak-data directory on the SD card
  152. ZipFile zip = new ZipFile("/sdcard/espeak-data/data.zip");
  153. Enumeration<? extends ZipEntry> zippedFiles = zip.entries();
  154. while (zippedFiles.hasMoreElements()) {
  155. ZipEntry entry = zippedFiles.nextElement();
  156. if (entry.isDirectory()) {
  157. File newDir = new File(espeakDataDir + entry.getName());
  158. newDir.mkdir();
  159. } else {
  160. InputStream is = zip.getInputStream(entry);
  161. String name = entry.getName();
  162. File outputFile = new File("/sdcard/espeak-data/" + name);
  163. String outputPath = outputFile.getCanonicalPath();
  164. name = outputPath.substring(outputPath.lastIndexOf("/") + 1);
  165. outputPath = outputPath.substring(0, outputPath.lastIndexOf("/"));
  166. File outputDir = new File(outputPath);
  167. outputDir.mkdirs();
  168. outputFile = new File(outputPath, name);
  169. outputFile.createNewFile();
  170. out = new FileOutputStream(outputFile);
  171. buf = new byte[16384];
  172. do {
  173. int numread = is.read(buf);
  174. if (numread <= 0) {
  175. break;
  176. } else {
  177. out.write(buf, 0, numread);
  178. }
  179. } while (true);
  180. is.close();
  181. out.close();
  182. }
  183. }
  184. } catch (IOException e) {
  185. e.printStackTrace();
  186. }
  187. }
  188. }