diff --git a/lib/utilcode/src/main/java/com/blankj/utilcode/util/ShellUtils.java b/lib/utilcode/src/main/java/com/blankj/utilcode/util/ShellUtils.java index 1f2d50888b..a4f1e4fa81 100644 --- a/lib/utilcode/src/main/java/com/blankj/utilcode/util/ShellUtils.java +++ b/lib/utilcode/src/main/java/com/blankj/utilcode/util/ShellUtils.java @@ -133,6 +133,21 @@ public static CommandResult execCmd(final String command, final boolean isRooted return execCmd(new String[]{command}, isRooted, true); } + /** + * Execute the command. + * + * @param command The command. + * @param envp The environment variable settings. + * @param isRooted True to use root, false otherwise. + * @return the single {@link CommandResult} instance + */ + public static CommandResult execCmd(final String command, final List envp, final boolean isRooted) { + return execCmd(new String[]{command}, + envp == null ? null : envp.toArray(new String[]{}), + isRooted, + true); + } + /** * Execute the command. * @@ -144,6 +159,23 @@ public static CommandResult execCmd(final List commands, final boolean i return execCmd(commands == null ? null : commands.toArray(new String[]{}), isRooted, true); } + /** + * Execute the command. + * + * @param commands The commands. + * @param envp The environment variable settings. + * @param isRooted True to use root, false otherwise. + * @return the single {@link CommandResult} instance + */ + public static CommandResult execCmd(final List commands, + final List envp, + final boolean isRooted) { + return execCmd(commands == null ? null : commands.toArray(new String[]{}), + envp == null ? null : envp.toArray(new String[]{}), + isRooted, + true); + } + /** * Execute the command. * @@ -169,6 +201,40 @@ public static CommandResult execCmd(final String command, return execCmd(new String[]{command}, isRooted, isNeedResultMsg); } + /** + * Execute the command. + * + * @param command The command. + * @param envp The environment variable settings. + * @param isRooted True to use root, false otherwise. + * @param isNeedResultMsg True to return the message of result, false otherwise. + * @return the single {@link CommandResult} instance + */ + public static CommandResult execCmd(final String command, + final List envp, + final boolean isRooted, + final boolean isNeedResultMsg) { + return execCmd(new String[]{command}, envp == null ? null : envp.toArray(new String[]{}), + isRooted, + isNeedResultMsg); + } + + /** + * Execute the command. + * + * @param command The command. + * @param envp The environment variable settings array. + * @param isRooted True to use root, false otherwise. + * @param isNeedResultMsg True to return the message of result, false otherwise. + * @return the single {@link CommandResult} instance + */ + public static CommandResult execCmd(final String command, + final String[] envp, + final boolean isRooted, + final boolean isNeedResultMsg) { + return execCmd(new String[]{command}, envp, isRooted, isNeedResultMsg); + } + /** * Execute the command. * @@ -196,6 +262,26 @@ public static CommandResult execCmd(final List commands, public static CommandResult execCmd(final String[] commands, final boolean isRooted, final boolean isNeedResultMsg) { + return execCmd(commands, null, isRooted, isNeedResultMsg); + } + + /** + * Execute the command. + * + * @param commands The commands. + * @param envp Array of strings, each element of which + * has environment variable settings in the format + * name=value, or + * null if the subprocess should inherit + * the environment of the current process. + * @param isRooted True to use root, false otherwise. + * @param isNeedResultMsg True to return the message of result, false otherwise. + * @return the single {@link CommandResult} instance + */ + public static CommandResult execCmd(final String[] commands, + final String[] envp, + final boolean isRooted, + final boolean isNeedResultMsg) { int result = -1; if (commands == null || commands.length == 0) { return new CommandResult(result, "", ""); @@ -207,7 +293,7 @@ public static CommandResult execCmd(final String[] commands, StringBuilder errorMsg = null; DataOutputStream os = null; try { - process = Runtime.getRuntime().exec(isRooted ? "su" : "sh"); + process = Runtime.getRuntime().exec(isRooted ? "su" : "sh", envp, null); os = new DataOutputStream(process.getOutputStream()); for (String command : commands) { if (command == null) continue;