2009-10-12 20:48:35 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2010-03-17 22:36:23 +00:00
|
|
|
import android.content.res.Configuration;
|
|
|
|
import android.content.res.Resources;
|
2009-10-12 20:48:35 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.preference.CheckBoxPreference;
|
|
|
|
import android.preference.PreferenceActivity;
|
|
|
|
import android.preference.PreferenceGroup;
|
|
|
|
import android.preference.PreferenceManager;
|
2010-02-24 03:01:43 +00:00
|
|
|
import android.text.TextUtils;
|
2009-10-12 20:48:35 +00:00
|
|
|
|
2010-11-29 08:57:48 +00:00
|
|
|
import java.text.Collator;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
2009-10-12 20:48:35 +00:00
|
|
|
public class InputLanguageSelection extends PreferenceActivity {
|
|
|
|
|
2010-11-26 04:08:36 +00:00
|
|
|
private SharedPreferences mPrefs;
|
2009-10-12 20:48:35 +00:00
|
|
|
private String mSelectedLanguages;
|
|
|
|
private ArrayList<Loc> mAvailableLanguages = new ArrayList<Loc>();
|
2010-03-04 12:43:15 +00:00
|
|
|
private static final String[] BLACKLIST_LANGUAGES = {
|
2010-11-18 00:55:23 +00:00
|
|
|
"ko", "ja", "zh", "el", "zz"
|
2010-03-04 12:43:15 +00:00
|
|
|
};
|
2009-10-12 20:48:35 +00:00
|
|
|
|
2010-08-24 03:38:39 +00:00
|
|
|
private static class Loc implements Comparable<Object> {
|
2009-10-12 20:48:35 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
@Override
|
2009-10-12 20:48:35 +00:00
|
|
|
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
|
2010-11-26 04:08:36 +00:00
|
|
|
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2010-12-09 12:06:26 +00:00
|
|
|
mSelectedLanguages = mPrefs.getString(Settings.PREF_SELECTED_LANGUAGES, "");
|
2009-10-12 20:48:35 +00:00
|
|
|
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;
|
2010-11-18 00:55:23 +00:00
|
|
|
pref.setTitle(SubtypeSwitcher.getFullDisplayName(locale, true));
|
2009-10-12 20:48:35 +00:00
|
|
|
boolean checked = isLocaleIn(locale, languageList);
|
|
|
|
pref.setChecked(checked);
|
2010-03-17 22:36:23 +00:00
|
|
|
if (hasDictionary(locale)) {
|
|
|
|
pref.setSummary(R.string.has_dictionary);
|
|
|
|
}
|
2009-10-12 20:48:35 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-03-17 22:36:23 +00:00
|
|
|
private boolean hasDictionary(Locale locale) {
|
|
|
|
Resources res = getResources();
|
|
|
|
Configuration conf = res.getConfiguration();
|
|
|
|
Locale saveLocale = conf.locale;
|
|
|
|
boolean haveDictionary = false;
|
|
|
|
conf.locale = locale;
|
|
|
|
res.updateConfiguration(conf, res.getDisplayMetrics());
|
2010-08-20 05:35:02 +00:00
|
|
|
|
|
|
|
int[] dictionaries = LatinIME.getDictionary(res);
|
|
|
|
BinaryDictionary bd = new BinaryDictionary(this, dictionaries, Suggest.DIC_MAIN);
|
|
|
|
|
2010-03-17 22:36:23 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2009-10-12 20:48:35 +00:00
|
|
|
private String get5Code(Locale locale) {
|
2010-02-24 03:01:43 +00:00
|
|
|
String country = locale.getCountry();
|
|
|
|
return locale.getLanguage()
|
|
|
|
+ (TextUtils.isEmpty(country) ? "" : "_" + country);
|
2009-10-12 20:48:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
2010-11-26 04:08:36 +00:00
|
|
|
Editor editor = mPrefs.edit();
|
2010-12-09 12:06:26 +00:00
|
|
|
editor.putString(Settings.PREF_SELECTED_LANGUAGES, checkedLanguages);
|
2010-09-12 12:05:25 +00:00
|
|
|
SharedPreferencesCompat.apply(editor);
|
2009-10-12 20:48:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2010-03-04 12:43:15 +00:00
|
|
|
// Exclude languages that are not relevant to LatinIME
|
|
|
|
if (arrayContains(BLACKLIST_LANGUAGES, language)) continue;
|
|
|
|
|
2009-10-12 20:48:35 +00:00
|
|
|
if (finalSize == 0) {
|
|
|
|
preprocess[finalSize++] =
|
2010-11-18 00:55:23 +00:00
|
|
|
new Loc(SubtypeSwitcher.getFullDisplayName(l, true), l);
|
2009-10-12 20:48:35 +00:00
|
|
|
} 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)) {
|
2010-11-18 00:55:23 +00:00
|
|
|
preprocess[finalSize-1].label = SubtypeSwitcher.getFullDisplayName(
|
|
|
|
preprocess[finalSize-1].locale, false);
|
2009-10-12 20:48:35 +00:00
|
|
|
preprocess[finalSize++] =
|
2010-11-18 00:55:23 +00:00
|
|
|
new Loc(SubtypeSwitcher.getFullDisplayName(l, false), l);
|
2009-10-12 20:48:35 +00:00
|
|
|
} else {
|
|
|
|
String displayName;
|
|
|
|
if (s.equals("zz_ZZ")) {
|
|
|
|
} else {
|
2010-11-18 00:55:23 +00:00
|
|
|
displayName = SubtypeSwitcher.getFullDisplayName(l, true);
|
2009-10-12 20:48:35 +00:00
|
|
|
preprocess[finalSize++] = new Loc(displayName, l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < finalSize ; i++) {
|
|
|
|
uniqueLocales.add(preprocess[i]);
|
|
|
|
}
|
|
|
|
return uniqueLocales;
|
|
|
|
}
|
2010-03-04 12:43:15 +00:00
|
|
|
|
|
|
|
private boolean arrayContains(String[] array, String value) {
|
|
|
|
for (int i = 0; i < array.length; i++) {
|
|
|
|
if (array[i].equalsIgnoreCase(value)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-10-12 20:48:35 +00:00
|
|
|
}
|