aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/android/inputmethod/latin/InputLanguageSelection.java
diff options
context:
space:
mode:
authorAndroid (Google) Code Review <android-gerrit@google.com>2009-12-16 17:49:59 -0800
committerAndroid (Google) Code Review <android-gerrit@google.com>2009-12-16 17:49:59 -0800
commit0ee83f27d70f34267dbbaf19668b6a66d3b4470d (patch)
treef3139b57fd8549dba7c0d8b39d8d0f3f3a0b9a0c /src/com/android/inputmethod/latin/InputLanguageSelection.java
parentf1557e197feaae0786c2ef251ffd4e274347b1bb (diff)
parent36fcf25833f7c8876cbc8286e0c159b052dc2626 (diff)
downloadlatinime-0ee83f27d70f34267dbbaf19668b6a66d3b4470d.tar.gz
latinime-0ee83f27d70f34267dbbaf19668b6a66d3b4470d.tar.xz
latinime-0ee83f27d70f34267dbbaf19668b6a66d3b4470d.zip
Merge change I8c9748c8 into eclair-mr2
* changes: Input language switching. Bug: 2331173
Diffstat (limited to 'src/com/android/inputmethod/latin/InputLanguageSelection.java')
-rw-r--r--src/com/android/inputmethod/latin/InputLanguageSelection.java178
1 files changed, 178 insertions, 0 deletions
diff --git a/src/com/android/inputmethod/latin/InputLanguageSelection.java b/src/com/android/inputmethod/latin/InputLanguageSelection.java
new file mode 100644
index 000000000..5bd1391c3
--- /dev/null
+++ b/src/com/android/inputmethod/latin/InputLanguageSelection.java
@@ -0,0 +1,178 @@
+/*
+ * Copyright (C) 2008-2009 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.android.inputmethod.latin;
+
+import java.text.Collator;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+
+import android.content.SharedPreferences;
+import android.content.SharedPreferences.Editor;
+import android.os.Bundle;
+import android.preference.CheckBoxPreference;
+import android.preference.PreferenceActivity;
+import android.preference.PreferenceGroup;
+import android.preference.PreferenceManager;
+import android.util.Log;
+
+public class InputLanguageSelection extends PreferenceActivity {
+
+ private String mSelectedLanguages;
+ private ArrayList<Loc> mAvailableLanguages = new ArrayList<Loc>();
+
+ private static class Loc implements Comparable {
+ static Collator sCollator = Collator.getInstance();
+
+ String label;
+ Locale locale;
+
+ public Loc(String label, Locale locale) {
+ this.label = label;
+ this.locale = locale;
+ }
+
+ @Override
+ public String toString() {
+ return this.label;
+ }
+
+ public int compareTo(Object o) {
+ return sCollator.compare(this.label, ((Loc) o).label);
+ }
+ }
+
+ @Override
+ protected void onCreate(Bundle icicle) {
+ super.onCreate(icicle);
+ addPreferencesFromResource(R.xml.language_prefs);
+ // Get the settings preferences
+ SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
+ mSelectedLanguages = sp.getString(LatinIME.PREF_SELECTED_LANGUAGES, "");
+ String[] languageList = mSelectedLanguages.split(",");
+ mAvailableLanguages = getUniqueLocales();
+ PreferenceGroup parent = getPreferenceScreen();
+ for (int i = 0; i < mAvailableLanguages.size(); i++) {
+ CheckBoxPreference pref = new CheckBoxPreference(this);
+ Locale locale = mAvailableLanguages.get(i).locale;
+ pref.setTitle(locale.getDisplayName(locale));
+ boolean checked = isLocaleIn(locale, languageList);
+ pref.setChecked(checked);
+ parent.addPreference(pref);
+ }
+ }
+
+ private boolean isLocaleIn(Locale locale, String[] list) {
+ String lang = get5Code(locale);
+ for (int i = 0; i < list.length; i++) {
+ if (lang.equalsIgnoreCase(list[i])) return true;
+ }
+ // If it matches the current locale
+ Locale displayLocale = getResources().getConfiguration().locale;
+ if (lang.equalsIgnoreCase(get5Code(displayLocale))) {
+ return true;
+ }
+ return false;
+ }
+
+ private String get5Code(Locale locale) {
+ return locale.getLanguage() + "_" + locale.getCountry();
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ }
+
+ @Override
+ protected void onPause() {
+ super.onPause();
+ // Save the selected languages
+ String checkedLanguages = "";
+ PreferenceGroup parent = getPreferenceScreen();
+ int count = parent.getPreferenceCount();
+ for (int i = 0; i < count; i++) {
+ CheckBoxPreference pref = (CheckBoxPreference) parent.getPreference(i);
+ if (pref.isChecked()) {
+ Locale locale = mAvailableLanguages.get(i).locale;
+ checkedLanguages += get5Code(locale) + ",";
+ }
+ }
+ if (checkedLanguages.length() < 1) checkedLanguages = null; // Save null
+ SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
+ Editor editor = sp.edit();
+ editor.putString(LatinIME.PREF_SELECTED_LANGUAGES, checkedLanguages);
+ editor.commit();
+ }
+
+ ArrayList<Loc> getUniqueLocales() {
+ String[] locales = getAssets().getLocales();
+ Arrays.sort(locales);
+ ArrayList<Loc> uniqueLocales = new ArrayList<Loc>();
+
+ final int origSize = locales.length;
+ Loc[] preprocess = new Loc[origSize];
+ int finalSize = 0;
+ for (int i = 0 ; i < origSize; i++ ) {
+ String s = locales[i];
+ int len = s.length();
+ if (len == 5) {
+ String language = s.substring(0, 2);
+ String country = s.substring(3, 5);
+ Locale l = new Locale(language, country);
+
+ if (finalSize == 0) {
+ preprocess[finalSize++] =
+ new Loc(toTitleCase(l.getDisplayName(l)), l);
+ } else {
+ // check previous entry:
+ // same lang and a country -> upgrade to full name and
+ // insert ours with full name
+ // diff lang -> insert ours with lang-only name
+ if (preprocess[finalSize-1].locale.getLanguage().equals(
+ language)) {
+ preprocess[finalSize-1].label = toTitleCase(
+ preprocess[finalSize-1].locale.getDisplayName());
+ preprocess[finalSize++] =
+ new Loc(toTitleCase(l.getDisplayName()), l);
+ } else {
+ String displayName;
+ if (s.equals("zz_ZZ")) {
+ } else {
+ displayName = toTitleCase(l.getDisplayName(l));
+ preprocess[finalSize++] = new Loc(displayName, l);
+ }
+ }
+ }
+ }
+ }
+ for (int i = 0; i < finalSize ; i++) {
+ uniqueLocales.add(preprocess[i]);
+ }
+ return uniqueLocales;
+ }
+
+ private static String toTitleCase(String s) {
+ if (s.length() == 0) {
+ return s;
+ }
+
+ return Character.toUpperCase(s.charAt(0)) + s.substring(1);
+ }
+
+}