PageRenderTime 18ms CodeModel.GetById 7ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/ime/latinime/src/com/googlecode/eyesfree/inputmethod/latin/LanguageSwitcher.java

http://eyes-free.googlecode.com/
Java | 201 lines | 117 code | 28 blank | 56 comment | 23 complexity | aebd63e1b88c5217960b5f982aa1f8e8 MD5 | raw file
  1/*
  2 * Copyright (C) 2010 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.googlecode.eyesfree.inputmethod.latin;
 18
 19import android.content.SharedPreferences;
 20import android.content.SharedPreferences.Editor;
 21import android.preference.PreferenceManager;
 22import android.text.TextUtils;
 23
 24import java.util.Locale;
 25
 26/**
 27 * Keeps track of list of selected input languages and the current
 28 * input language that the user has selected.
 29 */
 30public class LanguageSwitcher {
 31
 32    private Locale[] mLocales;
 33    private LatinIME mIme;
 34    private String[] mSelectedLanguageArray;
 35    private String   mSelectedLanguages;
 36    private int      mCurrentIndex = 0;
 37    private String   mDefaultInputLanguage;
 38    private Locale   mDefaultInputLocale;
 39    private Locale   mSystemLocale;
 40
 41    public LanguageSwitcher(LatinIME ime) {
 42        mIme = ime;
 43        mLocales = new Locale[0];
 44    }
 45
 46    public Locale[]  getLocales() {
 47        return mLocales;
 48    }
 49
 50    public int getLocaleCount() {
 51        return mLocales.length;
 52    }
 53
 54    /**
 55     * Loads the currently selected input languages from shared preferences.
 56     * @param sp
 57     * @return whether there was any change
 58     */
 59    public boolean loadLocales(SharedPreferences sp) {
 60        String selectedLanguages = sp.getString(LatinIME.PREF_SELECTED_LANGUAGES, null);
 61        String currentLanguage   = sp.getString(LatinIME.PREF_INPUT_LANGUAGE, null);
 62        if (selectedLanguages == null || selectedLanguages.length() < 1) {
 63            loadDefaults();
 64            if (mLocales.length == 0) {
 65                return false;
 66            }
 67            mLocales = new Locale[0];
 68            return true;
 69        }
 70        if (selectedLanguages.equals(mSelectedLanguages)) {
 71            return false;
 72        }
 73        mSelectedLanguageArray = selectedLanguages.split(",");
 74        mSelectedLanguages = selectedLanguages; // Cache it for comparison later
 75        constructLocales();
 76        mCurrentIndex = 0;
 77        if (currentLanguage != null) {
 78            // Find the index
 79            mCurrentIndex = 0;
 80            for (int i = 0; i < mLocales.length; i++) {
 81                if (mSelectedLanguageArray[i].equals(currentLanguage)) {
 82                    mCurrentIndex = i;
 83                    break;
 84                }
 85            }
 86            // If we didn't find the index, use the first one
 87        }
 88        return true;
 89    }
 90
 91    private void loadDefaults() {
 92        mDefaultInputLocale = mIme.getResources().getConfiguration().locale;
 93        String country = mDefaultInputLocale.getCountry();
 94        mDefaultInputLanguage = mDefaultInputLocale.getLanguage() +
 95                (TextUtils.isEmpty(country) ? "" : "_" + country);
 96    }
 97
 98    private void constructLocales() {
 99        mLocales = new Locale[mSelectedLanguageArray.length];
100        for (int i = 0; i < mLocales.length; i++) {
101            final String lang = mSelectedLanguageArray[i];
102            mLocales[i] = new Locale(lang.substring(0, 2),
103                    lang.length() > 4 ? lang.substring(3, 5) : "");
104        }
105    }
106
107    /**
108     * Returns the currently selected input language code, or the display language code if
109     * no specific locale was selected for input.
110     */
111    public String getInputLanguage() {
112        if (getLocaleCount() == 0) return mDefaultInputLanguage;
113
114        return mSelectedLanguageArray[mCurrentIndex];
115    }
116    
117    /**
118     * Returns the list of enabled language codes.
119     */
120    public String[] getEnabledLanguages() {
121        return mSelectedLanguageArray;
122    }
123
124    /**
125     * Returns the currently selected input locale, or the display locale if no specific
126     * locale was selected for input.
127     * @return the currently selected input locale
128     */
129    public Locale getInputLocale() {
130        if (getLocaleCount() == 0) return mDefaultInputLocale;
131
132        return mLocales[mCurrentIndex];
133    }
134
135    /**
136     * Returns the next input locale in the list. Wraps around to the beginning of the
137     * list if we're at the end of the list.
138     * @return the next input locale in the list
139     */
140    public Locale getNextInputLocale() {
141        if (getLocaleCount() == 0) return mDefaultInputLocale;
142
143        return mLocales[(mCurrentIndex + 1) % mLocales.length];
144    }
145
146    /**
147     * Sets the system locale (display UI) used for comparing with the input language.
148     * @param locale the locale of the system
149     */
150    public void setSystemLocale(Locale locale) {
151        mSystemLocale = locale;
152    }
153
154    /**
155     * Returns the system locale.
156     * @return the system locale
157     */
158    public Locale getSystemLocale() {
159        return mSystemLocale;
160    }
161
162    /**
163     * Returns the previous input locale in the list. Wraps around to the end of the
164     * list if we're at the beginning of the list.
165     * @return the previous input locale in the list
166     */
167    public Locale getPrevInputLocale() {
168        if (getLocaleCount() == 0) return mDefaultInputLocale;
169
170        return mLocales[(mCurrentIndex - 1 + mLocales.length) % mLocales.length];
171    }
172
173    public void reset() {
174        mCurrentIndex = 0;
175    }
176
177    public void next() {
178        mCurrentIndex++;
179        if (mCurrentIndex >= mLocales.length) mCurrentIndex = 0; // Wrap around
180    }
181
182    public void prev() {
183        mCurrentIndex--;
184        if (mCurrentIndex < 0) mCurrentIndex = mLocales.length - 1; // Wrap around
185    }
186
187    public void persist() {
188        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mIme);
189        Editor editor = sp.edit();
190        editor.putString(LatinIME.PREF_INPUT_LANGUAGE, getInputLanguage());
191        SharedPreferencesCompat.apply(editor);
192    }
193
194    static String toTitleCase(String s) {
195        if (s.length() == 0) {
196            return s;
197        }
198
199        return Character.toUpperCase(s.charAt(0)) + s.substring(1);
200    }
201}