1 /* 2 * Copyright (C) 2008-2009 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.android.inputmethod.latin; 18 19 import java.util.ArrayList; 20 import java.util.Locale; 21 22 import android.app.AlertDialog; 23 import android.app.Dialog; 24 import android.app.backup.BackupManager; 25 import android.content.DialogInterface; 26 import android.content.SharedPreferences; 27 import android.os.Bundle; 28 import android.preference.CheckBoxPreference; 29 import android.preference.ListPreference; 30 import android.preference.Preference; 31 import android.preference.PreferenceActivity; 32 import android.preference.PreferenceGroup; 33 import android.preference.Preference.OnPreferenceClickListener; 34 import android.speech.SpeechRecognizer; 35 import android.text.AutoText; 36 import android.util.Log; 37 38 import com.android.inputmethod.voice.SettingsUtil; 39 import com.android.inputmethod.voice.VoiceInputLogger; 40 41 public class LatinIMESettings extends PreferenceActivity 42 implements SharedPreferences.OnSharedPreferenceChangeListener, 43 DialogInterface.OnDismissListener { 44 45 private static final String QUICK_FIXES_KEY = "quick_fixes"; 46 private static final String SHOW_SUGGESTIONS_KEY = "show_suggestions"; 47 private static final String PREDICTION_SETTINGS_KEY = "prediction_settings"; 48 private static final String VOICE_SETTINGS_KEY = "voice_mode"; 49 private static final String VOICE_ON_PRIMARY_KEY = "voice_on_main"; 50 private static final String VOICE_SERVER_KEY = "voice_server_url"; 51 52 private static final String TAG = "LatinIMESettings"; 53 54 // Dialog ids 55 private static final int VOICE_INPUT_CONFIRM_DIALOG = 0; 56 57 private CheckBoxPreference mQuickFixes; 58 private CheckBoxPreference mShowSuggestions; 59 private ListPreference mVoicePreference; 60 private boolean mVoiceOn; 61 62 private VoiceInputLogger mLogger; 63 64 private boolean mOkClicked = false; 65 private String mVoiceModeOff; 66 67 @Override 68 protected void onCreate(Bundle icicle) { 69 super.onCreate(icicle); 70 addPreferencesFromResource(R.xml.prefs); 71 mQuickFixes = (CheckBoxPreference) findPreference(QUICK_FIXES_KEY); 72 mShowSuggestions = (CheckBoxPreference) findPreference(SHOW_SUGGESTIONS_KEY); 73 mVoicePreference = (ListPreference) findPreference(VOICE_SETTINGS_KEY); 74 SharedPreferences prefs = getPreferenceManager().getSharedPreferences(); 75 prefs.registerOnSharedPreferenceChangeListener(this); 76 77 mVoiceModeOff = getString(R.string.voice_mode_off); 78 mVoiceOn = !(prefs.getString(VOICE_SETTINGS_KEY, mVoiceModeOff).equals(mVoiceModeOff)); 79 mLogger = VoiceInputLogger.getLogger(this); 80 } 81 82 @Override 83 protected void onResume() { 84 super.onResume(); 85 int autoTextSize = AutoText.getSize(getListView()); 86 if (autoTextSize < 1) { 87 ((PreferenceGroup) findPreference(PREDICTION_SETTINGS_KEY)) 88 .removePreference(mQuickFixes); 89 } 90 if (!LatinIME.VOICE_INSTALLED 91 || !SpeechRecognizer.isRecognitionAvailable(this)) { 92 getPreferenceScreen().removePreference(mVoicePreference); 93 } else { 94 updateVoiceModeSummary(); 95 } 96 } 97 98 @Override 99 protected void onDestroy() { 100 getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener( 101 this); 102 super.onDestroy(); 103 } 104 105 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { 106 (new BackupManager(this)).dataChanged(); 107 // If turning on voice input, show dialog 108 if (key.equals(VOICE_SETTINGS_KEY) && !mVoiceOn) { 109 if (! prefs.getString(VOICE_SETTINGS_KEY, mVoiceModeOff) 110 .equals(mVoiceModeOff)) { 111 showVoiceConfirmation(); 112 } 113 } 114 mVoiceOn = !(prefs.getString(VOICE_SETTINGS_KEY, mVoiceModeOff).equals(mVoiceModeOff)); 115 updateVoiceModeSummary(); 116 } 117 118 private void showVoiceConfirmation() { 119 mOkClicked = false; 120 showDialog(VOICE_INPUT_CONFIRM_DIALOG); 121 } 122 123 private void updateVoiceModeSummary() { 124 mVoicePreference.setSummary( 125 getResources().getStringArray(R.array.voice_input_modes_summary) 126 [mVoicePreference.findIndexOfValue(mVoicePreference.getValue())]); 127 } 128 129 @Override 130 protected Dialog onCreateDialog(int id) { 131 switch (id) { 132 case VOICE_INPUT_CONFIRM_DIALOG: 133 DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { 134 public void onClick(DialogInterface dialog, int whichButton) { 135 if (whichButton == DialogInterface.BUTTON_NEGATIVE) { 136 mVoicePreference.setValue(mVoiceModeOff); 137 mLogger.settingsWarningDialogCancel(); 138 } else if (whichButton == DialogInterface.BUTTON_POSITIVE) { 139 mOkClicked = true; 140 mLogger.settingsWarningDialogOk(); 141 } 142 updateVoicePreference(); 143 } 144 }; 145 AlertDialog.Builder builder = new AlertDialog.Builder(this) 146 .setTitle(R.string.voice_warning_title) 147 .setPositiveButton(android.R.string.ok, listener) 148 .setNegativeButton(android.R.string.cancel, listener); 149 150 // Get the current list of supported locales and check the current locale against 151 // that list, to decide whether to put a warning that voice input will not work in 152 // the current language as part of the pop-up confirmation dialog. 153 String supportedLocalesString = SettingsUtil.getSettingsString( 154 getContentResolver(), 155 SettingsUtil.LATIN_IME_VOICE_INPUT_SUPPORTED_LOCALES, 156 LatinIME.DEFAULT_VOICE_INPUT_SUPPORTED_LOCALES); 157 ArrayList<String> voiceInputSupportedLocales = 158 LatinIME.newArrayList(supportedLocalesString.split("\\s+")); 159 boolean localeSupported = voiceInputSupportedLocales.contains( 160 Locale.getDefault().toString()); 161 162 if (localeSupported) { 163 String message = getString(R.string.voice_warning_may_not_understand) + "\n\n" + 164 getString(R.string.voice_hint_dialog_message); 165 builder.setMessage(message); 166 } else { 167 String message = getString(R.string.voice_warning_locale_not_supported) + 168 "\n\n" + getString(R.string.voice_warning_may_not_understand) + "\n\n" + 169 getString(R.string.voice_hint_dialog_message); 170 builder.setMessage(message); 171 } 172 173 AlertDialog dialog = builder.create(); 174 dialog.setOnDismissListener(this); 175 mLogger.settingsWarningDialogShown(); 176 return dialog; 177 default: 178 Log.e(TAG, "unknown dialog " + id); 179 return null; 180 } 181 } 182 183 public void onDismiss(DialogInterface dialog) { 184 mLogger.settingsWarningDialogDismissed(); 185 if (!mOkClicked) { 186 // This assumes that onPreferenceClick gets called first, and this if the user 187 // agreed after the warning, we set the mOkClicked value to true. 188 mVoicePreference.setValue(mVoiceModeOff); 189 } 190 } 191 192 private void updateVoicePreference() { 193 boolean isChecked = !mVoicePreference.getValue().equals(mVoiceModeOff); 194 if (isChecked) { 195 mLogger.voiceInputSettingEnabled(); 196 } else { 197 mLogger.voiceInputSettingDisabled(); 198 } 199 } 200 } 201