diff options
| author | Philip Junker <philipjunker@google.com> | 2022-01-24 18:39:18 +0000 |
|---|---|---|
| committer | Android (Google) Code Review <android-gerrit@google.com> | 2022-01-24 18:39:18 +0000 |
| commit | 83704e06b335f07ecb9e337321ce2ee114023448 (patch) | |
| tree | 4a0cdabce987aad8944f83521e1b6ab1a821fcea /core/java | |
| parent | 7e6787795e9d9c054b9d661b08a3ca7e14296bdd (diff) | |
| parent | 704afe20778edefca88c46b393923cac540fa5ae (diff) | |
Merge changes from topic "keyLayoutAPI"
* changes:
Add TestAPIs to test InputDevice#getKeyCodeForKeyLocation().
Add API to get KeyCode produced by physical key location.
Diffstat (limited to 'core/java')
| -rw-r--r-- | core/java/android/hardware/input/IInputManager.aidl | 4 | ||||
| -rw-r--r-- | core/java/android/hardware/input/InputDeviceIdentifier.java | 8 | ||||
| -rw-r--r-- | core/java/android/hardware/input/InputManager.java | 106 | ||||
| -rw-r--r-- | core/java/android/view/InputDevice.java | 17 |
4 files changed, 107 insertions, 28 deletions
diff --git a/core/java/android/hardware/input/IInputManager.aidl b/core/java/android/hardware/input/IInputManager.aidl index 2ac194b67192..0304815ef8fe 100644 --- a/core/java/android/hardware/input/IInputManager.aidl +++ b/core/java/android/hardware/input/IInputManager.aidl @@ -50,6 +50,10 @@ interface IInputManager { // Reports whether the hardware supports the given keys; returns true if successful boolean hasKeys(int deviceId, int sourceMask, in int[] keyCodes, out boolean[] keyExists); + // Returns the keyCode produced when pressing the key at the specified location, given the + // active keyboard layout. + int getKeyCodeForKeyLocation(int deviceId, in int locationKeyCode); + // Temporarily changes the pointer speed. void tryPointerSpeed(int speed); diff --git a/core/java/android/hardware/input/InputDeviceIdentifier.java b/core/java/android/hardware/input/InputDeviceIdentifier.java index c673e7ab7c53..a5b9a2a4da76 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 ef349a96ee17..cbc837393b6b 100644 --- a/core/java/android/hardware/input/InputManager.java +++ b/core/java/android/hardware/input/InputManager.java @@ -58,6 +58,7 @@ import android.util.SparseArray; import android.view.InputDevice; import android.view.InputEvent; import android.view.InputMonitor; +import android.view.KeyEvent; import android.view.MotionEvent; import android.view.PointerIcon; import android.view.VerifiedInputEvent; @@ -637,6 +638,30 @@ public final class InputManager { } /** + * Returns the descriptors of all supported keyboard layouts appropriate for the specified + * input device. + * <p> + * 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. + * </p> + * + * @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<String> getKeyboardLayoutDescriptorsForInputDevice(@NonNull InputDevice device) { + KeyboardLayout[] layouts = getKeyboardLayoutsForInputDevice(device.getIdentifier()); + List<String> 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. * <p> @@ -650,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) { @@ -680,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) { @@ -697,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. * <p> - * 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. * </p> * * @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"); } @@ -727,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) { @@ -749,15 +779,16 @@ public final class InputManager { /** * Adds the keyboard layout descriptor for the specified input device. * <p> - * 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. * </p> * * @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) { @@ -777,17 +808,19 @@ public final class InputManager { /** * Removes the keyboard layout descriptor for the specified input device. * <p> - * 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. * </p> * * @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"); } @@ -1044,6 +1077,27 @@ public final class InputManager { return ret; } + /** + * Gets the key code produced by the specified location on a US keyboard layout. + * Key code as defined in {@link android.view.KeyEvent}. + * This API is only functional for devices with {@link InputDevice#SOURCE_KEYBOARD} available + * which can alter their key mapping using country specific keyboard layouts. + * + * @param deviceId The input device id. + * @param locationKeyCode The location of a key on a US keyboard layout. + * @return The key code produced when pressing the key at the specified location, given the + * active keyboard layout. Returns {@link KeyEvent#KEYCODE_UNKNOWN} if the requested + * mapping could not be determined, or if an error occurred. + * @hide + */ + public int getKeyCodeForKeyLocation(int deviceId, int locationKeyCode) { + try { + return mIm.getKeyCodeForKeyLocation(deviceId, locationKeyCode); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + /** * Injects an input event into the event system on behalf of an application. diff --git a/core/java/android/view/InputDevice.java b/core/java/android/view/InputDevice.java index 4f1354d7eee6..188d7459f9a7 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; } @@ -735,6 +737,21 @@ public final class InputDevice implements Parcelable { } /** + * Gets the key code produced by the specified location on a US keyboard layout. + * Key code as defined in {@link android.view.KeyEvent}. + * This API is only functional for devices with {@link InputDevice#SOURCE_KEYBOARD} available + * which can alter their key mapping using country specific keyboard layouts. + * + * @param locationKeyCode The location of a key on a US keyboard layout. + * @return The key code produced when pressing the key at the specified location, given the + * active keyboard layout. Returns {@link KeyEvent#KEYCODE_UNKNOWN} if the requested + * mapping could not be determined, or if an error occurred. + */ + public int getKeyCodeForKeyLocation(int locationKeyCode) { + return InputManager.getInstance().getKeyCodeForKeyLocation(mId, locationKeyCode); + } + + /** * Gets information about the range of values for a particular {@link MotionEvent} axis. * If the device supports multiple sources, the same axis may have different meanings * for each source. Returns information about the first axis found for any source. |
