Android Framework层添加和修改按键键值

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/An_Times/article/details/88077819

framework按键修改以及上层处理

前言

前段时间有做一个客户修改了遥控器的按键排布,需要修改键值按键的功能,主要是修改framework层的按键上层处理,此篇做一个记录。

文件修改

总共需要修改下列文件

  1. device/mstar/S2/preinstall/keylayout/Vendor_0000_Product_0000.kl
  2. include/linux/input.h
  3. kitkat-mstar-master/device/mstar/S2/preinstall/modules/rc-mstar-tv.ko
  4. kitkat-mstar-master/frameworks/base/api/current.txt
  5. kitkat-mstar-master/frameworks/base/core/java/android/view/KeyEvent.java
  6. kitkat-mstar-master/frameworks/base/core/res/res/values/attrs.xml
  7. kitkat-mstar-master/frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
  8. kitkat-mstar-master/frameworks/native/include/android/keycodes.h
  9. kitkat-mstar-master/frameworks/native/include/input/KeycodeLabels.h

修改步骤

一、确定按键键值是否有上报:

使用串口输入getevent 命令:
/dev/input/event1: 0004 0000 0000029f
/dev/input/event1: 0004 0001 0000007a
/dev/input/event1: 0004 0002 fff0fc86
/dev/input/event1: 0004 0004 000001ac
/dev/input/event1: 0004 0007 f71c02a7
/dev/input/event1: 0000 0000 00000000
/dev/input/event1: 0004 0000 00002307
/dev/input/event1: 0004 0001 fffffc6d
/dev/input/event1: 0004 0002 fff12e53
/dev/input/event1: 0004 0004 000001ac
/dev/input/event1: 0004 0007 fc7a5385
/dev/input/event1: 0000 0000 00000000
/dev/input/event7: 0004 0004 00000035
/dev/input/event7: 0000 0000 00000000
/dev/input/event1: 0004 0000 00002a2d

当我们按下按键的时候会上报event7事件,接着输入:getevent dev/input/event7
测试我们需要修改的遥控器按键,按下按键,出现如下类似的内容说明有按键上报,说明底层驱动是已经适配好了,只需要修改上层就好了。

0004 0004 00000035
0000 0000 00000000
0004 0004 000000fc
0000 0000 00000000
0004 0004 00000018

二、确定KL键值映射文件
android kl(key layout)文件是一个映射文件,是标准linux与anroid的键值映射文件,kl文件可以有很多个,但是它有一个使用优先级,一般代码中的kl文件都是在device下面,我们需要找到代码中使用的kl文件是哪个

输入:cat /proc/bus/input/devices

130|root@H90:/ # cat /proc/bus/input/devices
I: Bus=0018 Vendor=3697 Product=0001 Version=0001
N: Name="MStar Smart TV IR Receiver"
P: Phys=/dev/ir
S: Sysfs=/devices/virtual/rc/rc0/input0
U: Uniq=
H: Handlers=kbd event0 
B: PROP=0
B: EV=13
B: KEY=7e00000000000000 40000800 1e16c000008000 10000000
B: MSC=10

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="MCE IR Keyboard/Mouse (ir)"
P: Phys=/input0
S: Sysfs=/devices/virtual/input/input1
U: Uniq=
H: Handlers=kbd mouse0 event1 
B: PROP=0
B: EV=100017
B: KEY=30000 7 ff87207ac14057ff febeffdfffefffff fffffffffffffffe
B: REL=3
B: MSC=10

I: Bus=0003 Vendor=058f Product=3841 Version=0001
N: Name="USB 2.0 PC Camera"
P: Phys=usb-mstar-1.2/button
S: Sysfs=/devices/Mstar-ehci-3.12/usb3/3-1/3-1.2/3-1.2:1.0/input/input2
U: Uniq=
H: Handlers=kbd event2 
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0018 Vendor=0000 Product=0000 Version=0000
N: Name="Accelerometer Sensor"
P: Phys=
S: Sysfs=/devices/lsm6ds3.29/input/input3
U: Uniq=
H: Handlers=event3 
B: PROP=0
B: EV=11
B: MSC=97

I: Bus=0018 Vendor=0000 Product=0000 Version=0000
N: Name="ST LSM6DS3 Gyroscope Sensor"
P: Phys=
S: Sysfs=/devices/lsm6ds3.29/input/input4
U: Uniq=
H: Handlers=event4 
B: PROP=0
B: EV=11
B: MSC=97

I: Bus=0018 Vendor=0000 Product=0000 Version=0000
N: Name="ST LSM6DS3 Significant Motion Sensor"
P: Phys=
S: Sysfs=/devices/lsm6ds3.29/input/input5
U: Uniq=
H: Handlers=event5 
B: PROP=0
B: EV=11
B: MSC=91

I: Bus=0018 Vendor=0000 Product=0000 Version=0000
N: Name="ST LSM6DS3 Step Counter Sensor"
P: Phys=
S: Sysfs=/devices/lsm6ds3.29/input/input6
U: Uniq=
H: Handlers=event6 
B: PROP=0
B: EV=11
B: MSC=91

I: Bus=0018 Vendor=0000 Product=0000 Version=0000
N: Name="ST LSM6DS3 Step Detector Sensor"
P: Phys=
S: Sysfs=/devices/lsm6ds3.29/input/input7
U: Uniq=
H: Handlers=event7 
B: PROP=0
B: EV=11
B: MSC=91

I: Bus=0018 Vendor=0000 Product=0000 Version=0000
N: Name="ST LSM6DS3 Tilt Sensor"
P: Phys=
S: Sysfs=/devices/lsm6ds3.29/input/input8
U: Uniq=
H: Handlers=event8 
B: PROP=0
B: EV=11
B: MSC=91

I: Bus=0006 Vendor=3697 Product=0002 Version=0000
N: Name="MStar Smart TV Keypad"
P: Phys=
S: Sysfs=/devices/virtual/input/input9
U: Uniq=
H: Handlers=kbd event9 
B: PROP=0
B: EV=3
B: KEY=7fffffffffffffff ffffffffffffffff ffffffffffffffff fffffffffffffffe

根据前面得到的input时间,比如前面得到的是input7事件,那么我们要找的kl文件就是Vendor_0000_Product_0000.kl

@@ -27,6 +27,9 @@ key 7       6
 key 8       7
 key 9       8
 key 10      9
+key 237     BLUETOOTH
+key 193     SETTINGS_KEY
+key 187     AUTO_FOUCS
 key 398     PROG_RED
 key 399     PROG_GREEN
 key 400     PROG_YELLOW

三、如果遥控器按键没有按键事件上报,说明驱动层没有适配,这里需要做适配
修改文件如下

--- a/3.1.10_Madison_TVOS/3.1.10/drivers/media/rc/keymaps/rc-mstar-tv.c
+++ b/3.1.10_Madison_TVOS/3.1.10/drivers/media/rc/keymaps/rc-mstar-tv.c
@@ -38,7 +38,15 @@ static struct rc_map_table mstar_tv[] = {
        { 0x001d, KEY_FAST_MENU },
        { 0x005A, KEY_FAST_3D },
        { 0x0015, KEY_FAST_SETTINGS },
-        { 0x0049, KEY_CUSTOM_KEY },
+    { 0x0049, KEY_CUSTOM_KEY },
+
+       { 0x0031, KEY_AUTO_FOUCS },     
+       { 0x0036, KEY_SETTINGS_KEY },
+       { 0x0037, KEY_BLUETOOTH },
+       { 0x0032, KEY_PROG_RED },
+       { 0x0033, KEY_PROG_GREEN },
+       { 0x0034, KEY_PROG_YELLOW },
+       { 0x0035, KEY_PROG_BLUE },
 /*
     { 0x0046, KEY_POWER },
     { 0x0050, KEY_0 },
-----------------------------------------------------------------------------------------
--- a/3.1.10_Madison_TVOS/3.1.10/include/linux/input.h
+++ b/3.1.10_Madison_TVOS/3.1.10/include/linux/input.h
@@ -426,7 +426,6 @@ struct input_keymap_entry {
 
 #define KEY_BATTERY            236
 
-#define KEY_BLUETOOTH          237
 #define KEY_WLAN               238
 #define KEY_UWB                        239
 
@@ -451,6 +450,15 @@ struct input_keymap_entry {
 #define KEY_FAST_SETTINGS    251  /* DLP foucs positive */
 #define KEY_FAST_MENU   253  /* HOLA fast menu */
 #define KEY_FAST_3D   254  /* HOLA fast menu */
+
+#define KEY_BLUETOOTH             237 /*hola buletooth */
+#define KEY_AUTO_FOUCS         187 /*hola auto focus */
+#define KEY_SETTINGS_KEY       193 /*hola setting */
+#define KEY_PROG_RED           0x18e /*hola red */
+#define KEY_PROG_GREEN         0x18f /*hola green */
+#define KEY_PROG_YELLOW        0x190 /*hola yellow */
+#define KEY_PROG_BLUE          0x191 /*hola blue */

关于驱动层的按键上报主要是一个input 子系统,关于Android的input 子系统,各位读者可以看
1、Android Framework------之Input子系统,写的非常详细

四、framework层的修改
1、kitkat-mstar-master/frameworks/base/api/current.txt

--- a/kitkat-mstar-master/frameworks/base/api/current.txt
+++ b/kitkat-mstar-master/frameworks/base/api/current.txt
@@ -27443,12 +27443,15 @@ package android.view {
     field public static final int KEYCODE_H = 36; // 0x24
     field public static final int KEYCODE_HEADSETHOOK = 79; // 0x4f
     field public static final int KEYCODE_HENKAN = 214; // 0xd6
+    field public static final int KEYCODE_HOLA_AUTO_FOUCS = 611; // 0x263
+    field public static final int KEYCODE_HOLA_BLUETOOTH = 609; // 0x261
     field public static final int KEYCODE_HOLA_CUSTOMIZABLE_KEY = 608; // 0x260
     field public static final int KEYCODE_HOLA_FAST_MENU = 605; // 0x25d
     field public static final int KEYCODE_HOLA_FAST_SETTINGS = 607; // 0x25f
     field public static final int KEYCODE_HOLA_FOUCS_POSITIVE = 601; // 0x259
     field public static final int KEYCODE_HOLA_FOUCS_REVERSE = 602; // 0x25a
     field public static final int KEYCODE_HOLA_ONEKEY_THREED = 606; // 0x25e
+    field public static final int KEYCODE_HOLA_SETTINGS_KEY = 610; // 0x262
     field public static final int KEYCODE_HOLA_VOICE_OFF = 604; // 0x25c
     field public static final int KEYCODE_HOLA_VOICE_ON = 603; // 0x25b
     field public static final int KEYCODE_HOME = 3; // 0x3

2、kitkat-mstar-master/frameworks/base/core/res/res/values/attrs.xml

--- a/kitkat-mstar-master/frameworks/base/core/res/res/values/attrs.xml
+++ b/kitkat-mstar-master/frameworks/base/core/res/res/values/attrs.xml
@@ -1644,6 +1644,12 @@
         <enum name="KEYCODE_KONKA_ENTER_FACTORY" value="519" />
         <enum name="KEYCODE_KONKA_FACTORY_BAKE_TV" value="520" />
         <!-- Skyworth section, range 601-700 -->
+        
+               <!-- Philp, range 609-703 -->
+        <enum name="KEYCODE_BLUETOOTH" value="609" />
+        <enum name="KEYCODE_SETTINGS_KEY" value="610" />
+               <enum name="KEYCODE_AUTO_FOCUS" value="611" />        
+        
 
         <!-- Tcl section, range 4001-4100 -->
         <enum name="KEYCODE_TCL_MITV" value="4001" />


3、kitkat-mstar-master/frameworks/native/include/android/keycodes.h

--- a/kitkat-mstar-master/frameworks/native/include/android/keycodes.h
+++ b/kitkat-mstar-master/frameworks/native/include/android/keycodes.h
@@ -336,6 +336,13 @@ enum {
     AKEYCODE_HAIER_CLEANSEARCH          = 406,
     // Skyworth section, range 601-700
 
+       //Holatek patch add by szh 20190214 for add keycode begin
+    AKEYCODE_BLUETOOTH  = 609,
+       AKEYCODE_SETTINGS_KEY  = 610,
+       AKEYCODE_AUTO_FOCUS    = 611,   
+    
+       //Holatek patch add by szh 20190214 for add keycode end
+
     // Tcl section, range 4001-4100
     AKEYCODE_TCL_MITV                   = 4001,
     AKEYCODE_TCL_USB_MENU               = 4002,
@@ -475,6 +482,7 @@ enum {
     AKEYCODE_HISENSE_TEST_BOARD_FAC_OK  = 4276,
     // MStar Android Patch End
 
+
     // NOTE: If you add a new keycode here you must also add it to several other files.
     //       Refer to frameworks/base/core/java/android/view/KeyEvent.java for the full list.
 };

4、kitkat-mstar-master/frameworks/native/include/android/keycodes.h

--- a/kitkat-mstar-master/frameworks/native/include/input/KeycodeLabels.h
+++ b/kitkat-mstar-master/frameworks/native/include/input/KeycodeLabels.h
@@ -466,7 +466,9 @@ static const KeycodeLabel KEYCODES[] = {
     { "FAST_3D", 606},
     { "FAST_SETTINGS",607},
     { "CUSTOM_KEY",608},
-
+    {"BLUETOOTH",609},
+    {"SETTINGS_KEY",610},
+    {"AUTO_FOUCS",611},
     //holatek keyevent start
 
     // NOTE: If you add a new keycode here you must also add it to several other files.
(END)

5、kitkat-mstar-master/frameworks/base/core/java/android/view/KeyEvent.java

--- a/kitkat-mstar-master/frameworks/base/core/java/android/view/KeyEvent.java
+++ b/kitkat-mstar-master/frameworks/base/core/java/android/view/KeyEvent.java
@@ -641,6 +641,12 @@ public class KeyEvent extends InputEvent implements Parcelable {
        public static final int KEYCODE_HOLA_ONEKEY_THREED         =  606;
        public static final int KEYCODE_HOLA_FAST_SETTINGS        =  607;  
        public static final int KEYCODE_HOLA_CUSTOMIZABLE_KEY        =  608;
+       /**Holatek patch add by szh for flp keycode 20190214 begin**/
+    public static final int KEYCODE_HOLA_BLUETOOTH = 609;
+       public static final int KEYCODE_HOLA_SETTINGS_KEY = 610;
+       public static final int KEYCODE_HOLA_AUTO_FOUCS = 611;  
+    
+    /**Holatek patch add by szh for flp keycode 20190214 end**/
        
     private static final int LAST_KEYCODE = KEYCODE_HOLA_CUSTOMIZABLE_KEY;

6、kitkat-mstar-master/frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
在PhoneWindowManager中处理按键事件

  @Override
    public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {
        final boolean keyguardOn = keyguardOn();
        final int keyCode = event.getKeyCode();
        final int repeatCount = event.getRepeatCount();
        final int metaState = event.getMetaState();
        final int flags = event.getFlags();
        final boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
        final boolean canceled = event.isCanceled();
        if (true) {
            Log.d(TAG, "interceptKeyTi keyCode=" + keyCode + " down=" + down + " repeatCount="
                  + repeatCount + " keyguardOn=" + keyguardOn + " mHomePressed=" + mHomePressed
                  + " canceled=" + canceled);
        }

        String threeDswitching = SystemProperties.get("sys.jmgo.enable3d");
        if ("1".equals(threeDswitching)) {
            Log.d(TAG, "Three D switching, not response to ir");
            return -1;
        }
        
        // If we think we might have a volume down & power key chord on the way
        // but we're not sure, then tell the dispatcher to wait a little while and
        // try again later before dispatching.
        if (mScreenshotChordEnabled && (flags & KeyEvent.FLAG_FALLBACK) == 0) {
            if (mVolumeDownKeyTriggered && !mPowerKeyTriggered) {
                final long now = SystemClock.uptimeMillis();
                final long timeoutTime = mVolumeDownKeyTime + SCREENSHOT_CHORD_DEBOUNCE_DELAY_MILLIS;
                if (now < timeoutTime) {
                    return timeoutTime - now;
                }
            }
            if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
                && mVolumeDownKeyConsumedByScreenshotChord) {
                if (!down) {
                    mVolumeDownKeyConsumedByScreenshotChord = false;
                }
                return -1;
            }
        }

        // First we always handle the home key here, so applications
        // can never break it, although if keyguard is on, we do let
        // it handle it, because that gives us the correct 5 second
        // timeout.
        if (keyCode == KeyEvent.KEYCODE_HOME) {

            // If we have released the home key, and didn't do anything else
            // while it was pressed, then it is time to go home!
            if (!down) {
                cancelPreloadRecentApps();

                mHomePressed = false;
                if (mHomeConsumed) {
                    mHomeConsumed = false;
                    return -1;
                }

                if (canceled) {
                    Log.i(TAG, "Ignoring HOME; event canceled.");
                    return -1;
                }

                // If an incoming call is ringing, HOME is totally disabled.
                // (The user is already on the InCallScreen at this point,
                // and his ONLY options are to answer or reject the call.)
                try {
                    ITelephony telephonyService = getTelephonyService();
                    if (telephonyService != null && telephonyService.isRinging()) {
                        Log.i(TAG, "Ignoring HOME; there's a ringing incoming call.");
                        return -1;
                    }
                } catch (RemoteException ex) {
                    Log.w(TAG, "RemoteException from getPhoneInterface()", ex);
                }

                // Delay handling home if a double-tap is possible.
                if (mDoubleTapOnHomeBehavior != DOUBLE_TAP_HOME_NOTHING) {
                    mHandler.removeCallbacks(mHomeDoubleTapTimeoutRunnable); // just in case
                    mHomeDoubleTapPending = true;
                    mHandler.postDelayed(mHomeDoubleTapTimeoutRunnable,
                                         ViewConfiguration.getDoubleTapTimeout());
                    return -1;
                }

                // Go home!
                if(mPowerManager.isDlpShutDown() == 0) {
                    launchHomeFromHotKey();
                } else if(mPowerManager.isDlpShutDown() == 1) {
                    Intent dlpPowerOn = new Intent();
                    dlpPowerOn.setAction("android.intent.action.HOLATEK_DLP_POWER_ON");
                    mContext.sendBroadcast(dlpPowerOn);
                }
                return -1;
            }

            // If a system window has focus, then it doesn't make sense
            // right now to interact with applications.
            WindowManager.LayoutParams attrs = win != null ? win.getAttrs() : null;
            if (attrs != null) {
                final int type = attrs.type;
                if (type == WindowManager.LayoutParams.TYPE_KEYGUARD
                    || type == WindowManager.LayoutParams.TYPE_KEYGUARD_SCRIM
                    || type == WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG) {
                    // the "app" is keyguard, so give it the key
                    return 0;
                }
                final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;
                for (int i=0; i<typeCount; i++) {
                    if (type == WINDOW_TYPES_WHERE_HOME_DOESNT_WORK[i]) {
                        // don't do anything, but also don't pass it to the app
                        return -1;
                    }
                }
            }
            // Remember that home is pressed and handle special actions.
            if (repeatCount == 0) {
                mHomePressed = true;
                if (mHomeDoubleTapPending) {
                    mHomeDoubleTapPending = false;
                    mHandler.removeCallbacks(mHomeDoubleTapTimeoutRunnable);
                    handleDoubleTapOnHome();
                } else if (mLongPressOnHomeBehavior == LONG_PRESS_HOME_RECENT_SYSTEM_UI
                           || mDoubleTapOnHomeBehavior == DOUBLE_TAP_HOME_RECENT_SYSTEM_UI) {
                    preloadRecentApps();
                }
            } else if ((event.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0) {
                if (!keyguardOn) {
                    handleLongPressOnHome();
                }
            }
            return -1;
            //Holatek patch begin
        } 
        /*else if (keyCode == KeyEvent.KEYCODE_HOLA_FAST_MENU) {
            if(down && repeatCount ==0) {
                Intent menuLongPress = new Intent();
                menuLongPress.setAction("android.intent.action.LONG_PRESS_MENU_KEY");
                mContext.sendBroadcast(menuLongPress);
            }
        } else if (keyCode == KeyEvent.KEYCODE_HOLA_ONEKEY_THREED) {
            if (down && repeatCount ==0) {
                Intent oneKeyTD = new Intent();
                oneKeyTD.setAction("android.intent.action.HOLATEK_ONE_KEY_THREED");
                mContext.sendBroadcastAsUser(oneKeyTD, UserHandle.ALL);
            }
        } else if (keyCode == KeyEvent.KEYCODE_HOLA_CUSTOMIZABLE_KEY) {
            if (down && repeatCount ==0) {
                Intent custKeyIntent = new Intent();
                custKeyIntent.setAction("android.intent.jmgo.action.customizable.key");
                mContext.sendBroadcast(custKeyIntent);
            }
        } 
        */
        else if (keyCode == KeyEvent.KEYCODE_HOLA_FOUCS_POSITIVE) {
            if (down) {
                Intent intent = new Intent();
                intent.setAction("android.jmgo.action.foucs");
                intent.putExtra("foucs_direct", 1);
                mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
                HolatekOSManager holatekManager = (HolatekOSManager)mContext.getSystemService("holatekos_mananger");
                if (holatekManager != null) {
                    holatekManager.set(HolatekOSConfig.Service.DLP, HolatekOSConfig.Command.DLP_CMD_FOUCS_POSITIVE, 1);
                }
            }
        } else if (keyCode == KeyEvent.KEYCODE_HOLA_FOUCS_REVERSE) {
            if (down) {
                Intent intent = new Intent();

				intent.setAction("android.jmgo.action.foucs");
                intent.putExtra("foucs_direct", 2);
                mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
                HolatekOSManager holatekManager = (HolatekOSManager)mContext.getSystemService("holatekos_mananger");
                if (holatekManager != null) {
                    holatekManager.set(HolatekOSConfig.Service.DLP, HolatekOSConfig.Command.DLP_CMD_FOUCS_REVERSE, 1);
                }
            }
        } else if(keyCode == KeyEvent.KEYCODE_HOLA_FAST_SETTINGS) {
            if(down && repeatCount ==0) {
                try {
                    Intent fastSetting = new Intent();
                    fastSetting.setAction("android.settings.SETTINGS");
                    fastSetting.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(fastSetting);
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
            //Holatek patch end
        } else if (keyCode == KeyEvent.KEYCODE_MENU) {
            // Hijack modified menu keys for debugging features
            final int chordBug = KeyEvent.META_SHIFT_ON;
            if (down && repeatCount == 0) {
                if (mEnableShiftMenuBugReports && (metaState & chordBug) == chordBug) {
                    Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
                    mContext.sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT,
                                                        null, null, null, 0, null, null);
                    return -1;
                } else if (SHOW_PROCESSES_ON_ALT_MENU &&
                           (metaState & KeyEvent.META_ALT_ON) == KeyEvent.META_ALT_ON) {
                    Intent service = new Intent();
                    service.setClassName(mContext, "com.android.server.LoadAverageService");
                    ContentResolver res = mContext.getContentResolver();
                    boolean shown = Settings.Global.getInt(
                                        res, Settings.Global.SHOW_PROCESSES, 0) != 0;
                    if (!shown) {
                        mContext.startService(service);
                    } else {
                        mContext.stopService(service);
                    }
                    Settings.Global.putInt(
                        res, Settings.Global.SHOW_PROCESSES, shown ? 0 : 1);
                    return -1;
                }
            }
        } else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
            if (down) {
                if (repeatCount == 0) {
                    mSearchKeyShortcutPending = true;
                    mConsumeSearchKeyUp = false;
                }
            } else {
                mSearchKeyShortcutPending = false;
                if (mConsumeSearchKeyUp) {
                    mConsumeSearchKeyUp = false;
                    return -1;
                }
            }
            return 0;
        } else if (keyCode == KeyEvent.KEYCODE_APP_SWITCH) {
            if (!keyguardOn) {
                if (down && repeatCount == 0) {
                    preloadRecentApps();
                } else if (!down) {
                    toggleRecentApps();
                }
            }
            return -1;
        } else if (keyCode == KeyEvent.KEYCODE_ASSIST) {
            if (down) {
                if (repeatCount == 0) {
                    mAssistKeyLongPressed = false;
                } else if (repeatCount == 1) {
                    mAssistKeyLongPressed = true;
                    if (!keyguardOn) {
                        launchAssistLongPressAction();
                    }
                }
            } else {
                if (mAssistKeyLongPressed) {
                    mAssistKeyLongPressed = false;
                } else {
                    if (!keyguardOn) {
                        launchAssistAction();
                    }
                }
            }
            return -1;
        } else if (keyCode == KeyEvent.KEYCODE_SYSRQ) {
            if (down && repeatCount == 0) {
                mHandler.post(mScreenshotRunnable);
            }
            return -1;
        } else if (keyCode == KeyEvent.KEYCODE_BRIGHTNESS_UP
                   || keyCode == KeyEvent.KEYCODE_BRIGHTNESS_DOWN) {
            if (down) {
                int direction = keyCode == KeyEvent.KEYCODE_BRIGHTNESS_UP ? 1 : -1;

                // Disable autobrightness if it's on
                int auto = Settings.System.getIntForUser(
                               mContext.getContentResolver(),
                               Settings.System.SCREEN_BRIGHTNESS_MODE,
                               Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL,
                               UserHandle.USER_CURRENT_OR_SELF);
                if (auto != 0) {
                    Settings.System.putIntForUser(mContext.getContentResolver(),
                                                  Settings.System.SCREEN_BRIGHTNESS_MODE,
                                                  Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL,
                                                  UserHandle.USER_CURRENT_OR_SELF);
                }

                int min = mPowerManager.getMinimumScreenBrightnessSetting();
                int max = mPowerManager.getMaximumScreenBrightnessSetting();
                int step = (max - min + BRIGHTNESS_STEPS - 1) / BRIGHTNESS_STEPS * direction;
                int brightness = Settings.System.getIntForUser(mContext.getContentResolver(),
                                 Settings.System.SCREEN_BRIGHTNESS,
                                 mPowerManager.getDefaultScreenBrightnessSetting(),
                                 UserHandle.USER_CURRENT_OR_SELF);
                brightness += step;
                // Make sure we don't go beyond the limits.
                brightness = Math.min(max, brightness);
                brightness = Math.max(min, brightness);

                Settings.System.putIntForUser(mContext.getContentResolver(),
                                              Settings.System.SCREEN_BRIGHTNESS, brightness,
                                              UserHandle.USER_CURRENT_OR_SELF);
                Intent intent = new Intent(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG);
                mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT_OR_SELF);
            }
            return -1;
            // MStar Android Patch Begin
            // Hotkey intent.
        } else if (keyCode == MKeyEvent.KEYCODE_SCREENSHOT) {
            if (down) {
                mHandler.postDelayed(mScreenshotRunnable,
                                     ViewConfiguration.getGlobalActionKeyTimeout());
                return -1;
            }
        } else if (keyCode == KeyEvent.KEYCODE_NOTIFICATION) {
            if (down) {
                Intent intent = new Intent(MIntent.ACTION_NOTIFICATION_BUTTON);
                mContext.sendBroadcast(intent);
                return -1;
            }
        }
		//Holatek patch add by szh 20190214 for add keycode begin 
         else if (keyCode == KeyEvent.KEYCODE_HOLA_AUTO_FOUCS) {
			if(Build.DEVICE.contains("H72P")||Build.DEVICE.contains("S4")||Build.DEVICE.contains("S2")){
                if (down && getHolatekManager(mContext)){
                    mHolatekManager.set(HolatekOSConfig.Service.DLP, HolatekOSConfig.DEV_INDEX_AUTOMOTOR_ALINE,1);
                }
            }
        }else if(keyCode == KeyEvent.KEYCODE_HOLA_SETTINGS_KEY){
			if(Build.DEVICE.contains("H72P")||Build.DEVICE.contains("S4")||Build.DEVICE.contains("S2")){
                if (down){
					Intent intent = new Intent();
                    intent.setAction("android.settings.SETTINGS");
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                }
			}
        }else if(keyCode == KeyEvent.KEYCODE_HOLA_BLUETOOTH){
			if(Build.DEVICE.contains("H72P")||Build.DEVICE.contains("S4")||Build.DEVICE.contains("S2")){
                if (down){
                    Intent intent = new Intent();
                    intent.setAction("android.settings.BLUETOOTH_SETTINGS");
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                }
			}
		}
        //Holatek patch add by szh 20190214 for add keycode end 
        // MStar Android Patch End

        // Shortcuts are invoked through Search+key, so intercept those here
        // Any printing key that is chorded with Search should be consumed
        // even if no shortcut was invoked.  This prevents text from being
        // inadvertently inserted when using a keyboard that has built-in macro
        // shortcut keys (that emit Search+x) and some of them are not registered.
        if (mSearchKeyShortcutPending) {
            final KeyCharacterMap kcm = event.getKeyCharacterMap();
            if (kcm.isPrintingKey(keyCode)) {
                mConsumeSearchKeyUp = true;
                mSearchKeyShortcutPending = false;
                if (down && repeatCount == 0 && !keyguardOn) {
                    Intent shortcutIntent = mShortcutManager.getIntent(kcm, keyCode, metaState);
                    if (shortcutIntent != null) {
                        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        try {
                            mContext.startActivityAsUser(shortcutIntent, UserHandle.CURRENT);
                        } catch (ActivityNotFoundException ex) {
                            Slog.w(TAG, "Dropping shortcut key combination because "
                                   + "the activity to which it is registered was not found: "
                                   + "SEARCH+" + KeyEvent.keyCodeToString(keyCode), ex);
                        }
                    } else {
                        Slog.i(TAG, "Dropping unregistered shortcut key combination: "
                               + "SEARCH+" + KeyEvent.keyCodeToString(keyCode));
                    }
                }
                return -1;
            }
        }

        // Invoke shortcuts using Meta.
        if (down && repeatCount == 0 && !keyguardOn
            && (metaState & KeyEvent.META_META_ON) != 0) {
            final KeyCharacterMap kcm = event.getKeyCharacterMap();
            if (kcm.isPrintingKey(keyCode)) {
                Intent shortcutIntent = mShortcutManager.getIntent(kcm, keyCode,
                                        metaState & ~(KeyEvent.META_META_ON
                                                      | KeyEvent.META_META_LEFT_ON | KeyEvent.META_META_RIGHT_ON));
                if (shortcutIntent != null) {
                    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    try {
                        mContext.startActivityAsUser(shortcutIntent, UserHandle.CURRENT);
                    } catch (ActivityNotFoundException ex) {
                        Slog.w(TAG, "Dropping shortcut key combination because "
                               + "the activity to which it is registered was not found: "
                               + "META+" + KeyEvent.keyCodeToString(keyCode), ex);
                    }
                    return -1;
                }
            }
        }

        // Handle application launch keys.
        if (down && repeatCount == 0 && !keyguardOn) {
            String category = sApplicationLaunchKeyCategories.get(keyCode);
            if (category != null) {
                Intent intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, category);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                try {
                    mContext.startActivityAsUser(intent, UserHandle.CURRENT);
                } catch (ActivityNotFoundException ex) {
                    Slog.w(TAG, "Dropping application launch key because "
                           + "the activity to which it is registered was not found: "
                           + "keyCode=" + keyCode + ", category=" + category, ex);
                }
                return -1;
            }
        }

        // Display task switcher for ALT-TAB or Meta-TAB.
        if (down && repeatCount == 0 && keyCode == KeyEvent.KEYCODE_TAB) {
            if (mRecentAppsDialogHeldModifiers == 0 && !keyguardOn) {
                final int shiftlessModifiers = event.getModifiers() & ~KeyEvent.META_SHIFT_MASK;
                if (KeyEvent.metaStateHasModifiers(shiftlessModifiers, KeyEvent.META_ALT_ON)
                    || KeyEvent.metaStateHasModifiers(
                        shiftlessModifiers, KeyEvent.META_META_ON)) {
                    mRecentAppsDialogHeldModifiers = shiftlessModifiers;
                    showOrHideRecentAppsDialog(RECENT_APPS_BEHAVIOR_EXIT_TOUCH_MODE_AND_SHOW);
                    return -1;
                }
            }
        } else if (!down && mRecentAppsDialogHeldModifiers != 0
                   && (metaState & mRecentAppsDialogHeldModifiers) == 0) {
            mRecentAppsDialogHeldModifiers = 0;
            showOrHideRecentAppsDialog(keyguardOn ? RECENT_APPS_BEHAVIOR_DISMISS :
                                       RECENT_APPS_BEHAVIOR_DISMISS_AND_SWITCH);
        }

        // Handle keyboard language switching.
        if (down && repeatCount == 0
            && (keyCode == KeyEvent.KEYCODE_LANGUAGE_SWITCH
                || (keyCode == KeyEvent.KEYCODE_SPACE
                    && (metaState & KeyEvent.META_CTRL_MASK) != 0))) {
            int direction = (metaState & KeyEvent.META_SHIFT_MASK) != 0 ? -1 : 1;
            mWindowManagerFuncs.switchKeyboardLayout(event.getDeviceId(), direction);
            return -1;
        }
        if (mLanguageSwitchKeyPressed && !down
            && (keyCode == KeyEvent.KEYCODE_LANGUAGE_SWITCH
                || keyCode == KeyEvent.KEYCODE_SPACE)) {
            mLanguageSwitchKeyPressed = false;
            return -1;
        }

        if (mGlobalKeyManager.handleGlobalKey(mContext, keyCode, event)) {
            return -1;
        }

        // Let the application handle the key.
        return 0;
    }
    

最后我们编译framework 的service.jar包验证修改就好了。

引用文章

1、Android Framework------之Input子系统
2、Android下添加新的自定义键值和按键处理流程

猜你喜欢

转载自blog.csdn.net/An_Times/article/details/88077819