Shell mount RW - Android Android OS

Android examples for Android OS:Shell

Description

Shell mount RW

Demo Code


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.util.Log;

public class Main{
    private static final String TAG = "ttttttt";
    private static String SYSTEM_PREFIX = null;
    public static boolean mountRW() {
        String prefix = getSystemPrefix();
        if (prefix != null) {
            String command = "mount -o remount,rw " + prefix;
            CommandResult result = execute(command, true);
            return result.exitValue == 0;
        }/*from  w  w  w.jav  a  2  s.co  m*/
        return false;
    }
    private static String getSystemPrefix() {
        if (SYSTEM_PREFIX == null) {
            synchronized (CommandUtils.class) {
                CommandResult result = execute("mount", false);
                String output = result.output;
                if (result.exitValue == 0 && output.length() > 0) {
                    String flag = " /system ";
                    for (String line : output.split("\n")) {
                        if (line.contains(flag)) {
                            SYSTEM_PREFIX = line.substring(0,
                                    line.indexOf(flag) + flag.length());
                        }
                    }
                }
            }
        }
        return SYSTEM_PREFIX;
    }
    public static CommandResult execute(String commands, boolean runAsRoot) {
        CommandResult result = new CommandResult();
        if (commands != null) {
            String[] lines = commands.split("\n");
            Process process = null;
            DataOutputStream os = null;
            try {
                //process = Runtime.getRuntime().exec("su"); 
                process = Runtime.getRuntime().exec(
                        runAsRoot ? "/system/xbin/su" : "sh"); 
                os = new DataOutputStream(process.getOutputStream());
                for (String line : lines) {
                    line = line.trim();
                    if (line.length() > 0 && !line.startsWith("#")) {
                        Log.i(TAG, line);
                        os.writeBytes(line + "\n");
                    }
                }
                os.writeBytes("exit\n");
                os.flush();
                String output = getSteamText(process.getInputStream());
                String error = getSteamText(process.getErrorStream());
                if (process.waitFor() != 0) {
                    result.exitValue = process.exitValue();
                    Log.e(TAG, "exit value = " + result.exitValue);
                }
                result.error = error;
                result.output = output;
                Log.i(TAG, output);
                Log.e(TAG, error);
            } catch (Exception e) {
                Log.e(TAG, e.getMessage(), e);
            } finally {
                try {
                    if (os != null) {
                        os.close();
                    }
                    process.destroy();
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage(), e);
                }
            }
        }
        return result;
    }
    private static String getSteamText(InputStream inputstream)
            throws IOException {
        StringBuilder inputText = new StringBuilder();
        InputStreamReader inputstreamreader = new InputStreamReader(
                inputstream);
        BufferedReader bufferedreader = new BufferedReader(
                inputstreamreader);
        // read the ls output  
        String line;
        while ((line = bufferedreader.readLine()) != null) {
            //System.out.println(line);  
            inputText.append(line);
            inputText.append('\n');
        }
        return inputText.toString();
    }
}

Related Tutorials