LatinIME切换语言

一、当切换语言后,将首先调用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());
        }
    }
语言切换的大致步骤就是这些,其中具体的逻辑会在相关的内容中进行分析


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值