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);
相关推荐
mmsx2 分钟前
Android 并发考古:newThread 与 RxJava 共存,以及 GlobalScope 的三宗罪
android·echarts·rxjava
骑着蜗牛撵大象3275 分钟前
大模型 SDK 编译就崩?用 CMakeLists 把 httplib、JSON 与 OpenSSL 装配成可复用的 ChatServer
android·java·c++·http·json·cmake·大模型sdk
海水冷却9 小时前
Android 群组聊天怎么实现:基于 ZIM 的完整落地指南(附可运行 Demo)
android
三少爷的鞋12 小时前
现代 Android 架构指南:不要再主动设计 Callback 接口
android
TimeFine18 小时前
AI 编码工作流的设计
android·ai编程
骑着蜗牛撵大象32718 小时前
C++20 协程 × 屏障:手写一个 co_await 版 Barrier,让等待不再钉死线程
android·c++20·协程·并发编程·co_await·屏障同步
山东孙燕姿20 小时前
设备台账重构:从一张表到三层模型
android·java·网络·低代码·重构·rxjava
蒸鱼Yuzheng21 小时前
移动端 CPU 调度分析:线程亲和性、大小核迁移与卡顿
android·perfetto·性能分析·线程优化·cpu调度
booboosui21 小时前
安卓唤醒词:12 维 MFCC 加 DTW,阈值 1.8
android
传奇开心果21 小时前
Compose Multiplatform 共享边界实战:架构契约、互操作同步与可回退设计
android·kotlin