From 10dd6ed66d817b8c13b2710871b8c72af417ede7 Mon Sep 17 00:00:00 2001 From: happyplus <894860970@qq.com> Date: Tue, 21 Feb 2023 17:45:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3Auto.js=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E8=AF=B4=E6=98=8E=EF=BC=9B=E6=89=93=E5=8D=B0shell?= =?UTF-8?q?=E6=97=A5=E5=BF=97=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.MD | 2 +- .../main/java/com/happyplus/adbshell/ADB.java | 40 +++++++++++++------ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/README.MD b/README.MD index b92615d..4843f2e 100644 --- a/README.MD +++ b/README.MD @@ -22,5 +22,5 @@ curl --location --request POST 'http://localhost:8999' \ ```shell # Auto.js -http.postJson('http://localhost:8999', 'input tap 500 500') +http.post('http://localhost:8999', 'input tap 540 277', { contentType: 'text/plain' }) ``` \ No newline at end of file diff --git a/app/src/main/java/com/happyplus/adbshell/ADB.java b/app/src/main/java/com/happyplus/adbshell/ADB.java index 32c65a9..58d789c 100644 --- a/app/src/main/java/com/happyplus/adbshell/ADB.java +++ b/app/src/main/java/com/happyplus/adbshell/ADB.java @@ -55,26 +55,40 @@ public ADB(File dataDir) { public String ExecCommand(String command) { try { - AdbStream stream = connection.open("shell:" + command); - while (!stream.isClosed()) try { - String res = new String(stream.read(), "US-ASCII"); - Log.e("HAPPYPLUS", res); - } catch (UnsupportedEncodingException e) { + final AdbStream adbStream; + try { + adbStream = connection.open("shell:"); + } catch (InterruptedException | IOException e) { e.printStackTrace(); return e.getMessage(); - } catch (InterruptedException e) { - e.printStackTrace(); - return e.getMessage(); - } catch (IOException e) { + } + // Start the receiving thread + new Thread(new Runnable() { + @Override + public void run() { + while (!adbStream.isClosed()) + try { + // Print each thing we read from the shell stream + Log.d("ADB OUTPUT", new String(adbStream.read(), "US-ASCII")); + } catch (InterruptedException | IOException e) { + e.printStackTrace(); + return; + } + } + }).start(); + try { + adbStream.write(command + '\n'); + } catch (IOException | InterruptedException e) { e.printStackTrace(); + Log.e(TAG, "ExecCommand Error " + e.getMessage()); return e.getMessage(); } } catch (Exception e) { String res = e.toString(); - Log.e("HAPPYPULS", "SHELL FAILED: " + e.toString()); - return res; + Log.e("HAPPYPULS", "SHELL FAILED: " + e); + return res; } - return "成功"; + return "ok"; } public Boolean adbConnect(String host, int port) { @@ -88,7 +102,7 @@ public Boolean adbConnect(String host, int port) { } catch (IOException | InterruptedException e) { String res = e.toString(); Log.e("HAPPYPLUS", res); - return false; + return false; } }