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
|
package jp.example.tethering;
import android.content.Context;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
/**
* Writes the bundled host-side scripts to /data/local/tmp via the root relay.
*
* pc-autoconnect.ps1 Windows / PowerShell
* pc-autoconnect.sh OS dispatcher
* pc-autoconnect-linux.sh Linux
* pc-autoconnect-freebsd.sh FreeBSD
*
* The PC can then fetch them with adb pull.
*/
public final class Ps1Exporter {
public static final String REMOTE_DIR = "/data/local/tmp/";
private Ps1Exporter() {
}
public static String exportAll(Context context) throws Exception {
StringBuilder cmd = new StringBuilder();
appendAsset(cmd, context, "pc-autoconnect.ps1",
REMOTE_DIR + "pc-autoconnect.ps1", "TETHER_PS1_EOF");
appendAsset(cmd, context, "pc-autoconnect.sh",
REMOTE_DIR + "pc-autoconnect.sh", "TETHER_SH_EOF");
appendAsset(cmd, context, "pc-autoconnect-linux.sh",
REMOTE_DIR + "pc-autoconnect-linux.sh", "TETHER_LINUX_EOF");
appendAsset(cmd, context, "pc-autoconnect-freebsd.sh",
REMOTE_DIR + "pc-autoconnect-freebsd.sh", "TETHER_FREEBSD_EOF");
cmd.append("chmod 644 ").append(REMOTE_DIR).append("pc-autoconnect.ps1\n");
cmd.append("chmod 755 ")
.append(REMOTE_DIR).append("pc-autoconnect.sh ")
.append(REMOTE_DIR).append("pc-autoconnect-linux.sh ")
.append(REMOTE_DIR).append("pc-autoconnect-freebsd.sh\n");
cmd.append("ls -l ").append(REMOTE_DIR).append("pc-autoconnect*\n");
if (!RootRelay.run(cmd.toString())) {
throw new Exception("root service (/data/local/tmp/rin) is not writable");
}
return REMOTE_DIR;
}
private static void appendAsset(StringBuilder cmd, Context context, String asset,
String remote, String marker) throws Exception {
InputStream in = context.getAssets().open(asset);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
int n;
while ((n = in.read(buf)) > 0) {
bos.write(buf, 0, n);
}
in.close();
String text = new String(bos.toByteArray(), "UTF-8");
cmd.append("cat > ").append(remote).append(" <<'").append(marker).append("'\n");
cmd.append(text);
if (!text.endsWith("\n")) {
cmd.append('\n');
}
cmd.append(marker).append('\n');
}
}
|