summaryrefslogtreecommitdiff
path: root/src/jp/example/tethering/RootRelay.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jp/example/tethering/RootRelay.java')
-rw-r--r--src/jp/example/tethering/RootRelay.java80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/jp/example/tethering/RootRelay.java b/src/jp/example/tethering/RootRelay.java
new file mode 100644
index 0000000..4219a6b
--- /dev/null
+++ b/src/jp/example/tethering/RootRelay.java
@@ -0,0 +1,80 @@
+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) {
+ }
+ }
+ }
+ }
+}