PageRenderTime 59ms CodeModel.GetById 17ms app.highlight 37ms RepoModel.GetById 1ms app.codeStats 1ms

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