public static Process nonBlockShell;
public static DataOutputStream nonBlockOutputStream;
public static BufferedReader nonBlockSuccessResult
=
null;
public static BufferedReader nonBlockErrorResult
=
null;
static {
initNonBlockShell();
}
public static
int
initNonBlockShell() {
try
{
nonBlockShell
=
Runtime.getRuntime().
exec
(
"su"
);
nonBlockOutputStream
=
new DataOutputStream(nonBlockShell.getOutputStream());
nonBlockSuccessResult
=
new BufferedReader(new InputStreamReader(nonBlockShell.getInputStream()));
nonBlockErrorResult
=
new BufferedReader(new InputStreamReader(nonBlockShell.getErrorStream()));
readNonBlockStream();
} catch (Throwable e) {
e.printStackTrace();
return
-
1
;
}
return
0
;
}
public static void readNonBlockStream() {
new Thread(new Runnable() {
@Override
public void run() {
try
{
while
(nonBlockSuccessResult !
=
null) {
String s;
while
((s
=
nonBlockSuccessResult.readLine()) !
=
null) {
Logger.i(
"[<< EXEC SUCCESS]"
+
s);
}
}
} catch (Exception e) {
Logger.e(
"read nonBlockSuccessResult error:"
, e);
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try
{
while
(nonBlockErrorResult !
=
null) {
String e;
while
((e
=
nonBlockErrorResult.readLine()) !
=
null) {
Logger.e(
"[<< EXEC ERROR]"
+
e);
}
}
} catch (Exception e) {
Logger.e(
"read nonBlockErrorResult error:"
, e);
}
}
}).start();
}
public static
int
execNonBlockRootCmd(String cmd) {
int
ret
=
0
;
if
(null
=
=
nonBlockShell || null
=
=
nonBlockOutputStream) {
if
(initNonBlockShell() <
0
) {
Logger.e(
"execNonBlockRootCmd getRuntime for su error!"
);
return
-
1
;
}
}
Logger.d(
"[>>]"
+
cmd);
try
{
nonBlockOutputStream.writeBytes(cmd
+
"\n"
);
nonBlockOutputStream.flush();
} catch (Exception e) {
Logger.e(
"execNonBlockRootCmd error:"
, e);
ret
=
-
1
;
}
return
ret;
}