OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser; | 5 package org.chromium.chrome.browser; |
6 | 6 |
| 7 import android.content.Context; |
7 import android.os.Build; | 8 import android.os.Build; |
8 import android.speech.tts.TextToSpeech; | 9 import android.speech.tts.TextToSpeech; |
9 import android.speech.tts.UtteranceProgressListener; | 10 import android.speech.tts.UtteranceProgressListener; |
10 | 11 |
11 import org.chromium.base.ContextUtils; | |
12 import org.chromium.base.ThreadUtils; | 12 import org.chromium.base.ThreadUtils; |
13 import org.chromium.base.TraceEvent; | 13 import org.chromium.base.TraceEvent; |
14 import org.chromium.base.annotations.CalledByNative; | 14 import org.chromium.base.annotations.CalledByNative; |
15 | 15 |
16 import java.util.ArrayList; | 16 import java.util.ArrayList; |
17 import java.util.HashMap; | 17 import java.util.HashMap; |
18 import java.util.Locale; | 18 import java.util.Locale; |
19 | 19 |
20 /** | 20 /** |
21 * This class is the Java counterpart to the C++ TtsPlatformImplAndroid class. | 21 * This class is the Java counterpart to the C++ TtsPlatformImplAndroid class. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 float mVolume; | 61 float mVolume; |
62 } | 62 } |
63 | 63 |
64 private long mNativeTtsPlatformImplAndroid; | 64 private long mNativeTtsPlatformImplAndroid; |
65 protected final TextToSpeech mTextToSpeech; | 65 protected final TextToSpeech mTextToSpeech; |
66 private boolean mInitialized; | 66 private boolean mInitialized; |
67 private ArrayList<TtsVoice> mVoices; | 67 private ArrayList<TtsVoice> mVoices; |
68 private String mCurrentLanguage; | 68 private String mCurrentLanguage; |
69 private PendingUtterance mPendingUtterance; | 69 private PendingUtterance mPendingUtterance; |
70 | 70 |
71 protected TtsPlatformImpl(long nativeTtsPlatformImplAndroid) { | 71 protected TtsPlatformImpl(long nativeTtsPlatformImplAndroid, Context context
) { |
72 mInitialized = false; | 72 mInitialized = false; |
73 mNativeTtsPlatformImplAndroid = nativeTtsPlatformImplAndroid; | 73 mNativeTtsPlatformImplAndroid = nativeTtsPlatformImplAndroid; |
74 mTextToSpeech = new TextToSpeech( | 74 mTextToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListene
r() { |
75 ContextUtils.getApplicationContext(), new TextToSpeech.OnInitLis
tener() { | 75 @Override |
76 @Override | 76 public void onInit(int status) { |
77 public void onInit(int status) { | 77 if (status == TextToSpeech.SUCCESS) { |
78 if (status == TextToSpeech.SUCCESS) { | 78 ThreadUtils.runOnUiThread(new Runnable() { |
79 ThreadUtils.runOnUiThread(new Runnable() { | 79 @Override |
80 @Override | 80 public void run() { |
81 public void run() { | 81 initialize(); |
82 initialize(); | 82 } |
83 } | 83 }); |
84 }); | |
85 } | |
86 } | 84 } |
87 }); | 85 } |
| 86 }); |
88 addOnUtteranceProgressListener(); | 87 addOnUtteranceProgressListener(); |
89 } | 88 } |
90 | 89 |
91 /** | 90 /** |
92 * Create a TtsPlatformImpl object, which is owned by TtsPlatformImplAndroid | 91 * Create a TtsPlatformImpl object, which is owned by TtsPlatformImplAndroid |
93 * on the C++ side. | 92 * on the C++ side. |
94 * @param nativeTtsPlatformImplAndroid The C++ object that owns us. | |
95 * | 93 * |
| 94 * @param nativeTtsPlatformImplAndroid The C++ object that owns us. |
| 95 * @param context The app context. |
96 */ | 96 */ |
97 @CalledByNative | 97 @CalledByNative |
98 private static TtsPlatformImpl create(long nativeTtsPlatformImplAndroid) { | 98 private static TtsPlatformImpl create(long nativeTtsPlatformImplAndroid, |
| 99 Context context) { |
99 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | 100 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
100 return new LollipopTtsPlatformImpl(nativeTtsPlatformImplAndroid); | 101 return new LollipopTtsPlatformImpl(nativeTtsPlatformImplAndroid, con
text); |
101 } else { | 102 } else { |
102 return new TtsPlatformImpl(nativeTtsPlatformImplAndroid); | 103 return new TtsPlatformImpl(nativeTtsPlatformImplAndroid, context); |
103 } | 104 } |
104 } | 105 } |
105 | 106 |
106 /** | 107 /** |
107 * Called when our C++ counterpoint is deleted. Clear the handle to our | 108 * Called when our C++ counterpoint is deleted. Clear the handle to our |
108 * native C++ object, ensuring it's never called. | 109 * native C++ object, ensuring it's never called. |
109 */ | 110 */ |
110 @CalledByNative | 111 @CalledByNative |
111 private void destroy() { | 112 private void destroy() { |
112 mNativeTtsPlatformImplAndroid = 0; | 113 mNativeTtsPlatformImplAndroid = 0; |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 if (mPendingUtterance != null) mPendingUtterance.speak(); | 318 if (mPendingUtterance != null) mPendingUtterance.speak(); |
318 | 319 |
319 TraceEvent.end("TtsPlatformImpl:initialize"); | 320 TraceEvent.end("TtsPlatformImpl:initialize"); |
320 } | 321 } |
321 | 322 |
322 private native void nativeVoicesChanged(long nativeTtsPlatformImplAndroid); | 323 private native void nativeVoicesChanged(long nativeTtsPlatformImplAndroid); |
323 private native void nativeOnEndEvent(long nativeTtsPlatformImplAndroid, int
utteranceId); | 324 private native void nativeOnEndEvent(long nativeTtsPlatformImplAndroid, int
utteranceId); |
324 private native void nativeOnStartEvent(long nativeTtsPlatformImplAndroid, in
t utteranceId); | 325 private native void nativeOnStartEvent(long nativeTtsPlatformImplAndroid, in
t utteranceId); |
325 private native void nativeOnErrorEvent(long nativeTtsPlatformImplAndroid, in
t utteranceId); | 326 private native void nativeOnErrorEvent(long nativeTtsPlatformImplAndroid, in
t utteranceId); |
326 } | 327 } |
OLD | NEW |