PageRenderTime 51ms CodeModel.GetById 13ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://eyes-free.googlecode.com/
Java | 205 lines | 158 code | 23 blank | 24 comment | 22 complexity | d1c8fbd45a60186fdcb04bcaea802e89 MD5 | raw file
  1/*
  2 * Copyright (C) 2008-2009 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.content.res.Configuration;
 22import android.content.res.Resources;
 23import android.os.Bundle;
 24import android.preference.CheckBoxPreference;
 25import android.preference.PreferenceActivity;
 26import android.preference.PreferenceGroup;
 27import android.preference.PreferenceManager;
 28import android.text.TextUtils;
 29
 30import java.text.Collator;
 31import java.util.ArrayList;
 32import java.util.Arrays;
 33import java.util.Locale;
 34
 35public class InputLanguageSelection extends PreferenceActivity {
 36
 37    private String mSelectedLanguages;
 38    private ArrayList<Loc> mAvailableLanguages = new ArrayList<Loc>();
 39    private static final String[] BLACKLIST_LANGUAGES = {
 40        "ko", "ja", "zh", "el"
 41    };
 42
 43    private static class Loc implements Comparable<Object> {
 44        static Collator sCollator = Collator.getInstance();
 45
 46        String label;
 47        Locale locale;
 48
 49        public Loc(String label, Locale locale) {
 50            this.label = label;
 51            this.locale = locale;
 52        }
 53
 54        @Override
 55        public String toString() {
 56            return this.label;
 57        }
 58
 59        public int compareTo(Object o) {
 60            return sCollator.compare(this.label, ((Loc) o).label);
 61        }
 62    }
 63
 64    @Override
 65    protected void onCreate(Bundle icicle) {
 66        super.onCreate(icicle);
 67        addPreferencesFromResource(R.xml.language_prefs);
 68        // Get the settings preferences
 69        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
 70        mSelectedLanguages = sp.getString(LatinIME.PREF_SELECTED_LANGUAGES, "");
 71        String[] languageList = mSelectedLanguages.split(",");
 72        mAvailableLanguages = getUniqueLocales();
 73        PreferenceGroup parent = getPreferenceScreen();
 74        for (int i = 0; i < mAvailableLanguages.size(); i++) {
 75            CheckBoxPreference pref = new CheckBoxPreference(this);
 76            Locale locale = mAvailableLanguages.get(i).locale;
 77            pref.setTitle(LanguageSwitcher.toTitleCase(locale.getDisplayName(locale)));
 78            boolean checked = isLocaleIn(locale, languageList);
 79            pref.setChecked(checked);
 80            if (hasDictionary(locale)) {
 81                pref.setSummary(R.string.has_dictionary);
 82            }
 83            parent.addPreference(pref);
 84        }
 85    }
 86
 87    private boolean isLocaleIn(Locale locale, String[] list) {
 88        String lang = get5Code(locale);
 89        for (int i = 0; i < list.length; i++) {
 90            if (lang.equalsIgnoreCase(list[i])) return true;
 91        }
 92        return false;
 93    }
 94
 95    private boolean hasDictionary(Locale locale) {
 96        Resources res = getResources();
 97        Configuration conf = res.getConfiguration();
 98        Locale saveLocale = conf.locale;
 99        boolean haveDictionary = false;
100        conf.locale = locale;
101        res.updateConfiguration(conf, res.getDisplayMetrics());
102
103        int[] dictionaries = LatinIME.getDictionary(res);
104        BinaryDictionary bd = new BinaryDictionary(this, dictionaries, Suggest.DIC_MAIN);
105
106        // Is the dictionary larger than a placeholder? Arbitrarily chose a lower limit of
107        // 4000-5000 words, whereas the LARGE_DICTIONARY is about 20000+ words.
108        if (bd.getSize() > Suggest.LARGE_DICTIONARY_THRESHOLD / 4) {
109            haveDictionary = true;
110        }
111        bd.close();
112        conf.locale = saveLocale;
113        res.updateConfiguration(conf, res.getDisplayMetrics());
114        return haveDictionary;
115    }
116
117    private String get5Code(Locale locale) {
118        String country = locale.getCountry();
119        return locale.getLanguage()
120                + (TextUtils.isEmpty(country) ? "" : "_" + country);
121    }
122
123    @Override
124    protected void onResume() {
125        super.onResume();
126    }
127
128    @Override
129    protected void onPause() {
130        super.onPause();
131        // Save the selected languages
132        String checkedLanguages = "";
133        PreferenceGroup parent = getPreferenceScreen();
134        int count = parent.getPreferenceCount();
135        for (int i = 0; i < count; i++) {
136            CheckBoxPreference pref = (CheckBoxPreference) parent.getPreference(i);
137            if (pref.isChecked()) {
138                Locale locale = mAvailableLanguages.get(i).locale;
139                checkedLanguages += get5Code(locale) + ",";
140            }
141        }
142        if (checkedLanguages.length() < 1) checkedLanguages = null; // Save null
143        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
144        Editor editor = sp.edit();
145        editor.putString(LatinIME.PREF_SELECTED_LANGUAGES, checkedLanguages);
146        SharedPreferencesCompat.apply(editor);
147    }
148
149    ArrayList<Loc> getUniqueLocales() {
150        String[] locales = getAssets().getLocales();
151        Arrays.sort(locales);
152        ArrayList<Loc> uniqueLocales = new ArrayList<Loc>();
153
154        final int origSize = locales.length;
155        Loc[] preprocess = new Loc[origSize];
156        int finalSize = 0;
157        for (int i = 0 ; i < origSize; i++ ) {
158            String s = locales[i];
159            int len = s.length();
160            if (len == 5) {
161                String language = s.substring(0, 2);
162                String country = s.substring(3, 5);
163                Locale l = new Locale(language, country);
164
165                // Exclude languages that are not relevant to LatinIME
166                if (arrayContains(BLACKLIST_LANGUAGES, language)) continue;
167
168                if (finalSize == 0) {
169                    preprocess[finalSize++] =
170                            new Loc(LanguageSwitcher.toTitleCase(l.getDisplayName(l)), l);
171                } else {
172                    // check previous entry:
173                    //  same lang and a country -> upgrade to full name and
174                    //    insert ours with full name
175                    //  diff lang -> insert ours with lang-only name
176                    if (preprocess[finalSize-1].locale.getLanguage().equals(
177                            language)) {
178                        preprocess[finalSize-1].label = LanguageSwitcher.toTitleCase(
179                                preprocess[finalSize-1].locale.getDisplayName());
180                        preprocess[finalSize++] =
181                                new Loc(LanguageSwitcher.toTitleCase(l.getDisplayName()), l);
182                    } else {
183                        String displayName;
184                        if (s.equals("zz_ZZ")) {
185                        } else {
186                            displayName = LanguageSwitcher.toTitleCase(l.getDisplayName(l));
187                            preprocess[finalSize++] = new Loc(displayName, l);
188                        }
189                    }
190                }
191            }
192        }
193        for (int i = 0; i < finalSize ; i++) {
194            uniqueLocales.add(preprocess[i]);
195        }
196        return uniqueLocales;
197    }
198
199    private boolean arrayContains(String[] array, String value) {
200        for (int i = 0; i < array.length; i++) {
201            if (array[i].equalsIgnoreCase(value)) return true;
202        }
203        return false;
204    }
205}