summaryrefslogtreecommitdiff
path: root/src/jp/example/tethering/RootRelay.java
blob: 4219a6b33d03f4c1596f42e0336c3bd6a43f856b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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) {
                }
            }
        }
    }
}