Chromium Code Reviews| Index: content/public/android/java/src/org/chromium/content/browser/SpeechRecognition.java |
| diff --git a/content/public/android/java/src/org/chromium/content/browser/SpeechRecognition.java b/content/public/android/java/src/org/chromium/content/browser/SpeechRecognition.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f23064c05615634afeab5c2cb22541df4650187a |
| --- /dev/null |
| +++ b/content/public/android/java/src/org/chromium/content/browser/SpeechRecognition.java |
| @@ -0,0 +1,116 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.content.browser; |
| + |
| +import android.content.Context; |
| +import android.content.Intent; |
| +import android.os.Bundle; |
| +import android.speech.RecognitionListener; |
| +import android.speech.RecognizerIntent; |
| +import android.speech.SpeechRecognizer; |
| +import android.widget.Toast; |
| + |
| +import org.chromium.base.CalledByNative; |
| +import org.chromium.base.JNINamespace; |
| +import org.chromium.base.ThreadUtils; |
| + |
| +import java.util.ArrayList; |
| +import java.util.concurrent.FutureTask; |
| + |
| +/** |
| + * Android speech recognition adapter. |
| + */ |
| +@JNINamespace("content") |
| +class SpeechRecognition { |
| + |
| + private Intent intent = null; |
| + private Context mContext = null; |
| + private SpeechRecognizer recognizer = null; |
| + private RecognitionListener listener = null; |
| + private int mNativeSpeechRecognizerImplAndroid = 0; |
|
Primiano Tucci (use gerrit)
2013/06/03 16:24:11
You'll need a reviewer for someone more experience
|
| + |
| + class Listener implements RecognitionListener { |
| + @Override |
| + public void onBeginningOfSpeech() { |
| + nativeOnSoundStartJNI(mNativeSpeechRecognizerImplAndroid); |
| + } |
| + @Override |
|
Primiano Tucci (use gerrit)
2013/06/03 16:24:11
Nit: \n? (here and below)
|
| + public void onBufferReceived(byte[] buffer) { } |
| + @Override |
| + public void onEndOfSpeech() { |
| + nativeOnSoundEndJNI(mNativeSpeechRecognizerImplAndroid); |
| + } |
| + @Override |
| + public void onError(int error) { |
| + nativeOnRecognitionErrorJNI(mNativeSpeechRecognizerImplAndroid, error); |
| + } |
| + @Override |
| + public void onEvent(int event, Bundle bundle) { } |
| + @Override |
| + public void onPartialResults(Bundle bundle) { |
| + handleResults(bundle, true); |
| + } |
| + @Override |
| + public void onReadyForSpeech(Bundle bundle) { |
| + nativeOnAudioStartJNI(mNativeSpeechRecognizerImplAndroid); |
| + } |
| + @Override |
| + public void onResults(Bundle bundle) { |
| + handleResults(bundle, false); |
| + // FIXME call recognizer.destroy() ? |
|
janx
2013/05/31 17:22:24
I am not sure when it makes the most sense to dest
Primiano Tucci (use gerrit)
2013/06/03 16:24:11
Just before calling the OnRecognitionEndJNI method
|
| + } |
| + @Override |
| + public void onRmsChanged(float rms) { |
| + nativeOnAudioLevelsChangeJNI(mNativeSpeechRecognizerImplAndroid, rms); |
|
Primiano Tucci (use gerrit)
2013/06/03 16:24:11
See my previous comments, I think you can remove c
|
| + } |
| + public void handleResults(Bundle bundle, boolean partial) { |
| + ArrayList<String> results = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); |
| + String[] strings = results.toArray(new String[results.size()]); |
|
Primiano Tucci (use gerrit)
2013/06/03 16:24:11
s/strings/transcripts/
|
| + float[] floats = bundle.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES); |
|
Primiano Tucci (use gerrit)
2013/06/03 16:24:11
s/floats/scores/
|
| + nativeOnRecognitionResultsJNI(mNativeSpeechRecognizerImplAndroid, strings, floats, partial); |
| + } |
| + } |
| + |
| + private SpeechRecognition(final Context context, int nativeSpeechRecognizerImplAndroid) { |
| + mContext = context; |
| + mNativeSpeechRecognizerImplAndroid = nativeSpeechRecognizerImplAndroid; |
| + listener = new Listener(); |
| + intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
| + recognizer = SpeechRecognizer.createSpeechRecognizer(mContext); |
| + recognizer.setRecognitionListener(listener); |
| + } |
| + |
| + @CalledByNative |
| + public static SpeechRecognition createSpeechRecognition(Context context, int nativeSpeechRecognizerImplAndroid) { |
| + return new SpeechRecognition(context, nativeSpeechRecognizerImplAndroid); |
| + } |
| + |
| + @CalledByNative |
| + public void StartRecognition(boolean continuous, boolean partial_results) { |
| + // TODO(janx): Uncomment following line when the EXTRA is supported. |
| + //intent.putExtra(RecognizerIntent.EXTRA_CONTINUOUS, continuous); |
| + intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, partial_results); |
| + recognizer.startListening(intent); |
| + } |
| + |
| + @CalledByNative |
| + public void CancelRecognition() { |
| + recognizer.cancel(); |
| + // FIXME call recognizer.destroy() ? |
|
janx
2013/05/31 17:22:24
ditto
Primiano Tucci (use gerrit)
2013/06/03 16:24:11
As in my previous comment, s/CancelRecognition/Abo
|
| + } |
| + |
| + @CalledByNative |
| + public void StopRecognition() { |
| + recognizer.stopListening(); |
| + // FIXME call recognizer.destroy() ? |
|
janx
2013/05/31 17:22:24
ditto
Primiano Tucci (use gerrit)
2013/06/03 16:24:11
Definitely not here. On StopRecognition we are int
|
| + } |
| + |
| + private native void nativeOnAudioStartJNI(int nativeSpeechRecognizerImplAndroid); |
| + private native void nativeOnSoundStartJNI(int nativeSpeechRecognizerImplAndroid); |
| + private native void nativeOnSoundEndJNI(int nativeSpeechRecognizerImplAndroid); |
| + private native void nativeOnRecognitionResultsJNI(int nativeSpeechRecognizerImplAndroid, String[] strings, float[] floats, boolean partial); |
| + private native void nativeOnRecognitionErrorJNI(int nativeSpeechRecognizerImplAndroid, int error); |
| + private native void nativeOnAudioLevelsChangeJNI(int nativeSpeechRecognizerImplAndroid, float rms); |
|
janx
2013/05/31 17:22:24
Those lines are more than 80 characters and the pr
Primiano Tucci (use gerrit)
2013/06/03 16:24:11
I'm fine with no wrapping if there are other simil
|
| +} |