1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.inputmethod; 18 19 import android.app.settings.SettingsEnums; 20 import android.content.Context; 21 import android.content.DialogInterface; 22 import android.content.pm.ApplicationInfo; 23 import android.os.Bundle; 24 import android.provider.Settings; 25 import android.util.Log; 26 import android.view.textservice.SpellCheckerInfo; 27 import android.view.textservice.SpellCheckerSubtype; 28 import android.view.textservice.TextServicesManager; 29 import android.widget.Switch; 30 31 import androidx.appcompat.app.AlertDialog; 32 import androidx.preference.Preference; 33 import androidx.preference.Preference.OnPreferenceChangeListener; 34 import androidx.preference.PreferenceScreen; 35 36 import com.android.settings.R; 37 import com.android.settings.SettingsActivity; 38 import com.android.settings.SettingsPreferenceFragment; 39 import com.android.settings.widget.SettingsMainSwitchBar; 40 import com.android.settingslib.widget.OnMainSwitchChangeListener; 41 42 public class SpellCheckersSettings extends SettingsPreferenceFragment 43 implements OnMainSwitchChangeListener, OnPreferenceChangeListener { 44 private static final String TAG = SpellCheckersSettings.class.getSimpleName(); 45 private static final boolean DBG = false; 46 47 private static final String KEY_SPELL_CHECKER_LANGUAGE = "spellchecker_language"; 48 private static final String KEY_DEFAULT_SPELL_CHECKER = "default_spellchecker"; 49 private static final int ITEM_ID_USE_SYSTEM_LANGUAGE = 0; 50 51 private SettingsMainSwitchBar mSwitchBar; 52 private Preference mSpellCheckerLanaguagePref; 53 private AlertDialog mDialog = null; 54 private SpellCheckerInfo mCurrentSci; 55 private SpellCheckerInfo[] mEnabledScis; 56 private TextServicesManager mTsm; 57 58 @Override getMetricsCategory()59 public int getMetricsCategory() { 60 return SettingsEnums.INPUTMETHOD_SPELL_CHECKERS; 61 } 62 63 @Override onCreate(final Bundle icicle)64 public void onCreate(final Bundle icicle) { 65 super.onCreate(icicle); 66 67 addPreferencesFromResource(R.xml.spellchecker_prefs); 68 mSpellCheckerLanaguagePref = findPreference(KEY_SPELL_CHECKER_LANGUAGE); 69 70 mTsm = (TextServicesManager) getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE); 71 mCurrentSci = mTsm.getCurrentSpellChecker(); 72 mEnabledScis = mTsm.getEnabledSpellCheckers(); 73 populatePreferenceScreen(); 74 } 75 populatePreferenceScreen()76 private void populatePreferenceScreen() { 77 final SpellCheckerPreference pref = new SpellCheckerPreference(getPrefContext(), 78 mEnabledScis); 79 pref.setTitle(R.string.default_spell_checker); 80 final int count = (mEnabledScis == null) ? 0 : mEnabledScis.length; 81 if (count > 0) { 82 pref.setSummary("%s"); 83 } else { 84 pref.setSummary(R.string.spell_checker_not_selected); 85 } 86 pref.setKey(KEY_DEFAULT_SPELL_CHECKER); 87 pref.setOnPreferenceChangeListener(this); 88 getPreferenceScreen().addPreference(pref); 89 } 90 91 @Override onResume()92 public void onResume() { 93 super.onResume(); 94 mSwitchBar = ((SettingsActivity) getActivity()).getSwitchBar(); 95 mSwitchBar.setTitle(getContext().getString(R.string.spell_checker_primary_switch_title)); 96 mSwitchBar.show(); 97 mSwitchBar.addOnSwitchChangeListener(this); 98 updatePreferenceScreen(); 99 } 100 101 @Override onPause()102 public void onPause() { 103 super.onPause(); 104 mSwitchBar.removeOnSwitchChangeListener(this); 105 } 106 107 @Override onSwitchChanged(final Switch switchView, final boolean isChecked)108 public void onSwitchChanged(final Switch switchView, final boolean isChecked) { 109 Settings.Secure.putInt(getContentResolver(), Settings.Secure.SPELL_CHECKER_ENABLED, 110 isChecked ? 1 : 0); 111 updatePreferenceScreen(); 112 } 113 updatePreferenceScreen()114 private void updatePreferenceScreen() { 115 mCurrentSci = mTsm.getCurrentSpellChecker(); 116 final boolean isSpellCheckerEnabled = mTsm.isSpellCheckerEnabled(); 117 mSwitchBar.setChecked(isSpellCheckerEnabled); 118 119 final SpellCheckerSubtype currentScs; 120 if (mCurrentSci != null) { 121 currentScs = mTsm.getCurrentSpellCheckerSubtype( 122 false /* allowImplicitlySelectedSubtype */); 123 } else { 124 currentScs = null; 125 } 126 mSpellCheckerLanaguagePref.setSummary(getSpellCheckerSubtypeLabel(mCurrentSci, currentScs)); 127 128 final PreferenceScreen screen = getPreferenceScreen(); 129 final int count = screen.getPreferenceCount(); 130 for (int index = 0; index < count; index++) { 131 final Preference preference = screen.getPreference(index); 132 preference.setEnabled(isSpellCheckerEnabled); 133 if (preference instanceof SpellCheckerPreference) { 134 final SpellCheckerPreference pref = (SpellCheckerPreference) preference; 135 pref.setSelected(mCurrentSci); 136 } 137 } 138 mSpellCheckerLanaguagePref.setEnabled(isSpellCheckerEnabled && mCurrentSci != null); 139 } 140 getSpellCheckerSubtypeLabel(final SpellCheckerInfo sci, final SpellCheckerSubtype subtype)141 private CharSequence getSpellCheckerSubtypeLabel(final SpellCheckerInfo sci, 142 final SpellCheckerSubtype subtype) { 143 if (sci == null) { 144 return getString(R.string.spell_checker_not_selected); 145 } 146 if (subtype == null) { 147 return getString(R.string.use_system_language_to_select_input_method_subtypes); 148 } 149 return subtype.getDisplayName( 150 getActivity(), sci.getPackageName(), sci.getServiceInfo().applicationInfo); 151 } 152 153 @Override onPreferenceTreeClick(Preference preference)154 public boolean onPreferenceTreeClick(Preference preference) { 155 if (KEY_SPELL_CHECKER_LANGUAGE.equals(preference.getKey())) { 156 writePreferenceClickMetric(preference); 157 showChooseLanguageDialog(); 158 return true; 159 } 160 return super.onPreferenceTreeClick(preference); 161 } 162 163 @Override onPreferenceChange(Preference preference, Object newValue)164 public boolean onPreferenceChange(Preference preference, Object newValue) { 165 final SpellCheckerInfo sci = (SpellCheckerInfo) newValue; 166 final boolean isSystemApp = 167 (sci.getServiceInfo().applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; 168 if (isSystemApp) { 169 changeCurrentSpellChecker(sci); 170 return true; 171 } else { 172 showSecurityWarnDialog(sci); 173 return false; 174 } 175 } 176 convertSubtypeIndexToDialogItemId(final int index)177 private static int convertSubtypeIndexToDialogItemId(final int index) { 178 return index + 1; 179 } 180 convertDialogItemIdToSubtypeIndex(final int item)181 private static int convertDialogItemIdToSubtypeIndex(final int item) { 182 return item - 1; 183 } 184 showChooseLanguageDialog()185 private void showChooseLanguageDialog() { 186 if (mDialog != null && mDialog.isShowing()) { 187 mDialog.dismiss(); 188 } 189 final SpellCheckerInfo currentSci = mTsm.getCurrentSpellChecker(); 190 if (currentSci == null) { 191 // This can happen in some situations. One example is that the package that the current 192 // spell checker belongs to was uninstalled or being in background. 193 return; 194 } 195 final SpellCheckerSubtype currentScs = mTsm.getCurrentSpellCheckerSubtype( 196 false /* allowImplicitlySelectedSubtype */); 197 final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 198 builder.setTitle(R.string.phone_language); 199 final int subtypeCount = currentSci.getSubtypeCount(); 200 final CharSequence[] items = new CharSequence[subtypeCount + 1 /* default */]; 201 items[ITEM_ID_USE_SYSTEM_LANGUAGE] = getSpellCheckerSubtypeLabel(currentSci, null); 202 int checkedItemId = ITEM_ID_USE_SYSTEM_LANGUAGE; 203 for (int index = 0; index < subtypeCount; ++index) { 204 final SpellCheckerSubtype subtype = currentSci.getSubtypeAt(index); 205 final int itemId = convertSubtypeIndexToDialogItemId(index); 206 items[itemId] = getSpellCheckerSubtypeLabel(currentSci, subtype); 207 if (subtype.equals(currentScs)) { 208 checkedItemId = itemId; 209 } 210 } 211 builder.setSingleChoiceItems(items, checkedItemId, new AlertDialog.OnClickListener() { 212 @Override 213 public void onClick(final DialogInterface dialog, final int item) { 214 final int subtypeId; 215 if (item == ITEM_ID_USE_SYSTEM_LANGUAGE) { 216 subtypeId = SpellCheckerSubtype.SUBTYPE_ID_NONE; 217 } else { 218 final int index = convertDialogItemIdToSubtypeIndex(item); 219 subtypeId = currentSci.getSubtypeAt(index).hashCode(); 220 } 221 222 Settings.Secure.putInt(getContentResolver(), 223 Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, subtypeId); 224 225 if (DBG) { 226 final SpellCheckerSubtype subtype = mTsm.getCurrentSpellCheckerSubtype( 227 true /* allowImplicitlySelectedSubtype */); 228 Log.d(TAG, "Current spell check locale is " 229 + subtype == null ? "null" : subtype.getLocale()); 230 } 231 dialog.dismiss(); 232 updatePreferenceScreen(); 233 } 234 }); 235 mDialog = builder.create(); 236 mDialog.show(); 237 } 238 showSecurityWarnDialog(final SpellCheckerInfo sci)239 private void showSecurityWarnDialog(final SpellCheckerInfo sci) { 240 if (mDialog != null && mDialog.isShowing()) { 241 mDialog.dismiss(); 242 } 243 final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 244 builder.setTitle(android.R.string.dialog_alert_title); 245 builder.setMessage(getString(R.string.spellchecker_security_warning, 246 sci.loadLabel(getPackageManager()))); 247 builder.setCancelable(true); 248 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 249 @Override 250 public void onClick(final DialogInterface dialog, final int which) { 251 changeCurrentSpellChecker(sci); 252 } 253 }); 254 builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { 255 @Override 256 public void onClick(final DialogInterface dialog, final int which) { 257 } 258 }); 259 mDialog = builder.create(); 260 mDialog.show(); 261 } 262 changeCurrentSpellChecker(final SpellCheckerInfo sci)263 private void changeCurrentSpellChecker(final SpellCheckerInfo sci) { 264 Settings.Secure.putString(getContentResolver(), Settings.Secure.SELECTED_SPELL_CHECKER, 265 sci.getId()); 266 // Reset the spell checker subtype 267 Settings.Secure.putInt(getContentResolver(), Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, 268 SpellCheckerSubtype.SUBTYPE_ID_NONE); 269 if (DBG) { 270 Log.d(TAG, "Current spell check is " + mTsm.getCurrentSpellChecker().getId()); 271 } 272 updatePreferenceScreen(); 273 } 274 } 275