From 704afe20778edefca88c46b393923cac540fa5ae Mon Sep 17 00:00:00 2001 From: Philip Junker Date: Tue, 14 Dec 2021 15:35:42 +0100 Subject: [PATCH] Add TestAPIs to test InputDevice#getKeyCodeForKeyLocation(). Mention @RequiredPermission(..) where applicable / move it from javadocs. Bug: 179812917 Test: atest KeyboardLayoutChangeTest Test: atest android.hardware.input.cts.tests -m Change-Id: I183734d3bb80a6f0c69f27629bfaaa577e4fab59 --- core/api/test-current.txt | 16 ++++ .../hardware/input/InputDeviceIdentifier.java | 8 +- .../android/hardware/input/InputManager.java | 84 +++++++++++++------ core/java/android/view/InputDevice.java | 2 + core/res/AndroidManifest.xml | 3 +- 5 files changed, 84 insertions(+), 29 deletions(-) diff --git a/core/api/test-current.txt b/core/api/test-current.txt index f1b46248ef854..b5955f63d31a5 100644 --- a/core/api/test-current.txt +++ b/core/api/test-current.txt @@ -38,6 +38,7 @@ package android { field public static final String RESET_APP_ERRORS = "android.permission.RESET_APP_ERRORS"; field public static final String REVOKE_POST_NOTIFICATIONS_WITHOUT_KILL = "android.permission.REVOKE_POST_NOTIFICATIONS_WITHOUT_KILL"; field public static final String SET_AND_VERIFY_LOCKSCREEN_CREDENTIALS = "android.permission.SET_AND_VERIFY_LOCKSCREEN_CREDENTIALS"; + field public static final String SET_KEYBOARD_LAYOUT = "android.permission.SET_KEYBOARD_LAYOUT"; field public static final String START_TASKS_FROM_RECENTS = "android.permission.START_TASKS_FROM_RECENTS"; field public static final String SUSPEND_APPS = "android.permission.SUSPEND_APPS"; field public static final String TEST_BIOMETRIC = "android.permission.TEST_BIOMETRIC"; @@ -1189,9 +1190,23 @@ package android.hardware.hdmi { package android.hardware.input { + public final class InputDeviceIdentifier implements android.os.Parcelable { + ctor public InputDeviceIdentifier(@NonNull String, int, int); + method public int describeContents(); + method @NonNull public String getDescriptor(); + method public int getProductId(); + method public int getVendorId(); + method public void writeToParcel(@NonNull android.os.Parcel, int); + field @NonNull public static final android.os.Parcelable.Creator CREATOR; + } + public final class InputManager { method public int getBlockUntrustedTouchesMode(@NonNull android.content.Context); + method @Nullable public String getCurrentKeyboardLayoutForInputDevice(@NonNull android.hardware.input.InputDeviceIdentifier); + method @NonNull public java.util.List getKeyboardLayoutDescriptorsForInputDevice(@NonNull android.view.InputDevice); + method @RequiresPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT) public void removeKeyboardLayoutForInputDevice(@NonNull android.hardware.input.InputDeviceIdentifier, @NonNull String); method @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) public void setBlockUntrustedTouchesMode(@NonNull android.content.Context, int); + method @RequiresPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT) public void setCurrentKeyboardLayoutForInputDevice(@NonNull android.hardware.input.InputDeviceIdentifier, @NonNull String); method @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) public void setMaximumObscuringOpacityForTouch(@FloatRange(from=0, to=1) float); field public static final long BLOCK_UNTRUSTED_TOUCHES = 158002302L; // 0x96aec7eL } @@ -2746,6 +2761,7 @@ package android.view { public final class InputDevice implements android.os.Parcelable { method @RequiresPermission("android.permission.DISABLE_INPUT_DEVICE") public void disable(); method @RequiresPermission("android.permission.DISABLE_INPUT_DEVICE") public void enable(); + method @NonNull public android.hardware.input.InputDeviceIdentifier getIdentifier(); } public class KeyEvent extends android.view.InputEvent implements android.os.Parcelable { diff --git a/core/java/android/hardware/input/InputDeviceIdentifier.java b/core/java/android/hardware/input/InputDeviceIdentifier.java index c673e7ab7c537..a5b9a2a4da766 100644 --- a/core/java/android/hardware/input/InputDeviceIdentifier.java +++ b/core/java/android/hardware/input/InputDeviceIdentifier.java @@ -16,7 +16,9 @@ package android.hardware.input; +import android.annotation.NonNull; import android.annotation.Nullable; +import android.annotation.TestApi; import android.os.Parcel; import android.os.Parcelable; import android.text.TextUtils; @@ -28,12 +30,13 @@ import java.util.Objects; * * @hide */ +@TestApi public final class InputDeviceIdentifier implements Parcelable { private final String mDescriptor; private final int mVendorId; private final int mProductId; - public InputDeviceIdentifier(String descriptor, int vendorId, int productId) { + public InputDeviceIdentifier(@NonNull String descriptor, int vendorId, int productId) { this.mDescriptor = descriptor; this.mVendorId = vendorId; this.mProductId = productId; @@ -51,12 +54,13 @@ public final class InputDeviceIdentifier implements Parcelable { } @Override - public void writeToParcel(Parcel dest, int flags) { + public void writeToParcel(@NonNull Parcel dest, int flags) { dest.writeString(mDescriptor); dest.writeInt(mVendorId); dest.writeInt(mProductId); } + @NonNull public String getDescriptor() { return mDescriptor; } diff --git a/core/java/android/hardware/input/InputManager.java b/core/java/android/hardware/input/InputManager.java index df59fefbf6d35..cbc837393b6b7 100644 --- a/core/java/android/hardware/input/InputManager.java +++ b/core/java/android/hardware/input/InputManager.java @@ -637,6 +637,30 @@ public final class InputManager { } } + /** + * Returns the descriptors of all supported keyboard layouts appropriate for the specified + * input device. + *

+ * The input manager consults the built-in keyboard layouts as well as all keyboard layouts + * advertised by applications using a {@link #ACTION_QUERY_KEYBOARD_LAYOUTS} broadcast receiver. + *

+ * + * @param device The input device to query. + * @return The ids of all keyboard layouts which are supported by the specified input device. + * + * @hide + */ + @TestApi + @NonNull + public List getKeyboardLayoutDescriptorsForInputDevice(@NonNull InputDevice device) { + KeyboardLayout[] layouts = getKeyboardLayoutsForInputDevice(device.getIdentifier()); + List res = new ArrayList<>(); + for (KeyboardLayout kl : layouts) { + res.add(kl.getDescriptor()); + } + return res; + } + /** * Gets information about all supported keyboard layouts appropriate * for a specific input device. @@ -651,7 +675,9 @@ public final class InputManager { * * @hide */ - public KeyboardLayout[] getKeyboardLayoutsForInputDevice(InputDeviceIdentifier identifier) { + @NonNull + public KeyboardLayout[] getKeyboardLayoutsForInputDevice( + @NonNull InputDeviceIdentifier identifier) { try { return mIm.getKeyboardLayoutsForInputDevice(identifier); } catch (RemoteException ex) { @@ -681,15 +707,17 @@ public final class InputManager { } /** - * Gets the current keyboard layout descriptor for the specified input - * device. + * Gets the current keyboard layout descriptor for the specified input device. * * @param identifier Identifier for the input device - * @return The keyboard layout descriptor, or null if no keyboard layout has - * been set. + * @return The keyboard layout descriptor, or null if no keyboard layout has been set. + * * @hide */ - public String getCurrentKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier) { + @TestApi + @Nullable + public String getCurrentKeyboardLayoutForInputDevice( + @NonNull InputDeviceIdentifier identifier) { try { return mIm.getCurrentKeyboardLayoutForInputDevice(identifier); } catch (RemoteException ex) { @@ -698,20 +726,21 @@ public final class InputManager { } /** - * Sets the current keyboard layout descriptor for the specified input - * device. + * Sets the current keyboard layout descriptor for the specified input device. *

- * This method may have the side-effect of causing the input device in - * question to be reconfigured. + * This method may have the side-effect of causing the input device in question to be + * reconfigured. *

* * @param identifier The identifier for the input device. - * @param keyboardLayoutDescriptor The keyboard layout descriptor to use, - * must not be null. + * @param keyboardLayoutDescriptor The keyboard layout descriptor to use, must not be null. + * * @hide */ - public void setCurrentKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier, - String keyboardLayoutDescriptor) { + @TestApi + @RequiresPermission(Manifest.permission.SET_KEYBOARD_LAYOUT) + public void setCurrentKeyboardLayoutForInputDevice(@NonNull InputDeviceIdentifier identifier, + @NonNull String keyboardLayoutDescriptor) { if (identifier == null) { throw new IllegalArgumentException("identifier must not be null"); } @@ -728,11 +757,11 @@ public final class InputManager { } /** - * Gets all keyboard layout descriptors that are enabled for the specified - * input device. + * Gets all keyboard layout descriptors that are enabled for the specified input device. * * @param identifier The identifier for the input device. * @return The keyboard layout descriptors. + * * @hide */ public String[] getEnabledKeyboardLayoutsForInputDevice(InputDeviceIdentifier identifier) { @@ -750,15 +779,16 @@ public final class InputManager { /** * Adds the keyboard layout descriptor for the specified input device. *

- * This method may have the side-effect of causing the input device in - * question to be reconfigured. + * This method may have the side-effect of causing the input device in question to be + * reconfigured. *

* * @param identifier The identifier for the input device. - * @param keyboardLayoutDescriptor The descriptor of the keyboard layout to - * add. + * @param keyboardLayoutDescriptor The descriptor of the keyboard layout to add. + * * @hide */ + @RequiresPermission(Manifest.permission.SET_KEYBOARD_LAYOUT) public void addKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier, String keyboardLayoutDescriptor) { if (identifier == null) { @@ -778,17 +808,19 @@ public final class InputManager { /** * Removes the keyboard layout descriptor for the specified input device. *

- * This method may have the side-effect of causing the input device in - * question to be reconfigured. + * This method may have the side-effect of causing the input device in question to be + * reconfigured. *

* * @param identifier The identifier for the input device. - * @param keyboardLayoutDescriptor The descriptor of the keyboard layout to - * remove. + * @param keyboardLayoutDescriptor The descriptor of the keyboard layout to remove. + * * @hide */ - public void removeKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier, - String keyboardLayoutDescriptor) { + @TestApi + @RequiresPermission(Manifest.permission.SET_KEYBOARD_LAYOUT) + public void removeKeyboardLayoutForInputDevice(@NonNull InputDeviceIdentifier identifier, + @NonNull String keyboardLayoutDescriptor) { if (identifier == null) { throw new IllegalArgumentException("inputDeviceDescriptor must not be null"); } diff --git a/core/java/android/view/InputDevice.java b/core/java/android/view/InputDevice.java index cadff69ffd253..188d7459f9a7f 100644 --- a/core/java/android/view/InputDevice.java +++ b/core/java/android/view/InputDevice.java @@ -572,6 +572,8 @@ public final class InputDevice implements Parcelable { * @return The identifier object for this device * @hide */ + @TestApi + @NonNull public InputDeviceIdentifier getIdentifier() { return mIdentifier; } diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml index d6bbe71586fa0..b36addaa282c8 100644 --- a/core/res/AndroidManifest.xml +++ b/core/res/AndroidManifest.xml @@ -4243,7 +4243,8 @@ + @hide + @TestApi -->