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;

public class Main {
    public final static String SCRIPT_NAME = "surunner.sh";

    public static void storeBuildPropertyBatched(Context c, String propArgument) {
        StringBuilder propName = new StringBuilder("");
        StringBuilder propValue = new StringBuilder("");
        StringBuilder commandLine = new StringBuilder("");
        boolean isValue = false;
        for (int i = 0; i < propArgument.length(); i++) {
            char ch = propArgument.charAt(i);
            if (ch == '=' && isValue == false) {
                isValue = true;
            } else if (ch == ';') {
                // Store the build property
                if (isBuildPropertyAvaliable(c, propName.toString())) {
                    // Change build property manually.
                    commandLine.append("busybox sed -i \"s/" + propName.toString() + "=.*/" + propName.toString()
                            + "=" + propValue.toString() + "/g\" /system/build.prop ; ");
                } else {
                    // Write new build property manually.
                    commandLine.append("echo " + propName.toString() + "=" + propValue.toString()
                            + " >> /system/build.prop ; ");
                }

                // Clear the value
                isValue = false;
                propName = new StringBuilder("");
                propValue = new StringBuilder("");
            } else {
                if (isValue)
                    propValue.append(ch);
                else
                    propName.append(ch);
            }
        }

        // Execute the process
        Process p = null;
        try {
            remountSystem(c);

            p = runSuCommandAsync(c, commandLine.toString());
            p.waitFor();
        } catch (Exception d) {
            Log.e("Helper", "Failed to batch store build.prop. errcode:" + d.toString());
        }
    }

    public static boolean isBuildPropertyAvaliable(Context c, String propName) {
        try {
            Process p = runSuCommandAsync(c, "busybox grep \"" + propName + "=\" /system/build.prop");
            try {
                p.waitFor();
                Log.d("Helper", "isBuildPropAvali : exitValue is : " + String.valueOf(p.exitValue()));
                if (p.exitValue() == 0)
                    return true;

            } catch (InterruptedException d) {
                Log.e("Helper", "Failed grep build.prop and waiting for process. errcode:" + d.toString());
            }
        } catch (Exception d) {
            Log.e("Helper", "Failed grep build.prop. errcode:" + d.toString());
        }
        return false;
    }

    public static void remountSystem(Context c) {
        try {
            runSuCommand(c, "mount -orw,remount mtd@system /system");
        } catch (Exception d) { /*Does not require error handler*/
        }
    }

    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;
    }

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