diff --git a/plugins/sonic-android-apk.apk b/plugins/sonic-android-apk.apk index 94c9d6e8..ebfedd88 100755 Binary files a/plugins/sonic-android-apk.apk and b/plugins/sonic-android-apk.apk differ diff --git a/src/main/java/org/cloud/sonic/agent/automation/AndroidStepHandler.java b/src/main/java/org/cloud/sonic/agent/automation/AndroidStepHandler.java index 479f4ee7..c36630ca 100755 --- a/src/main/java/org/cloud/sonic/agent/automation/AndroidStepHandler.java +++ b/src/main/java/org/cloud/sonic/agent/automation/AndroidStepHandler.java @@ -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; } @@ -164,7 +161,6 @@ public void switchWindowMode(HandleDes handleDes, boolean isMulti) throws SonicR */ public void closeAndroidDriver() { try { - stopKeyboard(); if (chromeDriver != null) { chromeDriver.quit(); } @@ -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; @@ -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); } diff --git a/src/main/java/org/cloud/sonic/agent/websockets/AndroidWSServer.java b/src/main/java/org/cloud/sonic/agent/websockets/AndroidWSServer.java index 6b25df06..978c4c4f 100755 --- a/src/main/java/org/cloud/sonic/agent/websockets/AndroidWSServer.java +++ b/src/main/java/org/cloud/sonic/agent/websockets/AndroidWSServer.java @@ -68,8 +68,6 @@ public class AndroidWSServer implements IAndroidWSServer { @Value("${sonic.agent.port}") private int port; private Map outputMap = new ConcurrentHashMap<>(); - private Map outputSendMap = new ConcurrentHashMap<>(); - private Map keyboardThreadMap = new ConcurrentHashMap<>(); private List NotStopSession = new ArrayList<>(); @Autowired private AgentManagerTool agentManagerTool; @@ -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 @@ -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; @@ -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); @@ -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); } }); } @@ -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 { @@ -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); - } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index d487694c..595bb72f 100755 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -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