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

Commit 14337f2c authored by Josep del Rio's avatar Josep del Rio
Browse files

Limit parceled InputDevice.MotionRange list

It has been reported that our parceling implementation will limit
the amount of MotionRange objects read from a parcel, but won't
limit them when creating the parcel.

Bug: 220115129
Test: added MotionRange unit tests
Change-Id: I595ffade47d8922342eb1395d35fd78b112bcc90
parent bc6d6365
Loading
Loading
Loading
Loading
+45 −5
Original line number Diff line number Diff line
@@ -555,6 +555,7 @@ public final class InputDevice implements Parcelable {
        private String mKeyboardLanguageTag = null;
        private String mKeyboardLayoutType = null;
        private boolean mSupportsUsi = false;
        private List<MotionRange> 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);
+15 −3
Original line number Diff line number Diff line
@@ -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);