summaryrefslogtreecommitdiff
path: root/navit/android/src/org/navitproject/navit/NavitSpeech2.java
blob: 6fc7f1d5f7bb32da2ff8ed7a8b935e0bfeb66b2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
  Navit, a modular navigation system.
  Copyright (C) 2005-2008 Navit Team

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  version 2 as published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA  02110-1301, USA.
 */

package org.navitproject.navit;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.speech.tts.TextToSpeech;
import android.util.Log;


@SuppressWarnings("unused")
class NavitSpeech2 implements TextToSpeech.OnInitListener, NavitActivityResult {
    private TextToSpeech mTts;
    private final Navit mNavit;
    private static final int MY_DATA_CHECK_CODE = 1;
    private static final String TAG = NavitSpeech2.class.getName();

    public void onInit(int status) {
        Log.d(TAG,"Status " + status);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG,"onActivityResult " + requestCode + " " + resultCode);
        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // success, create the TTS instance
                mTts = new TextToSpeech(mNavit, this);
            } else {
                // missing data, ask to install it
                AlertDialog.Builder builder = new AlertDialog.Builder(mNavit);
                builder.setTitle(R.string.TTS_title_data_missing).setMessage(R.string.TTS_qery_install_data)
                        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                Intent installIntent = new Intent();
                                installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                                mNavit.startActivity(installIntent);
                            }
                        }).setNegativeButton(R.string.no, null).show();
            }
        }
    }

    NavitSpeech2(Navit navit) {
        this.mNavit = navit;
        navit.setActivityResult(1, this);
        Log.d(TAG,"Create");
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        if (navit.getPackageManager().resolveActivity(checkIntent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
            Log.d(TAG,"ACTION_CHECK_TTS_DATA available");
            navit.startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
        } else {
            Log.e(TAG,"ACTION_CHECK_TTS_DATA not available, assume tts is working");
            mTts = new TextToSpeech(navit, this);
        }
    }

    void say(String what) {
        if (mTts != null) {
            mTts.speak(what, TextToSpeech.QUEUE_FLUSH, null);
        }
    }
}