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..ac3c9b3467ccd8cc207472106ec6f68002b060b5 |
| --- /dev/null |
| +++ b/content/public/android/java/src/org/chromium/content/browser/SpeechRecognition.java |
| @@ -0,0 +1,141 @@ |
| +// 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 org.chromium.base.CalledByNative; |
| +import org.chromium.base.JNINamespace; |
| + |
| +import java.util.ArrayList; |
| + |
| +/** |
| + * This class uses Android's SpeechRecognizer to perform speech recognition for |
| + * the Web Speech API on Android. Using Android's platform recognizer offers |
| + * several benefits, like good quality and good local fallback when no data |
| + * connection is available. |
| + */ |
| +@JNINamespace("content") |
| +class SpeechRecognition { |
|
janx
2013/06/07 12:43:58
Peter Beverloo 2013/06/06 11:48:13
In general, I f
|
| + |
| + private Intent mIntent = null; |
| + private Context mContext = null; |
| + private boolean mContinuous = false; |
| + private SpeechRecognizer mRecognizer = null; |
| + private RecognitionListener mListener = null; |
| + private int mNativeSpeechRecognizerImplAndroid = 0; |
|
janx
2013/06/07 12:43:58
Primiano Tucci 2013/06/03 16:24:11
You'll need a r
bulach
2013/06/07 15:35:10
I suppose almost all of these could be final and i
janx
2013/06/10 16:51:18
I moved all initializations into the ctor and made
|
| + |
| + class Listener implements RecognitionListener { |
| + @Override |
| + public void onBeginningOfSpeech() { |
| + nativeOnSoundStartJNI(mNativeSpeechRecognizerImplAndroid); |
| + } |
| + |
| + @Override |
| + public void onBufferReceived(byte[] buffer) { } |
| + |
| + @Override |
| + public void onEndOfSpeech() { |
| + nativeOnSoundEndJNI(mNativeSpeechRecognizerImplAndroid); |
| + // Since Android doesn't have a dedicated event for when audio |
| + // capture is finished, we fire it after speech has ended. |
| + nativeOnAudioEndJNI(mNativeSpeechRecognizerImplAndroid); |
| + } |
| + |
| + @Override |
| + public void onError(int error) { |
| + nativeOnRecognitionErrorJNI(mNativeSpeechRecognizerImplAndroid, error); |
| + terminate(); |
| + } |
| + |
| + @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); |
| + if (!mContinuous) |
| + terminate(); |
| + } |
| + |
| + @Override |
| + public void onRmsChanged(float rms) { } |
| + |
| + private void handleResults(Bundle bundle, boolean provisional) { |
| + ArrayList<String> list = |
| + bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); |
| + String[] results = list.toArray(new String[list.size()]); |
| + float[] scores = bundle.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES); |
| + nativeOnRecognitionResultsJNI(mNativeSpeechRecognizerImplAndroid, results, scores, |
| + provisional); |
| + } |
| + } |
| + |
| + private SpeechRecognition(final Context context, int nativeSpeechRecognizerImplAndroid) { |
| + mContext = context; |
| + mNativeSpeechRecognizerImplAndroid = nativeSpeechRecognizerImplAndroid; |
| + mListener = new Listener(); |
| + mIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
| + mRecognizer = SpeechRecognizer.createSpeechRecognizer(mContext); |
| + mRecognizer.setRecognitionListener(mListener); |
| + } |
| + |
| + private void terminate() { |
| + mRecognizer.destroy(); |
| + mRecognizer = null; |
| + nativeOnRecognitionEndJNI(mNativeSpeechRecognizerImplAndroid); |
| + } |
| + |
| + @CalledByNative |
| + public static SpeechRecognition createSpeechRecognition(Context context, |
|
bulach
2013/06/07 15:35:10
nit: we tend to keep @CalledByNative private, sinc
janx
2013/06/10 16:51:18
Ok thanks, changed to private. I wasn't sure how t
|
| + int nativeSpeechRecognizerImplAndroid) { |
| + return new SpeechRecognition(context, nativeSpeechRecognizerImplAndroid); |
| + } |
| + |
| + @CalledByNative |
| + public void StartRecognition(boolean continuous, boolean interim_results) { |
|
bulach
2013/06/07 15:35:10
nit:s/Start/start/, below s/Abort/abort/ and s/Sto
janx
2013/06/10 16:51:18
Fixed.
|
| + mContinuous = continuous; |
| + mIntent.putExtra("android.speech.extra.DICTATION_MODE", continuous); |
| + mIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, interim_results); |
| + mRecognizer.startListening(mIntent); |
| + } |
| + |
| + @CalledByNative |
| + public void AbortRecognition() { |
| + mRecognizer.cancel(); |
| + terminate(); |
| + } |
| + |
| + @CalledByNative |
| + public void StopRecognition() { |
| + mContinuous = false; |
| + mRecognizer.stopListening(); |
| + } |
| + |
| + private native void nativeOnAudioStartJNI(int nativeSpeechRecognizerImplAndroid); |
|
bulach
2013/06/07 15:35:10
nit: remove all JNI suffix.
janx
2013/06/10 16:51:18
Done.
|
| + private native void nativeOnSoundStartJNI(int nativeSpeechRecognizerImplAndroid); |
| + private native void nativeOnSoundEndJNI(int nativeSpeechRecognizerImplAndroid); |
| + private native void nativeOnAudioEndJNI(int nativeSpeechRecognizerImplAndroid); |
| + private native void nativeOnRecognitionResultsJNI(int nativeSpeechRecognizerImplAndroid, |
| + String[] results, float[] scores, boolean provisional); |
| + private native void nativeOnRecognitionErrorJNI(int nativeSpeechRecognizerImplAndroid, |
| + int error); |
| + private native void nativeOnRecognitionEndJNI(int nativeSpeechRecognizerImplAndroid); |
| +} |