Debian Codegraph @

android/src/com/reecedunn/espeak/CheckVoiceData.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 Beka Gozalishvili
3 * Copyright (C) 2012-2013 Reece H. Dunn
4 * Copyright (C) 2009 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 /*
20 * This Activity is used by Android to get the list of languages to display
21 * to the user when selecting the text-to-speech language. This is by locale,
22 * not voice name.
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 /** Resources required for eSpeak to run correctly. */
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 // Do nothing.
122 }
123
124 @Override
125 public void onSynthDataComplete() {
126 // Do nothing.
127 }
128 };
129 }