Fix for language bar switching

Change-Id: Id3974642b62b012968c537eff7c1a7ee37e2def2
This commit is contained in:
satok 2011-04-14 11:42:08 +09:00
parent 0851abf999
commit 6d9021527a
5 changed files with 39 additions and 16 deletions

View file

@ -107,7 +107,8 @@ public class InputMethodManagerCompatWrapper {
public List<InputMethodSubtypeCompatWrapper> getEnabledInputMethodSubtypeList( public List<InputMethodSubtypeCompatWrapper> getEnabledInputMethodSubtypeList(
InputMethodInfoCompatWrapper imi, boolean allowsImplicitlySelectedSubtypes) { InputMethodInfoCompatWrapper imi, boolean allowsImplicitlySelectedSubtypes) {
if (!SUBTYPE_SUPPORTED) { if (!SUBTYPE_SUPPORTED) {
String[] languages = mLanguageSwitcherProxy.getEnabledLanguages(); String[] languages = mLanguageSwitcherProxy.getEnabledLanguages(
allowsImplicitlySelectedSubtypes);
List<InputMethodSubtypeCompatWrapper> subtypeList = List<InputMethodSubtypeCompatWrapper> subtypeList =
new ArrayList<InputMethodSubtypeCompatWrapper>(); new ArrayList<InputMethodSubtypeCompatWrapper>();
for (String lang: languages) { for (String lang: languages) {
@ -195,10 +196,6 @@ public class InputMethodManagerCompatWrapper {
public void setInputMethodAndSubtype( public void setInputMethodAndSubtype(
IBinder token, String id, InputMethodSubtypeCompatWrapper subtype) { IBinder token, String id, InputMethodSubtypeCompatWrapper subtype) {
if (!SUBTYPE_SUPPORTED) {
mLanguageSwitcherProxy.setLocale(subtype.getLocale());
return;
}
CompatUtils.invoke(mImm, null, METHOD_setInputMethodAndSubtype, CompatUtils.invoke(mImm, null, METHOD_setInputMethodAndSubtype,
token, id, subtype.getOriginalObject()); token, id, subtype.getOriginalObject());
} }

View file

@ -16,6 +16,7 @@
package com.android.inputmethod.compat; package com.android.inputmethod.compat;
import com.android.inputmethod.deprecated.LanguageSwitcherProxy;
import com.android.inputmethod.latin.SubtypeSwitcher; import com.android.inputmethod.latin.SubtypeSwitcher;
import android.inputmethodservice.InputMethodService; import android.inputmethodservice.InputMethodService;
@ -51,6 +52,9 @@ public class InputMethodServiceCompatWrapper extends InputMethodService {
subtype = mImm.getCurrentInputMethodSubtype(); subtype = mImm.getCurrentInputMethodSubtype();
} }
if (subtype != null) { if (subtype != null) {
if (!InputMethodManagerCompatWrapper.SUBTYPE_SUPPORTED) {
LanguageSwitcherProxy.getInstance().setLocale(subtype.getLocale());
}
SubtypeSwitcher.getInstance().updateSubtype(subtype); SubtypeSwitcher.getInstance().updateSubtype(subtype);
} }
} }

View file

@ -19,6 +19,7 @@ package com.android.inputmethod.deprecated;
import com.android.inputmethod.compat.InputMethodManagerCompatWrapper; import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
import com.android.inputmethod.deprecated.languageswitcher.LanguageSwitcher; import com.android.inputmethod.deprecated.languageswitcher.LanguageSwitcher;
import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinIME;
import com.android.inputmethod.latin.Settings;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
@ -26,7 +27,7 @@ import android.content.res.Configuration;
import java.util.Locale; import java.util.Locale;
// This class is used only when the IME doesn't use method.xml for language switching. // This class is used only when the IME doesn't use method.xml for language switching.
public class LanguageSwitcherProxy { public class LanguageSwitcherProxy implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final LanguageSwitcherProxy sInstance = new LanguageSwitcherProxy(); private static final LanguageSwitcherProxy sInstance = new LanguageSwitcherProxy();
private LanguageSwitcher mLanguageSwitcher; private LanguageSwitcher mLanguageSwitcher;
private SharedPreferences mPrefs; private SharedPreferences mPrefs;
@ -42,6 +43,7 @@ public class LanguageSwitcherProxy {
sInstance.mLanguageSwitcher = new LanguageSwitcher(service); sInstance.mLanguageSwitcher = new LanguageSwitcher(service);
sInstance.mLanguageSwitcher.loadLocales(prefs, conf.locale); sInstance.mLanguageSwitcher.loadLocales(prefs, conf.locale);
sInstance.mPrefs = prefs; sInstance.mPrefs = prefs;
prefs.registerOnSharedPreferenceChangeListener(sInstance);
} }
public static void onConfigurationChanged(Configuration conf) { public static void onConfigurationChanged(Configuration conf) {
@ -58,8 +60,8 @@ public class LanguageSwitcherProxy {
return mLanguageSwitcher.getLocaleCount(); return mLanguageSwitcher.getLocaleCount();
} }
public String[] getEnabledLanguages() { public String[] getEnabledLanguages(boolean allowImplicitlySelectedLanguages) {
return mLanguageSwitcher.getEnabledLanguages(); return mLanguageSwitcher.getEnabledLanguages(allowImplicitlySelectedLanguages);
} }
public Locale getInputLocale() { public Locale getInputLocale() {
@ -70,4 +72,14 @@ public class LanguageSwitcherProxy {
mLanguageSwitcher.setLocale(localeStr); mLanguageSwitcher.setLocale(localeStr);
mLanguageSwitcher.persist(mPrefs); mLanguageSwitcher.persist(mPrefs);
} }
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
// PREF_SELECTED_LANGUAGES: enabled input subtypes
// PREF_INPUT_LANGUAGE: current input subtype
if (key.equals(Settings.PREF_SELECTED_LANGUAGES)
|| key.equals(Settings.PREF_INPUT_LANGUAGE)) {
mLanguageSwitcher.loadLocales(prefs, null);
}
}
} }

View file

@ -18,6 +18,7 @@ package com.android.inputmethod.deprecated.languageswitcher;
import com.android.inputmethod.compat.InputMethodSubtypeCompatWrapper; import com.android.inputmethod.compat.InputMethodSubtypeCompatWrapper;
import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinIME;
import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.Settings; import com.android.inputmethod.latin.Settings;
import com.android.inputmethod.latin.SharedPreferencesCompat; import com.android.inputmethod.latin.SharedPreferencesCompat;
@ -25,6 +26,7 @@ import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Locale; import java.util.Locale;
@ -34,12 +36,14 @@ import java.util.Locale;
* input language that the user has selected. * input language that the user has selected.
*/ */
public class LanguageSwitcher { public class LanguageSwitcher {
private static final String TAG = LanguageSwitcher.class.getSimpleName();
private static final String KEYBOARD_MODE = "keyboard"; private static final String KEYBOARD_MODE = "keyboard";
private static final String[] EMPTY_STIRNG_ARRAY = new String[0];
private final ArrayList<Locale> mLocales = new ArrayList<Locale>(); private final ArrayList<Locale> mLocales = new ArrayList<Locale>();
private final LatinIME mIme; private final LatinIME mIme;
private String[] mSelectedLanguageArray; private String[] mSelectedLanguageArray = EMPTY_STIRNG_ARRAY;
private String mSelectedLanguages; private String mSelectedLanguages;
private int mCurrentIndex = 0; private int mCurrentIndex = 0;
private String mDefaultInputLanguage; private String mDefaultInputLanguage;
@ -69,12 +73,16 @@ public class LanguageSwitcher {
* @return whether there was any change * @return whether there was any change
*/ */
public boolean loadLocales(SharedPreferences sp, Locale systemLocale) { public boolean loadLocales(SharedPreferences sp, Locale systemLocale) {
if (LatinImeLogger.sDBG) {
Log.d(TAG, "load locales");
}
if (systemLocale != null) { if (systemLocale != null) {
setSystemLocale(systemLocale); setSystemLocale(systemLocale);
} }
String selectedLanguages = sp.getString(Settings.PREF_SELECTED_LANGUAGES, null); String selectedLanguages = sp.getString(Settings.PREF_SELECTED_LANGUAGES, null);
String currentLanguage = sp.getString(Settings.PREF_INPUT_LANGUAGE, null); String currentLanguage = sp.getString(Settings.PREF_INPUT_LANGUAGE, null);
if (selectedLanguages == null || selectedLanguages.length() < 1) { if (TextUtils.isEmpty(selectedLanguages)) {
mSelectedLanguageArray = EMPTY_STIRNG_ARRAY;
loadDefaults(); loadDefaults();
if (mLocales.size() == 0) { if (mLocales.size() == 0) {
return false; return false;
@ -104,6 +112,9 @@ public class LanguageSwitcher {
} }
private void loadDefaults() { private void loadDefaults() {
if (LatinImeLogger.sDBG) {
Log.d(TAG, "load default locales:");
}
mDefaultInputLocale = mIme.getResources().getConfiguration().locale; mDefaultInputLocale = mIme.getResources().getConfiguration().locale;
String country = mDefaultInputLocale.getCountry(); String country = mDefaultInputLocale.getCountry();
mDefaultInputLanguage = mDefaultInputLocale.getLanguage() + mDefaultInputLanguage = mDefaultInputLocale.getLanguage() +
@ -132,7 +143,10 @@ public class LanguageSwitcher {
/** /**
* Returns the list of enabled language codes. * Returns the list of enabled language codes.
*/ */
public String[] getEnabledLanguages() { public String[] getEnabledLanguages(boolean allowImplicitlySelectedLanguages) {
if (mSelectedLanguageArray.length == 0 && allowImplicitlySelectedLanguages) {
return new String[] { mDefaultInputLanguage };
}
return mSelectedLanguageArray; return mSelectedLanguageArray;
} }
@ -218,9 +232,5 @@ public class LanguageSwitcher {
Editor editor = prefs.edit(); Editor editor = prefs.edit();
editor.putString(Settings.PREF_INPUT_LANGUAGE, getInputLanguage()); editor.putString(Settings.PREF_INPUT_LANGUAGE, getInputLanguage());
SharedPreferencesCompat.apply(editor); SharedPreferencesCompat.apply(editor);
// When the current language is changed, the event for this change should be handled
// internally as a subtype switching.
mIme.notifyOnCurrentInputMethodSubtypeChanged(new InputMethodSubtypeCompatWrapper(
0, 0, getInputLocale().toString(), KEYBOARD_MODE, ""));
} }
} }

View file

@ -375,10 +375,10 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
mPrefs = prefs; mPrefs = prefs;
LatinImeLogger.init(this, prefs); LatinImeLogger.init(this, prefs);
LanguageSwitcherProxy.init(this, prefs);
SubtypeSwitcher.init(this, prefs); SubtypeSwitcher.init(this, prefs);
KeyboardSwitcher.init(this, prefs); KeyboardSwitcher.init(this, prefs);
AccessibilityUtils.init(this, prefs); AccessibilityUtils.init(this, prefs);
LanguageSwitcherProxy.init(this, prefs);
super.onCreate(); super.onCreate();