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