Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit de1f9047 authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Allow keycode lookup without KEYCODE_ prefix

Currently, KeyEvent.keyCodeFromString(String name) requires the string
to either start with "KEYCODE_", or be directly convertible to an int.
However, the string representation of every keycode starts with
"KEYCODE_", so this requirement is redundant. Relax this requirement to
alllow both of the following usages:
1) keyCodeFromString("KEYCODE_BUTTON_A")
2) keyCodeFromString("BUTTON_A")

Currently, only 1) is supported.

The other usage,
3) keyCodeFromString("29")
is unchanged.

The input is no longer case-sensitive.
Improved the example of usage in the documentation: the input
"1001" suggests that the string must contain binary representation for
usage 3), while in fact it is supposed to be a base 10 number.

Test: atest cts.KeyEventTest#testKeyCodeFromString
Bug: 36069459

Change-Id: I54d7f9d1270748854143cc9d1e8af48c9ec0cd0f
parent dfe3c220
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -1319,6 +1319,7 @@ package android.view {

  public class KeyEvent extends android.view.InputEvent implements android.os.Parcelable {
    method public static java.lang.String actionToString(int);
    field public static final int LAST_KEYCODE = 285; // 0x11d
  }

  public final class KeyboardShortcutGroup implements android.os.Parcelable {
+0 −3
Original line number Diff line number Diff line
@@ -91,9 +91,6 @@ public class Input {
                    if (args.length > start) {
                        for (int i = start; i < args.length; i++) {
                            int keyCode = KeyEvent.keyCodeFromString(args[i]);
                            if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
                                keyCode = KeyEvent.keyCodeFromString("KEYCODE_" + args[i]);
                            }
                            sendKeyEvent(inputSource, keyCode, longpress);
                        }
                        return;
+26 −11
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.view;

import android.annotation.NonNull;
import android.annotation.TestApi;
import android.os.Parcel;
import android.os.Parcelable;
@@ -810,7 +811,12 @@ public class KeyEvent extends InputEvent implements Parcelable {
    /** Key code constant: Refresh key. */
    public static final int KEYCODE_REFRESH = 285;

    private static final int LAST_KEYCODE = KEYCODE_REFRESH;
    /**
     * Integer value of the last KEYCODE. Increases as new keycodes are added to KeyEvent.
     * @hide
     */
    @TestApi
    public static final int LAST_KEYCODE = KEYCODE_REFRESH;

    // NOTE: If you add a new keycode here you must also add it to:
    //  isSystem()
@@ -2889,25 +2895,34 @@ public class KeyEvent extends InputEvent implements Parcelable {

    /**
     * Gets a keycode by its symbolic name such as "KEYCODE_A" or an equivalent
     * numeric constant such as "1001".
     * numeric constant such as "29". For symbolic names,
     * starting in {@link android.os.Build.VERSION_CODES#Q} the prefix "KEYCODE_" is optional.
     *
     * @param symbolicName The symbolic name of the keycode.
     * @return The keycode or {@link #KEYCODE_UNKNOWN} if not found.
     * @see #keycodeToString(int)
     */
    public static int keyCodeFromString(String symbolicName) {
    public static int keyCodeFromString(@NonNull String symbolicName) {
        try {
            int keyCode = Integer.parseInt(symbolicName);
            if (keyCodeIsValid(keyCode)) {
                return keyCode;
            }
        } catch (NumberFormatException ex) {
        }

        if (symbolicName.startsWith(LABEL_PREFIX)) {
            symbolicName = symbolicName.substring(LABEL_PREFIX.length());
        }
        int keyCode = nativeKeyCodeFromString(symbolicName);
            if (keyCode > 0) {
        if (keyCodeIsValid(keyCode)) {
            return keyCode;
        }
        }
        try {
            return Integer.parseInt(symbolicName, 10);
        } catch (NumberFormatException ex) {
        return KEYCODE_UNKNOWN;
    }

    private static boolean keyCodeIsValid(int keyCode) {
        return keyCode >= KEYCODE_UNKNOWN && keyCode <= LAST_KEYCODE;
    }

    /**