Android 实体键盘 设置默认布局

外界实体物理键盘,需要选择键盘布局,不然会对应输入不正确,某些平台是这样的。

实体键盘在插入设备中后会自动设置一个布局,但是可能不是我们想要的。

修改两个功能:

根据系统语言设置默认的键盘。

默认添加两个键盘布局到'选择键盘布局' 的dialog界面

键盘的布局文件apk位置:

frameworks/base/packages/InputDevices/res/raw/

frameworks/base/packages/InputDevices/res/xml/keyboard_layouts.xml

追查设置中的布局找到这里,选择键盘布局的Dialog,packages/apps/Settings/src/com/android/settings/inputmethod/KeyboardLayoutDialogFragment.java

点击设置键盘布局,会走到 packages/apps/Settings/src/com/android/settings/inputmethod/KeyboardLayoutPickerController.java

查看到 addKeyboardLayoutForInputDevice 设置键盘布局,所以利用这里我们添加。

java 复制代码
    @Override
    public boolean handlePreferenceTreeClick(Preference preference) {
        if(DEBUG)Log.d("testkeyboard","----handlePreferenceTreeClick---");
        if (!(preference instanceof SwitchPreference)) {
            return false;
        }

        final SwitchPreference switchPref = (SwitchPreference) preference;
        final KeyboardLayout layout = mPreferenceMap.get(switchPref);
        if (layout != null) {
            final boolean checked = switchPref.isChecked();
            if (checked) {
                mIm.addKeyboardLayoutForInputDevice(mInputDeviceIdentifier,
                        layout.getDescriptor());
                if(DEBUG)Log.d("testkeyboard","layout.getDescriptor() = " + layout.getDescriptor());
            } else {
                mIm.removeKeyboardLayoutForInputDevice(mInputDeviceIdentifier,
                        layout.getDescriptor());
            }
        }
        return true;
    }

主要修改 frameworks/base/services/core/java/com/android/server/input/InputManagerService.java

addKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,String keyboardLayoutDescriptor)

如下修改:

diff 复制代码
diff --git a/frameworks/base/services/core/java/com/android/server/input/InputManagerService.java b/frameworks/base/services/core/java/com/android/server/input/InputManagerService.java
index 5fc3712d221..1bc1ee6cef7 100755
--- a/frameworks/base/services/core/java/com/android/server/input/InputManagerService.java
+++ b/frameworks/base/services/core/java/com/android/server/input/InputManagerService.java
@@ -1101,6 +1101,7 @@ public class InputManagerService extends IInputManager.Stub
   String layout =getCurrentKeyboardLayoutForInputDevice(inputDevice.getIdentifier());
                 if (layout == null) {
+                    if(DEBUG) Slog.d("testkeyboard", "---goto--getDefaultKeyboardLayout");
                     layout = getDefaultKeyboardLayout(inputDevice);
                     if (layout != null) {
                         setCurrentKeyboardLayoutForInputDevice(
@@ -1134,6 +1135,20 @@ public class InputManagerService extends IInputManager.Stub
         final Locale systemLocale = mContext.getResources().getConfiguration().locale;
         // If our locale doesn't have a language for some reason, then we don't really have a
         // reasonable default.
+
+        if(DEBUG) Slog.d("testkeyboard", "---getDefaultKeyboardLayout---getLanguage="+systemLocale.getLanguage());
+        String russuan = "com.android.inputdevices/com.android.inputdevices.InputDeviceReceiver/keyboard_layout_russian";
+        String english_us = "com.android.inputdevices/com.android.inputdevices.InputDeviceReceiver/keyboard_layout_english_us";
         //根据语言设置当前键盘
+        if(systemLocale.getLanguage().equals("en")){
+            return english_us;
+        }
+        if(systemLocale.getLanguage().equals("ru")){
+            return russuan;
+        }
+        //这里就是添加的两个键盘
+        addKeyboardLayoutForInputDevice(d.getIdentifier(),russuan);
+        addKeyboardLayoutForInputDevice(d.getIdentifier(),english_us);
+
         if (TextUtils.isEmpty(systemLocale.getLanguage())) {
             return null;
         }
@@ -1150,6 +1165,7 @@ public class InputManagerService extends IInputManager.Stub
             for (int localeIndex = 0; localeIndex < numLocales; ++localeIndex) {
                 if (isCompatibleLocale(systemLocale, locales.get(localeIndex))) {
                     layouts.add(layout);
+                    if(DEBUG) Slog.d("testkeyboard", "---getDefaultKeyboardLayout---for--getLanguage="+systemLocale.getLanguage());
                     break;
                 }
             }
@@ -1386,10 +1402,12 @@ public class InputManagerService extends IInputManager.Stub
     @Override // Binder call
     public KeyboardLayout[] getKeyboardLayoutsForInputDevice(
             final InputDeviceIdentifier identifier) {
+        if(DEBUG) Slog.d("testkeyboard", "---getKeyboardLayoutsForInputDevice---");
         final String[] enabledLayoutDescriptors =
                 getEnabledKeyboardLayoutsForInputDevice(identifier);
         final ArrayList<KeyboardLayout> enabledLayouts =
                 new ArrayList<>(enabledLayoutDescriptors.length);
+        if(DEBUG) Slog.d("testkeyboard", "---ArrayList enabledLayouts---");
         final ArrayList<KeyboardLayout> potentialLayouts = new ArrayList<>();
         visitAllKeyboardLayouts(new KeyboardLayoutVisitor() {
             boolean mHasSeenDeviceSpecificLayout;
@@ -1402,6 +1420,7 @@ public class InputManagerService extends IInputManager.Stub
                 for (String s : enabledLayoutDescriptors) {
                     if (s != null && s.equals(layout.getDescriptor())) {
                         enabledLayouts.add(layout);
+                        if(DEBUG) Slog.d("testkeyboard", "---ArrayList enabledLayouts.add---");
                         return;
                     }
                 }
@@ -1436,8 +1455,10 @@ public class InputManagerService extends IInputManager.Stub
     public KeyboardLayout getKeyboardLayout(String keyboardLayoutDescriptor) {
         Objects.requireNonNull(keyboardLayoutDescriptor,
                 "keyboardLayoutDescriptor must not be null");
+        if(DEBUG) Slog.d("testkeyboard", "---getKeyboardLayout---keyboardLayoutDescriptor="+keyboardLayoutDescriptor);
 
         final KeyboardLayout[] result = new KeyboardLayout[1];
+        if(result!=null) Slog.d("testkeyboard", "---getKeyboardLayout---result.length="+result.length);
         visitKeyboardLayout(keyboardLayoutDescriptor,
                 (resources, keyboardLayoutResId, layout) -> result[0] = layout);
         if (result[0] == null) {
@@ -1632,12 +1653,15 @@ public class InputManagerService extends IInputManager.Stub
     @Override // Binder call
     public String[] getEnabledKeyboardLayoutsForInputDevice(InputDeviceIdentifier identifier) {
         String key = getLayoutDescriptor(identifier);
+        if(DEBUG) Slog.d("testkeyboard", "---getEnabledKeyboardLayoutsForInputDevice---key="+key);
         synchronized (mDataStore) {
             String[] layouts = mDataStore.getKeyboardLayouts(key);
+            if(DEBUG) Slog.d("testkeyboard", "---getEnabledKeyboardLayoutsForInputDevice---getDescriptor="+identifier.getDescriptor());
             if ((layouts == null || layouts.length == 0)
                     && !key.equals(identifier.getDescriptor())) {
                 layouts = mDataStore.getKeyboardLayouts(identifier.getDescriptor());
             }
+            if(DEBUG) Slog.d("testkeyboard", "---getEnabledKeyboardLayoutsForInputDevice---length="+layouts.length);
             return layouts;
         }
     }
@@ -1645,6 +1669,7 @@ public class InputManagerService extends IInputManager.Stub
     @Override // Binder call
     public void addKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,
             String keyboardLayoutDescriptor) {
+        if(DEBUG) Slog.d("testkeyboard", "---addKeyboardLayoutForInputDevice---keyboardLayoutDescriptor="+keyboardLayoutDescriptor);
         if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
                 "addKeyboardLayoutForInputDevice()")) {
             throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
@@ -1653,6 +1678,7 @@ public class InputManagerService extends IInputManager.Stub
                 "keyboardLayoutDescriptor must not be null");
 
         String key = getLayoutDescriptor(identifier);
+        if(DEBUG) Slog.d("testkeyboard", "---addKeyboardLayoutForInputDevice---key="+key);
         synchronized (mDataStore) {
             try {
                 String oldLayout = mDataStore.getCurrentKeyboardLayout(key);
相关推荐
喜欢踢足球的老罗8 小时前
自动化模型管理:MediaPipe Android SDK 中的模型文件下载与加载机制
android·运维·自动化
AgilityBaby10 小时前
Untiy打包安卓踩坑
android·笔记·学习·unity·游戏引擎
硬件学长森哥11 小时前
Android音视频多媒体开源框架基础大全
android·图像处理·音视频
二流小码农12 小时前
鸿蒙开发:CodeGenie万能卡片生成
android·ios·harmonyos
没有了遇见12 小时前
Android 直播间动画动画队列实现
android
月山知了12 小时前
Android有的命令不需要root权限,有的命令需要root权限是如何实现的
android
SHUIPING_YANG13 小时前
tp3.1临时连接指定数据库,切片分类in查询,带过滤需要的数据
android·数据库
前端呆猿14 小时前
Vuex:Vue.js 应用程序的状态管理模式
android·vue.js·flutter
望佑14 小时前
Jetpack Compose 入门:从默认工程到实战开发
android