LatinIME切换语言
2015-07-27 12:32
351 查看
一、当切换语言后,将首先调用LatinIME中的onCurrentInputMethodSubtypeChanged方法,
该方法中,会调用SubtypeSwitcher的onSubtypeChanged方法,而其中又会通过SubtypeLocaleUtils的getSubtypeLocale方法构建出新的Locale方法(Locale方法是java中用来处理一些语言环境敏感的操作的类),接着与系统的语言环境进行对比,并将结果通过LanguageOnSpacebarHelper的updateIsSystemLanguageSameAsInputLanguage方法更新到LanguageOnSpacebarHelper类中,最后,调用updateShortcutIME来更新快捷输入法
updateShortcutIME方法中
<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()); } }语言切换的大致步骤就是这些,其中具体的逻辑会在相关的内容中进行分析
相关文章推荐
- mysql优化第三天
- Gym 100187H - Mysterious Photos
- POJ 题目2823 Sliding Window(RMQ,固定区间长度)
- ultraiso命令行使用
- mysql-一列的值要显示两列
- android service startService与bindService的区别
- 实现两个long long 相乘取模的汇编代码
- 互斥与锁和条件变量组合
- windows sc命令注意点
- SVN在SAE上部署代码-随记
- Mybatis在oracle、mysql、db2、sql server的like模糊查询
- Linux下用SSH退出符切换SSH会话的实现方法
- Gym 100187E - Two Labyrinths
- error C2664: 'CWnd::MessageBoxW' : cannot convert parameter 1 from 'char *' to 'LPCTSTR'的解决方法
- 1!+2!+3!+...n!Java史上最简洁的阶乘算法!!!
- poj 2689 区间筛素数
- HDU 2955
- 从优秀到卓越――反思应该如何创业
- AlertDialog
- dubbo总结(三)——dubbo介绍和工程创建