diff --git a/core/java/android/view/InputDevice.java b/core/java/android/view/InputDevice.java index 5a9a2520d839f..8683cc2a80097 100644 --- a/core/java/android/view/InputDevice.java +++ b/core/java/android/view/InputDevice.java @@ -555,6 +555,7 @@ public final class InputDevice implements Parcelable { private String mKeyboardLanguageTag = null; private String mKeyboardLayoutType = null; private boolean mSupportsUsi = false; + private List mMotionRanges = new ArrayList<>(); /** @see InputDevice#getId() */ public Builder setId(int id) { @@ -670,12 +671,50 @@ public final class InputDevice implements Parcelable { return this; } + /** @see InputDevice#getMotionRanges() */ + public Builder addMotionRange(int axis, int source, + float min, float max, float flat, float fuzz, float resolution) { + mMotionRanges.add(new MotionRange(axis, source, min, max, flat, fuzz, resolution)); + return this; + } + /** Build {@link InputDevice}. */ public InputDevice build() { - return new InputDevice(mId, mGeneration, mControllerNumber, mName, mVendorId, - mProductId, mDescriptor, mIsExternal, mSources, mKeyboardType, mKeyCharacterMap, - mKeyboardLanguageTag, mKeyboardLayoutType, mHasVibrator, mHasMicrophone, - mHasButtonUnderPad, mHasSensor, mHasBattery, mSupportsUsi); + InputDevice device = new InputDevice( + mId, + mGeneration, + mControllerNumber, + mName, + mVendorId, + mProductId, + mDescriptor, + mIsExternal, + mSources, + mKeyboardType, + mKeyCharacterMap, + mKeyboardLanguageTag, + mKeyboardLayoutType, + mHasVibrator, + mHasMicrophone, + mHasButtonUnderPad, + mHasSensor, + mHasBattery, + mSupportsUsi); + + final int numRanges = mMotionRanges.size(); + for (int i = 0; i < numRanges; i++) { + final MotionRange range = mMotionRanges.get(i); + device.addMotionRange( + range.getAxis(), + range.getSource(), + range.getMin(), + range.getMax(), + range.getFlat(), + range.getFuzz(), + range.getResolution()); + } + + return device; } } @@ -1378,7 +1417,8 @@ public final class InputDevice implements Parcelable { out.writeInt(mHasBattery ? 1 : 0); out.writeInt(mSupportsUsi ? 1 : 0); - final int numRanges = mMotionRanges.size(); + int numRanges = mMotionRanges.size(); + numRanges = numRanges > MAX_RANGES ? MAX_RANGES : numRanges; out.writeInt(numRanges); for (int i = 0; i < numRanges; i++) { MotionRange range = mMotionRanges.get(i); diff --git a/tests/Input/src/com/android/test/input/InputDeviceTest.java b/tests/Input/src/com/android/test/input/InputDeviceTest.java index 797e818285f96..ddcc8112d6ed7 100644 --- a/tests/Input/src/com/android/test/input/InputDeviceTest.java +++ b/tests/Input/src/com/android/test/input/InputDeviceTest.java @@ -69,7 +69,7 @@ public class InputDeviceTest { } private void assertInputDeviceParcelUnparcel(KeyCharacterMap keyCharacterMap) { - final InputDevice device = new InputDevice.Builder() + final InputDevice.Builder deviceBuilder = new InputDevice.Builder() .setId(DEVICE_ID) .setGeneration(42) .setControllerNumber(43) @@ -88,8 +88,20 @@ public class InputDeviceTest { .setHasBattery(true) .setKeyboardLanguageTag("en-US") .setKeyboardLayoutType("qwerty") - .setSupportsUsi(true) - .build(); + .setSupportsUsi(true); + + for (int i = 0; i < 30; i++) { + deviceBuilder.addMotionRange( + MotionEvent.AXIS_GENERIC_1, + InputDevice.SOURCE_UNKNOWN, + i, + i + 1, + i + 2, + i + 3, + i + 4); + } + + final InputDevice device = deviceBuilder.build(); Parcel parcel = Parcel.obtain(); device.writeToParcel(parcel, 0);