Sunday, November 8, 2015

Rooting android and executing Super commands

Android 4.4.4 has introduced additional restrictions to turn on GPS/internet programatically. My requirement was to auto flip GPS/internet connection.
With Android 4.4.4 security restriction the android.permission.WRITE_SECURE_SETTINGS would stop the code to flip gps/internet connection. I managed to get around by rooting and following few simple steps:

Steps required to take:

1. Root you Android phone (Note: the warranty of the phone will be void once you root it also do backup data just in case mobile gets bricked)
  Rooting basically means to gain complete access to modify mobile settings at system level. 
  Download and install KingoRoot from here

 Note:  There is an android APK also for rooting, but it didn't work for me, so better to use windows desktop version.

2. Open KingoRoot desktop application and connect your Android device( ensure USB debbuging is turned on  follow link if not)

3. Follow instructions on KingoRoot desktop application and the mobile will have KingoROOT and Superuser APK installed after the mobile gets rebooted. At this stage, the root access is available to KingoRoot and can be given to other APKs who request it.

4. The user apps can now run commands to remove security restrictions: Some command i ran programmatically to remove GPS/internet flip restrictions are below:

a. "pm grant " + context.getPackageName() + " android.permission.WRITE_SECURE_SETTINGS";
b. "settings put global mobile_data 1";  // to enable=1 disable =0 mobile data
c. "svc data enable/disable";   //to enable/disable mobile data incase a. doesnt work.

The code used to run above commands from Android is below.

public static void runAsRoot(String[] cmds) {
try {
Process p = Runtime.getRuntime().exec("su"); // this give super user access
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : cmds) {
Log.i(TAG, "Calling root command : " + tmpCmd);
os.writeBytes(tmpCmd + "\n");
}
os.writeBytes("exit\n");
os.flush();
} catch (Exception e) {
e.printStackTrace();
}
}