LanguageSwitcherProxy.java revision 6d9021527a38ba1e94225020389621a0d7227aa1
1/*
2 * Copyright (C) 2011 Google Inc.
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
17package com.android.inputmethod.deprecated;
18
19import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
20import com.android.inputmethod.deprecated.languageswitcher.LanguageSwitcher;
21import com.android.inputmethod.latin.LatinIME;
22import com.android.inputmethod.latin.Settings;
23
24import android.content.SharedPreferences;
25import android.content.res.Configuration;
26
27import java.util.Locale;
28
29// This class is used only when the IME doesn't use method.xml for language switching.
30public class LanguageSwitcherProxy implements SharedPreferences.OnSharedPreferenceChangeListener {
31    private static final LanguageSwitcherProxy sInstance = new LanguageSwitcherProxy();
32    private LanguageSwitcher mLanguageSwitcher;
33    private SharedPreferences mPrefs;
34
35    public static LanguageSwitcherProxy getInstance() {
36        if (InputMethodManagerCompatWrapper.SUBTYPE_SUPPORTED) return null;
37        return sInstance;
38    }
39
40    public static void init(LatinIME service, SharedPreferences prefs) {
41        if (InputMethodManagerCompatWrapper.SUBTYPE_SUPPORTED) return;
42        final Configuration conf = service.getResources().getConfiguration();
43        sInstance.mLanguageSwitcher = new LanguageSwitcher(service);
44        sInstance.mLanguageSwitcher.loadLocales(prefs, conf.locale);
45        sInstance.mPrefs = prefs;
46        prefs.registerOnSharedPreferenceChangeListener(sInstance);
47    }
48
49    public static void onConfigurationChanged(Configuration conf) {
50        if (InputMethodManagerCompatWrapper.SUBTYPE_SUPPORTED) return;
51        sInstance.mLanguageSwitcher.onConfigurationChanged(conf, sInstance.mPrefs);
52    }
53
54    public static void loadSettings() {
55        if (InputMethodManagerCompatWrapper.SUBTYPE_SUPPORTED) return;
56        sInstance.mLanguageSwitcher.loadLocales(sInstance.mPrefs, null);
57    }
58
59    public int getLocaleCount() {
60        return mLanguageSwitcher.getLocaleCount();
61    }
62
63    public String[] getEnabledLanguages(boolean allowImplicitlySelectedLanguages) {
64        return mLanguageSwitcher.getEnabledLanguages(allowImplicitlySelectedLanguages);
65    }
66
67    public Locale getInputLocale() {
68        return mLanguageSwitcher.getInputLocale();
69    }
70
71    public void setLocale(String localeStr) {
72        mLanguageSwitcher.setLocale(localeStr);
73        mLanguageSwitcher.persist(mPrefs);
74    }
75
76    @Override
77    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
78        // PREF_SELECTED_LANGUAGES: enabled input subtypes
79        // PREF_INPUT_LANGUAGE: current input subtype
80        if (key.equals(Settings.PREF_SELECTED_LANGUAGES)
81                || key.equals(Settings.PREF_INPUT_LANGUAGE)) {
82            mLanguageSwitcher.loadLocales(prefs, null);
83        }
84    }
85}
86