is Wrong Root - Android Android OS

Android examples for Android OS:Root

Description

is Wrong Root

Demo Code


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;

public class Main{
    private static final String SU_PATH = "/system/bin/su";
    private static final String SU_PATH_X = "/system/xbin/su";
    public static boolean isWrongRoot() {
        // -rwsr-sr-x
        String suStat = runCommand("ls -l " + SU_PATH, false).result
                + runCommand("ls -l " + SU_PATH_X, false).result;
        if (GlobalInstance.DEBUG) {
            Log.e("suStat", suStat);
        }/* w w w  .j  a  v  a  2 s. c  o m*/
        return ((!suStat.contains("-rwsr-sr-x")) && (!suStat
                .contains("-rwsr-xr-x")));
    }
    @SuppressWarnings("deprecation")
    public static CommandResult runCommand(String command, boolean root) {
        if (GlobalInstance.DEBUG) {
            Log.e("runRootCommand", command);
        }
        Process process = null;
        DataOutputStream os = null;
        DataInputStream stdout = null;
        DataInputStream stderr = null;
        CommandResult ret = new CommandResult();
        try {
            StringBuffer output = new StringBuffer();
            StringBuffer error = new StringBuffer();
            if (root) {
                process = Runtime.getRuntime().exec("su");
                os = new DataOutputStream(process.getOutputStream());
                os.writeBytes(command + "\n");
                os.writeBytes("exit\n");
                os.flush();
            } else {
                process = Runtime.getRuntime().exec(command);
            }
            stdout = new DataInputStream(process.getInputStream());
            String line;
            while ((line = stdout.readLine()) != null) {
                output.append(line).append('\n');
            }
            stderr = new DataInputStream(process.getErrorStream());
            while ((line = stderr.readLine()) != null) {
                error.append(line).append('\n');
            }
            process.waitFor();
            ret.result = output.toString().trim();
            ret.error = error.toString().trim();
        } catch (Exception e) {
            ret.result = "";
            ret.error = e.getMessage();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (stdout != null) {
                    stdout.close();
                }
                if (stderr != null) {
                    stderr.close();
                }
                // if (process != null) {
                // try {
                // process.destroy();
                // } catch (Throwable th) {
                //
                // }
                // }
            } catch (Exception e) {
                ret.result = "";
                ret.error = e.getMessage();
            }
        }
        if (GlobalInstance.DEBUG) {
            Log.e("runRootCommand-Result", String.format(
                    "result:%s, error:%s", ret.result, ret.error));
        }
        return ret;
    }
}

Related Tutorials