Gmail 5.0: Material Design and Multiple Account Support

Google started to materialize many of its applications right after announcing the … more

Some of Our Favorite Features in Android 5.0 Lollipop

Android 5.0 Lollipop is the latest major revision to Google’s mobile operating … more

Sony SmartEyeglass SDK Developer Preview Released

A little over a month ago in Tokyo, Japan, Sony unveiled its new SmartEyeglasstechnology. … more

XDA and Leaseweb: Server Trial/Moto 360 Contest (Winners!)

About a month ago, we held a contest along with XDA’s host, Leaseweb, to … more
Post Reply

Writing in the private area of other apps

OP KrauseDroid

23rd April 2014, 02:12 PM   |  #11  
Masrepus's Avatar
Senior Member
Thanks Meter: 74
 
667 posts
Join Date:Joined: Feb 2013
More
Ah i think i have read about executing an array of commands somewhere, so i guess this will work
Ya probably that is a more elegant solution

---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------

4d 61 73 72 65 70 75 73 20 66 74 77

Gesendet von Tapatalk
23rd April 2014, 07:18 PM   |  #12  
arpitkh96's Avatar
Senior Member
Flag panipat
Thanks Meter: 384
 
1,098 posts
Join Date:Joined: Feb 2012
More
I use this class to execute su commands(extracted from aokp rom control).
Code:
 public class CMDProcessor {

private final String TAG = getClass().getSimpleName(); 
private static final boolean DEBUG = false; 
private Boolean can_su; 
public SH sh; 
public SH su;

public CMDProcessor()
{ sh = new SH("sh"); su = new SH("su"); }

public SH suOrSH() { return canSU() ? su : sh; }

public boolean canSU() { return canSU(false); }

public class CommandResult { private final String resultTag = TAG + '.' + getClass().getSimpleName(); public final String stdout; public final String stderr; public final Integer exit_value;

CommandResult(final Integer exit_value_in) { this(exit_value_in, null, null); }

CommandResult(final Integer exit_value_in, final String stdout_in, final String stderr_in) { exit_value = exit_value_in; stdout = stdout_in; stderr = stderr_in; if (DEBUG) Log.d(TAG, resultTag + "( exit_value=" + exit_value + ", stdout=" + stdout + ", stderr=" + stderr + " )"); }

public boolean success() { return exit_value != null && exit_value == 0; }

}

public class SH { private String SHELL = "sh";

public SH(final String SHELL_in) { SHELL = SHELL_in; }

private String getStreamLines(final InputStream is) { String out = null; StringBuffer buffer = null; final DataInputStream dis = new DataInputStream(is); try { if (dis.available() > 0) { buffer = new StringBuffer(dis.readLine()); while (dis.available() > 0) { buffer.append("\n").append(dis.readLine()); } } dis.close(); } catch (final Exception ex) { Log.e(TAG, ex.getMessage()); } if (buffer != null) { out = buffer.toString(); } return out; }

public Process run(final String s) { Process process = null; try { process = Runtime.getRuntime().exec(SHELL); final DataOutputStream toProcess = new DataOutputStream( process.getOutputStream()); toProcess.writeBytes("exec " + s + "\n"); toProcess.flush(); } catch (final Exception e) { Log.e(TAG, "Exception while trying to run: '" + s + "' " + e.getMessage()); process = null; } return process; }

public CommandResult runWaitFor(final String s) { if (DEBUG) Log.d(TAG, "runWaitFor( " + s + " )"); final Process process = run(s); Integer exit_value = null; String stdout = null; String stderr = null; if (process != null) { try { exit_value = process.waitFor(); stdout = getStreamLines(process.getInputStream()); stderr = getStreamLines(process.getErrorStream()); } catch (final InterruptedException e) { Log.e(TAG, "runWaitFor " + e.toString()); } catch (final NullPointerException e) { Log.e(TAG, "runWaitFor " + e.toString()); } } return new CommandResult(exit_value, stdout, stderr); } }

public boolean canSU(final boolean force_check) { if (can_su == null || force_check) { final CommandResult r = su.runWaitFor("id"); final StringBuilder out = new StringBuilder(); if (r.stdout != null) { out.append(r.stdout).append(" ; "); } if (r.stderr != null) { out.append(r.stderr); } Log.d(TAG, "canSU() su[" + r.exit_value + "]: " + out); can_su = r.success(); } return can_su; }}
Sent from my GT-S5570 using XDA Premium 4 mobile app
23rd April 2014, 07:28 PM   |  #13  
OP Junior Member
Thanks Meter: 0
 
5 posts
Join Date:Joined: Feb 2014
I managed to solve it already. I'm using this class now:
Code:
public void RunAsRoot(String[] cmds){
        Process p = null;
		try {
			p = Runtime.getRuntime().exec("su");
		} catch (IOException e) {
			e.printStackTrace();
		}
        DataOutputStream os = new DataOutputStream(p.getOutputStream());            
        for (String tmpCmd : cmds) {
                try {
					os.writeBytes(tmpCmd+"\n");
				} catch (IOException e) {
					e.printStackTrace();
				}
        }           
        try {
			os.writeBytes("exit\n");
		} catch (IOException e) {
			e.printStackTrace();
		}  
        try {
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
Then simply call it via
Code:
String[] commands = {"command1", "command2", "command3", ...};
                	RunAsRoot(commands);
Thanks anyways!
23rd April 2014, 07:45 PM   |  #14  
Masrepus's Avatar
Senior Member
Thanks Meter: 74
 
667 posts
Join Date:Joined: Feb 2013
More
Oh okay that looks really nice

---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------

4d 61 73 72 65 70 75 73 20 66 74 77

Gesendet von Tapatalk
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Top Threads in Java for Android App Development by ThreadRank