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 int storeBuildProperty(Context c, String propName, String newValue) {
        // 0 - Process Failed
        // 1 - Process Succeed, and new property (ADD)
        // 2 - Process Succeed, and already avaliable property (MODIFY)

        if (isBuildPropertyAvaliable(c, propName)) {
            changeBuildProperty(c, propName, newValue);
            return 2;
        } else {
            writeBuildProperty(c, propName, newValue);
            return 1;
        }
    }

    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 Process changeBuildProperty(Context c, String propName, String newValue) {
        Process p = null;
        try {
            remountSystem(c);

            p = runSuCommandAsync(c, "busybox sed -i \"s/" + propName + "=.*/" + propName + "=" + newValue
                    + "/g\" /system/build.prop");
            p.waitFor();
        } catch (Exception d) {
            Log.e("Helper", "Failed to change build.prop. errcode:" + d.toString());
        }
        return p;
    }

    public static Process writeBuildProperty(Context c, String propName, String value) {
        Process p = null;
        try {
            remountSystem(c);

            p = runSuCommandAsync(c, "echo " + propName + "=" + value + " >> /system/build.prop");
            Log.d("Helper", "echo " + propName + "=" + value + " >> /system/build.prop");
            p.waitFor();
        } catch (Exception d) {
            Log.e("Helper", "Failed to write build.prop. errcode:" + d.toString());
        }
        return p;
    }

    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 void remountSystem(Context c) {
        try {
            runSuCommand(c, "mount -orw,remount mtd@system /system");
        } catch (Exception d) { /*Does not require error handler*/
        }
    }

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