Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.DataOutputStream;
import java.io.IOException;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class Main {
    static final String LOGTAG = "We do what we must because we can";
    public final static String SCRIPT_NAME = "surunner.sh";

    public static boolean instantExec(Context context, String command) {
        try {
            runSuCommand(context, command.toString());
        } catch (Exception e) {
            Toast.makeText(context, "E:ScriptRunner: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            Log.d(LOGTAG, "E:ScriptRunner: " + e.getMessage());
            return false;
        }
        return true;
    }

    public static int runSuCommand(Context context, String command) throws IOException, InterruptedException {
        return runSuCommandAsync(context, command).waitFor();
    }

    public static Process runSuCommandAsync(Context context, String command) throws IOException {
        DataOutputStream fout = new DataOutputStream(context.openFileOutput(SCRIPT_NAME, 0));
        fout.writeBytes(command);
        fout.close();

        String[] args = new String[] { "su", "-c",
                ". " + context.getFilesDir().getAbsolutePath() + "/" + SCRIPT_NAME };
        Process proc = Runtime.getRuntime().exec(args);
        return proc;
    }
}