+ * 来自网络~~ + */ + +public class ProcessShell extends AbstractShell implements AutoCloseable { + + private static final String TAG = "ProcessShell"; + + private Process mProcess; + private DataOutputStream mCommandOutputStream; + private BufferedReader mSucceedReader; + private BufferedReader mErrorReader; + + private StringBuilder mSucceedOutput = new StringBuilder(); + private StringBuilder mErrorOutput = new StringBuilder(); + + public ProcessShell() { + + } + + public ProcessShell(boolean root) { + super(root); + } + + @Override + protected void init(String initialCommand) { + try { + mProcess = new ProcessBuilder(initialCommand).redirectErrorStream(true).start(); + mCommandOutputStream = new DataOutputStream(mProcess.getOutputStream()); + mSucceedReader = new BufferedReader(new InputStreamReader(mProcess.getInputStream())); + mErrorReader = new BufferedReader(new InputStreamReader(mProcess.getErrorStream())); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + @Override + public void exec(String command) { + try { + mCommandOutputStream.writeBytes(command); + if (!command.endsWith(COMMAND_LINE_END)) { + mCommandOutputStream.writeBytes(COMMAND_LINE_END); + } + mCommandOutputStream.flush(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public void exit() { + if(mProcess != null) { + mProcess.destroy(); + mProcess = null; + } + if(mSucceedReader != null){ + try{ + mSucceedReader.close(); + }catch (IOException ignored){ + + } + mSucceedReader = null; + } + if(mErrorReader != null){ + try{ + mErrorReader.close(); + }catch (IOException ignored){ + + } + mErrorReader = null; + } + + } + + @Override + public void close() { + exit(); + } + + @Override + public void exitAndWaitFor() { + exec(COMMAND_EXIT); + waitFor(); + exit(); + } + + public int waitFor() { + try { + return mProcess.waitFor(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + + public ProcessShell readAll() { + return readSucceedOutput().readErrorOutput(); + } + + public ProcessShell readSucceedOutput() { + try { + while (mSucceedReader.ready()) { + String line = mSucceedReader.readLine(); + mSucceedOutput.append(line).append("\n"); + } + } catch (IOException e) { + throw new UncheckedIOException(e); + } + return this; + } + + public ProcessShell readErrorOutput() { + String line; + try { + while ((line = mErrorReader.readLine()) != null) { + mErrorOutput.append(line).append("\n"); + } + } catch (IOException e) { + throw new UncheckedIOException(e); + } + return this; + } + + public StringBuilder getSucceedOutput() { + return mSucceedOutput; + } + + public StringBuilder getErrorOutput() { + return mErrorOutput; + } + + public Process getProcess() { + return mProcess; + } + + public BufferedReader getSucceedReader() { + return mSucceedReader; + } + + public BufferedReader getErrorReader() { + return mErrorReader; + } + + public static Result exec(String command, boolean isRoot) { + String[] commands = command.split("\n"); + return exec(commands, isRoot); + } + + public static Result exec(String[] commands, boolean isRoot) { + try(ProcessShell shell = new ProcessShell(isRoot)){ + for(String command : commands){ + shell.exec(command); + } + shell.exec(COMMAND_EXIT); + Result result = new Result(); + result.code = shell.waitFor(); + shell.readAll(); + result.error = shell.getErrorOutput().toString(); + result.result = shell.getSucceedOutput().toString(); + shell.exit(); + return result; + } + } + +} \ No newline at end of file diff --git a/autojs/src/main/java/com/stardust/autojs/runtime/api/SlowShell.java b/autojs/src/main/java/com/stardust/autojs/runtime/api/SlowShell.java deleted file mode 100644 index eb113030..00000000 --- a/autojs/src/main/java/com/stardust/autojs/runtime/api/SlowShell.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.stardust.autojs.runtime.api; - -import com.stardust.util.Shell; - -/** - * Created by Stardust on 2017/3/7. - */ - -public class SlowShell extends AbstractShell { - - private Shell mShell; - - public SlowShell(boolean root) { - super(root); - } - - @Override - protected void init(String initialCommand) { - mShell = new Shell(initialCommand); - } - - @Override - public void exec(String command) { - mShell.execute(command); - } - - -} diff --git a/autojs/src/main/java/com/stardust/autojs/runtime/simple_action/SimpleActionAutomator.java b/autojs/src/main/java/com/stardust/autojs/runtime/simple_action/SimpleActionAutomator.java index a8a847d3..632a54d5 100644 --- a/autojs/src/main/java/com/stardust/autojs/runtime/simple_action/SimpleActionAutomator.java +++ b/autojs/src/main/java/com/stardust/autojs/runtime/simple_action/SimpleActionAutomator.java @@ -8,6 +8,7 @@ import android.util.Log; import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityNodeInfo; +import com.stardust.autojs.runtime.AbstractScriptRuntime; import com.stardust.autojs.runtime.AccessibilityBridge; import com.stardust.autojs.runtime.JavascriptInterface; import com.stardust.autojs.runtime.ScriptRuntime; @@ -45,9 +46,9 @@ public class SimpleActionAutomator { } private AccessibilityBridge mAccessibilityBridge; - private ScriptRuntime mScriptRuntime; + private AbstractScriptRuntime mScriptRuntime; - public SimpleActionAutomator(AccessibilityBridge accessibilityBridge, ScriptRuntime scriptRuntime) { + public SimpleActionAutomator(AccessibilityBridge accessibilityBridge, AbstractScriptRuntime scriptRuntime) { mAccessibilityBridge = accessibilityBridge; mScriptRuntime = scriptRuntime; } diff --git a/autojs/src/main/java/com/stardust/autojs/script/MultiScriptSource.java b/autojs/src/main/java/com/stardust/autojs/script/MultiScriptSource.java deleted file mode 100644 index 9903cb33..00000000 --- a/autojs/src/main/java/com/stardust/autojs/script/MultiScriptSource.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.stardust.autojs.script; - -/** - * Created by Stardust on 2017/4/2. - */ - -public class MultiScriptSource extends ScriptSource { - - private String mScript; - private FileScriptSource mFileScriptSource; - - - public MultiScriptSource(StringScriptSource stringScriptSource, FileScriptSource fileScriptSource) { - super(fileScriptSource.getName()); - mFileScriptSource = fileScriptSource; - StringBuilder stringBuilder = new StringBuilder(); - if (stringScriptSource.getScript() != null) - stringBuilder.append(stringScriptSource.getScript()).append("\n"); - stringBuilder.append(fileScriptSource.getScript()); - mScript = stringBuilder.toString(); - } - - @Override - public String getScript() { - return mScript; - } - - @Override - public String toString() { - return mFileScriptSource.toString(); - } - -} diff --git a/autojs/src/main/java/com/stardust/autojs/script/ScriptSourceWithInit.java b/autojs/src/main/java/com/stardust/autojs/script/ScriptSourceWithInit.java new file mode 100644 index 00000000..c85257f1 --- /dev/null +++ b/autojs/src/main/java/com/stardust/autojs/script/ScriptSourceWithInit.java @@ -0,0 +1,33 @@ +package com.stardust.autojs.script; + +/** + * Created by Stardust on 2017/4/2. + */ + +public class ScriptSourceWithInit extends ScriptSource { + + private String mScript; + private ScriptSource mMainScriptSource; + + + public ScriptSourceWithInit(ScriptSource initScriptSource, ScriptSource mainScriptSource) { + super(mainScriptSource.getName()); + mMainScriptSource = mainScriptSource; + StringBuilder stringBuilder = new StringBuilder(); + if (initScriptSource.getScript() != null) + stringBuilder.append(initScriptSource.getScript()).append("\n"); + stringBuilder.append(mainScriptSource.getScript()); + mScript = stringBuilder.toString(); + } + + @Override + public String getScript() { + return mScript; + } + + @Override + public String toString() { + return mMainScriptSource.toString(); + } + +} diff --git a/automator/build.gradle b/automator/build.gradle index b98a5471..4c1f3072 100644 --- a/automator/build.gradle +++ b/automator/build.gradle @@ -22,11 +22,9 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:appcompat-v7:25.2.0' testCompile 'junit:junit:4.12' compile project(path: ':common') } diff --git a/common/build.gradle b/common/build.gradle index 5e4b8d4b..b8009e44 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -22,7 +22,6 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) diff --git a/common/src/main/java/com/stardust/util/AssetsCache.java b/common/src/main/java/com/stardust/util/AssetsCache.java index 5458a7d7..06bbc269 100644 --- a/common/src/main/java/com/stardust/util/AssetsCache.java +++ b/common/src/main/java/com/stardust/util/AssetsCache.java @@ -2,15 +2,9 @@ package com.stardust.util; import android.app.Activity; import android.content.res.AssetManager; -import android.support.v4.app.FragmentActivity; - import com.stardust.pio.PFile; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - /** * Created by Stardust on 2017/3/14. */ diff --git a/common/src/main/java/com/stardust/util/Shell.java b/common/src/main/java/com/stardust/util/Shell.java deleted file mode 100644 index b51dd7e4..00000000 --- a/common/src/main/java/com/stardust/util/Shell.java +++ /dev/null @@ -1,264 +0,0 @@ -package com.stardust.util; - -import android.util.Log; - - -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; - -/** - * Created by Stardust on 2017/1/20. - *
- * 来自网络~~ - */ - -public class Shell { - - - private static final String TAG = "Shell"; - - private final static String COMMAND_SU = "su"; - private final static String COMMAND_SH = "sh"; - private final static String COMMAND_EXIT = "exit\n"; - private final static String COMMAND_LINE_END = "\n"; - - private Process mProcess; - private DataOutputStream mCommandOutputStream; - private BufferedReader mSucceedReader; - private BufferedReader mErrorReader; - - private StringBuilder mSucceedOutput = new StringBuilder(); - private StringBuilder mErrorOutput = new StringBuilder(); - - public Shell() { - this(false); - } - - public Shell(boolean root) { - this(root ? COMMAND_SU : COMMAND_SH); - } - - public Shell(String initialCommand) { - try { - mProcess = new ProcessBuilder(initialCommand).redirectErrorStream(true).start(); - mCommandOutputStream = new DataOutputStream(mProcess.getOutputStream()); - mSucceedReader = new BufferedReader(new InputStreamReader(mProcess.getInputStream())); - mErrorReader = new BufferedReader(new InputStreamReader(mProcess.getErrorStream())); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public Shell execute(String command) { - try { - mCommandOutputStream.writeBytes(command); - if (!command.endsWith(COMMAND_LINE_END)) { - mCommandOutputStream.writeBytes(COMMAND_LINE_END); - } - mCommandOutputStream.flush(); - } catch (IOException e) { - throw new RuntimeException(e); - } - return this; - } - - public Shell exitAndWaitFor() { - exit(); - try { - mProcess.waitFor(); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - return this; - } - - public Shell readAll() { - return readSucceedOutput().readErrorOutput(); - } - - public Shell readSucceedOutput() { - try { - while (mSucceedReader.ready()) { - String line = mSucceedReader.readLine(); - mSucceedOutput.append(line).append("\n"); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - return this; - } - - public Shell readErrorOutput() { - String line; - try { - while ((line = mErrorReader.readLine()) != null) { - mErrorOutput.append(line).append("\n"); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - return this; - } - - public StringBuilder getSucceedOutput() { - return mSucceedOutput; - } - - public StringBuilder getErrorOutput() { - return mErrorOutput; - } - - public Shell exit() { - execute(COMMAND_EXIT); - return this; - } - - public Shell destroy() { - mProcess.destroy(); - return this; - } - - public Process getProcess() { - return mProcess; - } - - public BufferedReader getSucceedReader() { - return mSucceedReader; - } - - public BufferedReader getErrorReader() { - return mErrorReader; - } - - public int waitFor() { - try { - return mProcess.waitFor(); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } - - /** - * Command执行结果 - * - * @author Mountain - */ - public static class CommandResult { - public int code = -1; - public String error; - public String result; - - @Override - public String toString() { - return "ShellResult{" + - "code=" + code + - ", error='" + error + '\'' + - ", result='" + result + '\'' + - '}'; - } - } - - /** - * 执行命令—单条 - * - * @param command - * @param isRoot - * @return - */ - public static CommandResult execCommand(String command, boolean isRoot) { - String[] commands = command.split("\n"); - return execCommand(commands, isRoot); - } - - /** - * 执行命令-多条 - * - * @param commands - * @param isRoot - * @return - */ - public static CommandResult execCommand(String[] commands, boolean isRoot) { - CommandResult commandResult = new CommandResult(); - if (commands == null || commands.length == 0) return commandResult; - Process process = null; - DataOutputStream os = null; - BufferedReader successResult = null; - BufferedReader errorResult = null; - StringBuilder successMsg = null; - StringBuilder errorMsg = null; - try { - process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH); - os = new DataOutputStream(process.getOutputStream()); - for (String command : commands) { - if (command != null) { - os.write(command.getBytes()); - os.writeBytes(COMMAND_LINE_END); - os.flush(); - } - } - os.writeBytes(COMMAND_EXIT); - os.flush(); - commandResult.code = process.waitFor(); - //获取错误信息 - successMsg = new StringBuilder(); - errorMsg = new StringBuilder(); - successResult = new BufferedReader(new InputStreamReader(process.getInputStream())); - errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream())); - String s; - while ((s = successResult.readLine()) != null) successMsg.append(s); - while ((s = errorResult.readLine()) != null) errorMsg.append(s); - commandResult.result = successMsg.toString(); - commandResult.error = errorMsg.toString(); - Log.i(TAG, commandResult.toString()); - } catch (Exception e) { - String errmsg = e.getMessage(); - if (errmsg != null) { - Log.e(TAG, errmsg); - } else { - e.printStackTrace(); - } - } finally { - try { - if (os != null) os.close(); - if (successResult != null) successResult.close(); - if (errorResult != null) errorResult.close(); - } catch (IOException e) { - String errmsg = e.getMessage(); - if (errmsg != null) { - Log.e(TAG, errmsg); - } else { - e.printStackTrace(); - } - } - if (process != null) process.destroy(); - } - return commandResult; - } - - public static Process exec(String[] commands, boolean isRoot) { - try { - Process process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH); - DataOutputStream os = new DataOutputStream(process.getOutputStream()); - for (String command : commands) { - if (command != null) { - os.write(command.getBytes()); - os.writeBytes(COMMAND_LINE_END); - os.flush(); - } - } - os.writeBytes(COMMAND_EXIT); - os.flush(); - os.close(); - return process; - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - - public static Process exec(String command, boolean isRoot) { - return exec(command.split("\n"), isRoot); - } -} \ No newline at end of file diff --git a/common/src/main/java/com/stardust/util/TextUtils.java b/common/src/main/java/com/stardust/util/TextUtils.java new file mode 100644 index 00000000..261d6f4a --- /dev/null +++ b/common/src/main/java/com/stardust/util/TextUtils.java @@ -0,0 +1,12 @@ +package com.stardust.util; + +/** + * Created by Stardust on 2017/5/3. + */ + +public class TextUtils { + + public static String join(CharSequence delimiter, Object... tokens){ + return android.text.TextUtils.join(delimiter, tokens); + } +}