1 /* 2 * Copyright 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.view.cts.util; 18 19 import android.view.InputDevice; 20 21 import java.util.function.BiConsumer; 22 import java.util.function.Consumer; 23 24 /** Static iterators over {@link InputDevice}s. */ 25 public class InputDeviceIterators { 26 /** Allows running a logic on some invalid InputDevice IDs. */ iteratorOverInvalidDeviceIds(Consumer<Integer> invalidDeviceIdConsumer)27 public static void iteratorOverInvalidDeviceIds(Consumer<Integer> invalidDeviceIdConsumer) { 28 // "50" randomly chosen to cover some array of integers. 29 for (int deviceId = -50; deviceId < 50; deviceId++) { 30 InputDevice device = InputDevice.getDevice(deviceId); 31 if (device == null) { 32 // No InputDevice found, so the ID is invalid. 33 invalidDeviceIdConsumer.accept(deviceId); 34 } 35 } 36 } 37 38 /** 39 * Allows running a logic on every motion range across every InputDevice. 40 * The motion range is provided to the consumer along with the InputDevice ID corresponding to 41 * the motion range. 42 */ iteratorOverEveryInputDeviceMotionRange( BiConsumer<Integer, InputDevice.MotionRange> motionRangeConsumer)43 public static void iteratorOverEveryInputDeviceMotionRange( 44 BiConsumer<Integer, InputDevice.MotionRange> motionRangeConsumer) { 45 iteratorOverEveryValidDeviceId((deviceId) -> { 46 InputDevice device = InputDevice.getDevice(deviceId); 47 for (InputDevice.MotionRange motionRange : device.getMotionRanges()) { 48 motionRangeConsumer.accept(deviceId, motionRange); 49 } 50 }); 51 } 52 53 /** Allows running a logic on every valid input device ID. */ iteratorOverEveryValidDeviceId(Consumer<Integer> deviceIdConsumer)54 public static void iteratorOverEveryValidDeviceId(Consumer<Integer> deviceIdConsumer) { 55 for (int deviceId : InputDevice.getDeviceIds()) { 56 deviceIdConsumer.accept(deviceId); 57 } 58 } 59 } 60