PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 17package com.svox.pico.unbundled;
 18
 19import android.app.Activity;
 20import android.content.Intent;
 21import android.os.Bundle;
 22import android.os.Environment;
 23import android.speech.tts.TextToSpeech;
 24
 25import java.io.File;
 26import java.util.ArrayList;
 27import java.util.HashMap;
 28
 29/*
 30 * Checks if the voice data for the SVOX Pico Engine is present on the
 31 * sd card.
 32 */
 33public class CheckVoiceData extends Activity {
 34
 35    // The following constants are the same path constants as the ones defined
 36    // in external/svox/pico/tts/com_svox_picottsengine.cpp
 37    private final static String PICO_LINGWARE_PATH =
 38            Environment.getExternalStorageDirectory() + "/svox/";
 39    //private final static String PICO_SYSTEM_LINGWARE_PATH = "/system/tts/lang_pico/";
 40
 41    private final static String[] dataFiles = {
 42            "de-DE_gl0_sg.bin", "de-DE_ta.bin", "en-GB_kh0_sg.bin", "en-GB_ta.bin",
 43            "en-US_lh0_sg.bin", "en-US_ta.bin", "es-ES_ta.bin", "es-ES_zl0_sg.bin",
 44            "fr-FR_nk0_sg.bin", "fr-FR_ta.bin", "it-IT_cm0_sg.bin", "it-IT_ta.bin"
 45    };
 46
 47    private final static String[] dataFilesInfo = {
 48        "deu-DEU", "deu-DEU", "eng-GBR", "eng-GBR", "eng-USA", "eng-USA",
 49        "spa-ESP", "spa-ESP", "fra-FRA", "fra-FRA", "ita-ITA", "ita-ITA"
 50    };
 51
 52    private final static String[] supportedLanguages = {
 53        "deu-DEU", "eng-GBR", "eng-USA", "spa-ESP", "fra-FRA", "ita-ITA"
 54    };
 55
 56    @Override
 57    protected void onCreate(Bundle savedInstanceState) {
 58        super.onCreate(savedInstanceState);
 59
 60        int result = TextToSpeech.Engine.CHECK_VOICE_DATA_PASS;
 61        boolean foundMatch = false;
 62
 63        ArrayList<String> available = new ArrayList<String>();
 64        ArrayList<String> unavailable = new ArrayList<String>();
 65
 66        HashMap<String, Boolean> languageCountry = new HashMap<String, Boolean>();
 67
 68        Bundle bundle = getIntent().getExtras();
 69        if (bundle != null){
 70            // TODO (clchen): Add this intent to TextToSpeech.Engine
 71            ArrayList<String> langCountryVars = bundle.getStringArrayList(
 72                    "TextToSpeech.Engine.EXTRA_CHECK_VOICE_DATA_FOR");
 73            if (langCountryVars != null){
 74                for (int i = 0; i < langCountryVars.size(); i++){
 75                    if (langCountryVars.get(i).length() > 0){
 76                        languageCountry.put(langCountryVars.get(i), true);
 77                    }
 78                }
 79            }
 80        }
 81
 82        // Check for files
 83        for (int i = 0; i < supportedLanguages.length; i++){
 84            if ((languageCountry.size() < 1) ||
 85                (languageCountry.containsKey(supportedLanguages[i]))){
 86                if (!fileExists(dataFiles[2 * i]) ||
 87                    !fileExists(dataFiles[(2 * i) + 1])){
 88                    result = TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA;
 89                    unavailable.add(supportedLanguages[i]);
 90                } else {
 91                    available.add(supportedLanguages[i]);
 92                    foundMatch = true;
 93                }
 94            }
 95        }
 96        
 97        if ((languageCountry.size() > 0) && !foundMatch){
 98            result = TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL;
 99        }
100
101        // Put the root directory for the sd card data + the data filenames
102        Intent returnData = new Intent();
103        returnData.putExtra(TextToSpeech.Engine.EXTRA_VOICE_DATA_ROOT_DIRECTORY, PICO_LINGWARE_PATH);
104        returnData.putExtra(TextToSpeech.Engine.EXTRA_VOICE_DATA_FILES, dataFiles);
105        returnData.putExtra(TextToSpeech.Engine.EXTRA_VOICE_DATA_FILES_INFO, dataFilesInfo);
106
107        // TODO (clchen): Add these intents to TextToSpeech.Engine
108        returnData.putStringArrayListExtra("availableVoices", available);
109        returnData.putStringArrayListExtra("unavailableVoices", unavailable);
110        setResult(result, returnData);
111        finish();
112    }
113
114    private boolean fileExists(String filename){
115        File tempFile = new File(PICO_LINGWARE_PATH + filename);
116        //File tempFileSys = new File(PICO_SYSTEM_LINGWARE_PATH + filename);
117        if ((!tempFile.exists()) ){ //&& (!tempFileSys.exists())) {
118            return false;
119        }
120        return true;
121    }
122
123}