1+ package com .hjq .device .compat .demo ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .DataOutputStream ;
5+ import java .io .IOException ;
6+ import java .io .InputStreamReader ;
7+ import java .util .List ;
8+
9+ public final class ShellUtils {
10+
11+ private static final String LINE_SEP = System .getProperty ("line.separator" );
12+
13+ private ShellUtils () {
14+ throw new UnsupportedOperationException ("u can't instantiate me..." );
15+ }
16+
17+ /**
18+ * Execute the command.
19+ *
20+ * @param command The command.
21+ * @param isRooted True to use root, false otherwise.
22+ * @return the single {@link CommandResult} instance
23+ */
24+ public static CommandResult execCmd (final String command , final boolean isRooted ) {
25+ return execCmd (new String []{command }, isRooted , true );
26+ }
27+
28+ /**
29+ * Execute the command.
30+ *
31+ * @param command The command.
32+ * @param envp The environment variable settings.
33+ * @param isRooted True to use root, false otherwise.
34+ * @return the single {@link CommandResult} instance
35+ */
36+ public static CommandResult execCmd (final String command , final List <String > envp , final boolean isRooted ) {
37+ return execCmd (new String []{command },
38+ envp == null ? null : envp .toArray (new String []{}),
39+ isRooted ,
40+ true );
41+ }
42+
43+ /**
44+ * Execute the command.
45+ *
46+ * @param commands The commands.
47+ * @param isRooted True to use root, false otherwise.
48+ * @return the single {@link CommandResult} instance
49+ */
50+ public static CommandResult execCmd (final List <String > commands , final boolean isRooted ) {
51+ return execCmd (commands == null ? null : commands .toArray (new String []{}), isRooted , true );
52+ }
53+
54+ /**
55+ * Execute the command.
56+ *
57+ * @param commands The commands.
58+ * @param envp The environment variable settings.
59+ * @param isRooted True to use root, false otherwise.
60+ * @return the single {@link CommandResult} instance
61+ */
62+ public static CommandResult execCmd (final List <String > commands ,
63+ final List <String > envp ,
64+ final boolean isRooted ) {
65+ return execCmd (commands == null ? null : commands .toArray (new String []{}),
66+ envp == null ? null : envp .toArray (new String []{}),
67+ isRooted ,
68+ true );
69+ }
70+
71+ /**
72+ * Execute the command.
73+ *
74+ * @param commands The commands.
75+ * @param isRooted True to use root, false otherwise.
76+ * @return the single {@link CommandResult} instance
77+ */
78+ public static CommandResult execCmd (final String [] commands , final boolean isRooted ) {
79+ return execCmd (commands , isRooted , true );
80+ }
81+
82+ /**
83+ * Execute the command.
84+ *
85+ * @param command The command.
86+ * @param isRooted True to use root, false otherwise.
87+ * @param isNeedResultMsg True to return the message of result, false otherwise.
88+ * @return the single {@link CommandResult} instance
89+ */
90+ public static CommandResult execCmd (final String command ,
91+ final boolean isRooted ,
92+ final boolean isNeedResultMsg ) {
93+ return execCmd (new String []{command }, isRooted , isNeedResultMsg );
94+ }
95+
96+ /**
97+ * Execute the command.
98+ *
99+ * @param command The command.
100+ * @param envp The environment variable settings.
101+ * @param isRooted True to use root, false otherwise.
102+ * @param isNeedResultMsg True to return the message of result, false otherwise.
103+ * @return the single {@link CommandResult} instance
104+ */
105+ public static CommandResult execCmd (final String command ,
106+ final List <String > envp ,
107+ final boolean isRooted ,
108+ final boolean isNeedResultMsg ) {
109+ return execCmd (new String []{command }, envp == null ? null : envp .toArray (new String []{}),
110+ isRooted ,
111+ isNeedResultMsg );
112+ }
113+
114+ /**
115+ * Execute the command.
116+ *
117+ * @param command The command.
118+ * @param envp The environment variable settings array.
119+ * @param isRooted True to use root, false otherwise.
120+ * @param isNeedResultMsg True to return the message of result, false otherwise.
121+ * @return the single {@link CommandResult} instance
122+ */
123+ public static CommandResult execCmd (final String command ,
124+ final String [] envp ,
125+ final boolean isRooted ,
126+ final boolean isNeedResultMsg ) {
127+ return execCmd (new String []{command }, envp , isRooted , isNeedResultMsg );
128+ }
129+
130+ /**
131+ * Execute the command.
132+ *
133+ * @param commands The commands.
134+ * @param isRooted True to use root, false otherwise.
135+ * @param isNeedResultMsg True to return the message of result, false otherwise.
136+ * @return the single {@link CommandResult} instance
137+ */
138+ public static CommandResult execCmd (final List <String > commands ,
139+ final boolean isRooted ,
140+ final boolean isNeedResultMsg ) {
141+ return execCmd (commands == null ? null : commands .toArray (new String []{}),
142+ isRooted ,
143+ isNeedResultMsg );
144+ }
145+
146+ /**
147+ * Execute the command.
148+ *
149+ * @param commands The commands.
150+ * @param isRooted True to use root, false otherwise.
151+ * @param isNeedResultMsg True to return the message of result, false otherwise.
152+ * @return the single {@link CommandResult} instance
153+ */
154+ public static CommandResult execCmd (final String [] commands ,
155+ final boolean isRooted ,
156+ final boolean isNeedResultMsg ) {
157+ return execCmd (commands , null , isRooted , isNeedResultMsg );
158+ }
159+
160+ /**
161+ * Execute the command.
162+ *
163+ * @param commands The commands.
164+ * @param envp Array of strings, each element of which
165+ * has environment variable settings in the format
166+ * <i>name</i>=<i>value</i>, or
167+ * <tt>null</tt> if the subprocess should inherit
168+ * the environment of the current process.
169+ * @param isRooted True to use root, false otherwise.
170+ * @param isNeedResultMsg True to return the message of result, false otherwise.
171+ * @return the single {@link CommandResult} instance
172+ */
173+ public static CommandResult execCmd (final String [] commands ,
174+ final String [] envp ,
175+ final boolean isRooted ,
176+ final boolean isNeedResultMsg ) {
177+ int result = -1 ;
178+ if (commands == null || commands .length == 0 ) {
179+ return new CommandResult (result , "" , "" );
180+ }
181+ Process process = null ;
182+ BufferedReader successResult = null ;
183+ BufferedReader errorResult = null ;
184+ StringBuilder successMsg = null ;
185+ StringBuilder errorMsg = null ;
186+ DataOutputStream os = null ;
187+ try {
188+ process = Runtime .getRuntime ().exec (isRooted ? "su" : "sh" , envp , null );
189+ os = new DataOutputStream (process .getOutputStream ());
190+ for (String command : commands ) {
191+ if (command == null ) continue ;
192+ os .write (command .getBytes ());
193+ os .writeBytes (LINE_SEP );
194+ os .flush ();
195+ }
196+ os .writeBytes ("exit" + LINE_SEP );
197+ os .flush ();
198+ result = process .waitFor ();
199+ if (isNeedResultMsg ) {
200+ successMsg = new StringBuilder ();
201+ errorMsg = new StringBuilder ();
202+ successResult = new BufferedReader (
203+ new InputStreamReader (process .getInputStream (), "UTF-8" )
204+ );
205+ errorResult = new BufferedReader (
206+ new InputStreamReader (process .getErrorStream (), "UTF-8" )
207+ );
208+ String line ;
209+ if ((line = successResult .readLine ()) != null ) {
210+ successMsg .append (line );
211+ while ((line = successResult .readLine ()) != null ) {
212+ successMsg .append (LINE_SEP ).append (line );
213+ }
214+ }
215+ if ((line = errorResult .readLine ()) != null ) {
216+ errorMsg .append (line );
217+ while ((line = errorResult .readLine ()) != null ) {
218+ errorMsg .append (LINE_SEP ).append (line );
219+ }
220+ }
221+ }
222+ } catch (Exception e ) {
223+ e .printStackTrace ();
224+ } finally {
225+ try {
226+ if (os != null ) {
227+ os .close ();
228+ }
229+ } catch (IOException e ) {
230+ e .printStackTrace ();
231+ }
232+ try {
233+ if (successResult != null ) {
234+ successResult .close ();
235+ }
236+ } catch (IOException e ) {
237+ e .printStackTrace ();
238+ }
239+ try {
240+ if (errorResult != null ) {
241+ errorResult .close ();
242+ }
243+ } catch (IOException e ) {
244+ e .printStackTrace ();
245+ }
246+ if (process != null ) {
247+ process .destroy ();
248+ }
249+ }
250+ return new CommandResult (
251+ result ,
252+ successMsg == null ? "" : successMsg .toString (),
253+ errorMsg == null ? "" : errorMsg .toString ()
254+ );
255+ }
256+
257+ /**
258+ * The result of command.
259+ */
260+ public static class CommandResult {
261+ public int result ;
262+ public String successMsg ;
263+ public String errorMsg ;
264+
265+ public CommandResult (final int result , final String successMsg , final String errorMsg ) {
266+ this .result = result ;
267+ this .successMsg = successMsg ;
268+ this .errorMsg = errorMsg ;
269+ }
270+
271+ @ Override
272+ public String toString () {
273+ return "result: " + result + "\n " +
274+ "successMsg: " + successMsg + "\n " +
275+ "errorMsg: " + errorMsg ;
276+ }
277+ }
278+ }
0 commit comments