From ad914db0143e87e94cb9f406b504d278955f1d2d Mon Sep 17 00:00:00 2001 From: Prabir Pradhan Date: Mon, 12 Sep 2022 20:41:40 +0000 Subject: [PATCH] Add Android keycodes for stylus buttons Ensure the new stylus buttons are not yet sent to apps. DD: go/android-stylus-buttons Bug: 246394583 Test: Build, Presubmit Change-Id: I440a359ec8cffb60b9e87f2e51304c9b0320e16f --- core/api/current.txt | 4 ++++ core/api/test-current.txt | 2 +- core/java/android/view/KeyEvent.java | 22 ++++++++++++++++++- data/keyboards/Generic.kl | 7 ++++++ .../server/policy/PhoneWindowManager.java | 15 +++++++++++++ 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/core/api/current.txt b/core/api/current.txt index 0ab00dfa4a34a..07b55410fdfdf 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -48770,6 +48770,10 @@ package android.view { field public static final int KEYCODE_STEM_2 = 266; // 0x10a field public static final int KEYCODE_STEM_3 = 267; // 0x10b field public static final int KEYCODE_STEM_PRIMARY = 264; // 0x108 + field public static final int KEYCODE_STYLUS_BUTTON_PRIMARY = 308; // 0x134 + field public static final int KEYCODE_STYLUS_BUTTON_SECONDARY = 309; // 0x135 + field public static final int KEYCODE_STYLUS_BUTTON_TAIL = 311; // 0x137 + field public static final int KEYCODE_STYLUS_BUTTON_TERTIARY = 310; // 0x136 field public static final int KEYCODE_SWITCH_CHARSET = 95; // 0x5f field public static final int KEYCODE_SYM = 63; // 0x3f field public static final int KEYCODE_SYSRQ = 120; // 0x78 diff --git a/core/api/test-current.txt b/core/api/test-current.txt index 8c537336bddee..46a73de780509 100644 --- a/core/api/test-current.txt +++ b/core/api/test-current.txt @@ -2849,7 +2849,7 @@ package android.view { method public static String actionToString(int); method public final void setDisplayId(int); field public static final int FLAG_IS_ACCESSIBILITY_EVENT = 2048; // 0x800 - field public static final int LAST_KEYCODE = 307; // 0x133 + field public static final int LAST_KEYCODE = 311; // 0x137 } public final class KeyboardShortcutGroup implements android.os.Parcelable { diff --git a/core/java/android/view/KeyEvent.java b/core/java/android/view/KeyEvent.java index 6c238e5698c55..9789b5670fbb9 100644 --- a/core/java/android/view/KeyEvent.java +++ b/core/java/android/view/KeyEvent.java @@ -872,13 +872,33 @@ public class KeyEvent extends InputEvent implements Parcelable { public static final int KEYCODE_KEYBOARD_BACKLIGHT_UP = 306; /** Key code constant: Keyboard backlight toggle */ public static final int KEYCODE_KEYBOARD_BACKLIGHT_TOGGLE = 307; + /** + * Key code constant: The primary button on the barrel of a stylus. + * This is usually the button closest to the tip of the stylus. + */ + public static final int KEYCODE_STYLUS_BUTTON_PRIMARY = 308; + /** + * Key code constant: The secondary button on the barrel of a stylus. + * This is usually the second button from the tip of the stylus. + */ + public static final int KEYCODE_STYLUS_BUTTON_SECONDARY = 309; + /** + * Key code constant: The tertiary button on the barrel of a stylus. + * This is usually the third button from the tip of the stylus. + */ + public static final int KEYCODE_STYLUS_BUTTON_TERTIARY = 310; + /** + * Key code constant: A button on the tail end of a stylus. + * The use of this button does not usually correspond to the function of an eraser. + */ + public static final int KEYCODE_STYLUS_BUTTON_TAIL = 311; /** * Integer value of the last KEYCODE. Increases as new keycodes are added to KeyEvent. * @hide */ @TestApi - public static final int LAST_KEYCODE = KEYCODE_KEYBOARD_BACKLIGHT_TOGGLE; + public static final int LAST_KEYCODE = KEYCODE_STYLUS_BUTTON_TAIL; // NOTE: If you add a new keycode here you must also add it to: // isSystem() diff --git a/data/keyboards/Generic.kl b/data/keyboards/Generic.kl index a186aca4648f4..af96c743f4000 100644 --- a/data/keyboards/Generic.kl +++ b/data/keyboards/Generic.kl @@ -302,6 +302,11 @@ key 317 BUTTON_THUMBL key 318 BUTTON_THUMBR +key 329 STYLUS_BUTTON_TERTIARY +key 331 STYLUS_BUTTON_PRIMARY +key 332 STYLUS_BUTTON_SECONDARY + + # key 352 "KEY_OK" key 353 DPAD_CENTER # key 354 "KEY_GOTO" @@ -424,6 +429,8 @@ key usage 0x0c007C KEYBOARD_BACKLIGHT_TOGGLE key usage 0x0c0173 MEDIA_AUDIO_TRACK key usage 0x0c019C PROFILE_SWITCH key usage 0x0c01A2 ALL_APPS +key usage 0x0d0044 STYLUS_BUTTON_PRIMARY +key usage 0x0d005a STYLUS_BUTTON_SECONDARY # Joystick and game controller axes. # Axes that are not mapped will be assigned generic axis numbers by the input subsystem. diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java index e332ac765633a..675819a772438 100644 --- a/services/core/java/com/android/server/policy/PhoneWindowManager.java +++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java @@ -3052,6 +3052,13 @@ public class PhoneWindowManager implements WindowManagerPolicy { } } break; + case KeyEvent.KEYCODE_STYLUS_BUTTON_PRIMARY: + case KeyEvent.KEYCODE_STYLUS_BUTTON_SECONDARY: + case KeyEvent.KEYCODE_STYLUS_BUTTON_TERTIARY: + case KeyEvent.KEYCODE_STYLUS_BUTTON_TAIL: + Slog.wtf(TAG, "KEYCODE_STYLUS_BUTTON_* should be handled in" + + " interceptKeyBeforeQueueing"); + return key_consumed; } if (isValidGlobalKey(keyCode) @@ -4104,6 +4111,14 @@ public class PhoneWindowManager implements WindowManagerPolicy { result &= ~ACTION_PASS_TO_USER; break; } + case KeyEvent.KEYCODE_STYLUS_BUTTON_PRIMARY: + case KeyEvent.KEYCODE_STYLUS_BUTTON_SECONDARY: + case KeyEvent.KEYCODE_STYLUS_BUTTON_TERTIARY: + case KeyEvent.KEYCODE_STYLUS_BUTTON_TAIL: { + // TODO(go/android-stylus-buttons): Handle stylus button presses. + result &= ~ACTION_PASS_TO_USER; + break; + } } if (useHapticFeedback) {