1/*
2 * Copyright (C) 2011 The Android Open Source Project
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 LatinIME mService;
33    private LanguageSwitcher mLanguageSwitcher;
34    private SharedPreferences mPrefs;
35
36    public static LanguageSwitcherProxy getInstance() {
37        if (InputMethodManagerCompatWrapper.SUBTYPE_SUPPORTED) return null;
38        return sInstance;
39    }
40
41    public static void init(LatinIME service, SharedPreferences prefs) {
42        if (InputMethodManagerCompatWrapper.SUBTYPE_SUPPORTED) return;
43        final Configuration conf = service.getResources().getConfiguration();
44        sInstance.mLanguageSwitcher = new LanguageSwitcher(service);
45        sInstance.mLanguageSwitcher.loadLocales(prefs, conf.locale);
46        sInstance.mPrefs = prefs;
47        sInstance.mService = service;
48        prefs.registerOnSharedPreferenceChangeListener(sInstance);
49    }
50
51    public static void onConfigurationChanged(Configuration conf) {
52        if (InputMethodManagerCompatWrapper.SUBTYPE_SUPPORTED) return;
53        sInstance.mLanguageSwitcher.onConfigurationChanged(conf, sInstance.mPrefs);
54    }
55
56    public static void loadSettings() {
57        if (InputMethodManagerCompatWrapper.SUBTYPE_SUPPORTED) return;
58        sInstance.mLanguageSwitcher.loadLocales(sInstance.mPrefs, null);
59    }
60
61    public int getLocaleCount() {
62        return mLanguageSwitcher.getLocaleCount();
63    }
64
65    public String[] getEnabledLanguages(boolean allowImplicitlySelectedLanguages) {
66        return mLanguageSwitcher.getEnabledLanguages(allowImplicitlySelectedLanguages);
67    }
68
69    public Locale getInputLocale() {
70        return mLanguageSwitcher.getInputLocale();
71    }
72
73    public void setLocale(String localeStr) {
74        mLanguageSwitcher.setLocale(localeStr);
75        mLanguageSwitcher.persist(mPrefs);
76    }
77
78    @Override
79    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
80        // PREF_SELECTED_LANGUAGES: enabled input subtypes
81        // PREF_INPUT_LANGUAGE: current input subtype
82        if (key.equals(Settings.PREF_SELECTED_LANGUAGES)
83                || key.equals(Settings.PREF_INPUT_LANGUAGE)) {
84            mLanguageSwitcher.loadLocales(prefs, null);
85            if (mService != null) {
86                mService.onRefreshKeyboard();
87            }
88        }
89    }
90}
91