• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
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.PreferenceActivity;
31 import android.preference.PreferenceGroup;
32 import android.speech.SpeechRecognizer;
33 import android.text.AutoText;
34 import android.util.Log;
35 
36 import com.android.inputmethod.voice.SettingsUtil;
37 import com.android.inputmethod.voice.VoiceInputLogger;
38 
39 public class LatinIMESettings extends PreferenceActivity
40         implements SharedPreferences.OnSharedPreferenceChangeListener,
41         DialogInterface.OnDismissListener {
42 
43     private static final String QUICK_FIXES_KEY = "quick_fixes";
44     private static final String PREDICTION_SETTINGS_KEY = "prediction_settings";
45     private static final String VOICE_SETTINGS_KEY = "voice_mode";
46     /* package */ static final String PREF_SETTINGS_KEY = "settings_key";
47 
48     private static final String TAG = "LatinIMESettings";
49 
50     // Dialog ids
51     private static final int VOICE_INPUT_CONFIRM_DIALOG = 0;
52 
53     private CheckBoxPreference mQuickFixes;
54     private ListPreference mVoicePreference;
55     private ListPreference mSettingsKeyPreference;
56     private boolean mVoiceOn;
57 
58     private VoiceInputLogger mLogger;
59 
60     private boolean mOkClicked = false;
61     private String mVoiceModeOff;
62 
63     @Override
onCreate(Bundle icicle)64     protected void onCreate(Bundle icicle) {
65         super.onCreate(icicle);
66         addPreferencesFromResource(R.xml.prefs);
67         mQuickFixes = (CheckBoxPreference) findPreference(QUICK_FIXES_KEY);
68         mVoicePreference = (ListPreference) findPreference(VOICE_SETTINGS_KEY);
69         mSettingsKeyPreference = (ListPreference) findPreference(PREF_SETTINGS_KEY);
70         SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
71         prefs.registerOnSharedPreferenceChangeListener(this);
72 
73         mVoiceModeOff = getString(R.string.voice_mode_off);
74         mVoiceOn = !(prefs.getString(VOICE_SETTINGS_KEY, mVoiceModeOff).equals(mVoiceModeOff));
75         mLogger = VoiceInputLogger.getLogger(this);
76     }
77 
78     @Override
onResume()79     protected void onResume() {
80         super.onResume();
81         int autoTextSize = AutoText.getSize(getListView());
82         if (autoTextSize < 1) {
83             ((PreferenceGroup) findPreference(PREDICTION_SETTINGS_KEY))
84                     .removePreference(mQuickFixes);
85         }
86         if (!LatinIME.VOICE_INSTALLED
87                 || !SpeechRecognizer.isRecognitionAvailable(this)) {
88             getPreferenceScreen().removePreference(mVoicePreference);
89         } else {
90             updateVoiceModeSummary();
91         }
92         updateSettingsKeySummary();
93     }
94 
95     @Override
onDestroy()96     protected void onDestroy() {
97         getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(
98                 this);
99         super.onDestroy();
100     }
101 
onSharedPreferenceChanged(SharedPreferences prefs, String key)102     public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
103         (new BackupManager(this)).dataChanged();
104         // If turning on voice input, show dialog
105         if (key.equals(VOICE_SETTINGS_KEY) && !mVoiceOn) {
106             if (!prefs.getString(VOICE_SETTINGS_KEY, mVoiceModeOff)
107                     .equals(mVoiceModeOff)) {
108                 showVoiceConfirmation();
109             }
110         }
111         mVoiceOn = !(prefs.getString(VOICE_SETTINGS_KEY, mVoiceModeOff).equals(mVoiceModeOff));
112         updateVoiceModeSummary();
113         updateSettingsKeySummary();
114     }
115 
updateSettingsKeySummary()116     private void updateSettingsKeySummary() {
117         mSettingsKeyPreference.setSummary(
118                 getResources().getStringArray(R.array.settings_key_modes)
119                 [mSettingsKeyPreference.findIndexOfValue(mSettingsKeyPreference.getValue())]);
120     }
121 
showVoiceConfirmation()122     private void showVoiceConfirmation() {
123         mOkClicked = false;
124         showDialog(VOICE_INPUT_CONFIRM_DIALOG);
125     }
126 
updateVoiceModeSummary()127     private void updateVoiceModeSummary() {
128         mVoicePreference.setSummary(
129                 getResources().getStringArray(R.array.voice_input_modes_summary)
130                 [mVoicePreference.findIndexOfValue(mVoicePreference.getValue())]);
131     }
132 
133     @Override
onCreateDialog(int id)134     protected Dialog onCreateDialog(int id) {
135         switch (id) {
136             case VOICE_INPUT_CONFIRM_DIALOG:
137                 DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
138                     public void onClick(DialogInterface dialog, int whichButton) {
139                         if (whichButton == DialogInterface.BUTTON_NEGATIVE) {
140                             mVoicePreference.setValue(mVoiceModeOff);
141                             mLogger.settingsWarningDialogCancel();
142                         } else if (whichButton == DialogInterface.BUTTON_POSITIVE) {
143                             mOkClicked = true;
144                             mLogger.settingsWarningDialogOk();
145                         }
146                         updateVoicePreference();
147                     }
148                 };
149                 AlertDialog.Builder builder = new AlertDialog.Builder(this)
150                         .setTitle(R.string.voice_warning_title)
151                         .setPositiveButton(android.R.string.ok, listener)
152                         .setNegativeButton(android.R.string.cancel, listener);
153 
154                 // Get the current list of supported locales and check the current locale against
155                 // that list, to decide whether to put a warning that voice input will not work in
156                 // the current language as part of the pop-up confirmation dialog.
157                 String supportedLocalesString = SettingsUtil.getSettingsString(
158                         getContentResolver(),
159                         SettingsUtil.LATIN_IME_VOICE_INPUT_SUPPORTED_LOCALES,
160                         LatinIME.DEFAULT_VOICE_INPUT_SUPPORTED_LOCALES);
161                 ArrayList<String> voiceInputSupportedLocales =
162                         LatinIME.newArrayList(supportedLocalesString.split("\\s+"));
163                 boolean localeSupported = voiceInputSupportedLocales.contains(
164                         Locale.getDefault().toString());
165 
166                 if (localeSupported) {
167                     String message = getString(R.string.voice_warning_may_not_understand) + "\n\n" +
168                             getString(R.string.voice_hint_dialog_message);
169                     builder.setMessage(message);
170                 } else {
171                     String message = getString(R.string.voice_warning_locale_not_supported) +
172                             "\n\n" + getString(R.string.voice_warning_may_not_understand) + "\n\n" +
173                             getString(R.string.voice_hint_dialog_message);
174                     builder.setMessage(message);
175                 }
176 
177                 AlertDialog dialog = builder.create();
178                 dialog.setOnDismissListener(this);
179                 mLogger.settingsWarningDialogShown();
180                 return dialog;
181             default:
182                 Log.e(TAG, "unknown dialog " + id);
183                 return null;
184         }
185     }
186 
onDismiss(DialogInterface dialog)187     public void onDismiss(DialogInterface dialog) {
188         mLogger.settingsWarningDialogDismissed();
189         if (!mOkClicked) {
190             // This assumes that onPreferenceClick gets called first, and this if the user
191             // agreed after the warning, we set the mOkClicked value to true.
192             mVoicePreference.setValue(mVoiceModeOff);
193         }
194     }
195 
updateVoicePreference()196     private void updateVoicePreference() {
197         boolean isChecked = !mVoicePreference.getValue().equals(mVoiceModeOff);
198         if (isChecked) {
199             mLogger.voiceInputSettingEnabled();
200         } else {
201             mLogger.voiceInputSettingDisabled();
202         }
203     }
204 }
205