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