From 15a0ba6d74feed57124938336f951ae14aa47ad9 Mon Sep 17 00:00:00 2001 From: satok Date: Thu, 21 Apr 2011 15:35:07 +0900 Subject: Check the availability of layouts for showing the input languages in the settings Bug: 4316889 Change-Id: I746b3ff79c2a6cd4925fca1817d5b209623b5108 --- .../languageswitcher/InputLanguageSelection.java | 248 +++++++++++++++++++++ .../inputmethod/keyboard/KeyboardParser.java | 22 ++ .../inputmethod/latin/InputLanguageSelection.java | 217 ------------------ .../com/android/inputmethod/latin/Settings.java | 6 + .../src/com/android/inputmethod/latin/Suggest.java | 2 +- 5 files changed, 277 insertions(+), 218 deletions(-) create mode 100644 java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java delete mode 100644 java/src/com/android/inputmethod/latin/InputLanguageSelection.java (limited to 'java/src') diff --git a/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java b/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java new file mode 100644 index 000000000..9ad64f876 --- /dev/null +++ b/java/src/com/android/inputmethod/deprecated/languageswitcher/InputLanguageSelection.java @@ -0,0 +1,248 @@ +/* + * 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.deprecated.languageswitcher; + +import com.android.inputmethod.keyboard.KeyboardParser; +import com.android.inputmethod.latin.BinaryDictionary; +import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.Settings; +import com.android.inputmethod.latin.SharedPreferencesCompat; +import com.android.inputmethod.latin.SubtypeSwitcher; +import com.android.inputmethod.latin.Suggest; +import com.android.inputmethod.latin.Utils; + +import org.xmlpull.v1.XmlPullParserException; + +import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Bundle; +import android.preference.CheckBoxPreference; +import android.preference.PreferenceActivity; +import android.preference.PreferenceGroup; +import android.preference.PreferenceManager; +import android.text.TextUtils; + +import java.io.IOException; +import java.text.Collator; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Locale; + +public class InputLanguageSelection extends PreferenceActivity { + + private SharedPreferences mPrefs; + private String mSelectedLanguages; + private ArrayList mAvailableLanguages = new ArrayList(); + + private static class Loc implements Comparable { + private static Collator sCollator = Collator.getInstance(); + + private String mLabel; + public final Locale mLocale; + + public Loc(String label, Locale locale) { + this.mLabel = label; + this.mLocale = locale; + } + + public void setLabel(String label) { + this.mLabel = label; + } + + @Override + public String toString() { + return this.mLabel; + } + + @Override + public int compareTo(Object o) { + return sCollator.compare(this.mLabel, ((Loc) o).mLabel); + } + } + + @Override + protected void onCreate(Bundle icicle) { + super.onCreate(icicle); + addPreferencesFromResource(R.xml.language_prefs); + // Get the settings preferences + mPrefs = PreferenceManager.getDefaultSharedPreferences(this); + mSelectedLanguages = mPrefs.getString(Settings.PREF_SELECTED_LANGUAGES, ""); + String[] languageList = mSelectedLanguages.split(","); + mAvailableLanguages = getUniqueLocales(); + PreferenceGroup parent = getPreferenceScreen(); + for (int i = 0; i < mAvailableLanguages.size(); i++) { + Locale locale = mAvailableLanguages.get(i).mLocale; + if (!hasDictionary(locale) && !hasLayout(locale)) { + continue; + } + CheckBoxPreference pref = new CheckBoxPreference(this); + pref.setTitle(SubtypeSwitcher.getFullDisplayName(locale, true)); + boolean checked = isLocaleIn(locale, languageList); + pref.setChecked(checked); + if (hasDictionary(locale)) { + pref.setSummary(R.string.has_dictionary); + } + 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; + } + return false; + } + + private boolean hasDictionary(Locale locale) { + final Resources res = getResources(); + final Configuration conf = res.getConfiguration(); + final Locale saveLocale = conf.locale; + boolean haveDictionary = false; + conf.locale = locale; + res.updateConfiguration(conf, res.getDisplayMetrics()); + + BinaryDictionary bd = BinaryDictionary.initDictionaryFromManager(this, Suggest.DIC_MAIN, + locale, Utils.getMainDictionaryResourceId(res)); + + // Is the dictionary larger than a placeholder? Arbitrarily chose a lower limit of + // 4000-5000 words, whereas the LARGE_DICTIONARY is about 20000+ words. + if (bd.getSize() > Suggest.LARGE_DICTIONARY_THRESHOLD / 4) { + haveDictionary = true; + } + bd.close(); + conf.locale = saveLocale; + res.updateConfiguration(conf, res.getDisplayMetrics()); + return haveDictionary; + } + + private boolean hasLayout(Locale locale) { + if (locale == null) return false; + final Resources res = getResources(); + final Configuration conf = res.getConfiguration(); + final Locale saveLocale = conf.locale; + conf.locale = locale; + res.updateConfiguration(conf, res.getDisplayMetrics()); + + try { + final String countryCode = locale.getLanguage(); + final String layoutCountryCode = KeyboardParser.parseKeyboardLocale( + this, R.xml.kbd_qwerty); + if (!TextUtils.isEmpty(countryCode) && !TextUtils.isEmpty(layoutCountryCode)) { + return countryCode.subSequence(0, 2).equals(layoutCountryCode.substring(0, 2)); + } + return false; + } catch (XmlPullParserException e) { + return false; + } catch (IOException e) { + return false; + } finally { + conf.locale = saveLocale; + res.updateConfiguration(conf, res.getDisplayMetrics()); + } + } + + private String get5Code(Locale locale) { + String country = locale.getCountry(); + return locale.getLanguage() + + (TextUtils.isEmpty(country) ? "" : "_" + country); + } + + @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).mLocale; + checkedLanguages += get5Code(locale) + ","; + } + } + if (checkedLanguages.length() < 1) checkedLanguages = null; // Save null + Editor editor = mPrefs.edit(); + editor.putString(Settings.PREF_SELECTED_LANGUAGES, checkedLanguages); + SharedPreferencesCompat.apply(editor); + } + + public ArrayList getUniqueLocales() { + String[] locales = getAssets().getLocales(); + Arrays.sort(locales); + ArrayList uniqueLocales = new ArrayList(); + + 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(); + String language = ""; + String country = ""; + if (len == 5) { + language = s.substring(0, 2); + country = s.substring(3, 5); + } else if (len < 5) { + language = s; + } + Locale l = new Locale(language, country); + + // Exclude languages that are not relevant to LatinIME + if (TextUtils.isEmpty(language)) { + continue; + } + + if (finalSize == 0) { + preprocess[finalSize++] = + new Loc(SubtypeSwitcher.getFullDisplayName(l, true), 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].mLocale.getLanguage().equals( + language)) { + preprocess[finalSize-1].setLabel(SubtypeSwitcher.getFullDisplayName( + preprocess[finalSize-1].mLocale, false)); + preprocess[finalSize++] = + new Loc(SubtypeSwitcher.getFullDisplayName(l, false), l); + } else { + String displayName; + if (s.equals("zz_ZZ")) { + // ignore this locale + } else { + displayName = SubtypeSwitcher.getFullDisplayName(l, true); + preprocess[finalSize++] = new Loc(displayName, l); + } + } + } + } + for (int i = 0; i < finalSize ; i++) { + uniqueLocales.add(preprocess[i]); + } + return uniqueLocales; + } +} diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardParser.java b/java/src/com/android/inputmethod/keyboard/KeyboardParser.java index 9c556c309..69ae7886a 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardParser.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardParser.java @@ -22,6 +22,7 @@ import com.android.inputmethod.latin.R; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; +import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; import android.content.res.XmlResourceParser; @@ -163,6 +164,27 @@ public class KeyboardParser { } } + public static String parseKeyboardLocale( + Context context, int resId) throws XmlPullParserException, IOException { + final Resources res = context.getResources(); + final XmlResourceParser parser = res.getXml(resId); + if (parser == null) return ""; + int event; + while ((event = parser.next()) != XmlPullParser.END_DOCUMENT) { + if (event == XmlPullParser.START_TAG) { + final String tag = parser.getName(); + if (TAG_KEYBOARD.equals(tag)) { + final TypedArray keyboardAttr = res.obtainAttributes(Xml.asAttributeSet(parser), + R.styleable.Keyboard); + return keyboardAttr.getString(R.styleable.Keyboard_keyboardLocale); + } else { + throw new IllegalStartTag(parser, TAG_KEYBOARD); + } + } + } + return ""; + } + private void parseKeyboardAttributes(XmlResourceParser parser) { final Keyboard keyboard = mKeyboard; final TypedArray keyboardAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser), diff --git a/java/src/com/android/inputmethod/latin/InputLanguageSelection.java b/java/src/com/android/inputmethod/latin/InputLanguageSelection.java deleted file mode 100644 index 40ab28c98..000000000 --- a/java/src/com/android/inputmethod/latin/InputLanguageSelection.java +++ /dev/null @@ -1,217 +0,0 @@ -/* - * 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 android.content.SharedPreferences; -import android.content.SharedPreferences.Editor; -import android.content.res.Configuration; -import android.content.res.Resources; -import android.os.Bundle; -import android.preference.CheckBoxPreference; -import android.preference.PreferenceActivity; -import android.preference.PreferenceGroup; -import android.preference.PreferenceManager; -import android.text.TextUtils; - -import java.text.Collator; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Locale; - -public class InputLanguageSelection extends PreferenceActivity { - - private SharedPreferences mPrefs; - private String mSelectedLanguages; - private ArrayList mAvailableLanguages = new ArrayList(); - private static final String[] BLACKLIST_LANGUAGES = { - "ko", "ja", "zh", "el", "zz" - }; - - private static class Loc implements Comparable { - private static Collator sCollator = Collator.getInstance(); - - private String mLabel; - public final Locale mLocale; - - public Loc(String label, Locale locale) { - this.mLabel = label; - this.mLocale = locale; - } - - public void setLabel(String label) { - this.mLabel = label; - } - - @Override - public String toString() { - return this.mLabel; - } - - @Override - public int compareTo(Object o) { - return sCollator.compare(this.mLabel, ((Loc) o).mLabel); - } - } - - @Override - protected void onCreate(Bundle icicle) { - super.onCreate(icicle); - addPreferencesFromResource(R.xml.language_prefs); - // Get the settings preferences - mPrefs = PreferenceManager.getDefaultSharedPreferences(this); - mSelectedLanguages = mPrefs.getString(Settings.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).mLocale; - pref.setTitle(SubtypeSwitcher.getFullDisplayName(locale, true)); - boolean checked = isLocaleIn(locale, languageList); - pref.setChecked(checked); - if (hasDictionary(locale)) { - pref.setSummary(R.string.has_dictionary); - } - 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; - } - return false; - } - - private boolean hasDictionary(Locale locale) { - final Resources res = getResources(); - final Configuration conf = res.getConfiguration(); - final Locale saveLocale = conf.locale; - boolean haveDictionary = false; - conf.locale = locale; - res.updateConfiguration(conf, res.getDisplayMetrics()); - - BinaryDictionary bd = BinaryDictionary.initDictionaryFromManager(this, Suggest.DIC_MAIN, - locale, Utils.getMainDictionaryResourceId(res)); - - // Is the dictionary larger than a placeholder? Arbitrarily chose a lower limit of - // 4000-5000 words, whereas the LARGE_DICTIONARY is about 20000+ words. - if (bd.getSize() > Suggest.LARGE_DICTIONARY_THRESHOLD / 4) { - haveDictionary = true; - } - bd.close(); - conf.locale = saveLocale; - res.updateConfiguration(conf, res.getDisplayMetrics()); - return haveDictionary; - } - - private String get5Code(Locale locale) { - String country = locale.getCountry(); - return locale.getLanguage() - + (TextUtils.isEmpty(country) ? "" : "_" + country); - } - - @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).mLocale; - checkedLanguages += get5Code(locale) + ","; - } - } - if (checkedLanguages.length() < 1) checkedLanguages = null; // Save null - Editor editor = mPrefs.edit(); - editor.putString(Settings.PREF_SELECTED_LANGUAGES, checkedLanguages); - SharedPreferencesCompat.apply(editor); - } - - public ArrayList getUniqueLocales() { - String[] locales = getAssets().getLocales(); - Arrays.sort(locales); - ArrayList uniqueLocales = new ArrayList(); - - 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(); - String language = ""; - String country = ""; - if (len == 5) { - language = s.substring(0, 2); - country = s.substring(3, 5); - } else if (len < 5) { - language = s; - } - Locale l = new Locale(language, country); - - // Exclude languages that are not relevant to LatinIME - if (arrayContains(BLACKLIST_LANGUAGES, language) || TextUtils.isEmpty(language)) { - continue; - } - - if (finalSize == 0) { - preprocess[finalSize++] = - new Loc(SubtypeSwitcher.getFullDisplayName(l, true), 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].mLocale.getLanguage().equals( - language)) { - preprocess[finalSize-1].setLabel(SubtypeSwitcher.getFullDisplayName( - preprocess[finalSize-1].mLocale, false)); - preprocess[finalSize++] = - new Loc(SubtypeSwitcher.getFullDisplayName(l, false), l); - } else { - String displayName; - if (s.equals("zz_ZZ")) { - // ignore this locale - } else { - displayName = SubtypeSwitcher.getFullDisplayName(l, true); - preprocess[finalSize++] = new Loc(displayName, l); - } - } - } - } - for (int i = 0; i < finalSize ; i++) { - uniqueLocales.add(preprocess[i]); - } - return uniqueLocales; - } - - private boolean arrayContains(String[] array, String value) { - for (int i = 0; i < array.length; i++) { - if (array[i].equalsIgnoreCase(value)) return true; - } - return false; - } -} diff --git a/java/src/com/android/inputmethod/latin/Settings.java b/java/src/com/android/inputmethod/latin/Settings.java index 8f7278e3a..1725ee7aa 100644 --- a/java/src/com/android/inputmethod/latin/Settings.java +++ b/java/src/com/android/inputmethod/latin/Settings.java @@ -18,6 +18,7 @@ package com.android.inputmethod.latin; import com.android.inputmethod.compat.CompatUtils; import com.android.inputmethod.compat.InputMethodManagerCompatWrapper; +import com.android.inputmethod.compat.InputMethodServiceCompatWrapper; import com.android.inputmethod.deprecated.VoiceProxy; import com.android.inputmethod.compat.VibratorCompatWrapper; @@ -67,6 +68,7 @@ public class Settings extends PreferenceActivity public static final String PREF_AUTO_CORRECTION_THRESHOLD = "auto_correction_threshold"; public static final String PREF_BIGRAM_SUGGESTIONS = "bigram_suggestion"; public static final String PREF_DEBUG_SETTINGS = "debug_settings"; + public static final String PREF_LANGUAGE_SELECTION = "language_selection"; public static final String PREF_USABILITY_STUDY_MODE = "usability_study_mode"; @@ -183,6 +185,10 @@ public class Settings extends PreferenceActivity if (!showUsabilityModeStudyOption) { getPreferenceScreen().removePreference(findPreference(PREF_USABILITY_STUDY_MODE)); } + + if (InputMethodServiceCompatWrapper.CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED) { + generalSettings.removePreference(findPreference(PREF_LANGUAGE_SELECTION)); + } } @Override diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index cd7f71c2a..f37206223 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -75,7 +75,7 @@ public class Suggest implements Dictionary.WordCallback { public static final String DICT_KEY_USER_BIGRAM = "user_bigram"; public static final String DICT_KEY_WHITELIST ="whitelist"; - static final int LARGE_DICTIONARY_THRESHOLD = 200 * 1000; + public static final int LARGE_DICTIONARY_THRESHOLD = 200 * 1000; private static final boolean DBG = LatinImeLogger.sDBG; -- cgit v1.2.3-83-g751a