diff options
Diffstat (limited to 'java/src/com/android/inputmethod/voice/VoiceInput.java')
-rw-r--r-- | java/src/com/android/inputmethod/voice/VoiceInput.java | 76 |
1 files changed, 56 insertions, 20 deletions
diff --git a/java/src/com/android/inputmethod/voice/VoiceInput.java b/java/src/com/android/inputmethod/voice/VoiceInput.java index 4c54dd3c5..392eea364 100644 --- a/java/src/com/android/inputmethod/voice/VoiceInput.java +++ b/java/src/com/android/inputmethod/voice/VoiceInput.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Google Inc. + * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of @@ -16,24 +16,26 @@ package com.android.inputmethod.voice; -import com.android.inputmethod.latin.EditingUtil; +import com.android.inputmethod.latin.EditingUtils; +import com.android.inputmethod.latin.LatinImeLogger; import com.android.inputmethod.latin.R; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; +import android.content.res.Configuration; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.Parcelable; import android.speech.RecognitionListener; -import android.speech.SpeechRecognizer; import android.speech.RecognizerIntent; +import android.speech.SpeechRecognizer; import android.util.Log; -import android.view.inputmethod.InputConnection; import android.view.View; import android.view.View.OnClickListener; +import android.view.inputmethod.InputConnection; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -57,6 +59,7 @@ public class VoiceInput implements OnClickListener { private static final String EXTRA_CALLING_PACKAGE = "calling_package"; private static final String EXTRA_ALTERNATES = "android.speech.extra.ALTERNATES"; private static final int MAX_ALT_LIST_LENGTH = 6; + private static boolean DBG = LatinImeLogger.sDBG; private static final String DEFAULT_RECOMMENDED_PACKAGES = "com.android.mms " + @@ -86,6 +89,7 @@ public class VoiceInput implements OnClickListener { private static final String ALTERNATES_BUNDLE = "alternates_bundle"; // This is copied from the VoiceSearch app. + @SuppressWarnings("unused") private static final class AlternatesBundleKeys { public static final String ALTERNATES = "alternates"; public static final String CONFIDENCE = "confidence"; @@ -126,12 +130,12 @@ public class VoiceInput implements OnClickListener { private int mState = DEFAULT; - private final static int MSG_CLOSE_ERROR_DIALOG = 1; + private final static int MSG_RESET = 1; private final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { - if (msg.what == MSG_CLOSE_ERROR_DIALOG) { + if (msg.what == MSG_RESET) { mState = DEFAULT; mRecognitionView.finish(); mUiListener.onCancelVoice(); @@ -240,7 +244,7 @@ public class VoiceInput implements OnClickListener { } public void incrementTextModificationInsertPunctuationCount(int count){ - mAfterVoiceInputInsertPunctuationCount += 1; + mAfterVoiceInputInsertPunctuationCount += count; if (mAfterVoiceInputSelectionSpan > 0) { // If text was highlighted before inserting the char, count this as // a delete. @@ -276,8 +280,9 @@ public class VoiceInput implements OnClickListener { * The configuration of the IME changed and may have caused the views to be layed out * again. Restore the state of the recognition view. */ - public void onConfigurationChanged() { + public void onConfigurationChanged(Configuration configuration) { mRecognitionView.restoreState(); + mRecognitionView.getView().dispatchConfigurationChanged(configuration); } /** @@ -305,8 +310,18 @@ public class VoiceInput implements OnClickListener { * @param swipe whether this voice input was started by swipe, for logging purposes */ public void startListening(FieldContext context, boolean swipe) { - mState = DEFAULT; - + if (DBG) { + Log.d(TAG, "startListening: " + context); + } + + if (mState != DEFAULT) { + Log.w(TAG, "startListening in the wrong status " + mState); + } + + // If everything works ok, the voice input should be already in the correct state. As this + // class can be called by third-party, we call reset just to be on the safe side. + reset(); + Locale locale = Locale.getDefault(); String localeString = locale.getLanguage() + "-" + locale.getCountry(); @@ -405,6 +420,7 @@ public class VoiceInput implements OnClickListener { /** * Handle the cancel button. */ + @Override public void onClick(View view) { switch(view.getId()) { case R.id.button: @@ -427,8 +443,7 @@ public class VoiceInput implements OnClickListener { public void logTextModifiedByChooseSuggestion(String suggestion, int index, String wordSeparators, InputConnection ic) { - EditingUtil.Range range = new EditingUtil.Range(); - String wordToBeReplaced = EditingUtil.getWordAtCursor(ic, wordSeparators, range); + String wordToBeReplaced = EditingUtils.getWordAtCursor(ic, wordSeparators); // If we enable phrase-based alternatives, only send up the first word // in suggestion and wordToBeReplaced. mLogger.textModifiedByChooseSuggestion(suggestion.length(), wordToBeReplaced.length(), @@ -491,6 +506,21 @@ public class VoiceInput implements OnClickListener { } /** + * Reset the current voice recognition. + */ + public void reset() { + if (mState != DEFAULT) { + mState = DEFAULT; + + // Remove all pending tasks (e.g., timers to cancel voice input) + mHandler.removeMessages(MSG_RESET); + + mSpeechRecognizer.cancel(); + mRecognitionView.finish(); + } + } + + /** * Cancel in-progress speech recognition. */ public void cancel() { @@ -505,14 +535,9 @@ public class VoiceInput implements OnClickListener { mLogger.cancelDuringError(); break; } - mState = DEFAULT; - - // Remove all pending tasks (e.g., timers to cancel voice input) - mHandler.removeMessages(MSG_CLOSE_ERROR_DIALOG); - mSpeechRecognizer.cancel(); + reset(); mUiListener.onCancelVoice(); - mRecognitionView.finish(); } private int getErrorStringId(int errorType, boolean endpointed) { @@ -547,7 +572,7 @@ public class VoiceInput implements OnClickListener { mState = ERROR; mRecognitionView.showError(error); // Wait a couple seconds and then automatically dismiss message. - mHandler.sendMessageDelayed(Message.obtain(mHandler, MSG_CLOSE_ERROR_DIALOG), 2000); + mHandler.sendMessageDelayed(Message.obtain(mHandler, MSG_RESET), 2000); } private class ImeRecognitionListener implements RecognitionListener { @@ -556,36 +581,45 @@ public class VoiceInput implements OnClickListener { int mSpeechStart; private boolean mEndpointed = false; + @Override public void onReadyForSpeech(Bundle noiseParams) { mRecognitionView.showListening(); } + @Override public void onBeginningOfSpeech() { mEndpointed = false; mSpeechStart = mWaveBuffer.size(); } + @Override public void onRmsChanged(float rmsdB) { mRecognitionView.updateVoiceMeter(rmsdB); } + @Override public void onBufferReceived(byte[] buf) { try { mWaveBuffer.write(buf); - } catch (IOException e) {} + } catch (IOException e) { + // ignore. + } } + @Override public void onEndOfSpeech() { mEndpointed = true; mState = WORKING; mRecognitionView.showWorking(mWaveBuffer, mSpeechStart, mWaveBuffer.size()); } + @Override public void onError(int errorType) { mState = ERROR; VoiceInput.this.onError(errorType, mEndpointed); } + @Override public void onResults(Bundle resultsBundle) { List<String> results = resultsBundle .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); @@ -638,10 +672,12 @@ public class VoiceInput implements OnClickListener { mRecognitionView.finish(); } + @Override public void onPartialResults(final Bundle partialResults) { // currently - do nothing } + @Override public void onEvent(int eventType, Bundle params) { // do nothing - reserved for events that might be added in the future } |