Search code examples
androidterminalterminal-emulator

Building a Terminal Emulator for Android


I've been trying to build a Terminal Emulator for Android. Being pretty new to this, my idea was to execute each command and store the output in a file, whose contents would be displayed after each execution. Pseudo Code :

public Boolean execCommands(String command) {
        try {
            rt = Runtime.getRuntime();
            process = rt.exec("su");
            DataOutputStream os = new DataOutputStream(process.getOutputStream());
            os.writeBytes("echo $ \""+command+ "\" >> /sdcard/Android/data/terminalemulatorlog.txt\n\n\n"); 
            /**** Note :  String command = (EditText)findViewById(R.id.command).getText().toString(); ****/
            os.flush();
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
            }
        // Error Handling
        displayOutput(); //Loads and displays the Text File (/sdcard/Android/data/terminalemulatorlog.txt)
        return true;
        }

This piece of code works except for a few special commands (Eg. 'clear'). But what I'm more concerned about are the following problems :

  1. Each time a command is to be executed, I end up seeking SuperUser permissions (second line of code). And I'd like to do away with this.
  2. In cases when the user enters one command followed by another,
    Such as :

    cd /sdcard    
    touch File.txt    
    

    The File.txt is created in '/' and not in '/sdcard'. As of now to avoid this, I'm keeping a track of all the 'cd' commands to figure out what the present working directory is. And I'm hoping that there is a better way around this.

I'd be grateful if someone could help me out here.


Solution

  • Not sure if you are still needing this or not, but here is how I am issuing multiple commands at one time and not using "su" to have them run.

    try {
        String[] commands = {           
                "dumpstate > /sdcard/LogFiles/dumpstate.txt",
                "dumpsys > /sdcard/LogFiles/dumpsys.txt",
                "logcat -d > /sdcard/LogFiles/log.txt",
                "cat /sdcard/LogFiles/dumpstate.txt /sdcard/LogFiles/dumpsys.txt /sdcard/LogFiles/log.txt > /sdcard/LogFiles/bugreport.rtf" };
        Process p = Runtime.getRuntime().exec("/system/bin/sh -");
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        for (String tmpCmd : commands) {
            os.writeBytes(tmpCmd + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }