接入物理键盘时, 默认弹出软件键盘

来源:互联网 发布:数据分析师考试大纲 编辑:程序博客网 时间:2024/06/11 00:31

在接入物理键盘后, 默认系统会在输入时, 不弹出软键盘.

用户可以在设置中去修改是否弹出, 一般是在 设置 > 语言和输入法 > 当前输入法 > 弹出的输入框中打开开关.


流程:

1. packages/apps/Settings/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java

    @Override    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {        // Input Method stuff        if (Utils.isMonkeyRunning()) {            return false;        }        if (preference instanceof PreferenceScreen) {            if (preference.getFragment() != null) {                // Fragment will be handled correctly by the super class.            } else if (KEY_CURRENT_INPUT_METHOD.equals(preference.getKey())) {                final InputMethodManager imm = (InputMethodManager)                        getSystemService(Context.INPUT_METHOD_SERVICE);                imm.showInputMethodPicker();//打开对话框.            }        } else if (preference instanceof SwitchPreference) {            final SwitchPreference pref = (SwitchPreference) preference;            if (pref == mGameControllerCategory.findPreference("vibrate_input_devices")) {                System.putInt(getContentResolver(), Settings.System.VIBRATE_INPUT_DEVICES,                        pref.isChecked() ? 1 : 0);                return true;            }        }        return super.onPreferenceTreeClick(preferenceScreen, preference);    }

framework/base/core/java/android/view/inputmethod/InputMethodManager.java

    public void showInputMethodPicker() {        synchronized (mH) {            showInputMethodPickerLocked();        }    }    private void showInputMethodPickerLocked() {        try {            mService.showInputMethodPickerFromClient(mClient);//从Service中打开        } catch (RemoteException e) {            Log.w(TAG, "IME died: " + mCurId, e);        }    }

frameworks/base/service/core/java/com/android/server/InputMethodManagerService.java

    @Override    public void showInputMethodPickerFromClient(IInputMethodClient client) {        if (!calledFromValidUser()) {            return;        }        synchronized (mMethodMap) {            if (mCurClient == null || client == null                    || mCurClient.client.asBinder() != client.asBinder()) {                Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "                        + Binder.getCallingUid() + ": " + client);            }            // Always call subtype picker, because subtype picker is a superset of input method            // picker.            mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);        }    }

    private void showInputMethodMenuInternal(boolean showSubtypes) {        if (DEBUG) Slog.v(TAG, "Show switching menu");        final Context context = mContext;        final boolean isScreenLocked = isScreenLocked();        final String lastInputMethodId = mSettings.getSelectedInputMethod();        int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);        if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);        synchronized (mMethodMap) {            final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =                    mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(                            mContext);            if (immis == null || immis.size() == 0) {                return;            }            hideInputMethodMenuLocked();            final List<ImeSubtypeListItem> imList =                    mSwitchingController.getSortedInputMethodAndSubtypeListLocked(                            showSubtypes, mInputShown, isScreenLocked);            if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {                final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();                if (currentSubtype != null) {                    final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);                    lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(                            currentImi, currentSubtype.hashCode());                }            }            final int N = imList.size();            mIms = new InputMethodInfo[N];            mSubtypeIds = new int[N];            int checkedItem = 0;            for (int i = 0; i < N; ++i) {                final ImeSubtypeListItem item = imList.get(i);                mIms[i] = item.mImi;                mSubtypeIds[i] = item.mSubtypeId;                if (mIms[i].getId().equals(lastInputMethodId)) {                    int subtypeId = mSubtypeIds[i];                    if ((subtypeId == NOT_A_SUBTYPE_ID)                            || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)                            || (subtypeId == lastInputMethodSubtypeId)) {                        checkedItem = i;                    }                }            }            final Context settingsContext = new ContextThemeWrapper(context,                    com.android.internal.R.style.Theme_DeviceDefault_Settings);            mDialogBuilder = new AlertDialog.Builder(settingsContext);            mDialogBuilder.setOnCancelListener(new OnCancelListener() {                @Override                public void onCancel(DialogInterface dialog) {                    hideInputMethodMenu();                }            });            final Context dialogContext = mDialogBuilder.getContext();            final TypedArray a = dialogContext.obtainStyledAttributes(null,                    com.android.internal.R.styleable.DialogPreference,                    com.android.internal.R.attr.alertDialogStyle, 0);            final Drawable dialogIcon = a.getDrawable(                    com.android.internal.R.styleable.DialogPreference_dialogIcon);            a.recycle();            mDialogBuilder.setIcon(dialogIcon);            final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(                    Context.LAYOUT_INFLATER_SERVICE);            final View tv = inflater.inflate(                    com.android.internal.R.layout.input_method_switch_dialog_title, null);            mDialogBuilder.setCustomTitle(tv);            // Setup layout for a toggle switch of the hardware keyboard            mSwitchingDialogTitleView = tv;            mSwitchingDialogTitleView                    .findViewById(com.android.internal.R.id.hard_keyboard_section)                    .setVisibility(mWindowManagerService.isHardKeyboardAvailable()                            ? View.VISIBLE : View.GONE);            final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(                    com.android.internal.R.id.hard_keyboard_switch);            hardKeySwitch.setChecked(mShowImeWithHardKeyboard);            hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {                @Override                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                    mSettings.setShowImeWithHardKeyboard(isChecked);//-------重点看这里                    // Ensure that the input method dialog is dismissed when changing                    // the hardware keyboard state.                    hideInputMethodMenu();                }            });            final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,                    com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);            final OnClickListener choiceListener = new OnClickListener() {                @Override                public void onClick(final DialogInterface dialog, final int which) {                    synchronized (mMethodMap) {                        if (mIms == null || mIms.length <= which || mSubtypeIds == null                                || mSubtypeIds.length <= which) {                            return;                        }                        final InputMethodInfo im = mIms[which];                        int subtypeId = mSubtypeIds[which];                        adapter.mCheckedItem = which;                        adapter.notifyDataSetChanged();                        hideInputMethodMenu();                        if (im != null) {                            if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {                                subtypeId = NOT_A_SUBTYPE_ID;                            }                            setInputMethodLocked(im.getId(), subtypeId);                        }                    }                }            };            mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);            if (showSubtypes && !isScreenLocked) {                final OnClickListener positiveListener = new OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int whichButton) {                        showConfigureInputMethods();                    }                };                mDialogBuilder.setPositiveButton(                        com.android.internal.R.string.configure_input_methods, positiveListener);            }            mSwitchingDialog = mDialogBuilder.create();            mSwitchingDialog.setCanceledOnTouchOutside(true);            mSwitchingDialog.getWindow().setType(                    WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);            mSwitchingDialog.getWindow().getAttributes().privateFlags |=                    WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;            mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");            updateImeWindowStatusLocked();            mSwitchingDialog.show();        }    }

frameworks/base/core/java/com/android/internal/inputmethod/InputMethodUtils.java

        public boolean isShowImeWithHardKeyboardEnabled() {                return Settings.Secure.getIntForUser(mResolver,                        Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD, 0, mCurrentUserId) == 1;        }        public void setShowImeWithHardKeyboard(boolean show) {            Settings.Secure.putIntForUser(mResolver, Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD,                    show ? 1 : 0, mCurrentUserId);        }

默认值在系统中并未设置, 那么, 只需要为其设置默认值即可.

diff --git a/frameworks/base/core/res/res/values/config.xml b/frameworks/base/core/res/res/values/config.xmlindex 378c336..e888acd 100755--- a/frameworks/base/core/res/res/values/config.xml+++ b/frameworks/base/core/res/res/values/config.xml@@ -1406,6 +1406,9 @@          will prevent the system from entering a low power state until the dream ends. -->     <bool name="config_dreamsSupported">true</bool> +       <!-- set def show ime when hard keyboard in -->+       <bool name="def_ime_with_hard_keyboard">true</bool>+     <!-- If supported, are dreams enabled? (by default) -->     <bool name="config_dreamsEnabledByDefault">true</bool>     <!-- If supported and enabled, are dreams activated when docked? (by default) -->diff --git a/frameworks/base/core/res/res/values/symbols.xml b/frameworks/base/core/res/res/values/symbols.xmlindex 664f3ce..41d6e92 100755--- a/frameworks/base/core/res/res/values/symbols.xml+++ b/frameworks/base/core/res/res/values/symbols.xml@@ -1773,6 +1773,7 @@   <java-symbol type="style" name="Theme.Toast" />   <java-symbol type="xml" name="storage_list" />   <java-symbol type="xml" name="storage_list_box" />+  <java-symbol type="bool" name="def_ime_with_hard_keyboard" />   <java-symbol type="bool" name="config_dreamsSupported" />   <java-symbol type="bool" name="config_dreamsEnabledByDefault" />   <java-symbol type="bool" name="config_dreamsEnabledOnBattery" />diff --git a/frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java b/frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.javaindex 168ea70..a141328 100644--- a/frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java+++ b/frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java@@ -2497,6 +2497,10 @@ public class DatabaseHelper extends SQLiteOpenHelper {             loadBooleanSetting(stmt, Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,                     R.bool.def_lock_screen_allow_private_notifications); +                       //AnsonCode 2017.5.8 make ime show when hard keyboard in.+                       loadBooleanSetting(stmt, Secure.SHOW_IME_WITH_HARD_KEYBOARD,+                    com.android.internal.R.bool.def_ime_with_hard_keyboard);+             loadIntegerSetting(stmt, Settings.Secure.SLEEP_TIMEOUT,                     R.integer.def_sleep_timeout);         } finally {

修改完后, 编译:

mmm frameworks/base/core/res  编译默认值

mmm frameworks/base 字段暴露, 否则SettingsProvider会找不到

def_ime_with_hard_keyboard

mmm frameworks/base/packages/SettingsProvider


---- END ---

0 0