From 758393a388313ac6ff57c23754962e0623cbba3c Mon Sep 17 00:00:00 2001 From: Arthur Hung Date: Wed, 9 Mar 2022 13:51:58 +0800 Subject: [PATCH] Prevent NPE for onKeyDown and onKeyUp We expected `onKeyDown` and `onKeyUp` should be used for the key event delivery from input, but some apps may misuse it and pass a null key event, that would cause app itself crash if it didn't override onKeyDown and onKeyUp properly. To prevent this, we swap the conditions that first check the confirm key code then access the other states of the key event. Bug: 219708835 Test: manual Change-Id: I612ca560d768134bdcaa688c83217738b5254b5a --- core/java/android/view/View.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index c2229911bf128..95c7f0f9c441d 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -15617,7 +15617,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback, * @param event the KeyEvent object that defines the button action */ public boolean onKeyDown(int keyCode, KeyEvent event) { - if (event.hasNoModifiers() && KeyEvent.isConfirmKey(keyCode)) { + if (KeyEvent.isConfirmKey(keyCode) && event.hasNoModifiers()) { if ((mViewFlags & ENABLED_MASK) == DISABLED) { return true; } @@ -15674,7 +15674,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback, * @param event The KeyEvent object that defines the button action. */ public boolean onKeyUp(int keyCode, KeyEvent event) { - if (event.hasNoModifiers() && KeyEvent.isConfirmKey(keyCode)) { + if (KeyEvent.isConfirmKey(keyCode) && event.hasNoModifiers()) { if ((mViewFlags & ENABLED_MASK) == DISABLED) { return true; }