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