Android custom button addition process

c6bbebd66e476a9e4c506e2b1b68ff8e.gif

Learn with you for lifeXi, this is Programmer Android

Recommended classic articles. By reading this article, you will gain the following knowledge points:

1. Driving buttons connected through GPIO
2. Adding key response methods in the Framework layer
3. References

1. Drive buttons connected through GPIO

This type of button is connected using GPIO, and the action of the button is judged by monitoring its interrupt. It is necessary to configure the gpio_keys node in the dts file corresponding to the project according to the specific hardware design.

The underlying driver mainly modifies the following two files to report key values.

1. Modify sp9863a-3c10.dts

The modification method is as follows:

bsp/kernel/kernel4.14/arch/arm64/boot/dts/sprd/sp9863a-3c10.dts
    gpio-keys {
        compatible = "gpio-keys";
        key-power {
            label = "Power Key";
            linux,code = <KEY_POWER>;
            gpios = <&pmic_eic 1 GPIO_ACTIVE_LOW>;
            debounce-interval = <2>;
            wakeup-source;
        };
+
+               key-smart {
+                       label = "Smart Key";
+                       linux,code = <KEY_OK>;/* linux下的key code,linux下input.h中有定义键值 */
+                       gpios = <&ap_gpio 53 GPIO_ACTIVE_LOW>;/* 按键连接的GPIO */
+                       debounce-interval = <2>;/* 按键去抖时间,单位ms,如果出现按键不稳定,请适当加大 */
+                       wakeup-source;
+               };
    };

2. Modify pinmap-sp9863a.c file

The modification method is as follows:

+++ /bsp/bootloader/u-boot15/board/spreadtrum/sp9863a_3c10/pinmap-sp9863a.c
@@ -348,8 +348,8 @@ static pinmap_t pinmap[]={
 {REG_MISC_PIN_LVDSRF0_DACON,            BITS_PIN_DS(1)|BIT_PIN_NULL|BIT_PIN_NUL|BIT_PIN_SLP_AP|BIT_PIN_SLP_NUL|BIT_PIN_SLP_OE},//LCM_SOURCE_AVEEEN
 {REG_PIN_SPI2_CSN,                      BITS_PIN_AF(2)},
 {REG_MISC_PIN_SPI2_CSN,                 BITS_PIN_DS(1)|BIT_PIN_NULL|BIT_PIN_WPU|BIT_PIN_SLP_CM4|BIT_PIN_SLP_WPU|BIT_PIN_SLP_IE},//PROX_INT
-{REG_PIN_SPI2_DO,                       BITS_PIN_AF(2)},
-{REG_MISC_PIN_SPI2_DO,                  BITS_PIN_DS(1)|BIT_PIN_NULL|BIT_PIN_NUL|BIT_PIN_SLP_CM4|BIT_PIN_SLP_NUL|BIT_PIN_SLP_OE},//M_RSTN
+{REG_PIN_SPI2_DO,                       BITS_PIN_AF(3)},
+{REG_MISC_PIN_SPI2_DO,                  BITS_PIN_DS(1)|BIT_PIN_NULL|BIT_PIN_WPU|BIT_PIN_SLP_AP|BIT_PIN_SLP_WPU|BIT_PIN_SLP_IE},//SMART_KEY
 {REG_PIN_SPI2_DI,                       BITS_PIN_AF(3)},
 {REG_MISC_PIN_SPI2_DI,                  BITS_PIN_DS(1)|BIT_PIN_NULL|BIT_PIN_WPD|BIT_PIN_SLP_AP|BIT_PIN_SLP_WPD|BIT_PIN_SLP_Z},//NC
 {REG_PIN_SPI2_CLK,                      BITS_PIN_AF(2)},

2. Add key response method to Framework layer

Check geteventwhether the key value adjusted by the driver is uploaded ok.

The command viewing method is as follows:

C:\Users\Administrator>adb shell getevent
 
/dev/input/event2: 0001 0160 00000001
/dev/input/event2: 0000 0000 00000000
/dev/input/event2: 0001 0160 00000000
/dev/input/event2: 0000 0000 00000000

Among them:
0160: is a hexadecimal number, corresponding to the decimal number 352.
1, 0: refers to the action of pressing and popping up.

1. Add custom key value in gpio-keys.kl file

In the kl file we can add the key value imitating the power key.

+++ b/Android/device/sprd/sharkl3/common/rootdir/system/usr/keylayout/gpio-keys.kl
@@ -3,3 +3,4 @@ key 115     VOLUME_UP       WAKE
 key 116     POWER           WAKE
 key 212     CAMERA          WAKE
 key 0x210   FOCUS           WAKE
+key 352     KEY_OK          WAKE

In this way, the mapping of the physical key kl file to KEY_OK can be completed.

This kl file is DeviceCommon.mkcompiled into the system through , and can be found in the /system/usr/keylayout directory of the mobile phone.
There may be differences in the code for different platforms.

device\sprd\sharkl3\common\DeviceCommon.mkPart of the code is as follows:

PRODUCT_COPY_FILES += \
    $(LOCAL_PATH)/rootdir/root/init.common.rc:$(TARGET_COPY_OUT_VENDOR)/etc/init/hw/init.common.rc \
    $(LOCAL_PATH)/recovery/init.recovery.common.rc:root/init.recovery.common.rc \
    $(BOARDDIR)/recovery/init.recovery.$(TARGET_BOARD).rc:root/init.recovery.$(TARGET_BOARD).rc \
    $(LOCAL_PATH)/rootdir/system/usr/keylayout/gpio-keys.kl:$(TARGET_COPY_OUT_VENDOR)/usr/keylayout/gpio-keys.kl \

2. Add key value in Generic.kl file

The modification method is as follows:

+++ b/Android/frameworks/base/data/keyboards/Generic.kl
@@ -299,7 +299,7 @@ key 317   BUTTON_THUMBL
 key 318   BUTTON_THUMBR

-# key 352 "KEY_OK"
+key 352   KEY_OK
 key 353   DPAD_CENTER
 # key 354 "KEY_GOTO"
 # key 355 "KEY_CLEAR"

3. Add the key value in the qwerty.kl file

The modification method is as follows:

+++ b/Android/frameworks/base/data/keyboards/qwerty.kl
@@ -129,3 +129,4 @@ key 581   STEM_PRIMARY
 key 582   STEM_1
 key 583   STEM_2
 key 584   STEM_3
+key 352   KEY_OK

4. Add keycode value and label to the Native layer

Note that the 289 keycode value below is a continuation of the 288 keycode value above. It is different from the 352 reported by the driver. It is the value of the bottom layer. It is best for the upper layer to be different from the bottom layer.

The modification method is as follows:

+++ b/Android/frameworks/native/include/android/keycodes.h
@@ -776,8 +776,11 @@ enum {
     AKEYCODE_THUMBS_DOWN = 287,
     /** Used to switch current account that is consuming content.
      * May be consumed by system to switch current viewer profile. */
-    AKEYCODE_PROFILE_SWITCH = 288
-
+    AKEYCODE_PROFILE_SWITCH = 288,
+    /**
+       *Nokia custom key 
+       **/
+    AKEYCODE_KEY_OK = 289,

Similarly, we follow the definition of 288 and add label definitions in InputEventLabels.h

+++ b/Android/frameworks/native/include/input/InputEventLabels.h
@@ -328,7 +328,7 @@ static const InputEventLabel KEYCODES[] = {
     DEFINE_KEYCODE(THUMBS_UP),
     DEFINE_KEYCODE(THUMBS_DOWN),
     DEFINE_KEYCODE(PROFILE_SWITCH),
+    DEFINE_KEYCODE(KEY_OK),

5. Add attribute values ​​in attrs.xml

The modification method is as follows:

+++ b/Android/frameworks/base/core/res/res/values/attrs.xml
@@ -1924,6 +1924,7 @@
         <enum name="KEYCODE_THUMBS_UP" value="286" />
         <enum name="KEYCODE_THUMBS_DOWN" value="287" />
         <enum name="KEYCODE_PROFILE_SWITCH" value="288" />
+               <enum name="KEYCODE_KEY_OK" value="289" />
     </attr>

6. Add the key value in KeyEvent to facilitate calling in PhoneWindowMangager

The modification method is as follows:

+++ b/Android/frameworks/base/core/java/android/view/KeyEvent.java
@@ -823,6 +823,11 @@ public class KeyEvent extends InputEvent implements Parcelable {
      * consuming content. May be consumed by system to set account globally.
      */
     public static final int KEYCODE_PROFILE_SWITCH = 288;
+        /**
+     * Integer value of the last KEYCODE. Nokia custom  ok key.
+     * @hide
+     */
+       public static final int KEYCODE_KEY_OK = 289;

7. Finally we handle the key press behavior in PhoneWindowManager

After tuning, we can interceptKeyBeforeDispatchingInnerdo what we want to do in the PWM method, such as lifting Google Assist, etc.

+++ b/Android/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

@@ -2870,7 +2870,15 @@ public class PhoneWindowManager extends AbsPhoneWindowManager implements WindowM
         if (mPendingCapsLockToggle && !KeyEvent.isMetaKey(keyCode) && !KeyEvent.isAltKey(keyCode)) {
             mPendingCapsLockToggle = false;
         }
-
+               // add for key ok
+               if(keyCode == KeyEvent.KEYCODE_KEY_OK){
+                       if("Nokia_India".equals(android.os.Build.CUSTOMER_SKU)){
+                         // launch Google Assist
+                         launchAssistAction(null, event.getDeviceId());
+                       }
+                       return -1;
+               }
+               // add for key ok
Follow Programmer Android and reply 按键添加to get the complete code.

references:

[Tencent Documentation] Android Framework Knowledge Base
https://docs.qq.com/doc/DSXBmSG9VbEROUXF5

Friendly recommendation:

Collection of useful information on Android development

At this point, this article has ended. The editor thinks the article is reprinted from the Internet and is excellent. You are welcome to click to read the original article and support the original author. If there is any infringement, please contact the editor to delete it. Your suggestions and corrections are welcome. We look forward to your attention and thank you for reading, thank you!

50d708a1dc1886fff146babf3df56833.jpeg

Click to read the original article and like the boss!

Guess you like

Origin blog.csdn.net/wjky2014/article/details/131714436