1 |
/**
|
2 |
* ZANavi, Zoff Android Navigation system.
|
3 |
* Copyright (C) 2011 - 2012 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 |
import android.view.View;
|
51 |
|
52 |
// new TTS, this is used now!
|
53 |
public class NavitSpeech2 implements TextToSpeech.OnInitListener, NavitActivityResult
|
54 |
{
|
55 |
private static TextToSpeech mTts;
|
56 |
private Navit navit;
|
57 |
int MY_DATA_CHECK_CODE = 1; // this needs to be "1" for the C-code !!
|
58 |
private Locale want_locale = null;
|
59 |
|
60 |
public void onInit(int status)
|
61 |
{
|
62 |
Log.e("NavitSpeech2", "onInit: Status " + status);
|
63 |
int result = -1;
|
64 |
try
|
65 |
{
|
66 |
// set the new locale here -----------------------------------
|
67 |
Locale locale2 = want_locale;
|
68 |
Locale.setDefault(locale2);
|
69 |
Configuration config2 = new Configuration();
|
70 |
config2.locale = locale2;
|
71 |
// set the new locale here -----------------------------------
|
72 |
result = mTts.setLanguage(locale2);
|
73 |
|
74 |
try
|
75 |
{
|
76 |
Log.e("NavitSpeech2", "3.1 want locale=" + locale2.getLanguage());
|
77 |
//Log.e("NavitSpeech2", "3 E=" + mTts.getDefaultEngine());
|
78 |
//Log.e("NavitSpeech2", "3 def.enf.=" + mTts.areDefaultsEnforced());
|
79 |
}
|
80 |
catch (NoSuchMethodError e2)
|
81 |
{
|
82 |
e2.printStackTrace();
|
83 |
}
|
84 |
catch (Exception e)
|
85 |
{
|
86 |
e.printStackTrace();
|
87 |
}
|
88 |
try
|
89 |
{
|
90 |
Log.e("NavitSpeech2", "3 lang. Country=" + mTts.getLanguage().getDisplayCountry());
|
91 |
Log.e("NavitSpeech2", "3 lang. Country=" + mTts.getLanguage().getDisplayLanguage());
|
92 |
Log.e("NavitSpeech2", "3 lang. Country=" + mTts.getLanguage().getDisplayName());
|
93 |
Log.e("NavitSpeech2", "3 lang. Country=" + mTts.getLanguage().getDisplayVariant());
|
94 |
}
|
95 |
catch (NoSuchMethodError e2)
|
96 |
{
|
97 |
e2.printStackTrace();
|
98 |
}
|
99 |
catch (Exception e)
|
100 |
{
|
101 |
e.printStackTrace();
|
102 |
}
|
103 |
|
104 |
String want_lang_code = locale2.getISO3Language();
|
105 |
Log.e("NavitSpeech2", "want:" + want_lang_code);
|
106 |
String will_use_lang_code = mTts.getLanguage().getISO3Language();
|
107 |
Log.e("NavitSpeech2", "will use:" + will_use_lang_code);
|
108 |
if (want_lang_code.compareToIgnoreCase(will_use_lang_code) != 0)
|
109 |
{
|
110 |
result = TextToSpeech.LANG_NOT_SUPPORTED;
|
111 |
}
|
112 |
|
113 |
Log.e("NavitSpeech2", "3 ok result=" + result);
|
114 |
}
|
115 |
catch (Exception e1)
|
116 |
{
|
117 |
e1.printStackTrace();
|
118 |
result = TextToSpeech.LANG_NOT_SUPPORTED;
|
119 |
}
|
120 |
|
121 |
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
|
122 |
{
|
123 |
// Lanuage data is missing or the language is not supported.
|
124 |
Log.e("NavitSpeech2", "3 Language is not available.");
|
125 |
|
126 |
try
|
127 |
{
|
128 |
// lang for TTS not found, show toast
|
129 |
Message msg = Navit.Navit_progress_h.obtainMessage();
|
130 |
Bundle b = new Bundle();
|
131 |
msg.what = 3;
|
132 |
b.putString("text", Navit.get_text("Language is not available for TTS! Using your phone's default settings")); //TRANS
|
133 |
msg.setData(b);
|
134 |
Navit.Navit_progress_h.sendMessage(msg);
|
135 |
}
|
136 |
catch (Exception e1)
|
137 |
{
|
138 |
e1.printStackTrace();
|
139 |
}
|
140 |
}
|
141 |
else
|
142 |
{
|
143 |
try
|
144 |
{
|
145 |
// lang for TTS not found, show toast
|
146 |
Message msg = Navit.Navit_progress_h.obtainMessage();
|
147 |
Bundle b = new Bundle();
|
148 |
msg.what = 3;
|
149 |
b.putString("text", Navit.get_text("Using Voice for:") + "\n" + mTts.getLanguage().getDisplayName()); //TRANS
|
150 |
msg.setData(b);
|
151 |
Navit.Navit_progress_h.sendMessage(msg);
|
152 |
}
|
153 |
catch (Exception e1)
|
154 |
{
|
155 |
e1.printStackTrace();
|
156 |
}
|
157 |
}
|
158 |
}
|
159 |
|
160 |
public void onActivityResult(int requestCode, int resultCode, Intent data)
|
161 |
{
|
162 |
Log.e("NavitSpeech2", "onActivityResult " + requestCode + " " + resultCode);
|
163 |
try
|
164 |
{
|
165 |
if (requestCode == MY_DATA_CHECK_CODE)
|
166 |
{
|
167 |
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
|
168 |
{
|
169 |
// success, create the TTS instance
|
170 |
mTts = new TextToSpeech(navit, this);
|
171 |
|
172 |
try
|
173 |
{
|
174 |
// just for info -------
|
175 |
Log.e("NavitSpeech2", Arrays.toString(Locale.getAvailableLocales()));
|
176 |
// just for info -------
|
177 |
}
|
178 |
catch (Exception e)
|
179 |
{
|
180 |
e.printStackTrace();
|
181 |
}
|
182 |
}
|
183 |
else
|
184 |
{
|
185 |
// missing data, install it
|
186 |
Intent installIntent = new Intent();
|
187 |
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
|
188 |
navit.startActivity(installIntent);
|
189 |
}
|
190 |
}
|
191 |
}
|
192 |
catch (Exception e)
|
193 |
{
|
194 |
e.printStackTrace();
|
195 |
}
|
196 |
}
|
197 |
|
198 |
NavitSpeech2(Navit navit)
|
199 |
{
|
200 |
Log.e("NavitSpeech2", "constructor");
|
201 |
Log.e("NavitSpeech2", "locale=" + java.util.Locale.getDefault().getDisplayName());
|
202 |
want_locale = java.util.Locale.getDefault();
|
203 |
try
|
204 |
{
|
205 |
this.navit = navit;
|
206 |
navit.setActivityResult(MY_DATA_CHECK_CODE, this);
|
207 |
}
|
208 |
catch (Exception e1)
|
209 |
{
|
210 |
e1.printStackTrace();
|
211 |
}
|
212 |
|
213 |
try
|
214 |
{
|
215 |
Intent checkIntent = new Intent();
|
216 |
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
|
217 |
navit.startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
|
218 |
}
|
219 |
catch (Exception e2)
|
220 |
{
|
221 |
e2.printStackTrace();
|
222 |
}
|
223 |
}
|
224 |
|
225 |
public void say(String what)
|
226 |
{
|
227 |
try
|
228 |
{
|
229 |
if (mTts != null)
|
230 |
{
|
231 |
mTts.speak(what, TextToSpeech.QUEUE_FLUSH, null);
|
232 |
if (NavitGraphics.NavitMsgTv2_.getVisibility() == View.VISIBLE)
|
233 |
{
|
234 |
Navit.N_NavitGraphics.NavitMsgTv2_.append("SAY:" + what + "\n");
|
235 |
}
|
236 |
if (Navit.PREF_show_debug_messages)
|
237 |
{
|
238 |
Navit.set_debug_messages3_wrapper(what);
|
239 |
}
|
240 |
}
|
241 |
}
|
242 |
catch (Exception e)
|
243 |
{
|
244 |
e.printStackTrace();
|
245 |
}
|
246 |
}
|
247 |
|
248 |
public static void stop_me()
|
249 |
{
|
250 |
// Log.e("NavitSpeech2", "shutdown");
|
251 |
try
|
252 |
{
|
253 |
mTts.shutdown();
|
254 |
}
|
255 |
catch (Exception e)
|
256 |
{
|
257 |
e.printStackTrace();
|
258 |
}
|
259 |
}
|
260 |
}
|