workaround for missing input languages on few devices by being less strict if not enough languages matched the white-list

This commit is contained in:
Philipp Crocoll 2014-12-25 22:15:54 +01:00
parent 44b005f9d7
commit ae66f10690

View File

@ -42,14 +42,23 @@ public class InputLanguageSelection extends PreferenceActivity {
private static final String[] WHITELIST_LANGUAGES = { private static final String[] WHITELIST_LANGUAGES = {
"cs", "da", "de", "en_GB", "en_US", "es", "es_US", "fr", "it", "nb", "nl", "pl", "pt", "cs", "da", "de", "en_GB", "en_US", "es", "es_US", "fr", "it", "nb", "nl", "pl", "pt",
"ru", "tr", "ru", "tr"
}; };
private static boolean isWhitelisted(String lang) { private static final String[] WEAK_WHITELIST_LANGUAGES = {
for (String s : 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, boolean strict) {
for (String s : (strict? WHITELIST_LANGUAGES : WEAK_WHITELIST_LANGUAGES)) {
if (s.equalsIgnoreCase(lang)) { if (s.equalsIgnoreCase(lang)) {
return true; return true;
} }
if ((!strict) && (s.length()==2) && lang.toLowerCase(Locale.US).startsWith(s))
{
return true;
}
} }
return false; return false;
} }
@ -86,7 +95,14 @@ public class InputLanguageSelection extends PreferenceActivity {
addPreferencesFromResource(R.xml.language_prefs); addPreferencesFromResource(R.xml.language_prefs);
mSelectedLanguages = sp.getString(KP2AKeyboard.PREF_SELECTED_LANGUAGES, ""); mSelectedLanguages = sp.getString(KP2AKeyboard.PREF_SELECTED_LANGUAGES, "");
String[] languageList = mSelectedLanguages.split(","); 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(); PreferenceGroup parent = getPreferenceScreen();
for (int i = 0; i < mAvailableLanguages.size(); i++) { for (int i = 0; i < mAvailableLanguages.size(); i++) {
CheckBoxPreference pref = new CheckBoxPreference(this); CheckBoxPreference pref = new CheckBoxPreference(this);
@ -168,7 +184,7 @@ public class InputLanguageSelection extends PreferenceActivity {
SharedPreferencesCompat.apply(editor); SharedPreferencesCompat.apply(editor);
} }
ArrayList<Loc> getUniqueLocales() { ArrayList<Loc> getUniqueLocales(boolean strict) {
String[] locales = getAssets().getLocales(); String[] locales = getAssets().getLocales();
Arrays.sort(locales); Arrays.sort(locales);
ArrayList<Loc> uniqueLocales = new ArrayList<Loc>(); ArrayList<Loc> uniqueLocales = new ArrayList<Loc>();
@ -178,6 +194,7 @@ public class InputLanguageSelection extends PreferenceActivity {
int finalSize = 0; int finalSize = 0;
for (int i = 0 ; i < origSize; i++ ) { for (int i = 0 ; i < origSize; i++ ) {
String s = locales[i]; String s = locales[i];
int len = s.length(); int len = s.length();
final Locale l; final Locale l;
final String language; final String language;
@ -189,11 +206,17 @@ public class InputLanguageSelection extends PreferenceActivity {
language = s; language = s;
l = new Locale(language); l = new Locale(language);
} else { } else {
android.util.Log.d("KP2AK", "locale "+s+" has unexpected length.");
continue; continue;
} }
// Exclude languages that are not relevant to LatinIME // 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) { if (finalSize == 0) {
preprocess[finalSize++] = preprocess[finalSize++] =
new Loc(LanguageSwitcher.toTitleCase(l.getDisplayName(l), l), l); new Loc(LanguageSwitcher.toTitleCase(l.getDisplayName(l), l), l);