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
This commit is contained in:
Siarhei Vishniakou
2018-05-09 09:54:43 -07:00
parent dfe3c22069
commit de1f904713
3 changed files with 27 additions and 14 deletions

View File

@@ -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 {

View File

@@ -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;

View File

@@ -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) {
if (symbolicName.startsWith(LABEL_PREFIX)) {
symbolicName = symbolicName.substring(LABEL_PREFIX.length());
int keyCode = nativeKeyCodeFromString(symbolicName);
if (keyCode > 0) {
public static int keyCodeFromString(@NonNull String symbolicName) {
try {
int keyCode = Integer.parseInt(symbolicName);
if (keyCodeIsValid(keyCode)) {
return keyCode;
}
}
try {
return Integer.parseInt(symbolicName, 10);
} catch (NumberFormatException ex) {
return KEYCODE_UNKNOWN;
}
if (symbolicName.startsWith(LABEL_PREFIX)) {
symbolicName = symbolicName.substring(LABEL_PREFIX.length());
}
int keyCode = nativeKeyCodeFromString(symbolicName);
if (keyCodeIsValid(keyCode)) {
return keyCode;
}
return KEYCODE_UNKNOWN;
}
private static boolean keyCodeIsValid(int keyCode) {
return keyCode >= KEYCODE_UNKNOWN && keyCode <= LAST_KEYCODE;
}
/**