您的位置:首页 > 其它

LatinIME切换语言

2015-07-27 12:32 351 查看
一、当切换语言后,将首先调用LatinIME中的onCurrentInputMethodSubtypeChanged方法,

<span style="font-size:18px;">    @Override
public void onCurrentInputMethodSubtypeChanged(final InputMethodSubtype subtype) {
LogUtils.shared().dLog(TAG, "LatinIME onCurrentInputMethodSubtypeChanged");
// Note that the calling sequence of onCreate() and onCurrentInputMethodSubtypeChanged()
// is not guaranteed. It may even be called at the same time on a different thread.
mSubtypeSwitcher.onSubtypeChanged(subtype);
mInputLogic.onSubtypeChanged(SubtypeLocaleUtils.getCombiningRulesExtraValue(subtype),
mSettings.getCurrent());
loadKeyboard();
}</span>


该方法中,会调用SubtypeSwitcher的onSubtypeChanged方法,而其中又会通过SubtypeLocaleUtils的getSubtypeLocale方法构建出新的Locale方法(Locale方法是java中用来处理一些语言环境敏感的操作的类),接着与系统的语言环境进行对比,并将结果通过LanguageOnSpacebarHelper的updateIsSystemLanguageSameAsInputLanguage方法更新到LanguageOnSpacebarHelper类中,最后,调用updateShortcutIME来更新快捷输入法

<span style="font-size:18px;">    // Update the current subtype. LatinIME.onCurrentInputMethodSubtypeChanged calls this function.
public void onSubtypeChanged(final InputMethodSubtype newSubtype) {
if (DBG) {
Log.w(TAG, "onSubtypeChanged: "
+ SubtypeLocaleUtils.getSubtypeNameForLogging(newSubtype));
}
LogUtils.shared().dLog(TAG, "onSubtypeChanged : " + newSubtype);
final Locale newLocale = SubtypeLocaleUtils.getSubtypeLocale(newSubtype);
final Locale systemLocale = mResources.getConfiguration().locale;
final boolean sameLocale = systemLocale.equals(newLocale);
final boolean sameLanguage = systemLocale.getLanguage().equals(newLocale.getLanguage());
final boolean implicitlyEnabled =
mRichImm.checkIfSubtypeBelongsToThisImeAndImplicitlyEnabled(newSubtype);
mLanguageOnSpacebarHelper.updateIsSystemLanguageSameAsInputLanguage(
sameLocale || (sameLanguage && implicitlyEnabled));

updateShortcutIME();
}</span>


updateShortcutIME方法中

<span style="font-size:18px;">private void updateShortcutIME() {
if (DBG) {
Log.d(TAG, "Update shortcut IME from : "
+ (mShortcutInputMethodInfo == null
? "<null>" : mShortcutInputMethodInfo.getId()) + ", "
+ (mShortcutSubtype == null ? "<null>" : (
mShortcutSubtype.getLocale() + ", " + mShortcutSubtype.getMode())));
}
// TODO: Update an icon for shortcut IME
final Map<InputMethodInfo, List<InputMethodSubtype>> shortcuts =
mRichImm.getInputMethodManager().getShortcutInputMethodsAndSubtypes();
mShortcutInputMethodInfo = null;
mShortcutSubtype = null;
for (final InputMethodInfo imi : shortcuts.keySet()) {
final List<InputMethodSubtype> subtypes = shortcuts.get(imi);
// TODO: Returns the first found IMI for now. Should handle all shortcuts as
// appropriate.
mShortcutInputMethodInfo = imi;
// TODO: Pick up the first found subtype for now. Should handle all subtypes
// as appropriate.
mShortcutSubtype = subtypes.size() > 0 ? subtypes.get(0) : null;
break;
}
if (DBG) {
Log.d(TAG, "Update shortcut IME to : "
+ (mShortcutInputMethodInfo == null
? "<null>" : mShortcutInputMethodInfo.getId()) + ", "
+ (mShortcutSubtype == null ? "<null>" : (
mShortcutSubtype.getLocale() + ", " + mShortcutSubtype.getMode())));
}
}</span>
二、上面处理完毕后,onCurrentInputMethodSubtypeChanged方法将调用InputLogic的onSubtypeChanged方法,来重新启动输入

/**
* Call this when the subtype changes.
* @param combiningSpec the spec string for the combining rules
* @param settingsValues the current settings values
*/
public void onSubtypeChanged(final String combiningSpec, final SettingsValues settingsValues) {
LogUtils.shared().dLog(TAG, "onSubtypeChanged : combiningSpec : " + combiningSpec);
finishInput();
startInput(combiningSpec, settingsValues);
}
三、接下来,onCurrentInputMethodSubtypeChanged方法调用loadKeyboard方法,在该方法中,首先会通过mHander.postReopenDictionaries()来加载词典(为了重新确定联想词),接着通过loadSettings方法加载更新设置,最后通过KeyboardSwitcher的loadKeyboard方法来重新加载键盘

// TODO: Make this private
// Outside LatinIME, only used by the {@link InputTestsBase} test suite.
@UsedForTesting
void loadKeyboard() {
// Since we are switching languages, the most urgent thing is to let the keyboard graphics
// update. LoadKeyboard does that, but we need to wait for buffer flip for it to be on
// the screen. Anything we do right now will delay this, so wait until the next frame
// before we do the rest, like reopening dictionaries and updating suggestions. So we
// post a message.
LogUtils.shared().dLog("dingc", "LatinIME loadKeyboard");
mHandler.postReopenDictionaries();
loadSettings();
if (mKeyboardSwitcher.getMainKeyboardView() != null) {
// Reload keyboard because the current language has been changed.
mKeyboardSwitcher.loadKeyboard(getCurrentInputEditorInfo(), mSettings.getCurrent(),
getCurrentAutoCapsState(), getCurrentRecapitalizeState());
}
}
语言切换的大致步骤就是这些,其中具体的逻辑会在相关的内容中进行分析
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: