[ML12] Add a getPrimaryLocale function and some refactoring

Bug: 11230254
Change-Id: Ic579b93702c24f47e6cdb0c9a4da6b11f1fa76c8
main
Jean Chalard 2014-10-02 15:08:16 +09:00
parent c91166de54
commit 1c0876dc49
2 changed files with 52 additions and 17 deletions

View File

@ -98,6 +98,30 @@ public class DictionaryFacilitator {
Arrays.copyOfRange(DICT_TYPES_ORDERED_TO_GET_SUGGESTIONS, 1 /* start */,
DICT_TYPES_ORDERED_TO_GET_SUGGESTIONS.length);
/**
* Returns whether this facilitator is exactly for this list of locales.
* @param locales the list of locales to test against
* @return true if this facilitator handles exactly this list of locales, false otherwise
*/
public boolean isForLocales(final Locale[] locales) {
if (locales.length != mDictionaryGroups.length) {
return false;
}
for (final Locale locale : locales) {
boolean found = false;
for (final DictionaryGroup group : mDictionaryGroups) {
if (locale.equals(group.mLocale)) {
found = true;
break;
}
}
if (!found) {
return false;
}
}
return true;
}
/**
* A group of dictionaries that work together for a single language.
*/
@ -199,6 +223,18 @@ public class DictionaryFacilitator {
return mDictionaryGroups[0].mLocale;
}
/**
* Returns the primary locale among all currently active locales. BE CAREFUL using this.
*
* DO NOT USE THIS just because it's convenient. Use it when it's correct, for example when
* choosing what dictionary to put a word in, or when changing the capitalization of a typed
* string.
* @return the primary active locale
*/
public Locale getPrimaryLocale() {
return mDictionaryGroups[0].mLocale;
}
private static ExpandableBinaryDictionary getSubDict(final String dictType,
final Context context, final Locale locale, final File dictFile,
final String dictNamePrefix) {

View File

@ -89,7 +89,6 @@ import com.android.inputmethod.latin.utils.CapsModeUtils;
import com.android.inputmethod.latin.utils.CoordinateUtils;
import com.android.inputmethod.latin.utils.CursorAnchorInfoUtils;
import com.android.inputmethod.latin.utils.DialogUtils;
import com.android.inputmethod.latin.utils.DistracterFilterCheckingExactMatchesAndSuggestions;
import com.android.inputmethod.latin.utils.ImportantNoticeUtils;
import com.android.inputmethod.latin.utils.IntentUtils;
import com.android.inputmethod.latin.utils.JniUtils;
@ -253,7 +252,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// We need to re-evaluate the currently composing word in case the script has
// changed.
postWaitForDictionaryLoad();
latinIme.resetSuggest();
latinIme.resetDictionaryFacilitatorIfNecessary();
break;
case MSG_UPDATE_TAIL_BATCH_INPUT_COMPLETED:
latinIme.mInputLogic.onUpdateTailBatchInputCompleted(
@ -537,9 +536,10 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mHandler.onCreate();
DEBUG = DebugFlags.DEBUG_ENABLED;
// TODO: Resolve mutual dependencies of {@link #loadSettings()} and {@link #initSuggest()}.
// TODO: Resolve mutual dependencies of {@link #loadSettings()} and
// {@link #resetDictionaryFacilitatorIfNecessary()}.
loadSettings();
resetSuggest();
resetDictionaryFacilitatorIfNecessary();
// Register to receive ringer mode change and network state change.
// Also receive installation and removal of a dictionary pack.
@ -580,7 +580,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// been displayed. Opening dictionaries never affects responsivity as dictionaries are
// asynchronously loaded.
if (!mHandler.hasPendingReopenDictionaries()) {
resetSuggestForLocale(locale);
resetDictionaryFacilitatorForLocale(locale);
}
mDictionaryFacilitator.updateEnabledSubtypes(mRichImm.getMyEnabledInputMethodSubtypeList(
true /* allowsImplicitlySelectedSubtypes */));
@ -621,8 +621,11 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
}
}
private void resetSuggest() {
private void resetDictionaryFacilitatorIfNecessary() {
final Locale switcherSubtypeLocale = mSubtypeSwitcher.getCurrentSubtypeLocale();
if (mDictionaryFacilitator.isForLocales(new Locale[] { switcherSubtypeLocale })) {
return;
}
final String switcherLocaleStr = switcherSubtypeLocale.toString();
final Locale subtypeLocale;
if (TextUtils.isEmpty(switcherLocaleStr)) {
@ -637,15 +640,16 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
} else {
subtypeLocale = switcherSubtypeLocale;
}
resetSuggestForLocale(subtypeLocale);
resetDictionaryFacilitatorForLocale(subtypeLocale);
}
/**
* Reset suggest by loading dictionaries for the locale and the current settings values.
* Reset the facilitator by loading dictionaries for the locale and the current settings values.
*
* @param locale the locale
*/
private void resetSuggestForLocale(final Locale locale) {
// TODO: make sure the current settings always have the right locale, and read from them
private void resetDictionaryFacilitatorForLocale(final Locale locale) {
final SettingsValues settingsValues = mSettings.getCurrent();
mDictionaryFacilitator.resetDictionaries(this /* context */, locale,
settingsValues.mUseContactsDict, settingsValues.mUsePersonalizedDicts,
@ -901,12 +905,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mInputLogic.startInput(mSubtypeSwitcher.getCombiningRulesExtraValueOfCurrentSubtype(),
currentSettingsValues);
// Note: the following does a round-trip IPC on the main thread: be careful
final Locale currentLocale = mSubtypeSwitcher.getCurrentSubtypeLocale();
if (null != currentLocale && !currentLocale.equals(suggest.getLocale())) {
// TODO: Do this automatically.
resetSuggest();
}
resetDictionaryFacilitatorIfNecessary();
// TODO[IL]: Can the following be moved to InputLogic#startInput?
if (!mInputLogic.mConnection.resetCachesUponCursorMoveAndReturnSuccess(
@ -1554,7 +1553,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
}
final String wordToShow;
if (CapsModeUtils.isAutoCapsMode(mInputLogic.mLastComposedWord.mCapitalizedMode)) {
wordToShow = word.toLowerCase(mSubtypeSwitcher.getCurrentSubtypeLocale());
wordToShow = word.toLowerCase(mDictionaryFacilitator.getPrimaryLocale());
} else {
wordToShow = word;
}
@ -1840,7 +1839,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
public void dumpDictionaryForDebug(final String dictName) {
if (mDictionaryFacilitator.getLocale() == null) {
resetSuggest();
resetDictionaryFacilitatorIfNecessary();
}
mDictionaryFacilitator.dumpDictionaryForDebug(dictName);
}