Skip to content

Commit

Permalink
fix: keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhouYixun committed Nov 18, 2022
1 parent f6cb237 commit 2a3c5e1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 183 deletions.
Binary file modified plugins/sonic-android-apk.apk
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ public class AndroidStepHandler {
private int pocoPort = 0;
private int targetPort = 0;

private Thread keyboardThread = null;
private OutputStream keyboardOutputStream = null;

public LogUtil getLog() {
return log;
}
Expand Down Expand Up @@ -164,7 +161,6 @@ public void switchWindowMode(HandleDes handleDes, boolean isMulti) throws SonicR
*/
public void closeAndroidDriver() {
try {
stopKeyboard();
if (chromeDriver != null) {
chromeDriver.quit();
}
Expand Down Expand Up @@ -1620,90 +1616,18 @@ public void sendKeyForce(HandleDes handleDes, String text) {
text = TextHandler.replaceTrans(text, globalParams);
handleDes.setStepDes("Sonic输入法输入文本");
handleDes.setDetail("输入" + text);
startKeyboard();
if (keyboardOutputStream != null) {
try {
keyboardOutputStream.write(text.getBytes(StandardCharsets.UTF_8));
keyboardOutputStream.flush();
} catch (IOException e) {
handleDes.setE(e);
}
String currentIme = AndroidDeviceBridgeTool.executeCommand(iDevice, "settings get secure default_input_method");
if (!currentIme.contains("org.cloud.sonic.android/.keyboard.SonicKeyboard")) {
AndroidDeviceBridgeTool.executeCommand(iDevice, "ime enable org.cloud.sonic.android/.keyboard.SonicKeyboard");
AndroidDeviceBridgeTool.executeCommand(iDevice, "ime set org.cloud.sonic.android/.keyboard.SonicKeyboard");
}
AndroidDeviceBridgeTool.executeCommand(iDevice, "am broadcast -a SONIC_KEYBOARD --es msg \"" + text + "\"");
}

public void startKeyboard() {
if (keyboardThread != null && keyboardThread.isAlive()) {
return;
}
AndroidDeviceBridgeTool.executeCommand(iDevice, "ime enable org.cloud.sonic.android/.keyboard.SonicKeyboard");
AndroidDeviceBridgeTool.executeCommand(iDevice, "ime set org.cloud.sonic.android/.keyboard.SonicKeyboard");
Thread keyboard = new Thread(() -> {
int socketPort = PortTool.getPort();
AndroidDeviceBridgeTool.forward(iDevice, socketPort, 2335);
Socket keyboardSocket = null;
try {
keyboardSocket = new Socket("localhost", socketPort);
keyboardOutputStream = keyboardSocket.getOutputStream();
while (keyboardSocket.isConnected() && !Thread.interrupted()) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
break;
}
}
} catch (IOException e) {
} finally {
if (keyboardOutputStream != null) {
try {
keyboardOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (keyboardSocket != null) {
try {
keyboardSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
AndroidDeviceBridgeTool.removeForward(iDevice, socketPort, 2335);
keyboardOutputStream = null;
});
keyboard.start();
int w = 0;
while (keyboardOutputStream == null) {
if (w > 10) {
break;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
w++;
}
keyboardThread = keyboard;
}

private void stopKeyboard() {
if (keyboardThread != null) {
keyboardThread.interrupt();
int wait = 0;
while (!keyboardThread.isInterrupted()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
wait++;
if (wait >= 3) {
break;
}
}
}
keyboardThread = null;
private void closeKeyboard(HandleDes handleDes) {
handleDes.setStepDes("关闭Sonic输入法");
handleDes.setDetail("");
AndroidDeviceBridgeTool.executeCommand(iDevice, "ime disable org.cloud.sonic.android/.keyboard.SonicKeyboard");
}

private int holdTime = 0;
Expand Down Expand Up @@ -1967,6 +1891,9 @@ public void runStep(JSONObject stepJSON, HandleDes handleDes) throws Throwable {
case "switchWindowMode":
switchWindowMode(handleDes, step.getBoolean("content"));
break;
case "closeKeyboard":
closeKeyboard(handleDes);
break;
}
switchType(step, handleDes);
}
Expand Down
115 changes: 18 additions & 97 deletions src/main/java/org/cloud/sonic/agent/websockets/AndroidWSServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ public class AndroidWSServer implements IAndroidWSServer {
@Value("${sonic.agent.port}")
private int port;
private Map<Session, OutputStream> outputMap = new ConcurrentHashMap<>();
private Map<Session, OutputStream> outputSendMap = new ConcurrentHashMap<>();
private Map<Session, Thread> keyboardThreadMap = new ConcurrentHashMap<>();
private List<Session> NotStopSession = new ArrayList<>();
@Autowired
private AgentManagerTool agentManagerTool;
Expand Down Expand Up @@ -261,6 +259,12 @@ public boolean isCancelled() {
AndroidSupplyTool.startShare(udId, session);

openDriver(iDevice, session);

String currentIme = AndroidDeviceBridgeTool.executeCommand(iDevice, "settings get secure default_input_method");
if (!currentIme.contains("org.cloud.sonic.android/.keyboard.SonicKeyboard")) {
AndroidDeviceBridgeTool.executeCommand(iDevice, "ime enable org.cloud.sonic.android/.keyboard.SonicKeyboard");
AndroidDeviceBridgeTool.executeCommand(iDevice, "ime set org.cloud.sonic.android/.keyboard.SonicKeyboard");
}
}

@OnClose
Expand Down Expand Up @@ -289,6 +293,17 @@ public void onMessage(String message, Session session) {
log.info("{} send: {}", session.getId(), msg);
IDevice iDevice = udIdMap.get(session);
switch (msg.getString("type")) {
case "startKeyboard": {
String currentIme = AndroidDeviceBridgeTool.executeCommand(iDevice, "settings get secure default_input_method");
if (!currentIme.contains("org.cloud.sonic.android/.keyboard.SonicKeyboard")) {
AndroidDeviceBridgeTool.executeCommand(iDevice, "ime enable org.cloud.sonic.android/.keyboard.SonicKeyboard");
AndroidDeviceBridgeTool.executeCommand(iDevice, "ime set org.cloud.sonic.android/.keyboard.SonicKeyboard");
}
break;
}
case "stopKeyboard":
AndroidDeviceBridgeTool.executeCommand(iDevice, "ime disable org.cloud.sonic.android/.keyboard.SonicKeyboard");
break;
case "clearProxy":
AndroidDeviceBridgeTool.clearProxy(iDevice);
break;
Expand Down Expand Up @@ -342,14 +357,7 @@ public void onMessage(String message, Session session) {
AndroidDeviceBridgeTool.pushToCamera(iDevice, msg.getString("url"));
break;
case "text":
if (outputSendMap.get(session) != null) {
try {
outputSendMap.get(session).write(msg.getString("detail").getBytes(StandardCharsets.UTF_8));
outputSendMap.get(session).flush();
} catch (IOException e) {
e.printStackTrace();
}
}
AndroidDeviceBridgeTool.executeCommand(iDevice, "am broadcast -a SONIC_KEYBOARD --es msg \"" + msg.getString("detail") + "\"");
break;
case "touch":
OutputStream outputStream = outputMap.get(session);
Expand Down Expand Up @@ -565,9 +573,6 @@ private void openDriver(IDevice iDevice, Session session) {
} finally {
result.put("msg", "openDriver");
BytesTool.sendText(session, result.toJSONString());
AndroidDeviceBridgeTool.executeCommand(iDevice, "ime enable org.cloud.sonic.android/.keyboard.SonicKeyboard");
AndroidDeviceBridgeTool.executeCommand(iDevice, "ime set org.cloud.sonic.android/.keyboard.SonicKeyboard");
startKeyboard(iDevice, session);
}
});
}
Expand All @@ -594,7 +599,6 @@ private void exit(Session session) {
AndroidAPKMap.getMap().remove(iDevice.getSerialNumber());
}
outputMap.remove(session);
stopKeyboard(session);
removeUdIdMapAndSet(session);
WebSocketSessionMap.removeSession(session);
try {
Expand All @@ -604,87 +608,4 @@ private void exit(Session session) {
}
log.info("{} : quit.", session.getId());
}

public void startKeyboard(IDevice iDevice, Session session) {
if (keyboardThreadMap.get(session) != null && keyboardThreadMap.get(session).isAlive()) {
return;
}
try {
Thread.sleep(2500);
} catch (InterruptedException e) {
log.info(e.getMessage());
}
Thread keyboard = new Thread(() -> {
int socketPort = PortTool.getPort();
AndroidDeviceBridgeTool.forward(iDevice, socketPort, 2335);
Socket keyboardSocket = null;
OutputStream outputStream = null;
try {
keyboardSocket = new Socket("localhost", socketPort);
outputStream = keyboardSocket.getOutputStream();
outputSendMap.put(session, outputStream);
while (keyboardSocket.isConnected() && !Thread.interrupted()) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
break;
}
}
} catch (IOException e) {
log.info("error: {}", e.getMessage());
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (keyboardSocket != null) {
try {
keyboardSocket.close();
log.info("keyboard socket closed.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
AndroidDeviceBridgeTool.removeForward(iDevice, socketPort, 2335);
outputSendMap.remove(session);
log.info("keyboard done.");
});
keyboard.start();
int w = 0;
while (outputSendMap.get(session) == null) {
if (w > 10) {
break;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
w++;
}
keyboardThreadMap.put(session, keyboard);
}

private void stopKeyboard(Session session) {
if (keyboardThreadMap.get(session) != null) {
keyboardThreadMap.get(session).interrupt();
int wait = 0;
while (!keyboardThreadMap.get(session).isInterrupted()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
wait++;
if (wait >= 3) {
break;
}
}
}
keyboardThreadMap.remove(session);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ spring:
active: @project.artifactId@

sonic:
saa: 2.0.2
saa: 2.0.3
sgm: 1.3.3
sib: 1.2.5
sas: 0.0.2
Expand Down

0 comments on commit 2a3c5e1

Please sign in to comment.