/[zanavi_public1]/navit/navit/android/src/com/zoffcc/applications/zanavi/NavitSpeech2.java
ZANavi

Contents of /navit/navit/android/src/com/zoffcc/applications/zanavi/NavitSpeech2.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 47 - (show annotations) (download)
Sat Sep 12 11:02:16 2015 UTC (8 years, 7 months ago) by zoff99
File size: 14451 byte(s)
v2.0.48
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.HashMap;
42 import java.util.Locale;
43
44 import android.annotation.SuppressLint;
45 import android.content.Intent;
46 import android.content.res.Configuration;
47 import android.media.AudioManager;
48 import android.os.AsyncTask;
49 import android.os.Bundle;
50 import android.os.Message;
51 import android.speech.tts.TextToSpeech;
52 import android.speech.tts.TextToSpeech.OnInitListener;
53 import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
54 import android.util.Log;
55 import android.view.View;
56
57 // new TTS, this is used now!
58 public class NavitSpeech2 implements TextToSpeech.OnInitListener, NavitActivityResult
59 {
60 private Navit navit;
61 int MY_DATA_CHECK_CODE = 1; // this needs to be "1" for the C-code !!
62 private Locale want_locale = null;
63 private int request_focus_result = 0;
64 private int need_audio_focus = 0;
65 float debug_lat = 0;
66 float debug_lon = 0;
67 HashMap<String, String> tts_params = new HashMap<String, String>();
68
69 @SuppressWarnings("deprecation")
70 public void onInit(final int status)
71 {
72 if (status == TextToSpeech.SUCCESS)
73 {
74 new Thread(new Runnable()
75 {
76 public void run()
77 {
78
79 try
80 {
81 tts_params.clear();
82 }
83 catch (Exception e)
84 {
85 }
86
87 try
88 {
89 tts_params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ZANaviUtterID");
90 }
91 catch (Exception e)
92 {
93 }
94
95 Log.e("NavitSpeech2", "onInit: Status " + status);
96 int result = -1;
97 try
98 {
99 // set the new locale here -----------------------------------
100 Locale locale2 = want_locale;
101 Locale.setDefault(locale2);
102 Configuration config2 = new Configuration();
103 config2.locale = locale2;
104 // set the new locale here -----------------------------------
105 try
106 {
107 result = navit.mTts.setLanguage(locale2);
108 }
109 catch (Exception e)
110 {
111 e.printStackTrace();
112 }
113
114 try
115 {
116 Log.e("NavitSpeech2", "3.1 want locale=" + locale2.getLanguage());
117 //Log.e("NavitSpeech2", "3 E=" + navit.mTts.getDefaultEngine());
118 //Log.e("NavitSpeech2", "3 def.enf.=" + navit.mTts.areDefaultsEnforced());
119 }
120 catch (NoSuchMethodError e2)
121 {
122 e2.printStackTrace();
123 }
124 catch (Exception e)
125 {
126 e.printStackTrace();
127 }
128
129 try
130 {
131 Log.e("NavitSpeech2", "3 lang. Country=" + navit.mTts.getLanguage().getDisplayCountry());
132 Log.e("NavitSpeech2", "3 lang. Country=" + navit.mTts.getLanguage().getDisplayLanguage());
133 Log.e("NavitSpeech2", "3 lang. Country=" + navit.mTts.getLanguage().getDisplayName());
134 Log.e("NavitSpeech2", "3 lang. Country=" + navit.mTts.getLanguage().getDisplayVariant());
135 }
136 catch (NoSuchMethodError e2)
137 {
138 e2.printStackTrace();
139 }
140 catch (Exception e)
141 {
142 e.printStackTrace();
143 }
144
145 String want_lang_code = locale2.getISO3Language();
146 Log.e("NavitSpeech2", "want:" + want_lang_code);
147 String will_use_lang_code = navit.mTts.getLanguage().getISO3Language();
148 Log.e("NavitSpeech2", "will use:" + will_use_lang_code);
149 if (want_lang_code.compareToIgnoreCase(will_use_lang_code) != 0)
150 {
151 result = TextToSpeech.LANG_NOT_SUPPORTED;
152 }
153
154 Log.e("NavitSpeech2", "3 ok result=" + result);
155 }
156 catch (Exception e1)
157 {
158 e1.printStackTrace();
159 result = TextToSpeech.LANG_NOT_SUPPORTED;
160 }
161
162 try
163 {
164 navit.mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener()
165 {
166
167 @Override
168 public void onUtteranceCompleted(String utteranceId)
169 {
170 try
171 {
172 if (need_audio_focus == 0)
173 {
174 Navit.NavitAudioManager.abandonAudioFocus(Navit.focusChangeListener);
175 }
176 }
177 catch (Exception e44)
178 {
179 }
180 // runOnUiThread(new Runnable()
181 // {
182 //
183 // @Override
184 // public void run()
185 // {
186 // //UI changes
187 // }
188 // });
189 }
190 });
191 }
192 catch (Exception e77)
193 {
194 e77.printStackTrace();
195 }
196
197 if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
198 {
199 // Lanuage data is missing or the language is not supported.
200 Log.e("NavitSpeech2", "3 Language is not available.");
201
202 try
203 {
204 // lang for TTS not found, show toast
205 Message msg = Navit.Navit_progress_h.obtainMessage();
206 Bundle b = new Bundle();
207 msg.what = 2;
208 b.putString("text", Navit.get_text("Language is not available for TTS! Using your phone's default settings")); //TRANS
209 msg.setData(b);
210 Navit.Navit_progress_h.sendMessage(msg);
211 }
212 catch (Exception e1)
213 {
214 e1.printStackTrace();
215 }
216 }
217 else
218 {
219 try
220 {
221 // lang for TTS not found, show toast
222 Message msg = Navit.Navit_progress_h.obtainMessage();
223 Bundle b = new Bundle();
224 msg.what = 2;
225 b.putString("text", Navit.get_text("Using Voice for:") + "\n" + navit.mTts.getLanguage().getDisplayName()); //TRANS
226 msg.setData(b);
227 Navit.Navit_progress_h.sendMessage(msg);
228 }
229 catch (Exception e1)
230 {
231 e1.printStackTrace();
232 }
233 }
234
235 }
236 }).start();
237
238 }
239 }
240
241 public void onActivityResult(int requestCode, int resultCode, Intent data)
242 {
243 Log.e("NavitSpeech2", "onActivityResult " + requestCode + " " + resultCode);
244
245 // disable for now ------------
246 // disable for now ------------
247
248 // try
249 // {
250 // if (requestCode == MY_DATA_CHECK_CODE)
251 // {
252 // if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
253 // {
254 // // success, create the TTS instance
255 // if (navit.mTts != null)
256 // {
257 // stop_me();
258 // }
259 // Log.e("NavitSpeech2", "init_me");
260 // mTts = new TextToSpeech(navit, this);
261 //
262 // try
263 // {
264 // // just for info -------
265 // Log.e("NavitSpeech2", Arrays.toString(Locale.getAvailableLocales()));
266 // // just for info -------
267 // }
268 // catch (Exception e)
269 // {
270 // e.printStackTrace();
271 // }
272 // }
273 // else
274 // {
275 // // missing data, install it
276 // Intent installIntent = new Intent();
277 // installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
278 // navit.startActivity(installIntent);
279 // }
280 // }
281 // }
282 // catch (Exception e)
283 // {
284 // e.printStackTrace();
285 // }
286
287 // disable for now ------------
288 // disable for now ------------
289
290 }
291
292 NavitSpeech2(Navit navit)
293 {
294 Log.e("NavitSpeech2", "constructor");
295 Log.e("NavitSpeech2", "locale=" + java.util.Locale.getDefault().getDisplayName());
296 want_locale = java.util.Locale.getDefault();
297 try
298 {
299 this.navit = navit;
300 //navit.setActivityResult(MY_DATA_CHECK_CODE, this);
301 }
302 catch (Exception e1)
303 {
304 e1.printStackTrace();
305 }
306
307 // try
308 // {
309 // Intent checkIntent = new Intent();
310 // checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
311 // navit.startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
312 // }
313 // catch (Exception e2)
314 // {
315 // e2.printStackTrace();
316 // }
317 }
318
319 @SuppressLint("NewApi")
320 public void say(String what, int lat, int lon)
321 {
322
323 if (Navit.p.PREF_enable_debug_write_gpx)
324 {
325 // ------- SPEECH DEBUG -------------------------------
326 // ------- SPEECH DEBUG -------------------------------
327 // ------- SPEECH DEBUG -------------------------------
328 if ((lat != 0) || (lon != 0))
329 {
330 debug_lat = (float) (lat) / 100000.0f;
331 debug_lon = (float) (lon) / 100000.0f;
332 }
333 else
334 {
335 debug_lat = 0;
336 debug_lon = 0;
337 }
338
339 if (!NavitVehicle.speech_recording_started)
340 {
341 NavitVehicle.speech_recording_start();
342 }
343 // System.out.println("SPEECH:J:lat=" + debug_lat + " lon=" + debug_lon);
344 NavitVehicle.speech_recording_add(debug_lat, debug_lon, what, System.currentTimeMillis());
345 // ------- SPEECH DEBUG -------------------------------
346 // ------- SPEECH DEBUG -------------------------------
347 // ------- SPEECH DEBUG -------------------------------
348 }
349
350 try
351 {
352 if (navit.mTts != null)
353 {
354
355 try
356 {
357 need_audio_focus = 1;
358 // Request audio focus for playback
359 request_focus_result = Navit.NavitAudioManager.requestAudioFocus(Navit.focusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
360 // AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK // --> other players just lower their audio volume
361
362 //if (request_focus_result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
363 //{
364 // // other app had stopped playing song now , so u can do u stuff now .
365 //}
366 }
367 catch (Exception e33)
368 {
369 }
370
371 if (Navit.p.PREF_speak_filter_special_chars)
372 {
373 what = filter_out_special_chars(what);
374 }
375
376 what = filter_out_special_chars_google(what);
377
378 //try
379 //{
380 //if (Integer.valueOf(android.os.Build.VERSION.SDK) < 21)
381 //{
382 navit.mTts.speak(what, TextToSpeech.QUEUE_FLUSH, tts_params);
383 //}
384 //else
385 //{
386 // navit.mTts.speak(what, TextToSpeech.QUEUE_FLUSH, null, "ZANavi.NAVSpeech");
387 //}
388 //}
389 //catch (Exception e)
390 //{
391 // navit.mTts.speak(what, TextToSpeech.QUEUE_FLUSH, tts_params);
392 //}
393
394 need_audio_focus = 0;
395
396 if (NavitGraphics.NavitMsgTv2_.getVisibility() == View.VISIBLE)
397 {
398 Navit.set_debug_messages_say_wrapper("SAY:" + what + "\n");
399 }
400
401 if (Navit.p.PREF_show_debug_messages)
402 {
403 Navit.set_debug_messages3_wrapper(what);
404 }
405 }
406 }
407 catch (Exception e)
408 {
409 e.printStackTrace();
410 }
411 }
412
413 public static String filter_out_special_chars_google(String in)
414 {
415 String out = in;
416 // google TTS seems to say "Staatsstrasse" instead of "St." , try to correct it here
417 // nice when you want to go to "St. Pölten" :-)
418 out = out.replace("St.", "St ");
419 return out;
420 }
421
422 public static String filter_out_special_chars(String in)
423 {
424 String out = in;
425 out = out.replace("-", " ");
426 out = out.replace("\"", "");
427 out = out.replace("'", "");
428 out = out.replace("\\n", "");
429 out = out.replace("\\r", "");
430 out = out.replace("\\\\", "");
431 return out;
432 }
433
434 public static String filter_out_special_chars_for_dest_string(String in)
435 {
436 String out = in;
437 out = out.replace(" ", "_");
438 out = out.replace("\\t", "_");
439 out = out.replace("\"", "_");
440 out = out.replace("'", "_");
441 out = out.replace(",", "_");
442 out = out.replace(".", "_");
443 out = out.replace("-", "_");
444 out = out.replace("\\n", "_");
445 out = out.replace("\\r", "_");
446 out = out.replace("\\\\", "_");
447 return out;
448 }
449
450 public void resume_me()
451 {
452 // if (Navit.METHOD_DEBUG) Navit.my_func_name(0);
453
454 Log.e("NavitSpeech2", "resume_me");
455
456 if (navit.mTts != null)
457 {
458 // try
459 // {
460 // Log.e("NavitSpeech2", "resume_me 00a mTts=" + navit.mTts);
461 // navit.mTts.shutdown();
462 // Log.e("NavitSpeech2", "resume_me 00b");
463 // }
464 // catch (Exception e)
465 // {
466 // e.printStackTrace();
467 // }
468 //
469 // Log.e("NavitSpeech2", "resume_me 001");
470 //
471 // try
472 // {
473 // navit.mTts.shutdown();
474 // }
475 // catch (Exception e)
476 // {
477 // e.printStackTrace();
478 // }
479 //
480 // Log.e("NavitSpeech2", "resume_me 002");
481 //
482 // try
483 // {
484 // navit.mTts.shutdown();
485 // }
486 // catch (Exception e)
487 // {
488 // e.printStackTrace();
489 // }
490
491 }
492 else
493 {
494 Log.e("NavitSpeech2", "resume_me 003");
495
496 try
497 {
498 // navit.mTts = new TextToSpeech(navit, this);
499 new startup_in_other_thread().execute(this);
500 }
501 catch (Exception e)
502 {
503 e.printStackTrace();
504 }
505 }
506
507 Log.e("NavitSpeech2", "resume_me finished");
508
509 // if (Navit.METHOD_DEBUG) Navit.my_func_name(1);
510 }
511
512 private class startup_in_other_thread extends AsyncTask<Object, Void, String>
513 {
514
515 @Override
516 protected String doInBackground(Object... params)
517 {
518 // if (Navit.METHOD_DEBUG) Navit.my_func_name(0);
519 // try
520 // {
521 // Thread.sleep(6000);
522 // }
523 // catch (InterruptedException e)
524 // {
525 // }
526 navit.mTts = new TextToSpeech(navit, (OnInitListener) params[0]);
527
528 // if (Navit.METHOD_DEBUG) Navit.my_func_name(1);
529 return "Executed";
530 }
531
532 @Override
533 protected void onPostExecute(String result)
534 {
535 }
536
537 @Override
538 protected void onPreExecute()
539 {
540 }
541
542 @Override
543 protected void onProgressUpdate(Void... values)
544 {
545 }
546 }
547
548 public void stop_me()
549 {
550 Log.e("NavitSpeech2", "stop_me");
551 try
552 {
553 navit.mTts.shutdown();
554 }
555 catch (Exception e)
556 {
557 e.printStackTrace();
558 }
559
560 try
561 {
562 navit.mTts.shutdown();
563 }
564 catch (Exception e)
565 {
566 e.printStackTrace();
567 }
568
569 try
570 {
571 navit.mTts.shutdown();
572 }
573 catch (Exception e)
574 {
575 e.printStackTrace();
576 }
577
578 // mTts = null;
579 }
580 }

   
Visit the ZANavi Wiki