1 |
/**
|
2 |
* ZANavi, Zoff Android Navigation system.
|
3 |
* Copyright (C) 2011 Zoff <zoff@zoff.cc>
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* version 2 as published by the Free Software Foundation.
|
8 |
*
|
9 |
* This program is distributed in the hope that it will be useful,
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
* GNU General Public License for more details.
|
13 |
*
|
14 |
* You should have received a copy of the GNU General Public License
|
15 |
* along with this program; if not, write to the
|
16 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
17 |
* Boston, MA 02110-1301, USA.
|
18 |
*/
|
19 |
|
20 |
/**
|
21 |
* Navit, a modular navigation system.
|
22 |
* Copyright (C) 2005-2008 Navit Team
|
23 |
*
|
24 |
* This program is free software; you can redistribute it and/or
|
25 |
* modify it under the terms of the GNU General Public License
|
26 |
* version 2 as published by the Free Software Foundation.
|
27 |
*
|
28 |
* This program is distributed in the hope that it will be useful,
|
29 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
30 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
31 |
* GNU General Public License for more details.
|
32 |
*
|
33 |
* You should have received a copy of the GNU General Public License
|
34 |
* along with this program; if not, write to the
|
35 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
36 |
* Boston, MA 02110-1301, USA.
|
37 |
*/
|
38 |
|
39 |
package com.zoffcc.applications.zanavi;
|
40 |
|
41 |
import java.util.Arrays;
|
42 |
import java.util.Locale;
|
43 |
|
44 |
import android.content.Intent;
|
45 |
import android.content.res.Configuration;
|
46 |
import android.os.Bundle;
|
47 |
import android.os.Message;
|
48 |
import android.speech.tts.TextToSpeech;
|
49 |
import android.util.Log;
|
50 |
|
51 |
// new TTS, this is used now!
|
52 |
public class NavitSpeech2 implements TextToSpeech.OnInitListener, NavitActivityResult
|
53 |
{
|
54 |
private static TextToSpeech mTts;
|
55 |
private Navit navit;
|
56 |
int MY_DATA_CHECK_CODE = 1; // this needs to be "1" for the C-code !!
|
57 |
private Locale want_locale = null;
|
58 |
|
59 |
public void onInit(int status)
|
60 |
{
|
61 |
Log.e("NavitSpeech2", "onInit: Status " + status);
|
62 |
int result = -1;
|
63 |
try
|
64 |
{
|
65 |
// set the new locale here -----------------------------------
|
66 |
Locale locale2 = want_locale;
|
67 |
Locale.setDefault(locale2);
|
68 |
Configuration config2 = new Configuration();
|
69 |
config2.locale = locale2;
|
70 |
// set the new locale here -----------------------------------
|
71 |
result = mTts.setLanguage(locale2);
|
72 |
|
73 |
try
|
74 |
{
|
75 |
Log.e("NavitSpeech2", "3.1 want locale=" + locale2.getLanguage());
|
76 |
Log.e("NavitSpeech2", "3 E=" + mTts.getDefaultEngine());
|
77 |
Log.e("NavitSpeech2", "3 def.enf.=" + mTts.areDefaultsEnforced());
|
78 |
}
|
79 |
catch (NoSuchMethodError e2)
|
80 |
{
|
81 |
e2.printStackTrace();
|
82 |
}
|
83 |
catch (Exception e)
|
84 |
{
|
85 |
e.printStackTrace();
|
86 |
}
|
87 |
try
|
88 |
{
|
89 |
Log.e("NavitSpeech2", "3 lang. Country=" + mTts.getLanguage().getDisplayCountry());
|
90 |
Log.e("NavitSpeech2", "3 lang. Country=" + mTts.getLanguage().getDisplayLanguage());
|
91 |
Log.e("NavitSpeech2", "3 lang. Country=" + mTts.getLanguage().getDisplayName());
|
92 |
Log.e("NavitSpeech2", "3 lang. Country=" + mTts.getLanguage().getDisplayVariant());
|
93 |
}
|
94 |
catch (NoSuchMethodError e2)
|
95 |
{
|
96 |
e2.printStackTrace();
|
97 |
}
|
98 |
catch (Exception e)
|
99 |
{
|
100 |
e.printStackTrace();
|
101 |
}
|
102 |
|
103 |
String want_lang_code = locale2.getISO3Language();
|
104 |
Log.e("NavitSpeech2", "want:" + want_lang_code);
|
105 |
String will_use_lang_code = mTts.getLanguage().getISO3Language();
|
106 |
Log.e("NavitSpeech2", "will use:" + will_use_lang_code);
|
107 |
if (want_lang_code.compareToIgnoreCase(will_use_lang_code) != 0)
|
108 |
{
|
109 |
result = TextToSpeech.LANG_NOT_SUPPORTED;
|
110 |
}
|
111 |
|
112 |
Log.e("NavitSpeech2", "3 ok result=" + result);
|
113 |
}
|
114 |
catch (Exception e1)
|
115 |
{
|
116 |
e1.printStackTrace();
|
117 |
result = TextToSpeech.LANG_NOT_SUPPORTED;
|
118 |
}
|
119 |
|
120 |
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
|
121 |
{
|
122 |
// Lanuage data is missing or the language is not supported.
|
123 |
Log.e("NavitSpeech2", "3 Language is not available.");
|
124 |
|
125 |
try
|
126 |
{
|
127 |
// lang for TTS not found, show toast
|
128 |
Message msg = Navit.Navit_progress_h.obtainMessage();
|
129 |
Bundle b = new Bundle();
|
130 |
msg.what = 3;
|
131 |
b.putString("text", Navit.get_text("Language is not available for TTS! Using your phone's default settings")); //TRANS
|
132 |
msg.setData(b);
|
133 |
Navit.Navit_progress_h.sendMessage(msg);
|
134 |
}
|
135 |
catch (Exception e1)
|
136 |
{
|
137 |
e1.printStackTrace();
|
138 |
}
|
139 |
}
|
140 |
else
|
141 |
{
|
142 |
try
|
143 |
{
|
144 |
// lang for TTS not found, show toast
|
145 |
Message msg = Navit.Navit_progress_h.obtainMessage();
|
146 |
Bundle b = new Bundle();
|
147 |
msg.what = 3;
|
148 |
b.putString("text", Navit.get_text("Using Voice for:") + "\n" + mTts.getLanguage().getDisplayName()); //TRANS
|
149 |
msg.setData(b);
|
150 |
Navit.Navit_progress_h.sendMessage(msg);
|
151 |
}
|
152 |
catch (Exception e1)
|
153 |
{
|
154 |
e1.printStackTrace();
|
155 |
}
|
156 |
}
|
157 |
}
|
158 |
|
159 |
public void onActivityResult(int requestCode, int resultCode, Intent data)
|
160 |
{
|
161 |
Log.e("NavitSpeech2", "onActivityResult " + requestCode + " " + resultCode);
|
162 |
try
|
163 |
{
|
164 |
if (requestCode == MY_DATA_CHECK_CODE)
|
165 |
{
|
166 |
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
|
167 |
{
|
168 |
// success, create the TTS instance
|
169 |
mTts = new TextToSpeech(navit, this);
|
170 |
|
171 |
try
|
172 |
{
|
173 |
// just for info -------
|
174 |
Log.e("NavitSpeech2", Arrays.toString(Locale.getAvailableLocales()));
|
175 |
// just for info -------
|
176 |
}
|
177 |
catch (Exception e)
|
178 |
{
|
179 |
e.printStackTrace();
|
180 |
}
|
181 |
}
|
182 |
else
|
183 |
{
|
184 |
// missing data, install it
|
185 |
Intent installIntent = new Intent();
|
186 |
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
|
187 |
navit.startActivity(installIntent);
|
188 |
}
|
189 |
}
|
190 |
}
|
191 |
catch (Exception e)
|
192 |
{
|
193 |
e.printStackTrace();
|
194 |
}
|
195 |
}
|
196 |
|
197 |
NavitSpeech2(Navit navit)
|
198 |
{
|
199 |
Log.e("NavitSpeech2", "constructor");
|
200 |
Log.e("NavitSpeech2", "locale=" + java.util.Locale.getDefault().getDisplayName());
|
201 |
want_locale = java.util.Locale.getDefault();
|
202 |
try
|
203 |
{
|
204 |
this.navit = navit;
|
205 |
navit.setActivityResult(MY_DATA_CHECK_CODE, this);
|
206 |
}
|
207 |
catch (Exception e1)
|
208 |
{
|
209 |
e1.printStackTrace();
|
210 |
}
|
211 |
|
212 |
try
|
213 |
{
|
214 |
Intent checkIntent = new Intent();
|
215 |
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
|
216 |
navit.startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
|
217 |
}
|
218 |
catch (Exception e2)
|
219 |
{
|
220 |
e2.printStackTrace();
|
221 |
}
|
222 |
}
|
223 |
|
224 |
public void say(String what)
|
225 |
{
|
226 |
try
|
227 |
{
|
228 |
if (mTts != null)
|
229 |
{
|
230 |
mTts.speak(what, TextToSpeech.QUEUE_FLUSH, null);
|
231 |
Navit.set_debug_messages3(what);
|
232 |
}
|
233 |
}
|
234 |
catch (Exception e)
|
235 |
{
|
236 |
e.printStackTrace();
|
237 |
}
|
238 |
}
|
239 |
|
240 |
public static void stop_me()
|
241 |
{
|
242 |
Log.e("NavitSpeech2", "shutdown");
|
243 |
try
|
244 |
{
|
245 |
mTts.shutdown();
|
246 |
}
|
247 |
catch (Exception e)
|
248 |
{
|
249 |
e.printStackTrace();
|
250 |
}
|
251 |
}
|
252 |
}
|