Using ADBKeyboard for injecting text.

This commit is contained in:
Vladimir Chebotarev
2020-09-10 03:09:19 +03:00
committed by Romain Vimont
parent 576814bcec
commit e970b4bda3
8 changed files with 46 additions and 12 deletions

View File

@@ -20,6 +20,7 @@ public class Controller {
private final Device device;
private final DesktopConnection connection;
private final Options options;
private final DeviceMessageSender sender;
private final KeyCharacterMap charMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
@@ -31,9 +32,10 @@ public class Controller {
private boolean keepPowerModeOff;
public Controller(Device device, DesktopConnection connection) {
public Controller(Device device, DesktopConnection connection, Options options) {
this.device = device;
this.connection = connection;
this.options = options;
initPointers();
sender = new DeviceMessageSender(connection);
}
@@ -145,18 +147,28 @@ public class Controller {
}
private boolean injectChar(char c) {
String decomposed = KeyComposition.decompose(c);
char[] chars = decomposed != null ? decomposed.toCharArray() : new char[]{c};
KeyEvent[] events = charMap.getEvents(chars);
if (events == null) {
return false;
}
for (KeyEvent event : events) {
if (!device.injectEvent(event)) {
if (options.useADBKeyboard()) {
// Process latin keys the same way in order to provide same reaction speed.
try {
Process process = Runtime.getRuntime().exec("am broadcast -a ADB_INPUT_CHARS --eia chars " + String.valueOf((int) c));
return process.waitFor() == 0;
} catch (Throwable throwable) {
return false;
}
} else {
String decomposed = KeyComposition.decompose(c);
char[] chars = decomposed != null ? decomposed.toCharArray() : new char[]{c};
KeyEvent[] events = charMap.getEvents(chars);
if (events == null) {
return false;
}
for (KeyEvent event : events) {
if (!device.injectEvent(event)) {
return false;
}
}
return true;
}
return true;
}
private int injectText(String text) {

View File

@@ -17,6 +17,7 @@ public class Options {
private boolean stayAwake;
private String codecOptions;
private String encoderName;
private boolean useADBKeyboard;
public Ln.Level getLogLevel() {
return logLevel;
@@ -129,4 +130,12 @@ public class Options {
public void setEncoderName(String encoderName) {
this.encoderName = encoderName;
}
public boolean useADBKeyboard() {
return useADBKeyboard;
}
public void setUseADBKeyboard(boolean useADBKeyboard) {
this.useADBKeyboard = useADBKeyboard;
}
}

View File

@@ -59,7 +59,7 @@ public final class Server {
options.getEncoderName());
if (options.getControl()) {
final Controller controller = new Controller(device, connection);
final Controller controller = new Controller(device, connection, options);
// asynchronous
startController(controller);
@@ -122,7 +122,7 @@ public final class Server {
"The server version (" + BuildConfig.VERSION_NAME + ") does not match the client " + "(" + clientVersion + ")");
}
final int expectedParameters = 15;
final int expectedParameters = 16;
if (args.length != expectedParameters) {
throw new IllegalArgumentException("Expecting " + expectedParameters + " parameters");
}
@@ -172,6 +172,9 @@ public final class Server {
String encoderName = "-".equals(args[14]) ? null : args[14];
options.setEncoderName(encoderName);
boolean useADBKeyboard = Boolean.parseBoolean(args[15]);
options.setUseADBKeyboard(useADBKeyboard);
return options;
}