1 /* 2 * Copyright (C) 2016 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.content.Context; 20 import android.support.v7.preference.Preference; 21 import android.support.v7.preference.PreferenceScreen; 22 import android.view.textservice.SpellCheckerInfo; 23 import android.view.textservice.TextServicesManager; 24 25 import com.android.settings.R; 26 import com.android.settings.core.PreferenceController; 27 import com.android.settingslib.inputmethod.InputMethodAndSubtypeUtil; 28 29 public class SpellCheckerPreferenceController extends PreferenceController { 30 31 public static final String KEY_SPELL_CHECKERS = "spellcheckers_settings"; 32 33 private final TextServicesManager mTextServicesManager; 34 SpellCheckerPreferenceController(Context context)35 public SpellCheckerPreferenceController(Context context) { 36 super(context); 37 mTextServicesManager = (TextServicesManager) context.getSystemService( 38 Context.TEXT_SERVICES_MANAGER_SERVICE); 39 } 40 41 @Override displayPreference(PreferenceScreen screen)42 public void displayPreference(PreferenceScreen screen) { 43 super.displayPreference(screen); 44 final Preference preference = screen.findPreference(KEY_SPELL_CHECKERS); 45 if (preference != null) { 46 InputMethodAndSubtypeUtil.removeUnnecessaryNonPersistentPreference(preference); 47 } 48 } 49 50 @Override isAvailable()51 public boolean isAvailable() { 52 return true; 53 } 54 55 @Override getPreferenceKey()56 public String getPreferenceKey() { 57 return KEY_SPELL_CHECKERS; 58 } 59 60 @Override updateState(Preference preference)61 public void updateState(Preference preference) { 62 if (preference == null) { 63 return; 64 } 65 if (!mTextServicesManager.isSpellCheckerEnabled()) { 66 preference.setSummary(R.string.switch_off_text); 67 } else { 68 final SpellCheckerInfo sci = mTextServicesManager.getCurrentSpellChecker(); 69 if (sci != null) { 70 preference.setSummary(sci.loadLabel(mContext.getPackageManager())); 71 } else { 72 preference.setSummary(R.string.spell_checker_not_selected); 73 } 74 } 75 } 76 } 77