EMMA Coverage Report (generated Fri Aug 23 16:39:17 PDT 2013)
[all classes][org.chromium.chrome.browser]

COVERAGE SUMMARY FOR SOURCE FILE [TtsPlatformImpl.java]

nameclass, %method, %block, %line, %
TtsPlatformImpl.java0%   (0/8)0%   (0/34)0%   (0/466)0%   (0/75)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class TtsPlatformImpl0%   (0/1)0%   (0/16)0%   (0/316)0%   (0/50)
<static initializer> 0%   (0/1)0%   (0/8)0%   (0/1)
TtsPlatformImpl (int, Context): void 0%   (0/1)0%   (0/27)0%   (0/6)
access$000 (TtsPlatformImpl): void 0%   (0/1)0%   (0/3)0%   (0/1)
access$100 (TtsPlatformImpl): int 0%   (0/1)0%   (0/3)0%   (0/1)
access$200 (TtsPlatformImpl, int, int): void 0%   (0/1)0%   (0/5)0%   (0/1)
access$300 (TtsPlatformImpl, int, int): void 0%   (0/1)0%   (0/5)0%   (0/1)
access$400 (TtsPlatformImpl, int, int): void 0%   (0/1)0%   (0/5)0%   (0/1)
create (int, Context): TtsPlatformImpl 0%   (0/1)0%   (0/6)0%   (0/1)
destroy (): void 0%   (0/1)0%   (0/4)0%   (0/2)
getVoiceCount (): int 0%   (0/1)0%   (0/14)0%   (0/2)
getVoiceLanguage (int): String 0%   (0/1)0%   (0/17)0%   (0/2)
getVoiceName (int): String 0%   (0/1)0%   (0/17)0%   (0/2)
initialize (): void 0%   (0/1)0%   (0/113)0%   (0/18)
isInitialized (): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
speak (int, String, String, float, float, float): boolean 0%   (0/1)0%   (0/71)0%   (0/12)
stop (): void 0%   (0/1)0%   (0/15)0%   (0/3)
     
class TtsPlatformImpl$10%   (0/1)0%   (0/2)0%   (0/14)0%   (0/4)
TtsPlatformImpl$1 (TtsPlatformImpl): void 0%   (0/1)0%   (0/6)0%   (0/1)
onInit (int): void 0%   (0/1)0%   (0/8)0%   (0/3)
     
class TtsPlatformImpl$1$10%   (0/1)0%   (0/2)0%   (0/11)0%   (0/3)
TtsPlatformImpl$1$1 (TtsPlatformImpl$1): void 0%   (0/1)0%   (0/6)0%   (0/1)
run (): void 0%   (0/1)0%   (0/5)0%   (0/2)
     
class TtsPlatformImpl$20%   (0/1)0%   (0/4)0%   (0/27)0%   (0/7)
TtsPlatformImpl$2 (TtsPlatformImpl): void 0%   (0/1)0%   (0/6)0%   (0/1)
onDone (String): void 0%   (0/1)0%   (0/7)0%   (0/2)
onError (String): void 0%   (0/1)0%   (0/7)0%   (0/2)
onStart (String): void 0%   (0/1)0%   (0/7)0%   (0/2)
     
class TtsPlatformImpl$2$10%   (0/1)0%   (0/2)0%   (0/26)0%   (0/4)
TtsPlatformImpl$2$1 (TtsPlatformImpl$2, String): void 0%   (0/1)0%   (0/9)0%   (0/1)
run (): void 0%   (0/1)0%   (0/17)0%   (0/3)
     
class TtsPlatformImpl$2$20%   (0/1)0%   (0/2)0%   (0/26)0%   (0/4)
TtsPlatformImpl$2$2 (TtsPlatformImpl$2, String): void 0%   (0/1)0%   (0/9)0%   (0/1)
run (): void 0%   (0/1)0%   (0/17)0%   (0/3)
     
class TtsPlatformImpl$2$30%   (0/1)0%   (0/2)0%   (0/26)0%   (0/4)
TtsPlatformImpl$2$3 (TtsPlatformImpl$2, String): void 0%   (0/1)0%   (0/9)0%   (0/1)
run (): void 0%   (0/1)0%   (0/17)0%   (0/3)
     
class TtsPlatformImpl$TtsVoice0%   (0/1)0%   (0/4)0%   (0/20)0%   (0/5)
TtsPlatformImpl$TtsVoice (String, String): void 0%   (0/1)0%   (0/9)0%   (0/4)
TtsPlatformImpl$TtsVoice (String, String, TtsPlatformImpl$1): void 0%   (0/1)0%   (0/5)0%   (0/1)
access$500 (TtsPlatformImpl$TtsVoice): String 0%   (0/1)0%   (0/3)0%   (0/1)
access$600 (TtsPlatformImpl$TtsVoice): String 0%   (0/1)0%   (0/3)0%   (0/1)

1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4 
5package org.chromium.chrome.browser;
6 
7import android.content.Context;
8import android.speech.tts.TextToSpeech;
9import android.speech.tts.UtteranceProgressListener;
10import java.lang.Double;
11import java.lang.Integer;
12import java.util.ArrayList;
13import java.util.HashMap;
14import java.util.Locale;
15import org.chromium.base.CalledByNative;
16import org.chromium.base.ThreadUtils;
17 
18/**
19 * This class is the Java counterpart to the C++ TtsPlatformImplAndroid class.
20 * It implements the Android-native text-to-speech code to support the web
21 * speech synthesis API.
22 *
23 * Threading model note: all calls from C++ must happen on the UI thread.
24 * Callbacks from Android may happen on a different thread, so we always
25 * use ThreadUtils.runOnUiThread when calling back to C++.
26 */
27class TtsPlatformImpl {
28    private static class TtsVoice {
29        private TtsVoice(String name, String language) {
30            mName = name;
31            mLanguage = language;
32        }
33        private final String mName;
34        private final String mLanguage;
35    };
36 
37    private int mNativeTtsPlatformImplAndroid;
38    private final TextToSpeech mTextToSpeech;
39    private boolean mInitialized;
40    private ArrayList<TtsVoice> mVoices;
41    private String mCurrentLanguage;
42 
43    private TtsPlatformImpl(int nativeTtsPlatformImplAndroid, Context context) {
44        mInitialized = false;
45        mNativeTtsPlatformImplAndroid = nativeTtsPlatformImplAndroid;
46        mTextToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
47                public void onInit(int status) {
48                    if (status == TextToSpeech.SUCCESS) {
49                        ThreadUtils.runOnUiThread(new Runnable() {
50                            @Override
51                            public void run() {
52                                initialize();
53                            }
54                        });
55                    }
56                }
57            });
58        mTextToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
59                public void onDone(final String utteranceId) {
60                    ThreadUtils.runOnUiThread(new Runnable() {
61                        @Override
62                        public void run() {
63                            if (mNativeTtsPlatformImplAndroid != 0) {
64                                nativeOnEndEvent(mNativeTtsPlatformImplAndroid,
65                                                 Integer.parseInt(utteranceId));
66                            }
67                        }
68                    });
69                }
70 
71                public void onError(final String utteranceId) {
72                    ThreadUtils.runOnUiThread(new Runnable() {
73                        @Override
74                        public void run() {
75                            if (mNativeTtsPlatformImplAndroid != 0) {
76                                nativeOnErrorEvent(mNativeTtsPlatformImplAndroid,
77                                                   Integer.parseInt(utteranceId));
78                            }
79                        }
80                    });
81                }
82 
83                public void onStart(final String utteranceId) {
84                    ThreadUtils.runOnUiThread(new Runnable() {
85                        @Override
86                        public void run() {
87                            if (mNativeTtsPlatformImplAndroid != 0) {
88                                nativeOnStartEvent(mNativeTtsPlatformImplAndroid,
89                                                   Integer.parseInt(utteranceId));
90                            }
91                        }
92                    });
93                }
94            });
95    };
96 
97    /**
98     * Create a TtsPlatformImpl object, which is owned by TtsPlatformImplAndroid
99     * on the C++ side.
100     *
101     * @param nativeTtsPlatformImplAndroid The C++ object that owns us.
102     * @param context The app context.
103     */
104    @CalledByNative
105    private static TtsPlatformImpl create(int nativeTtsPlatformImplAndroid,
106                                          Context context) {
107        return new TtsPlatformImpl(nativeTtsPlatformImplAndroid, context);
108    }
109 
110    /**
111     * Called when our C++ counterpoint is deleted. Clear the handle to our
112     * native C++ object, ensuring it's never called.
113     */
114    @CalledByNative
115    private void destroy() {
116        mNativeTtsPlatformImplAndroid = 0;
117    }
118 
119    /**
120     * @return true if our TextToSpeech object is initialized and we've
121     * finished scanning the list of voices.
122     */
123    @CalledByNative
124    private boolean isInitialized() {
125        return mInitialized;
126    }
127 
128    /**
129     * @return the number of voices.
130     */
131    @CalledByNative
132    private int getVoiceCount() {
133        assert mInitialized == true;
134        return mVoices.size();
135    }
136 
137    /**
138     * @return the name of the voice at a given index.
139     */
140    @CalledByNative
141    private String getVoiceName(int voiceIndex) {
142        assert mInitialized == true;
143        return mVoices.get(voiceIndex).mName;
144    }
145 
146    /**
147     * @return the language of the voice at a given index.
148     */
149    @CalledByNative
150    private String getVoiceLanguage(int voiceIndex) {
151        assert mInitialized == true;
152        return mVoices.get(voiceIndex).mLanguage;
153    }
154 
155    /**
156     * Attempt to start speaking an utterance. If it returns true, will call back on
157     * start and end.
158     *
159     * @param utteranceId A unique id for this utterance so that callbacks can be tied
160     *     to a particular utterance.
161     * @param text The text to speak.
162     * @param lang The language code for the text (e.g., "en-US").
163     * @param rate The speech rate, in the units expected by Android TextToSpeech.
164     * @param pitch The speech pitch, in the units expected by Android TextToSpeech.
165     * @param volume The speech volume, in the units expected by Android TextToSpeech.
166     * @return true on success.
167     */
168    @CalledByNative
169    private boolean speak(int utteranceId, String text, String lang,
170                          float rate, float pitch, float volume) {
171        assert mInitialized == true;
172        if (!lang.equals(mCurrentLanguage)) {
173            mTextToSpeech.setLanguage(new Locale(lang));
174            mCurrentLanguage = lang;
175        }
176 
177        mTextToSpeech.setSpeechRate(rate);
178        mTextToSpeech.setPitch(pitch);
179        HashMap<String, String> params = new HashMap<String, String>();
180        if (volume != 1.0) {
181            params.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, Double.toString(volume));
182        }
183        params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, Integer.toString(utteranceId));
184        int result = mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, params);
185        return (result == TextToSpeech.SUCCESS);
186    }
187 
188    /**
189     * Stop the current utterance.
190     */
191    @CalledByNative
192    private void stop() {
193        assert mInitialized == true;
194        mTextToSpeech.stop();
195    }
196 
197    /**
198     * Note: we enforce that this method is called on the UI thread, so
199     * we can call nativeVoicesChanged directly.
200     */
201    private void initialize() {
202        assert mNativeTtsPlatformImplAndroid != 0;
203 
204        // Note: Android supports multiple speech engines, but querying the
205        // metadata about all of them is expensive. So we deliberately only
206        // support the default speech engine, and expose the different
207        // supported languages for the default engine as different voices.
208        String defaultEngineName = mTextToSpeech.getDefaultEngine();
209        String engineLabel = defaultEngineName;
210        for (TextToSpeech.EngineInfo info : mTextToSpeech.getEngines()) {
211            if (info.name.equals(defaultEngineName)) engineLabel = info.label;
212        }
213        Locale[] locales = Locale.getAvailableLocales();
214        mVoices = new ArrayList<TtsVoice>();
215        for (int i = 0; i < locales.length; ++i) {
216            if (!locales[i].getVariant().isEmpty()) continue;
217            if (mTextToSpeech.isLanguageAvailable(locales[i]) > 0) {
218                String name = locales[i].getDisplayLanguage();
219                if (!locales[i].getCountry().isEmpty()) {
220                    name += " " + locales[i].getDisplayCountry();
221                }
222                TtsVoice voice = new TtsVoice(name, locales[i].toString());
223                mVoices.add(voice);
224            }
225        }
226 
227        mInitialized = true;
228        nativeVoicesChanged(mNativeTtsPlatformImplAndroid);
229    }
230 
231    private native void nativeVoicesChanged(int nativeTtsPlatformImplAndroid);
232    private native void nativeOnEndEvent(int nativeTtsPlatformImplAndroid, int utteranceId);
233    private native void nativeOnStartEvent(int nativeTtsPlatformImplAndroid, int utteranceId);
234    private native void nativeOnErrorEvent(int nativeTtsPlatformImplAndroid, int utteranceId);
235}

[all classes][org.chromium.chrome.browser]
EMMA 2.0.5312 (C) Vladimir Roubtsov