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);
相关推荐
每次的天空1 小时前
Android-自定义View的实战学习总结
android·学习·kotlin·音视频
恋猫de小郭1 小时前
Flutter Widget Preview 功能已合并到 master,提前在体验毛坯的预览支持
android·flutter·ios
断剑重铸之日2 小时前
Android自定义相机开发(类似OCR扫描相机)
android
随心最为安2 小时前
Android Library Maven 发布完整流程指南
android
岁月玲珑3 小时前
【使用Android Studio调试手机app时候手机老掉线问题】
android·ide·android studio
还鮟7 小时前
CTF Web的数组巧用
android
小蜜蜂嗡嗡8 小时前
Android Studio flutter项目运行、打包时间太长
android·flutter·android studio
aqi008 小时前
FFmpeg开发笔记(七十一)使用国产的QPlayer2实现双播放器观看视频
android·ffmpeg·音视频·流媒体
zhangphil10 小时前
Android理解onTrimMemory中ComponentCallbacks2的内存警戒水位线值
android
你过来啊你10 小时前
Android View的绘制原理详解
android