package jp.example.tethering; import java.io.File; import java.io.FileOutputStream; import java.io.RandomAccessFile; /** * The phone already runs /kcroot.sh as root. It watches /data/local/tmp/rin, * executes its contents as root and appends stdout/stderr to /data/local/tmp/rout. * Both files are world-writable/readable, so this app can use that service * without needing root itself. */ public final class RootRelay { public static final String RIN = "/data/local/tmp/rin"; public static final String ROUT = "/data/local/tmp/rout"; private RootRelay() { } public static boolean available() { File f = new File(RIN); return f.exists() && f.canWrite(); } /** * Kyocera's KcAlertMobiledataActivity shows a mobile-data confirmation for * app UIDs that are not accepted. Mark our own UID as accepted so the * dialog does not cover the app after a reinstall / UID change. */ public static String acceptMobileData() { int uid = android.os.Process.myUid(); return "content query --uri content://settings/accept_mobile_data | grep -q \"uid=" + uid + ",\" || " + "content insert --uri content://settings/accept_mobile_data --bind uid:i:" + uid + " --bind status:i:1 >/dev/null 2>&1\n" + "content update --uri content://settings/accept_mobile_data --bind status:i:1 --where \"uid=" + uid + "\" >/dev/null 2>&1\n" + "pm disable --user 0 android/com.android.internal.app.KcAlertMobiledataActivity >/dev/null 2>&1\n"; } public static boolean run(String command) { FileOutputStream out = null; try { out = new FileOutputStream(RIN, false); out.write((command + "\n").getBytes("UTF-8")); out.flush(); return true; } catch (Exception e) { return false; } finally { if (out != null) { try { out.close(); } catch (Exception ignored) { } } } } public static String tail() { RandomAccessFile f = null; try { f = new RandomAccessFile(ROUT, "r"); long len = f.length(); int n = (int) Math.min(len, 16384); f.seek(len - n); byte[] buf = new byte[n]; f.readFully(buf); return new String(buf, "UTF-8"); } catch (Exception e) { return ""; } finally { if (f != null) { try { f.close(); } catch (Exception ignored) { } } } } }