该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import android.annotation.SuppressLint;
import android.os.*;
import java.io.*;
import java.lang.Process;
public class RootShellTask extends Thread
{
@SuppressLint("HandlerLeak")
Handler h = new Handler(){
@Override
public void handleMessage(Message msg)
{
// TODO: Implement this method
super.handleMessage(msg);
Bundle b = msg.getData();
int flag = b.getInt("flag");
if (flag == FLAG_START)
{
boolean root = b.getBoolean("root");
String command = b.getString("data");
RootShellTask.this.call.onStart(command, root);
}
else if (flag == FLAG_RUN)
{
String output = b.getString("data");
RootShellTask.this.call.onRun(output);
}
else if (flag == FLAG_FINISH)
{
int code = b.getInt("data");
RootShellTask.this.call.onFinish(code);
}
}
};
public void run()
{
Message m;
Bundle b;
try
{
String shell = root ? "su" : "sh";
process = Runtime.getRuntime().exec(shell, envp);
os = new DataOutputStream(process.getOutputStream());
if (call != null)
{
m = new Message();
b = new Bundle();
b.putInt("flag", FLAG_START);
b.putString("data", command);
b.putBoolean("root", root);
m.setData(b);
h.sendMessage(m);
}
os.write((command).getBytes());
os.write(("\n").getBytes());
os.write(("exit $?\n").getBytes());
os.flush();
if (call != null)
{
stdout = new BufferedReader(new InputStreamReader(process.getInputStream()));
stderr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String out = "";
String err = "";
while ((out = stdout.readLine()) != null | (err = stderr.readLine()) != null)
{
StringBuffer s = new StringBuffer();
if (out != null)
{
s.append(out + "\n");
}
if (err != null)
{
s.append(err + "\n");
}
m = new Message();
b = new Bundle();
b.putInt("flag", FLAG_RUN);
b.putString("data", s.toString());
m.setData(b);
h.sendMessage(m);
}
}
int code = process.waitFor();
if (call != null)
{
m = new Message();
b = new Bundle();
b.putInt("flag", FLAG_FINISH);
b.putInt("data", code);
m.setData(b);
h.sendMessage(m);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (os != null)
{
os.close();
}
if (stderr != null)
{
stderr.close();
}
if (stdout != null)
{
stdout.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
int FLAG_FINISH = 0;
int FLAG_RUN = 1;
int FLAG_START = 2;
private Process process;
private DataOutputStream os;
private BufferedReader stderr;
private BufferedReader stdout;
private RootShellCallback call;
private String command;
private String[] envp;
private boolean root;
public RootShellTask(String command)
{
this(command, true);
}
public RootShellTask(String command, RootShellCallback call)
{
this(command, true, call);
}
public RootShellTask(String command, boolean root)
{
this(command, root, null);
}
public RootShellTask(String command, boolean root, RootShellCallback call)
{
this(command, root, call, null);
}
public RootShellTask(String command, boolean root, RootShellCallback call, String[] envp)
{
this.call = call;
this.envp = envp;
this.command = command;
this.root = root;
}
public void setProcess(Process process)
{
this.process = process;
}
public Process getProcess()
{
return process;
}
}
interface RootShellCallback
{
void onStart(String command, boolean root);
void onRun(String output);
void onFinish(int exitCode);
}