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);
相关推荐
Kapaseker3 小时前
你不看会后悔的2025年终总结
android·kotlin
alexhilton6 小时前
务实的模块化:连接模块(wiring modules)的妙用
android·kotlin·android jetpack
ji_shuke7 小时前
opencv-mobile 和 ncnn-android 环境配置
android·前端·javascript·人工智能·opencv
sunnyday04269 小时前
Spring Boot 项目中使用 Dynamic Datasource 实现多数据源管理
android·spring boot·后端
幽络源小助理10 小时前
下载安装AndroidStudio配置Gradle运行第一个kotlin程序
android·开发语言·kotlin
inBuilder低代码平台10 小时前
浅谈安卓Webview从初级到高级应用
android·java·webview
豌豆学姐10 小时前
Sora2 短剧视频创作中如何保持人物一致性?角色创建接口教程
android·java·aigc·php·音视频·uniapp
白熊小北极10 小时前
Android Jetpack Compose折叠屏感知与适配
android
HelloBan11 小时前
setHintTextColor不生效
android
洞窝技术13 小时前
从0到30+:智能家居配网协议融合的实战与思考
android