From ae66f10690df5c2b72b2ab3ad7b458a46ccb88df Mon Sep 17 00:00:00 2001 From: Philipp Crocoll Date: Thu, 25 Dec 2014 22:15:54 +0100 Subject: [PATCH] workaround for missing input languages on few devices by being less strict if not enough languages matched the white-list --- .../softkeyboard/InputLanguageSelection.java | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/java/KP2ASoftKeyboard2/java/src/keepass2android/softkeyboard/InputLanguageSelection.java b/src/java/KP2ASoftKeyboard2/java/src/keepass2android/softkeyboard/InputLanguageSelection.java index f97e5251..66cb46f0 100644 --- a/src/java/KP2ASoftKeyboard2/java/src/keepass2android/softkeyboard/InputLanguageSelection.java +++ b/src/java/KP2ASoftKeyboard2/java/src/keepass2android/softkeyboard/InputLanguageSelection.java @@ -42,14 +42,23 @@ public class InputLanguageSelection extends PreferenceActivity { private static final String[] WHITELIST_LANGUAGES = { "cs", "da", "de", "en_GB", "en_US", "es", "es_US", "fr", "it", "nb", "nl", "pl", "pt", - "ru", "tr", + "ru", "tr" + }; + + private static final String[] WEAK_WHITELIST_LANGUAGES = { + "cs", "da", "de", "en_GB", "en_US", "es", "es_US", "fr", "it", "nb", "nl", "pl", "pt", + "ru", "tr", "en" }; - private static boolean isWhitelisted(String lang) { - for (String s : WHITELIST_LANGUAGES) { + private static boolean isWhitelisted(String lang, boolean strict) { + for (String s : (strict? WHITELIST_LANGUAGES : WEAK_WHITELIST_LANGUAGES)) { if (s.equalsIgnoreCase(lang)) { return true; } + if ((!strict) && (s.length()==2) && lang.toLowerCase(Locale.US).startsWith(s)) + { + return true; + } } return false; } @@ -86,7 +95,14 @@ public class InputLanguageSelection extends PreferenceActivity { addPreferencesFromResource(R.xml.language_prefs); mSelectedLanguages = sp.getString(KP2AKeyboard.PREF_SELECTED_LANGUAGES, ""); String[] languageList = mSelectedLanguages.split(","); - mAvailableLanguages = getUniqueLocales(); + + //first try to get the unique locales in a strict mode (filtering most redundant layouts like English (Jamaica) etc.) + mAvailableLanguages = getUniqueLocales(true); + //sometimes the strict check returns only EN_US, EN_GB and ES_US. Accept more in these cases: + if (mAvailableLanguages.size() < 5) + { + mAvailableLanguages = getUniqueLocales(false); + } PreferenceGroup parent = getPreferenceScreen(); for (int i = 0; i < mAvailableLanguages.size(); i++) { CheckBoxPreference pref = new CheckBoxPreference(this); @@ -168,7 +184,7 @@ public class InputLanguageSelection extends PreferenceActivity { SharedPreferencesCompat.apply(editor); } - ArrayList getUniqueLocales() { + ArrayList getUniqueLocales(boolean strict) { String[] locales = getAssets().getLocales(); Arrays.sort(locales); ArrayList uniqueLocales = new ArrayList(); @@ -178,6 +194,7 @@ public class InputLanguageSelection extends PreferenceActivity { int finalSize = 0; for (int i = 0 ; i < origSize; i++ ) { String s = locales[i]; + int len = s.length(); final Locale l; final String language; @@ -189,11 +206,17 @@ public class InputLanguageSelection extends PreferenceActivity { language = s; l = new Locale(language); } else { + android.util.Log.d("KP2AK", "locale "+s+" has unexpected length."); continue; } // Exclude languages that are not relevant to LatinIME - if (!isWhitelisted(s)) continue; + if (!isWhitelisted(s, strict)) + { + android.util.Log.d("KP2AK", "locale "+s+" is not white-listed"); + continue; + } + android.util.Log.d("KP2AK", "adding locale "+s); if (finalSize == 0) { preprocess[finalSize++] = new Loc(LanguageSwitcher.toTitleCase(l.getDisplayName(l), l), l);