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..3ad04268dc22f90c896080c5fd7535800ed376d1 |
--- /dev/null |
+++ b/content/public/android/java/src/org/chromium/content/browser/SpeechRecognition.java |
@@ -0,0 +1,126 @@ |
+// 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; |
Peter Beverloo
2013/06/06 11:48:13
I don't think you are using Toast.
janx
2013/06/06 16:09:37
Nor am I using ThreadUtils or FutureTask. Nice cat
|
+ |
+import org.chromium.base.CalledByNative; |
+import org.chromium.base.JNINamespace; |
+import org.chromium.base.ThreadUtils; |
Peter Beverloo
2013/06/06 11:48:13
I don't think you are using ThreadUtils.
|
+ |
+import java.util.ArrayList; |
+import java.util.concurrent.FutureTask; |
+ |
+/** |
+ * Android speech recognition adapter. |
+ */ |
+@JNINamespace("content") |
+class SpeechRecognition { |
Peter Beverloo
2013/06/06 11:48:13
In general, I feel this class can do with a little
|
+ |
+ private Intent intent = null; |
Peter Beverloo
2013/06/06 11:48:13
s/intent/mIntent/
|
+ private Context mContext = null; |
+ private boolean mContinuous = false; |
+ private SpeechRecognizer recognizer = null; |
Peter Beverloo
2013/06/06 11:48:13
s/recognizer/mRecognizer/
janx
2013/06/06 16:09:37
Done reluctantly.
|
+ private RecognitionListener listener = null; |
Peter Beverloo
2013/06/06 11:48:13
s/listener/mListener/
|
+ private int mNativeSpeechRecognizerImplAndroid = 0; |
janx
2013/06/06 09:34:55
Primiano Tucci 2013/06/03 16:24:11
You'll need a r
|
+ |
+ class Listener implements RecognitionListener { |
+ @Override |
Peter Beverloo
2013/06/06 11:48:13
Java really should have some kind of @Implements t
janx
2013/06/06 16:09:37
Maybe you should file a bug at http://bugreport.su
|
+ public void onBeginningOfSpeech() { |
+ nativeOnSoundStartJNI(mNativeSpeechRecognizerImplAndroid); |
+ } |
+ @Override |
+ public void onBufferReceived(byte[] buffer) { } |
+ @Override |
+ public void onEndOfSpeech() { |
+ nativeOnSoundEndJNI(mNativeSpeechRecognizerImplAndroid); |
+ // Since there is no AudioEnd event, we emulate it. |
Peter Beverloo
2013/06/06 11:48:13
Maybe rephrase this as something like "Android doe
janx
2013/06/06 16:09:37
Rephrased to something similar.
|
+ nativeOnAudioEndJNI(mNativeSpeechRecognizerImplAndroid); |
+ } |
+ @Override |
+ public void onError(int error) { |
+ nativeOnRecognitionErrorJNI(mNativeSpeechRecognizerImplAndroid, error); |
+ terminate(); |
+ } |
+ @Override |
+ public void onEvent(int event, Bundle bundle) { } |
Peter Beverloo
2013/06/06 11:48:13
nit: even when empty, please have an empty line be
janx
2013/06/06 16:09:37
Done.
|
+ @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(); |
Peter Beverloo
2013/06/06 11:48:13
If we don't call terminate() here, will it just re
janx
2013/06/06 16:09:37
The way it behaves now is that if mContinuous is t
|
+ } |
+ @Override |
+ public void onRmsChanged(float rms) { } |
+ public void handleResults(Bundle bundle, boolean provisional) { |
Peter Beverloo
2013/06/06 11:48:13
This method can be private.
janx
2013/06/06 16:09:37
Done.
|
+ ArrayList<String> list = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); |
Peter Beverloo
2013/06/06 11:48:13
nit: line length limit in Java is a hundred charac
janx
2013/06/06 16:09:37
Done.
|
+ 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; |
+ listener = new Listener(); |
+ intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
+ recognizer = SpeechRecognizer.createSpeechRecognizer(mContext); |
Peter Beverloo
2013/06/06 11:48:13
While more of a follow up, have you thought about
janx
2013/06/06 16:09:37
Yes, I think that we could easily make a JNI call
|
+ recognizer.setRecognitionListener(listener); |
+ } |
+ |
+ private void terminate() { |
+ recognizer.destroy(); |
+ recognizer = null; |
+ nativeOnRecognitionEndJNI(mNativeSpeechRecognizerImplAndroid); |
+ } |
+ |
+ @CalledByNative |
+ public static SpeechRecognition createSpeechRecognition(Context context, int nativeSpeechRecognizerImplAndroid) { |
+ return new SpeechRecognition(context, nativeSpeechRecognizerImplAndroid); |
+ } |
+ |
+ @CalledByNative |
+ public void StartRecognition(boolean continuous, boolean interim_results) { |
+ mContinuous = continuous; |
+ intent.putExtra("android.speech.extra.DICTATION_MODE", continuous); |
+ intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, interim_results); |
+ recognizer.startListening(intent); |
+ } |
+ |
+ @CalledByNative |
+ public void AbortRecognition() { |
+ recognizer.cancel(); |
+ terminate(); |
+ } |
+ |
+ @CalledByNative |
+ public void StopRecognition() { |
+ mContinuous = false; |
+ recognizer.stopListening(); |
+ } |
+ |
+ private native void nativeOnAudioStartJNI(int nativeSpeechRecognizerImplAndroid); |
+ 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); |
+} |