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