Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ch.unibas.cn.ana.service;
- import java.io.FileDescriptor;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.LineNumberReader;
- import java.io.PrintStream;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.ArrayList;
- import java.util.Map.Entry;
- import ch.unibas.cn.ana.Constants;
- import android.util.Log;
- public class ShellExecutor {
- protected static final String TAG = "ShellExecutor";
- protected static int mShellPid;
- protected static FileDescriptor mShellFd;
- protected static FileOutputStream mShellOut;
- protected static FileInputStream mShellIn;
- protected static PrintStream mOut;
- protected static LineNumberReader mIn;
- protected static Environment mEnvironment;
- private static Method createSubprocess;
- private static Method waitFor;
- private static boolean firstTime = true;
- private static ShellExecutor instance = new ShellExecutor();
- private ShellExecutor() {}
- public static synchronized ShellExecutor getInstance(){
- if(firstTime){
- instance.initShell();
- firstTime = false;
- }
- return instance;
- }
- @Override
- protected void finalize() throws Throwable {
- executeCmd("exit", null);
- try {
- waitFor.invoke(null, mShellPid);
- } catch (IllegalArgumentException e) {
- Log.e(TAG,e.toString());
- } catch (IllegalAccessException e) {
- Log.e(TAG,e.toString());
- } catch (InvocationTargetException e) {
- Log.e(TAG,e.toString());
- }
- try {
- mIn.close();
- mOut.close();
- mShellIn.close();
- mShellOut.close();
- } catch (IOException e) {
- Log.e(TAG,e.toString());
- }
- super.finalize();
- }
- private void initShell(){
- // android.os.Exec is not included in android.jar so we need to use reflection.
- createSubprocess = getMethodFromClass("android.os.Exec", "createSubprocess",
- String.class, String.class, String.class, int[].class);
- waitFor = getMethodFromClass("android.os.Exec", "waitFor", int.class);
- int[] pid = new int[1];
- try {
- mShellFd = (FileDescriptor)createSubprocess.invoke(null, Constants.SHELL, "-" , null, pid);
- mShellPid = pid[0];
- mShellOut = new FileOutputStream(mShellFd);
- mShellIn = new FileInputStream(mShellFd);
- mOut = new PrintStream(mShellOut, true /* autoflush */);
- mIn = new LineNumberReader(new InputStreamReader(mShellIn));
- // Wait until the shell has produced some output before we start writing to it. This prevents
- // misplaced $ prompts in the output.
- while (!mIn.ready()) {
- Thread.sleep(1);
- }
- exportEnvironment();
- } catch (IllegalArgumentException e) {
- Log.e(TAG,e.toString());
- } catch (IllegalAccessException e) {
- Log.e(TAG,e.toString());
- } catch (InvocationTargetException e) {
- Log.e(TAG,e.toString());
- } catch (IOException e) {
- Log.e(TAG,e.toString());
- } catch (InterruptedException e) {
- Log.e(TAG,e.toString());
- }
- }
- public boolean executeCmd(String cmd, ArrayList<String> result){
- mOut.println(cmd + "; echo sentinel");
- boolean flag = true;
- String line;
- try {
- //Thread.sleep(100); /* time until the command should be executed, and we may send a sentinel */
- //mOut.println("echo sentinel > /dev/null");
- do {
- line = mIn.readLine();
- if(line.equals("sentinel")){
- flag = false;
- }else{
- if(result != null){
- if(lineIsResult(line, cmd)){
- Log.i(TAG, "Line: "+ line);
- result.add(line);
- }
- }
- }
- } while (flag);
- mIn.setLineNumber(mIn.getLineNumber());
- return true;
- } catch (IOException e) {
- Log.e(TAG,e.toString());
- }
- return false;
- }
- private boolean lineIsResult(String line, String cmd){
- if(!line.contains(cmd)){
- if(!line.equals("\n") || !line.equals("\r\n")){
- return true;
- }
- }
- return false;
- }
- private void exportEnvironment() {
- for (Entry<String, String> e : mEnvironment.getEnv().entrySet()) {
- executeCmd(String.format("export %s=\"%s\"", e.getKey(), e.getValue()), null);
- }
- }
- private Method getMethodFromClass(final String _class, final String method, final Class<?>... paramTypes){
- Class<?> iclass;
- try {
- iclass = Class.forName(_class);
- return iclass.getMethod(method, paramTypes);
- } catch (ClassNotFoundException e) {
- Log.e(TAG,e.toString());
- } catch (SecurityException e) {
- Log.e(TAG,e.toString());
- } catch (NoSuchMethodException e) {
- Log.e(TAG,e.toString());
- }
- return null;
- }
- }
Add Comment
Please, Sign In to add comment