This source file includes following definitions.
- JNINamespace
- onBeginningOfSpeech
- onBufferReceived
- onEndOfSpeech
- onError
- onEvent
- onPartialResults
- onReadyForSpeech
- onResults
- onRmsChanged
- handleResults
- initialize
- terminate
- createSpeechRecognition
- startRecognition
- abortRecognition
- stopRecognition
- nativeOnAudioStart
- nativeOnSoundStart
- nativeOnSoundEnd
- nativeOnAudioEnd
- nativeOnRecognitionResults
- nativeOnRecognitionError
- nativeOnRecognitionEnd
package org.chromium.content.browser;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognitionService;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import java.util.ArrayList;
import java.util.List;
@JNINamespace("content")
public class SpeechRecognition {
private static final String PROVIDER_PACKAGE_NAME = "com.google.android.googlequicksearchbox";
private static final int PROVIDER_MIN_VERSION = 300207030;
private static final int STATE_IDLE = 0;
private static final int STATE_AWAITING_SPEECH = 1;
private static final int STATE_CAPTURING_SPEECH = 2;
private int mState;
private static ComponentName sRecognitionProvider;
private final Context mContext;
private final Intent mIntent;
private final RecognitionListener mListener;
private SpeechRecognizer mRecognizer;
private long mNativeSpeechRecognizerImplAndroid;
private boolean mContinuous;
class Listener implements RecognitionListener {
@Override
public void onBeginningOfSpeech() {
mState = STATE_CAPTURING_SPEECH;
nativeOnSoundStart(mNativeSpeechRecognizerImplAndroid);
}
@Override
public void onBufferReceived(byte[] buffer) { }
@Override
public void onEndOfSpeech() {
if (!mContinuous) {
nativeOnSoundEnd(mNativeSpeechRecognizerImplAndroid);
nativeOnAudioEnd(mNativeSpeechRecognizerImplAndroid);
mState = STATE_IDLE;
}
}
@Override
public void onError(int error) {
int code = SpeechRecognitionError.NONE;
switch(error) {
case SpeechRecognizer.ERROR_AUDIO:
code = SpeechRecognitionError.AUDIO;
break;
case SpeechRecognizer.ERROR_CLIENT:
code = SpeechRecognitionError.ABORTED;
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
code = SpeechRecognitionError.NOT_ALLOWED;
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
case SpeechRecognizer.ERROR_NETWORK:
case SpeechRecognizer.ERROR_SERVER:
code = SpeechRecognitionError.NETWORK;
break;
case SpeechRecognizer.ERROR_NO_MATCH:
code = SpeechRecognitionError.NO_MATCH;
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
code = SpeechRecognitionError.NO_SPEECH;
break;
default:
assert false;
return;
}
terminate(code);
}
@Override
public void onEvent(int event, Bundle bundle) { }
@Override
public void onPartialResults(Bundle bundle) {
handleResults(bundle, true);
}
@Override
public void onReadyForSpeech(Bundle bundle) {
mState = STATE_AWAITING_SPEECH;
nativeOnAudioStart(mNativeSpeechRecognizerImplAndroid);
}
@Override
public void onResults(Bundle bundle) {
handleResults(bundle, false);
terminate(SpeechRecognitionError.NONE);
}
@Override
public void onRmsChanged(float rms) { }
private void handleResults(Bundle bundle, boolean provisional) {
if (mContinuous && provisional) {
provisional = false;
}
ArrayList<String> list = bundle.getStringArrayList(
SpeechRecognizer.RESULTS_RECOGNITION);
String[] results = list.toArray(new String[list.size()]);
float[] scores = bundle.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES);
nativeOnRecognitionResults(mNativeSpeechRecognizerImplAndroid,
results,
scores,
provisional);
}
}
public static boolean initialize(Context context) {
if (!SpeechRecognizer.isRecognitionAvailable(context))
return false;
PackageManager pm = context.getPackageManager();
Intent intent = new Intent(RecognitionService.SERVICE_INTERFACE);
final List<ResolveInfo> list = pm.queryIntentServices(intent, PackageManager.GET_SERVICES);
for (ResolveInfo resolve : list) {
ServiceInfo service = resolve.serviceInfo;
if (!service.packageName.equals(PROVIDER_PACKAGE_NAME))
continue;
int versionCode;
try {
versionCode = pm.getPackageInfo(service.packageName, 0).versionCode;
} catch (NameNotFoundException e) {
continue;
}
if (versionCode < PROVIDER_MIN_VERSION)
continue;
sRecognitionProvider = new ComponentName(service.packageName, service.name);
return true;
}
return false;
}
private SpeechRecognition(final Context context, long nativeSpeechRecognizerImplAndroid) {
mContext = context;
mContinuous = false;
mNativeSpeechRecognizerImplAndroid = nativeSpeechRecognizerImplAndroid;
mListener = new Listener();
mIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
if (sRecognitionProvider != null) {
mRecognizer = SpeechRecognizer.createSpeechRecognizer(mContext, sRecognitionProvider);
} else {
mRecognizer = SpeechRecognizer.createSpeechRecognizer(mContext);
}
mRecognizer.setRecognitionListener(mListener);
}
private void terminate(int error) {
if (mState != STATE_IDLE) {
if (mState == STATE_CAPTURING_SPEECH) {
nativeOnSoundEnd(mNativeSpeechRecognizerImplAndroid);
}
nativeOnAudioEnd(mNativeSpeechRecognizerImplAndroid);
mState = STATE_IDLE;
}
if (error != SpeechRecognitionError.NONE)
nativeOnRecognitionError(mNativeSpeechRecognizerImplAndroid, error);
mRecognizer.destroy();
mRecognizer = null;
nativeOnRecognitionEnd(mNativeSpeechRecognizerImplAndroid);
mNativeSpeechRecognizerImplAndroid = 0;
}
@CalledByNative
private static SpeechRecognition createSpeechRecognition(
Context context, long nativeSpeechRecognizerImplAndroid) {
return new SpeechRecognition(context, nativeSpeechRecognizerImplAndroid);
}
@CalledByNative
private void startRecognition(String language, boolean continuous, boolean interimResults) {
if (mRecognizer == null)
return;
mContinuous = continuous;
mIntent.putExtra("android.speech.extra.DICTATION_MODE", continuous);
mIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
mIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, interimResults);
mRecognizer.startListening(mIntent);
}
@CalledByNative
private void abortRecognition() {
if (mRecognizer == null)
return;
mRecognizer.cancel();
terminate(SpeechRecognitionError.ABORTED);
}
@CalledByNative
private void stopRecognition() {
if (mRecognizer == null)
return;
mContinuous = false;
mRecognizer.stopListening();
}
private native void nativeOnAudioStart(long nativeSpeechRecognizerImplAndroid);
private native void nativeOnSoundStart(long nativeSpeechRecognizerImplAndroid);
private native void nativeOnSoundEnd(long nativeSpeechRecognizerImplAndroid);
private native void nativeOnAudioEnd(long nativeSpeechRecognizerImplAndroid);
private native void nativeOnRecognitionResults(long nativeSpeechRecognizerImplAndroid,
String[] results,
float[] scores,
boolean provisional);
private native void nativeOnRecognitionError(long nativeSpeechRecognizerImplAndroid, int error);
private native void nativeOnRecognitionEnd(long nativeSpeechRecognizerImplAndroid);
}