diff options
Diffstat (limited to 'java/src/com/android/inputmethod/deprecated/VoiceProxy.java')
-rw-r--r-- | java/src/com/android/inputmethod/deprecated/VoiceProxy.java | 190 |
1 files changed, 114 insertions, 76 deletions
diff --git a/java/src/com/android/inputmethod/deprecated/VoiceProxy.java b/java/src/com/android/inputmethod/deprecated/VoiceProxy.java index 85993ea4d..9397483ce 100644 --- a/java/src/com/android/inputmethod/deprecated/VoiceProxy.java +++ b/java/src/com/android/inputmethod/deprecated/VoiceProxy.java @@ -17,11 +17,11 @@ package com.android.inputmethod.deprecated; import com.android.inputmethod.compat.InputMethodManagerCompatWrapper; +import com.android.inputmethod.compat.InputMethodServiceCompatWrapper; import com.android.inputmethod.deprecated.voice.FieldContext; import com.android.inputmethod.deprecated.voice.Hints; import com.android.inputmethod.deprecated.voice.SettingsUtil; import com.android.inputmethod.deprecated.voice.VoiceInput; -import com.android.inputmethod.deprecated.voice.VoiceInputLogger; import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.latin.EditingUtils; import com.android.inputmethod.latin.LatinIME; @@ -71,7 +71,8 @@ import java.util.Map; public class VoiceProxy implements VoiceInput.UiListener { private static final VoiceProxy sInstance = new VoiceProxy(); - public static final boolean VOICE_INSTALLED = true; + public static final boolean VOICE_INSTALLED = + !InputMethodServiceCompatWrapper.CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED; private static final boolean ENABLE_VOICE_BUTTON = true; private static final String PREF_VOICE_MODE = "voice_mode"; // Whether or not the user has used voice input before (and thus, whether to show the @@ -125,24 +126,23 @@ public class VoiceProxy implements VoiceInput.UiListener { } private void initInternal(LatinIME service, SharedPreferences prefs, UIHandler h) { + if (!VOICE_INSTALLED) { + return; + } mService = service; mHandler = h; mMinimumVoiceRecognitionViewHeightPixel = Utils.dipToPixel( Utils.getDipScale(service), RECOGNITIONVIEW_MINIMUM_HEIGHT_DIP); - mImm = InputMethodManagerCompatWrapper.getInstance(service); + mImm = InputMethodManagerCompatWrapper.getInstance(); mSubtypeSwitcher = SubtypeSwitcher.getInstance(); - if (VOICE_INSTALLED) { - mVoiceInput = new VoiceInput(service, this); - mHints = new Hints(service, prefs, new Hints.Display() { - @Override - public void showHint(int viewResource) { - View view = LayoutInflater.from(mService).inflate(viewResource, null); -// mService.setCandidatesView(view); -// mService.setCandidatesViewShown(true); - mIsShowingHint = true; - } - }); - } + mVoiceInput = new VoiceInput(service, this); + mHints = new Hints(service, prefs, new Hints.Display() { + @Override + public void showHint(int viewResource) { + View view = LayoutInflater.from(mService).inflate(viewResource, null); + mIsShowingHint = true; + } + }); } private VoiceProxy() { @@ -158,7 +158,10 @@ public class VoiceProxy implements VoiceInput.UiListener { } public void flushVoiceInputLogs(boolean configurationChanged) { - if (VOICE_INSTALLED && !configurationChanged) { + if (!VOICE_INSTALLED) { + return; + } + if (!configurationChanged) { if (mAfterVoiceInput) { mVoiceInput.flushAllTextModificationCounters(); mVoiceInput.logInputEnded(); @@ -170,6 +173,9 @@ public class VoiceProxy implements VoiceInput.UiListener { public void flushAndLogAllTextModificationCounters(int index, CharSequence suggestion, String wordSeparators) { + if (!VOICE_INSTALLED) { + return; + } if (mAfterVoiceInput && mShowingVoiceSuggestions) { mVoiceInput.flushAllTextModificationCounters(); // send this intent AFTER logging any prior aggregated edits. @@ -298,6 +304,9 @@ public class VoiceProxy implements VoiceInput.UiListener { } public void showPunctuationHintIfNecessary() { + if (!VOICE_INSTALLED) { + return; + } InputConnection ic = mService.getCurrentInputConnection(); if (!mImmediatelyAfterVoiceInput && mAfterVoiceInput && ic != null) { if (mHints.showPunctuationHintIfNecessary(ic)) { @@ -308,6 +317,9 @@ public class VoiceProxy implements VoiceInput.UiListener { } public void hideVoiceWindow(boolean configurationChanging) { + if (!VOICE_INSTALLED) { + return; + } if (!configurationChanging) { if (mAfterVoiceInput) mVoiceInput.logInputEnded(); @@ -324,6 +336,9 @@ public class VoiceProxy implements VoiceInput.UiListener { } public void setCursorAndSelection(int newSelEnd, int newSelStart) { + if (!VOICE_INSTALLED) { + return; + } if (mAfterVoiceInput) { mVoiceInput.setCursorPos(newSelEnd); mVoiceInput.setSelectionSpan(newSelEnd - newSelStart); @@ -382,7 +397,10 @@ public class VoiceProxy implements VoiceInput.UiListener { } public boolean logAndRevertVoiceInput() { - if (VOICE_INSTALLED && mVoiceInputHighlighted) { + if (!VOICE_INSTALLED) { + return false; + } + if (mVoiceInputHighlighted) { mVoiceInput.incrementTextModificationDeleteCount( mVoiceResults.candidates.get(0).toString().length()); revertVoiceInput(); @@ -393,6 +411,9 @@ public class VoiceProxy implements VoiceInput.UiListener { } public void rememberReplacedWord(CharSequence suggestion,String wordSeparators) { + if (!VOICE_INSTALLED) { + return; + } if (mShowingVoiceSuggestions) { // Retain the replaced word in the alternatives array. String wordToBeReplaced = EditingUtils.getWordAtCursor( @@ -419,6 +440,9 @@ public class VoiceProxy implements VoiceInput.UiListener { * @return true if an alternative was found, false otherwise. */ public boolean applyVoiceAlternatives(EditingUtils.SelectedWord touching) { + if (!VOICE_INSTALLED) { + return false; + } // Search for result in spoken word alternatives String selectedWord = touching.mWord.toString().trim(); if (!mWordToSuggestions.containsKey(selectedWord)) { @@ -448,6 +472,9 @@ public class VoiceProxy implements VoiceInput.UiListener { } public void handleBackspace() { + if (!VOICE_INSTALLED) { + return; + } if (mAfterVoiceInput) { // Don't log delete if the user is pressing delete at // the beginning of the text box (hence not deleting anything) @@ -462,6 +489,9 @@ public class VoiceProxy implements VoiceInput.UiListener { } public void handleCharacter() { + if (!VOICE_INSTALLED) { + return; + } commitVoiceInput(); if (mAfterVoiceInput) { // Assume input length is 1. This assumption fails for smiley face insertions. @@ -470,6 +500,9 @@ public class VoiceProxy implements VoiceInput.UiListener { } public void handleSeparator() { + if (!VOICE_INSTALLED) { + return; + } commitVoiceInput(); if (mAfterVoiceInput){ // Assume input length is 1. This assumption fails for smiley face insertions. @@ -478,13 +511,19 @@ public class VoiceProxy implements VoiceInput.UiListener { } public void handleClose() { - if (VOICE_INSTALLED & mRecognizing) { + if (!VOICE_INSTALLED) { + return; + } + if (mRecognizing) { mVoiceInput.cancel(); } } public void handleVoiceResults(boolean capitalizeFirstWord) { + if (!VOICE_INSTALLED) { + return; + } mAfterVoiceInput = true; mImmediatelyAfterVoiceInput = true; @@ -523,6 +562,9 @@ public class VoiceProxy implements VoiceInput.UiListener { } public void switchToRecognitionStatusView(final Configuration configuration) { + if (!VOICE_INSTALLED) { + return; + } mHandler.post(new Runnable() { @Override public void run() { @@ -567,6 +609,9 @@ public class VoiceProxy implements VoiceInput.UiListener { } private void switchToLastInputMethod() { + if (!VOICE_INSTALLED) { + return; + } final IBinder token = mService.getWindow().getWindow().getAttributes().token; new AsyncTask<Void, Void, Boolean>() { @Override @@ -632,14 +677,15 @@ public class VoiceProxy implements VoiceInput.UiListener { } public void startListening(final boolean swipe, IBinder token) { + if (!VOICE_INSTALLED) { + return; + } // TODO: remove swipe which is no longer used. - if (VOICE_INSTALLED) { - if (needsToShowWarningDialog()) { - // Calls reallyStartListening if user clicks OK, does nothing if user clicks Cancel. - showVoiceWarningDialog(swipe, token); - } else { - reallyStartListening(swipe); - } + if (needsToShowWarningDialog()) { + // Calls reallyStartListening if user clicks OK, does nothing if user clicks Cancel. + showVoiceWarningDialog(swipe, token); + } else { + reallyStartListening(swipe); } } @@ -659,7 +705,14 @@ public class VoiceProxy implements VoiceInput.UiListener { && SpeechRecognizer.isRecognitionAvailable(mService); } + public static boolean isRecognitionAvailable(Context context) { + return SpeechRecognizer.isRecognitionAvailable(context); + } + public void loadSettings(EditorInfo attribute, SharedPreferences sp) { + if (!VOICE_INSTALLED) { + return; + } mHasUsedVoiceInput = sp.getBoolean(PREF_HAS_USED_VOICE_INPUT, false); mHasUsedVoiceInputUnsupportedLocale = sp.getBoolean(PREF_HAS_USED_VOICE_INPUT_UNSUPPORTED_LOCALE, false); @@ -667,22 +720,26 @@ public class VoiceProxy implements VoiceInput.UiListener { mLocaleSupportedForVoiceInput = SubtypeSwitcher.getInstance().isVoiceSupported( SubtypeSwitcher.getInstance().getInputLocaleStr()); - if (VOICE_INSTALLED) { - final String voiceMode = sp.getString(PREF_VOICE_MODE, - mService.getString(R.string.voice_mode_main)); - mVoiceButtonEnabled = !voiceMode.equals(mService.getString(R.string.voice_mode_off)) - && shouldShowVoiceButton(makeFieldContext(), attribute); - mVoiceButtonOnPrimary = voiceMode.equals(mService.getString(R.string.voice_mode_main)); - } + final String voiceMode = sp.getString(PREF_VOICE_MODE, + mService.getString(R.string.voice_mode_main)); + mVoiceButtonEnabled = !voiceMode.equals(mService.getString(R.string.voice_mode_off)) + && shouldShowVoiceButton(makeFieldContext(), attribute); + mVoiceButtonOnPrimary = voiceMode.equals(mService.getString(R.string.voice_mode_main)); } public void destroy() { - if (VOICE_INSTALLED && mVoiceInput != null) { + if (!VOICE_INSTALLED) { + return; + } + if (mVoiceInput != null) { mVoiceInput.destroy(); } } public void onStartInputView(IBinder keyboardViewToken) { + if (!VOICE_INSTALLED) { + return; + } // If keyboardViewToken is null, keyboardView is not attached but voiceView is attached. IBinder windowToken = keyboardViewToken != null ? keyboardViewToken : mVoiceInput.getView().getWindowToken(); @@ -699,12 +756,18 @@ public class VoiceProxy implements VoiceInput.UiListener { } public void onAttachedToWindow() { + if (!VOICE_INSTALLED) { + return; + } // After onAttachedToWindow, we can show the voice warning dialog. See startListening() // above. VoiceInputWrapper.getInstance().setVoiceInput(mVoiceInput, mSubtypeSwitcher); } public void onConfigurationChanged(Configuration configuration) { + if (!VOICE_INSTALLED) { + return; + } if (mRecognizing) { switchToRecognitionStatusView(configuration); } @@ -712,6 +775,9 @@ public class VoiceProxy implements VoiceInput.UiListener { @Override public void onCancelVoice() { + if (!VOICE_INSTALLED) { + return; + } if (mRecognizing) { if (mSubtypeSwitcher.isVoiceMode()) { // If voice mode is being canceled within LatinIME (i.e. time-out or user @@ -733,6 +799,9 @@ public class VoiceProxy implements VoiceInput.UiListener { @Override public void onVoiceResults(List<String> candidates, Map<String, List<CharSequence>> alternatives) { + if (!VOICE_INSTALLED) { + return; + } if (!mRecognizing) { return; } @@ -748,59 +817,22 @@ public class VoiceProxy implements VoiceInput.UiListener { switcher.getEnabledLanguages()); } - private class VoiceResults { + // TODO: make this private (proguard issue) + public static class VoiceResults { List<String> candidates; Map<String, List<CharSequence>> alternatives; } - public static class VoiceLoggerWrapper { - private static final VoiceLoggerWrapper sLoggerWrapperInstance = new VoiceLoggerWrapper(); - private VoiceInputLogger mLogger; - - public static VoiceLoggerWrapper getInstance(Context context) { - if (sLoggerWrapperInstance.mLogger == null) { - // Not thread safe, but it's ok. - sLoggerWrapperInstance.mLogger = VoiceInputLogger.getLogger(context); - } - return sLoggerWrapperInstance; - } - - // private for the singleton - private VoiceLoggerWrapper() { - } - - public void settingsWarningDialogCancel() { - mLogger.settingsWarningDialogCancel(); - } - - public void settingsWarningDialogOk() { - mLogger.settingsWarningDialogOk(); - } - - public void settingsWarningDialogShown() { - mLogger.settingsWarningDialogShown(); - } - - public void settingsWarningDialogDismissed() { - mLogger.settingsWarningDialogDismissed(); - } - - public void voiceInputSettingEnabled(boolean enabled) { - if (enabled) { - mLogger.voiceInputSettingEnabled(); - } else { - mLogger.voiceInputSettingDisabled(); - } - } - } - public static class VoiceInputWrapper { private static final VoiceInputWrapper sInputWrapperInstance = new VoiceInputWrapper(); private VoiceInput mVoiceInput; public static VoiceInputWrapper getInstance() { return sInputWrapperInstance; } - public void setVoiceInput(VoiceInput voiceInput, SubtypeSwitcher switcher) { + private void setVoiceInput(VoiceInput voiceInput, SubtypeSwitcher switcher) { + if (!VOICE_INSTALLED) { + return; + } if (mVoiceInput == null && voiceInput != null) { mVoiceInput = voiceInput; } @@ -811,10 +843,16 @@ public class VoiceProxy implements VoiceInput.UiListener { } public void cancel() { + if (!VOICE_INSTALLED) { + return; + } if (mVoiceInput != null) mVoiceInput.cancel(); } public void reset() { + if (!VOICE_INSTALLED) { + return; + } if (mVoiceInput != null) mVoiceInput.reset(); } } |