Uinput: Use enums for commands

Bug: 291881033
Bug: 294275314
Test: manual, presubmit
Change-Id: I928da2ad53afa91bc9fe7b0dc9cddeb3c918c6a2
Merged-In: I928da2ad53afa91bc9fe7b0dc9cddeb3c918c6a2
This commit is contained in:
Prabir Pradhan
2023-08-02 21:52:30 +00:00
parent 59e9f7fe95
commit 895fe8613c
2 changed files with 52 additions and 34 deletions

View File

@@ -25,6 +25,7 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.IntStream;
import src.com.android.commands.uinput.InputAbsInfo;
@@ -36,11 +37,20 @@ import src.com.android.commands.uinput.InputAbsInfo;
public class Event {
private static final String TAG = "UinputEvent";
public static final String COMMAND_REGISTER = "register";
public static final String COMMAND_DELAY = "delay";
public static final String COMMAND_INJECT = "inject";
private static final int ABS_CNT = 64;
enum Command {
REGISTER("register"),
DELAY("delay"),
INJECT("inject");
final String mCommandName;
Command(String command) {
mCommandName = command;
}
}
// These constants come from "include/uapi/linux/input.h" in the kernel
enum Bus {
USB(0x03), BLUETOOTH(0x05);
@@ -56,7 +66,7 @@ public class Event {
}
private int mId;
private String mCommand;
private Command mCommand;
private String mName;
private int mVid;
private int mPid;
@@ -72,7 +82,7 @@ public class Event {
return mId;
}
public String getCommand() {
public Command getCommand() {
return mCommand;
}
@@ -146,7 +156,14 @@ public class Event {
}
private void setCommand(String command) {
mEvent.mCommand = command;
Objects.requireNonNull(command, "Command must not be null");
for (Command cmd : Command.values()) {
if (cmd.mCommandName.equals(command)) {
mEvent.mCommand = cmd;
return;
}
}
throw new IllegalStateException("Unrecognized command: " + command);
}
public void setName(String name) {
@@ -195,21 +212,23 @@ public class Event {
} else if (mEvent.mCommand == null) {
throw new IllegalStateException("Event does not contain a command");
}
if (COMMAND_REGISTER.equals(mEvent.mCommand)) {
if (mEvent.mConfiguration == null) {
throw new IllegalStateException(
"Device registration is missing configuration");
switch (mEvent.mCommand) {
case REGISTER -> {
if (mEvent.mConfiguration == null) {
throw new IllegalStateException(
"Device registration is missing configuration");
}
}
} else if (COMMAND_DELAY.equals(mEvent.mCommand)) {
if (mEvent.mDuration <= 0) {
throw new IllegalStateException("Delay has missing or invalid duration");
case DELAY -> {
if (mEvent.mDuration <= 0) {
throw new IllegalStateException("Delay has missing or invalid duration");
}
}
} else if (COMMAND_INJECT.equals(mEvent.mCommand)) {
if (mEvent.mInjections == null) {
throw new IllegalStateException("Inject command is missing injection data");
case INJECT -> {
if (mEvent.mInjections == null) {
throw new IllegalStateException("Inject command is missing injection data");
}
}
} else {
throw new IllegalStateException("Unknown command " + mEvent.mCommand);
}
return mEvent;
}

View File

@@ -25,6 +25,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Objects;
/**
* Uinput class encapsulates execution of "uinput" command. It parses the provided input stream
@@ -96,28 +97,26 @@ public class Uinput {
private void process(Event e) {
final int index = mDevices.indexOfKey(e.getId());
if (index >= 0) {
Device d = mDevices.valueAt(index);
if (Event.COMMAND_DELAY.equals(e.getCommand())) {
d.addDelay(e.getDuration());
} else if (Event.COMMAND_INJECT.equals(e.getCommand())) {
d.injectEvent(e.getInjections());
} else {
if (Event.COMMAND_REGISTER.equals(e.getCommand())) {
error("Device id=" + e.getId() + " is already registered. Ignoring event.");
} else {
error("Unknown command \"" + e.getCommand() + "\". Ignoring event.");
}
if (index < 0) {
if (e.getCommand() != Event.Command.REGISTER) {
Log.e(TAG, "Unknown device id specified. Ignoring event.");
return;
}
} else if (Event.COMMAND_REGISTER.equals(e.getCommand())) {
registerDevice(e);
} else {
Log.e(TAG, "Unknown device id specified. Ignoring event.");
return;
}
final Device d = mDevices.valueAt(index);
switch (Objects.requireNonNull(e.getCommand())) {
case REGISTER ->
error("Device id=" + e.getId() + " is already registered. Ignoring event.");
case INJECT -> d.injectEvent(e.getInjections());
case DELAY -> d.addDelay(e.getDuration());
}
}
private void registerDevice(Event e) {
if (!Event.COMMAND_REGISTER.equals(e.getCommand())) {
if (!Event.Command.REGISTER.equals(e.getCommand())) {
throw new IllegalStateException(
"Tried to send command \"" + e.getCommand() + "\" to an unregistered device!");
}